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

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

#create_instance(credentials, image_id, opts) ⇒ Object



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

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



298
299
300
301
302
303
304
305
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 298

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

#destroy_instance(credentials, id) ⇒ Object



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

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



307
308
309
310
311
312
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 307

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

#generate_instance(ec2, id, backup) ⇒ Object



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

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 = backup.instancesSet.item.first.currentState.name
    Instance.new( {
      :id => id,
      :state => state,
      :actions => instance_actions_for( state ),
    } )
  end
end

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

Images



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

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



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

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



281
282
283
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 281

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

#keys(credentials, opts = nil) ⇒ Object



285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 285

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



142
143
144
145
146
147
148
149
150
151
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 142

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



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

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



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

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



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 262

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



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

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



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

def supported_collections
  DEFAULT_COLLECTIONS + [ :keys ]
end