Class: Deltacloud::Drivers::Rackspace::RackspaceDriver

Inherits:
BaseDriver
  • Object
show all
Defined in:
lib/deltacloud/drivers/rackspace/rackspace_driver.rb

Constant Summary

Constants inherited from BaseDriver

BaseDriver::MEMBER_SHOW_METHODS, BaseDriver::STATE_MACHINE_OPTS

Constants included from Deltacloud

API_VERSION, CIMI_API_VERSION

Instance Method Summary collapse

Methods inherited from BaseDriver

#address, #api_provider, #blob, #bucket, #catched_exceptions_list, #configured_providers, constraints, define_hardware_profile, define_instance_states, driver_name, feature, features, #filter_hardware_profiles, #filter_on, #find_hardware_profile, #firewall, #hardware_profile, hardware_profiles, #has_capability?, #has_feature?, has_feature?, #image, #instance, #instance_actions_for, instance_state_machine, #instance_state_machine, #key, #name, #realm, #storage_snapshot, #storage_volume, #supported_collections, #valid_credentials?

Methods included from Deltacloud

[], config, configure, connect, database, default_frontend, drivers, enabled_frontends, frontend_required?, frontends, generate_routes, initialize_database, need_database?, new, require_frontend!

Methods included from CollectionMethods

#collection_exists?, #collection_names, #collections

Methods included from Exceptions

exception_from_status, exceptions, included, logger, #safely

Instance Method Details

#blob_data(credentials, bucket_id, blob_id, opts = {}) ⇒ Object

- Blob data -



254
255
256
257
258
259
# File 'lib/deltacloud/drivers/rackspace/rackspace_driver.rb', line 254

def blob_data(credentials, bucket_id, blob_id, opts = {})
  cf = cloudfiles_client(credentials)
  cf.container(bucket_id).object(blob_id).data_stream do |chunk|
    yield chunk
  end
end

#blob_metadata(credentials, opts = {}) ⇒ Object

- Blob Metadada -



300
301
302
303
304
305
306
307
# File 'lib/deltacloud/drivers/rackspace/rackspace_driver.rb', line 300

def (credentials, opts = {})
  cf = cloudfiles_client(credentials)
  meta = {}
  safely do
    meta = cf.container(opts['bucket']).object(opts[:id]).
  end
  meta
end

#blob_stream_connection(params) ⇒ Object

params: :user,:password,:bucket,:blob,:content_type,:content_length,:metadata



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/deltacloud/drivers/rackspace/rackspace_driver.rb', line 324

def blob_stream_connection(params)
  #create a cloudfiles connection object to get the authtoken
  cf, cf_host, cf_path, cf_authtoken = nil
  safely do
    cf = CloudFiles::Connection.new(:username => params[:user],
                              :api_key => params[:password])
    cf_authtoken = cf.authtoken
    cf_host = cf.storagehost
    cf_path = cf.storagepath
  end
  provider = "https://#{cf_host}"
  uri = URI.parse(provider)
  http = Net::HTTP.new(uri.host, uri.port )
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  request = Net::HTTP::Put.new("#{cf_path}/#{params[:bucket]}/#{params[:blob]}")
  request['Host'] = "#{cf_host}"
  request['X-Auth-Token'] = "#{cf_authtoken}"
  request['Content-Type'] = params[:content_type]
  request['Content-Length'] = params[:content_length]
  request['Expect'] = "100-continue"
   = params[:metadata] || {}
  BlobHelper::(, 'X-Object-Meta-')
  .each{|k,v| request[k] = v}
  return http, request
end

#blobs(credentials, opts = {}) ⇒ Object

– Blobs –



238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/deltacloud/drivers/rackspace/rackspace_driver.rb', line 238

def blobs(credentials, opts = {})
  cf = cloudfiles_client(credentials)
  blobs = []
  safely do
    cf_container = cf.container(opts['bucket'])
    cf_container.objects.each do |object_name|
      blobs << convert_object(cf_container.object(object_name))
    end
  end
  blobs = filter_on(blobs, :id, opts)
  blobs
end

#buckets(credentials, opts = {}) ⇒ Object

– Buckets –



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/deltacloud/drivers/rackspace/rackspace_driver.rb', line 196

