Class: Anynines::Swift::Utility

Inherits:
Object
  • Object
show all
Defined in:
lib/a9s_swift/utility.rb

Constant Summary collapse

SWIFT_HOST =
"https://swift.hydranodes.de"

Class Method Summary collapse

Class Method Details

.configure_carrierwave(image_bucket_name, options = {}, provider = "hp") ⇒ Object

Sets up carrierwave for the usage with the anynines service. Creates a bucket with the given name if not already present.

Parameters:

  • image_bucket_name (String)

    the bucket name to use

  • options (Hash) (defaults to: {})

    a hash of additional options

  • provider (String) (defaults to: "hp")

    ‘hp’ or ‘openstack’



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/a9s_swift/utility.rb', line 45

def self.configure_carrierwave(image_bucket_name, options = {}, provider = "hp")
  raise "CarrierWave wasn't found in your environment! Please verify that carrierwave is included within your Gemfile and loaded correctly." if defined?(CarrierWave).nil?

  opts = initialize_options options
  create_new_bucket(image_bucket_name, opts[:fog_public], provider)

  CarrierWave.configure do |config|
    config.fog_credentials = fog_credentials_hash(provider)

    config.storage = :fog
    config.fog_directory  = image_bucket_name
    config.fog_public     = opts[:fog_public]                                   # optional, defaults to true
  end
end

.configure_paperclip(image_bucket_name, options = {}, provider = "hp") ⇒ Object

Sets up paperclip for the usage with the anynines service. Creates a bucket with the given name if not already present.

Parameters:

  • image_bucket_name (String)

    the bucket name to use

  • options (Hash) (defaults to: {})

    a hash of additional options



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/a9s_swift/utility.rb', line 21

def self.configure_paperclip(image_bucket_name, options = {}, provider = "hp")
  raise "Paperclip wasn't found in your environment! Please verify that paperclip is included within your Gemfile and loaded correctly." if defined?(Paperclip).nil?

  opts = initialize_options options
  create_new_bucket(image_bucket_name, opts[:fog_public], provider)
  fog_hash = self.fog_credentials_hash(provider)

  # configure paperclip to use the credentials provided by the anynines environment
  Paperclip::Attachment.default_options.update(
    {
      :path => ":class/:id/:attachment/:style/img_:fingerprint",
      :storage => :fog,
      :fog_credentials => fog_hash,
      :fog_directory => image_bucket_name,
      :fog_public => opts[:fog_public],
      :fog_host => "#{SWIFT_HOST}/v1/AUTH_#{fog_hash[:hp_tenant_id]}/#{image_bucket_name}"
    })
end

.create_new_bucket(bucket_name, public_access, provider = "hp") ⇒ Object

Creates a new bucket with the given name if not already present

Parameters:

  • bucket_name (String)

    a name for the bucket

  • public_access (Boolean)

    should the bucket be publicly accessible?

  • provider (String) (defaults to: "hp")

    ‘hp’ or ‘openstack’



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/a9s_swift/utility.rb', line 83

def self.create_new_bucket(bucket_name, public_access, provider = "hp")
  connection = fog_connection provider
  if connection.directories.get(bucket_name).nil?
    puts "The bucket with key=#{bucket_name} wasn't found. Creating bucket with key=#{bucket_name} ."
    bucket = connection.directories.create key: bucket_name

    # set the directory to be public
    bucket.public = public_access
    bucket.save

    connection = nil
    return true
  else
    puts "The bucket with key=#{bucket_name} is already present! Skipping bucket creation."
    return false
  end
end

.fog_connection(provider = "hp") ⇒ Fog::Storage

Returns a fog storage connection to the swift service

Returns:

  • (Fog::Storage)

    a fog storage connection



9
10
11
# File 'lib/a9s_swift/utility.rb', line 9

def self.fog_connection(provider = "hp")
  Fog::Storage.new fog_credentials_hash(provider)
end

.fog_credentials_hash(provider = "hp") ⇒ Object

Returns a fog compatible credentials hash for the swift service

Parameters:

  • provider (String) (defaults to: "hp")

    ‘hp’ or ‘openstack’



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/a9s_swift/utility.rb', line 62

def self.fog_credentials_hash(provider = "hp")
  provider = provider.downcase
  # parse the VCAP_SERVICES environment variable
  services = JSON.parse(ENV["VCAP_SERVICES"])
  raise "Couldn't find the VCAP_SERVICE env variable! Are you running within an anynines environment?" if services.nil?
  raise "Couldn't access the a9s swift service credentials from env! Have you bound a swift service instance to the application?" if services["swift-1.0"].nil?
  swift_service = services["swift-1.0"].first

  if provider == "hp"
    fog_credentials_hash_hp_provider swift_service
  elsif provider == "openstack"
    fog_credentials_hash_openstack_provider swift_service
  else
    raise "No recognized provider. Please use 'hp' or 'openstack' as provider choice."
  end
end

.get_fog_versionObject



13
14
15
# File 'lib/a9s_swift/utility.rb', line 13

def self.get_fog_version
  Gem.loaded_specs['fog'].version.to_s
end