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



278
279
280
281
282
283
284
285
286
287
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 278

def attach_storage_volume(credentials, opts)
  ec2 = new_client(credentials)
  id = opts[:id]
  ec2.attach_volume(
                    :volume_id => id,
                    :instance_id => opts[:instance_id],
                    :device => opts[:device]
                    )
  storage_volumes( credentials, :id => id ).first
end

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

– Blob data –



430
431
432
433
434
435
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 430

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

#blobs(credentials, opts = nil) ⇒ Object

– Blobs –



414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 414

def blobs(credentials, opts = nil)
  s3_client = s3_client(credentials)
  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

– Buckets – get a list of your buckets from the s3 service



363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 363

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

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

– Create bucket – valid values for bucket location: ‘EU’|‘us-west1’|‘ap-southeast-1’ - if you don’t specify a location then by default buckets are created in ‘us-east’

but if you do specify ‘us-east’ things blow up


382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 382

def create_bucket(credentials, name, opts={})
  bucket = nil
  safely do
    begin
      s3_client = s3_client(credentials)
      bucket_location = opts['location']
      if bucket_location
        bucket = RightAws::S3::Bucket.create(s3_client, name, true, nil, :location => bucket_location)
      else
        bucket = RightAws::S3::Bucket.create(s3_client, name, true)
      end #if
      rescue RightAws::AwsError => e
        raise e unless e.message =~ /BucketAlreadyExists/
        raise Deltacloud::BackendError.new(409, e.class.to_s, e.message, e.backtrace)
    end #begin
  end #do
  convert_bucket(bucket)
end

#create_instance(credentials, image_id, opts) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 178

def create_instance(credentials, image_id, opts)
  ec2 = new_client( credentials )
  realm_id = opts[:realm_id]
  safely do
    image = image(credentials, :id => image_id )
    hwp = find_hardware_profile(credentials, opts[:hwp_id], image.id)
    ec2_instances = ec2.run_instances(
      :image_id => image.id,
      :user_data => opts[:user_data],
      :key_name => opts[:keyname],
      :availability_zone => realm_id,
      :monitoring_enabled => true,
      :instance_type => hwp.name,
      :disable_api_termination => false,
      :instance_initiated_shutdown_behavior => 'terminate',
      :security_group => opts[:security_group]
    )
    return convert_instance( ec2_instances.instancesSet.item.first, 'pending' )
  end
end

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



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

def create_key(credentials, opts={})
  key = Key.new
  ec2 = new_client( credentials )
  safely do
    key = convert_key(ec2.create_keypair(opts))
  end
  return key
end

#create_storage_volume(credentials, opts) ⇒ Object



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

def create_storage_volume(credentials, opts)
  ec2 = new_client( credentials )
  safely do
    ec2_volume = ec2.create_volume(
      :availability_zone => opts[:realm_id],
      :size => opts[:capacity]
    )
    return convert_volume( ec2_volume )
  end
end

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

– Delete_bucket –



404
405
406
407
408
409
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 404

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

#destroy_instance(credentials, id) ⇒ Object



231
232
233
234
235
236
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 231

def destroy_instance(credentials, id)
  ec2 = new_client(credentials)
  backup = ec2.terminate_instances( :instance_id => id )

  generate_instance(ec2, id, backup)
end

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



344
345
346
347
348
349
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 344

def destroy_key(credentials, opts={})
  safely do
    ec2 = new_client( credentials )
    ec2.delete_keypair(opts)
  end
end

#destroy_storage_volume(credentials, id) ⇒ Object



272
273
274
275
276
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 272

def destroy_storage_volume(credentials, id)
  ec2 = new_client(credentials)
  ec2.delete_volume( :volume_id => id )
  storage_volumes( credentials, :id => id ).first
end

#detach_storage_volume(credentials, id) ⇒ Object



289
290
291
292
293
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 289

def detach_storage_volume(credentials, id)
  ec2 = new_client(credentials)
  ec2.detach_volume( :volume_id => id )
  storage_volumes( credentials, :id => id ).first
end

#generate_instance(ec2, id, backup) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 199

def generate_instance(ec2, id, backup)
  begin
    this_instance = ec2.describe_instances( :instance_id => id ).reservationSet.item.first.instancesSet.item.first
    convert_instance(this_instance, this_instance.ownerId)
  rescue Exception => e
    puts "WARNING: ignored error during instance refresh: #{e.message}"
    # at this point, the action has succeeded but our follow-up
    # "describe_instances" failed for some reason.  Create a simple Instance
    # object with only the ID and new state in place
    state = convert_state(backup.instancesSet.item.first.currentState.name)
    Instance.new( {
      :id => id,
      :state => state,
      :actions => instance_actions_for( state ),
    } )
  end