def buckets(credentials, opts = {})
  bucket_list = []
  cf = cloudfiles_client(credentials)
  safely do
    unless (opts[:id].nil?)
      bucket = cf.container(opts[:id])
      bucket_list << convert_container(bucket)
    else
      cf.containers.each do |container_name|
        bucket_list << Bucket.new({:id => container_name, :name => container_name})
      end #containers.each
    end #unless
  end #safely
  filter_on(bucket_list, :id, opts)
end

#create_blob(credentials, bucket_id, blob_id, blob_data, opts = {}) ⇒ Object

– Create Blob –



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/deltacloud/drivers/rackspace/rackspace_driver.rb', line 264

def create_blob(credentials, bucket_id, blob_id, blob_data, opts={})
  cf = cloudfiles_client(credentials)
  #insert ec2-specific header for user metadata ... X-Object-Meta-KEY = VALUE
  BlobHelper::(opts, "X-Object-Meta-")
  opts['Content-Type'] = blob_data[:type]
  object = nil
  safely do
    #must first create the object using cloudfiles_client.create_object
    #then can write using object.write(data)
    object = cf.container(bucket_id).create_object(blob_id)
    #blob_data is a construct with data in .tempfile and content-type in {:type}
    res = object.write(blob_data[:tempfile], opts)
  end
  Blob.new( { :id => object.name,
              :bucket => object.container.name,
              :content_length => blob_data[:tempfile].length,
              :content_type => blob_data[:type],
              :last_modified => '',
              :user_metadata => opts.select{|k,v| k.match(/^X-Object-Meta-/i)}
            }
          )
end

#create_bucket(credentials, name, opts = {}) ⇒ Object

– Create Bucket –



215
216
217
218
219
220
221
222
223
# File 'lib/deltacloud/drivers/rackspace/rackspace_driver.rb', line 215

def create_bucket(credentials, name, opts = {})
  bucket = nil
  cf = cloudfiles_client(credentials)
  safely do
    new_bucket = cf.create_container(name)
    bucket = convert_container(new_bucket)
  end
  bucket
end

#create_image(credentials, opts = {}) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/deltacloud/drivers/rackspace/rackspace_driver.rb', line 97

def create_image(credentials, opts={})
  rs = new_client(credentials)
  safely do
    server = rs.get_server(opts[:id].to_i)
    image = server.create_image(opts[:name])
    Image.new(
      :id => image.id.to_s,
      :name => opts[:name] || image.name,
      :description => opts[:description] || image.description,
      :owner_id => credentials.user,
      :state => image.status,
      :architecture => 'x86_64'
    )
  end
end

#create_instance(credentials, image_id, opts) ⇒ Object

create instance. Default to flavor 1 - really need a name though… In rackspace, all flavors work with all images.



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/deltacloud/drivers/rackspace/rackspace_driver.rb', line 83

def create_instance(credentials, image_id, opts)
  rs = new_client( credentials )
  result = nil
  params = extract_personality(opts)
  params[:name] = opts[:name] || Time.now.to_s
  params[:imageId] = image_id.to_i
  params[:flavorId] = (opts[:hwp_id] && opts[:hwp_id].length>0) ? opts[:hwp_id].to_i : 1
  safely do
    server = rs.create_server(params)
    result = convert_instance_after_create(server, credentials.user, server.adminPass)
  end
  result
end

#delete_blob(credentials, bucket_id, blob_id, opts = {}) ⇒ Object

– Delete Blob –



290
291
292
293
294
295
# File 'lib/deltacloud/drivers/rackspace/rackspace_driver.rb', line 290

def delete_blob(credentials, bucket_id, blob_id, opts={})
  cf = cloudfiles_client(credentials)
  safely do
    cf.container(bucket_id).delete_object(blob_id)
  end
end

#delete_bucket(credentials, name, opts = {}) ⇒ Object

– Delete Bucket –



228
229
230
231
232
233
# File 'lib/deltacloud/drivers/rackspace/rackspace_driver.rb', line 228

def delete_bucket(credentials, name, opts = {})
  cf = cloudfiles_client(credentials)
  safely do
    cf.delete_container(name)
  end
end

#destroy_image(credentials, image_id) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/deltacloud/drivers/rackspace/rackspace_driver.rb', line 113

def destroy_image(credentials, image_id)
  rax_client = new_client(credentials)
  safely do
    image = rax_client.get_image(image_id.to_i)
    unless image.delete!
      raise "ERROR: Cannot delete image with ID:#{image_id}"
    end
  end
