Class: Deltacloud::Drivers::EC2::EC2Driver

Inherits:
BaseDriver
  • Object
show all
Defined in:
lib/deltacloud/drivers/ec2/ec2_driver.rb

Instance Method Summary collapse

Methods inherited from BaseDriver

#blob, #bucket, declare_feature, define_hardware_profile, define_instance_states, feature, feature_decl_for, feature_decls, #features, features, #filter_hardware_profiles, #filter_on, #find_hardware_profile, #hardware_profile, hardware_profiles, #hardware_profiles, #has_collection?, #image, #instance, #instance_actions_for, instance_state_machine, #instance_state_machine, #realm, #safely, #start_instance, #storage_snapshot, #storage_volume

Instance Method Details

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



356
357
358
359
360
361
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 356

def attach_storage_volume(credentials, opts={})
  ec2 = new_client(credentials)
  safely do
    convert_volume(ec2.attach_volume(opts[:id], opts[:instance_id], opts[:device]))
  end
end

#blob_data(credentials, bucket_id, blob_id, opts) ⇒ Object



318
319
320
321
322
323
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 318

def blob_data(credentials, bucket_id, blob_id, opts)
  s3_client = new_client(credentials, :s3)
  s3_client.interface.get(bucket_id, blob_id) do |chunk|
    yield chunk
  end
end

#blobs(credentials, opts = nil) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 270

def blobs(credentials, opts = nil)
  s3_client = new_client(credentials, :s3)
  blobs = []
  safely do
    s3_bucket = s3_client.bucket(opts['bucket'])
    s3_bucket.keys({}, true).each do |s3_object|
      blobs << convert_object(s3_object)
    end
  end
  blobs = filter_on(blobs, :id, opts)
  blobs
end

#buckets(credentials, opts) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 237

def buckets(credentials, opts)
  buckets = []
  safely do
    s3_client = new_client(credentials, :s3)
    bucket_list = s3_client.buckets
    bucket_list.each do |current|
      buckets << convert_bucket(current)
    end
  end
  filter_on(buckets, :id, opts)
end

#create_blob(credentials, bucket_id, blob_id, data = nil, opts = nil) ⇒ Object

– Create Blob –



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 286

def create_blob(credentials, bucket_id, blob_id, data = nil, opts = nil)
  s3_client = new_client(credentials, :s3)
  #data is a construct with the temporary file created by server @.tempfile
  #also file[:type] will give us the content-type
  res = nil
  # File stream needs to be reopened in binary mode for whatever reason
  file = File::open(data[:tempfile].path, 'rb')
  safely do
    res = s3_client.interface.put(bucket_id, 
                                blob_id, 
                                file, 
                                {"Content-Type" => data[:type]})
  end
  #create a new Blob object and return that
  Blob.new( { :id => blob_id,
              :bucket => bucket_id,
              :content_length => data[:tempfile].length,
              :content_type => data[:type],
              :last_modified => ''
            }
          )
end

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



249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 249

def create_bucket(credentials, name, opts={})
  bucket = nil
  safely do
    s3_client = new_client(credentials, :s3)
    bucket_location = opts['location']
    if bucket_location
      bucket = Aws::S3::Bucket.create(s3_client, name, true, nil, :location => bucket_location)
    else
      bucket = Aws::S3::Bucket.create(s3_client, name, true)
    end
  end
  convert_bucket(bucket)
end

#create_instance(credentials, image_id, opts = {}) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 168

def create_instance(credentials, image_id, opts={})
  ec2 = new_client(credentials)
  instance_options = {}
  instance_options.merge!(:user_data => opts[:user_data]) if opts[:user_data]
  instance_options.merge!(:key_name => opts[:key_name]) if opts[:key_name]
  instance_options.merge!(:availability_zone => opts[:realm_id]) if opts[:realm_id]
  instance_options.merge!(:instance_type => opts[:hwp_id]) if opts[:hwp_id]
  instance_options.merge!(:group_ids => opts[:security_group]) if opts[:security_group]
  safely do
    new_instance = convert_instance(ec2.launch_instances(image_id, instance_options).first)
    if opts[:public_ip]
      ec2.associate_address(new_instance.id, opts[:public_ip])
    end
    new_instance
  end
end

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



220
221
222
223
224
225
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 220

def create_key(credentials, opts={})
  ec2 = new_client(credentials)
  safely do
    convert_key(ec2.create_key_pair(opts[:key_name]))
  end
end

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



380
381
382
383
384
385
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 380

def create_storage_snapshot(credentials, opts={})
  ec2 = new_client(credentials)
  safely do
    convert_snapshot(ec2.try_create_snapshot(opts[:volume_id]))
  end
end

#create_storage_volume(credentials, opts = nil) ⇒ Object



335
336
337
338
339
340
341
342
343
344
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 335

def create_storage_volume(credentials, opts=nil)
  ec2 = new_client(credentials)
  opts ||= {}
  opts[:snapshot_id] ||= ""
  opts[:capacity] ||= "1"
  opts[:realm_id] ||= realms(credentials).first.id
  safely do
    convert_volume(ec2.create_volume(opts[:snapshot_id], opts[:capacity], opts[:realm_id]))
  end