end

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

Images



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 121

def images(credentials, opts={} )
  ec2 = new_client(credentials)
  img_arr = []
  # if we know the image_id, we don't want to limit by owner_id, since this
  # will exclude public images
  if (opts and opts[:id])
    config = { :image_id => opts[:id] }
  else
    config = { :owner_id => "amazon" }
    config.merge!({ :owner_id => opts[:owner_id] }) if opts and opts[:owner_id]
  end
  safely do
    ec2.describe_images(config).imagesSet.item.each do |image|
      img_arr << convert_image(image)
    end
  end
  img_arr = filter_on( img_arr, :architecture, opts )
  img_arr.sort_by{|e| [e.owner_id, e.name]}
end

#instances(credentials, opts = nil) ⇒ Object

Instances



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 159

def instances(credentials, opts=nil)
  ec2 = new_client(credentials)
  instances = []
  safely do
    param = opts.nil? ? nil : opts[:id]
    ec2_instances = ec2.describe_instances.reservationSet
    return [] unless ec2_instances
    ec2_instances.item.each do |item|
      item.instancesSet.item.each do |ec2_instance|
        instances << convert_instance( ec2_instance, item.ownerId )
      end
    end
  end
  instances = filter_on( instances, :id, opts )
  instances = filter_on( instances, :state, opts )
  instances
end

#key(credentials, opts = nil) ⇒ Object



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

def key(credentials, opts=nil)
  keys(credentials, opts).first
end

#keys(credentials, opts = nil) ⇒ Object



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

def keys(credentials, opts=nil)
  ec2 = new_client( credentials )
  opts[:key_name] = opts[:id] if opts and opts[:id]
  keypairs = ec2.describe_keypairs(opts || {})
  result = []
  safely do
    keypairs.keySet.item.each do |keypair|
      result << convert_key(keypair)
    end
  end
  result
end

#realms(credentials, opts = nil) ⇒ Object

Realms



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

def realms(credentials, opts=nil)
  ec2 = new_client(credentials)
  realms = []
  safely do
    ec2.describe_availability_zones.availabilityZoneInfo.item.each do |ec2_realm|
      realms << convert_realm( ec2_realm )
    end
  end
  realms
end

#reboot_instance(credentials, id) ⇒ Object



217
218
219
220
221
222
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 217

def reboot_instance(credentials, id)
  ec2 = new_client(credentials)
  backup = ec2.reboot_instances( :instance_id => id )

  generate_instance(ec2, id, backup)
end

#stop_instance(credentials, id) ⇒ Object



224
225
226
227
228
229
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 224

def stop_instance(credentials, id)
  ec2 = new_client(credentials)
  backup = ec2.terminate_instances( :instance_id => id )

  generate_instance(ec2, id, backup)
end

#storage_snapshots(credentials, opts = nil) ⇒ Object

Storage Snapshots



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 299

def storage_snapshots(credentials, opts=nil)
  ec2 = new_client( credentials )
  snapshots = []
  safely do
    if (opts)
      ec2.describe_snapshots(:owner => 'self', :snapshot_id => opts[:id]).snapshotSet.item.each do |ec2_snapshot|
        snapshots << convert_snapshot( ec2_snapshot )
      end
    else
      ec2_snapshots = ec2.describe_snapshots(:owner => 'self').snapshotSet
      return [] unless ec2_snapshots
      ec2_snapshots.item.each do |ec2_snapshot|
        snapshots << convert_snapshot( ec2_snapshot )
      end
    end
  end
  snapshots
end

#storage_volumes(credentials, opts = nil) ⇒ Object

Storage Volumes



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 242

def storage_volumes(credentials, opts=nil)
  ec2 = new_client( credentials )
  volumes = []
  safely do
    if (opts)
      ec2.describe_volumes(:volume_id => opts[:id]).volumeSet.item.each do |ec2_volume|
        volumes << convert_volume( ec2_volume )
      end
    else
      ec2_volumes = ec2.describe_volumes.volumeSet
      return [] unless ec2_volumes
      ec2_volumes.item.each do |ec2_volume|
        volumes << convert_volume( ec2_volume )
      end
    end
  end
  volumes
end

#supported_collectionsObject



40
41
42
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 40

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

#valid_credentials?(credentials) ⇒ Boolean

Returns:

  • (Boolean)


351
352
353
354
355
356
357
358
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 351

def valid_credentials?(credentials)
  client = new_client(credentials)
  # FIXME: We need to do this call to determine if
  #        EC2 is working with given credentials. There is no
  #        other way to check, if given credentials are valid or not.
  realms = client.describe_availability_zones rescue false
  return realms ? true : false
end