end

#destroy_instance(credentials, instance_id) ⇒ Object Also known as: stop_instance



146
147
148
149
150
151
152
153
# File 'lib/deltacloud/drivers/rackspace/rackspace_driver.rb', line 146

def destroy_instance(credentials, instance_id)
  rs = new_client(credentials)
  safely do
    server = rs.get_server(instance_id.to_i)
    server.delete!
    convert_instance_after_create(server, credentials.user)
  end
end

#hardware_profiles(credentials, opts = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/deltacloud/drivers/rackspace/rackspace_driver.rb', line 35

def hardware_profiles(credentials, opts = {})
  rs = new_client( credentials )
  results = []
  safely do
    results = rs.list_flavors.collect do |f|
      HardwareProfile.new(f[:id].to_s) do
        architecture 'x86_64'
        memory f[:ram].to_i
        storage f[:disk].to_i
      end
    end
  end
  filter_hardware_profiles(results, opts)
end

#images(credentials, opts = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/deltacloud/drivers/rackspace/rackspace_driver.rb', line 50

def images(credentials, opts=nil)
  rs = new_client(credentials)
  results = []
  safely do
    results = rs.list_images.collect do |img|
      Image.new(
        :id => img[:id].to_s,
        :name => img[:name],
        :description => img[:name],
        :owner_id => credentials.user,
        :state => img[:status],
        :architecture => 'x86_64'
      )
    end
  end
  profiles = hardware_profiles(credentials)
  results.each { |img| img.hardware_profiles = profiles }
  filter_on( results, :id, opts )
end

#instances(credentials, opts = {}) ⇒ Object

Instances



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/deltacloud/drivers/rackspace/rackspace_driver.rb', line 160

def instances(credentials, opts={})

  rs = new_client(credentials)
  insts = []

  safely do
    begin
      if opts[:id]
        server = rs.get_server(opts[:id].to_i)
        insts << convert_instance_after_create(server, credentials.user)
      else
        insts = rs.list_servers_detail.collect do |server|
          convert_instance(server, credentials.user)
        end
      end
    rescue CloudServers::Exception::ItemNotFound
    end
  end

  insts = filter_on( insts, :id, opts )
  insts = filter_on( insts, :state, opts )
  insts
end

#realms(credentials, opts = nil) ⇒ Object

rackspace does not at this stage have realms… its all US/TX, all the time (at least at time of writing)



71
72
73
74
75
76
77
# File 'lib/deltacloud/drivers/rackspace/rackspace_driver.rb', line 71

def realms(credentials, opts=nil)
  [Realm.new( {
    :id=>"us",
    :name=>"United States",
    :state=> "AVAILABLE"
  } )]
end

#reboot_instance(credentials, instance_id) ⇒ Object



137
138
139
140
141
142
143
144
# File 'lib/deltacloud/drivers/rackspace/rackspace_driver.rb', line 137

def reboot_instance(credentials, instance_id)
  rs = new_client(credentials)
  safely do
    server = rs.get_server(instance_id.to_i)
    server.reboot!
    convert_instance_after_create(server, credentials.user)
  end
end

#run_on_instance(credentials, opts = {}) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/deltacloud/drivers/rackspace/rackspace_driver.rb', line 123

def run_on_instance(credentials, opts={})
  target = instance(credentials, :id => opts[:id])
  param = {}
  param[:credentials] = {
    :username => 'root',
    :password => opts[:password]
  }
  param[:port] = opts[:port] || '22'
  param[:ip] = opts[:ip] || target.public_addresses.first.address
  safely do
    Deltacloud::Runner.execute(opts[:cmd], param)
  end
end

#update_blob_metadata(credentials, opts = {}) ⇒ Object

- Update Blob Metahash -



312
313
314
315
316
317
318
319
320
321
# File 'lib/deltacloud/drivers/rackspace/rackspace_driver.rb', line 312

def (credentials, opts={})
  cf = cloudfiles_client(credentials)
  meta_hash = opts['meta_hash']
  #the set_metadata method actually places the 'X-Object-Meta-' prefix for us:
  BlobHelper::(meta_hash, '')
  safely do
    blob = cf.container(opts['bucket']).object(opts[:id])
    blob.(meta_hash)
  end
end