Class: Deltacloud::Drivers::EC2::EC2Driver
Instance Method Summary
collapse
-
#attach_storage_volume(credentials, opts = {}) ⇒ Object
-
#blob_data(credentials, bucket_id, blob_id, opts) ⇒ Object
-
#blobs(credentials, opts = nil) ⇒ Object
-
#buckets(credentials, opts) ⇒ Object
-
#create_blob(credentials, bucket_id, blob_id, data = nil, opts = nil) ⇒ Object
-
#create_bucket(credentials, name, opts = {}) ⇒ Object
-
#create_instance(credentials, image_id, opts = {}) ⇒ Object
-
#create_key(credentials, opts = {}) ⇒ Object
-
#create_storage_snapshot(credentials, opts = {}) ⇒ Object
-
#create_storage_volume(credentials, opts = nil) ⇒ Object
-
#delete_blob(credentials, bucket_id, blob_id, opts = nil) ⇒ Object
-
#delete_bucket(credentials, name, opts = {}) ⇒ Object
-
#destroy_instance(credentials, instance_id) ⇒ Object
(also: #stop_instance)
-
#destroy_key(credentials, opts = {}) ⇒ Object
-
#destroy_storage_snapshot(credentials, opts = {}) ⇒ Object
-
#destroy_storage_volume(credentials, opts = {}) ⇒ Object
-
#detach_storage_volume(credentials, opts = {}) ⇒ Object
-
#images(credentials, opts = {}) ⇒ Object
-
#instances(credentials, opts = {}) ⇒ Object
-
#key(credentials, opts = {}) ⇒ Object
-
#keys(credentials, opts = {}) ⇒ Object
-
#realms(credentials, opts = {}) ⇒ Object
-
#reboot_instance(credentials, instance_id) ⇒ Object
-
#storage_snapshots(credentials, opts = {}) ⇒ Object
-
#storage_volumes(credentials, opts = {}) ⇒ Object
-
#supported_collections ⇒ Object
-
#valid_credentials?(credentials) ⇒ Boolean
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
364
365
366
367
368
369
|
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 364
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
326
327
328
329
330
331
|
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 326
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
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
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)
res = nil
file = File::open(data[:tempfile].path, 'rb')
safely do
res = s3_client.interface.put(bucket_id,
blob_id,
file,
{"Content-Type" => data[:type]})
end
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 176
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
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
388
389
390
391
392
393
|
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 388
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
343
344
345
346
347
348
349
350
351
352
|
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 343
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
320
321
322
323
|
# 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)
s3_client.interface.delete(bucket_id, blob_id)
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
202
203
204
205
206
207
208
209
210
|
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 202
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
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
395
396
397
398
399
400
401
402
|
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 395
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
354
355
356
357
358
359
360
361
362
|
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 354
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
371
372
373
374
375
376
|
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 371
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 132
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
164
165
166
167
168
169
170
171
172
173
174
|
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 164
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
224
225
226
|
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 224
def key(credentials, opts={})
keys(credentials, :id => opts[:id]).first
end
|
#keys(credentials, opts = {}) ⇒ Object
214
215
216
217
218
219
220
221
222
|
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 214
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
154
155
156
157
158
159
160
161
162
|
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 154
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
193
194
195
196
197
198
199
200
|
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 193
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
378
379
380
381
382
383
384
385
386
|
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 378
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
333
334
335
336
337
338
339
340
341
|
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 333
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_collections ⇒ Object
38
39
40
|
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 38
def supported_collections
DEFAULT_COLLECTIONS + [ :keys, :buckets ]
end
|
#valid_credentials?(credentials) ⇒ Boolean
124
125
126
127
128
129
130
|
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 124
def valid_credentials?(credentials)
realms = realms(credentials) rescue false
return realms ? true : false
end
|