end

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

– Delete Blob –



312
313
314
315
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 312

def delete_blob(credentials, bucket_id, blob_id, opts=nil)
  s3_client = new_client(credentials, :s3)
  s3_client.interface.delete(bucket_id, blob_id)
end

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



263
264
265
266
267
268
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 263

def delete_bucket(credentials, name, opts={})
  s3_client = new_client(credentials, :s3)
  safely do
    s3_client.interface.delete_bucket(name)
  end
end

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



194
195
196
197
198
199
200
201
202
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 194

def destroy_instance(credentials, instance_id)
  ec2 = new_client(credentials)
  puts "Terminating instance #{instance_id}"
  if ec2.terminate_instances([instance_id])
    instance(credentials, instance_id)
  else
    raise Deltacloud::BackendError.new(500, "Instance", "Instance cannot be terminated", "")
  end
end

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



227
228
229
230
231
232
233
234
235
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 227

def destroy_key(credentials, opts={})
  ec2 = new_client(credentials)
  original_key = key(credentials, opts)
  safely do
    ec2.delete_key_pair(original_key.id)
    original_key= original_key.state = "DELETED"
  end
  original_key
end

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



387
388
389
390
391
392
393
394
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 387

def destroy_storage_snapshot(credentials, opts={})
  ec2 = new_client(credentials)
  safely do
    unless convert_snapshot(opts[:id])
      raise Deltacloud::BackendError.new(500, "StorageSnapshot", "Cannot destroy this snapshot")
    end
  end
end

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



346
347
348
349
350
351
352
353
354
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 346

def destroy_storage_volume(credentials, opts={})
  ec2 = new_client(credentials)
  safely do
    unless ec2.delete_volume(opts[:id]) 
      raise Deltacloud::BackendError.new(500, "StorageVolume", "Cannot delete storage volume")
    end
    storage_volume(credentials, opts[:id])
  end
end

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



363
364
365
366
367
368
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 363

def detach_storage_volume(credentials, opts={})
  ec2 = new_client(credentials)
  safely do
    convert_volume(ec2.detach_volume(opts[:id], opts[:instance_id], opts[:device], true))
  end
end

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



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 124

def images(credentials, opts={})
  ec2 = new_client(credentials)
  img_arr = []
  opts ||= {}
  if opts[:id]
    safely do
      img_arr = ec2.describe_images(opts[:id]).collect do |image|
        convert_image(image)
      end
    end
  else
    owner_id = opts[:owner_id] || "amazon"
    safely do
      img_arr = ec2.describe_images_by_owner(owner_id, "machine").collect do |image|
        convert_image(image)
      end
    end
  end
  img_arr = filter_on( img_arr, :architecture, opts )
  img_arr.sort_by { |e| [e.owner_id, e.name] }
end

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



156
157
158
159
160
161
162
163
164
165
166
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 156

def instances(credentials, opts={})
  ec2 = new_client(credentials)
  inst_arr = []
  safely do
    inst_arr = ec2.describe_instances.collect do |instance| 
      convert_instance(instance) if instance
    end.flatten
  end
  inst_arr = filter_on( inst_arr, :id, opts )
  filter_on( inst_arr, :state, opts )
end

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



216
217
218
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 216

def key(credentials, opts={})
  keys(credentials, :id => opts[:id]).first
end

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



206
207
208
209
210
211
212
213
214
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 206

def keys(credentials, opts={})
  ec2 = new_client(credentials)
  opts ||= {}
  safely do
    ec2.describe_key_pairs(opts[:id] || nil).collect do |key|
      convert_key(key)
    end
  end
end

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



146
147
148
149
150
151
152
153
154
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 146

def realms(credentials, opts={})
  ec2 = new_client(credentials)
  zone_id = opts ? opts[:id] : nil
  safely do
    return ec2.describe_availability_zones(zone_id).collect do |realm|
      convert_realm(realm)
    end
  end
end

#reboot_instance(credentials, instance_id) ⇒ Object



185
186
187
188
189
190
191
192
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 185

def reboot_instance(credentials, instance_id)
  ec2 = new_client(credentials)
  if ec2.reboot_instances([instance_id])
    instance(credentials, instance_id)
  else
    raise Deltacloud::BackendError.new(500, "Instance", "Instance reboot failed", "")
  end
end

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



370
371
372
373
374
375
376
377
378
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 370

def storage_snapshots(credentials, opts={})
  ec2 = new_client(credentials)
  snapshot_list = (opts and opts[:id]) ? opts[:id] : []
  safely do
    ec2.describe_snapshots(snapshot_list).collect do |snapshot|
      convert_snapshot(snapshot)
    end
  end
end

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



325
326
327
328
329
330
331
332
333
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 325

def storage_volumes(credentials, opts={})
  ec2 = new_client( credentials )
  volume_list = (opts and opts[:id]) ? opts[:id] : nil
  safely do
    ec2.describe_volumes(volume_list).collect do |volume|
      convert_volume(volume)
    end
  end
end

#supported_collectionsObject



38
39
40
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 38

def supported_collections
  DEFAULT_COLLECTIONS + [ :keys, :buckets ]
end