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

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

Constant Summary collapse

DEFAULT_REGION =
'us-east-1'

Constants inherited from BaseDriver

BaseDriver::MEMBER_SHOW_METHODS

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, #features_for_operation, #filter_hardware_profiles, #filter_on, #find_hardware_profile, #hardware_profile, #hardware_profiles, hardware_profiles, #has_capability?, #has_collection?, #image, #instance, #instance_actions_for, instance_state_machine, #instance_state_machine, #key, #realm, #safely, #storage_snapshot, #storage_volume

Instance Method Details

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



368
369
370
371
372
373
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 368

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



328
329
330
331
332
333
334
335
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 328

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

#blobs(credentials, opts = nil) ⇒ Object



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

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



245
246
247
248
249
250
251
252
253
254
255
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 245

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 –



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

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



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

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



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

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[:name]
      tag_instance(credentials, new_instance, opts[:name])
    end
    new_instance
  end
end

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



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

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



392
393
394
395
396
397
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 392

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



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

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 –



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

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

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



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

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



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

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

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



235
236
237
238
239
240
241
242
243
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 235

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



399
400
401
402
403
404
405
406
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 399

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



358
359
360
361
362
363
364
365
366
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 358

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



375
376
377
378
379
380
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 375

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
167
168
169
170
171
172
173
174
175
176
# 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
    tags = ec2.describe_tags(
      'Filter.1.Name' => 'resource-type', 'Filter.1.Value' => 'instance'
    )
    inst_arr.each do |inst|
      name_tag = tags.select { |t| (t[:aws_resource_id] == inst.id) and t[:aws_key] == 'name' }
      unless name_tag.empty?
        inst.name = name_tag.first[:aws_value]
      end
    end
    delete_unused_tags(credentials, inst_arr.collect {|inst| inst.id})
  end
  inst_arr = filter_on( inst_arr, :id, opts )
  filter_on( inst_arr, :state, opts )
end

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



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

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



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

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



382
383
384
385
386
387
388
389
390
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 382

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



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

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



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

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

#valid_credentials?(credentials) ⇒ Boolean

Returns:

  • (Boolean)


408
409
410
411
412
413
414
415
416
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 408

def valid_credentials?(credentials)
  retval = true
  begin
    realms(credentials)
  rescue Deltacloud::BackendError
    retval = false
  end
  retval
end