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, BaseDriver::STATE_MACHINE_OPTS

Constants included from Deltacloud

API_VERSION, CIMI_API_VERSION

Instance Method Summary collapse

Methods inherited from BaseDriver

#address, #api_provider, #blob, #bucket, #catched_exceptions_list, constraints, define_hardware_profile, define_instance_states, driver_name, feature, features, #filter_hardware_profiles, #filter_on, #find_hardware_profile, #firewall, #hardware_profile, #hardware_profiles, hardware_profiles, #has_capability?, #has_feature?, has_feature?, #image, #instance_actions_for, #instance_state_machine, instance_state_machine, #key, #name, #realm, #storage_snapshot, #storage_volume, #supported_collections

Methods included from Deltacloud

[], config, configure, connect, database, default_frontend, drivers, enabled_frontends, frontend_required?, frontends, generate_routes, initialize_database, need_database?, new, require_frontend!

Methods included from CollectionMethods

#collection_exists?, #collection_names, #collections

Methods included from Exceptions

exception_from_status, exceptions, included, logger, #safely

Instance Method Details

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



720
721
722
723
724
725
726
727
728
729
730
731
732
733
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 720

def addresses(credentials, opts={})
  ec2 = new_client(credentials)
  address_id = (opts and opts[:id]) ? [opts[:id]] : []
  safely do
    begin
      ec2.describe_addresses(address_id).collect do |address|
        Address.new(:id => address[:public_ip], :instance_id => address[:instance_id])
      end
    rescue Exception => e
      return [] if e.message =~ /InvalidAddress\.NotFound:/
      raise e
    end
  end
end

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



749
750
751
752
753
754
755
756
757
758
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 749

def associate_address(credentials, opts={})
  ec2 = new_client(credentials)
  safely do
    if ec2.associate_address(opts[:instance_id], opts[:id])
      Address.new(:id => opts[:id], :instance_id => opts[:instance_id])
    else
      raise "ERROR: Cannot associate IP address to an Instance"
    end
  end
end

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



672
673
674
675
676
677
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 672

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



594
595
596
597
598
599
600
601
602
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 594

def blob_data(credentials, bucket_id, blob_id, opts={})
  s3_client = new_client(credentials, :s3)
  s3_client = get_bucket_with_endpoint(s3_client, credentials, bucket_id)[1]
  safely do
    s3_client.interface.get(bucket_id, blob_id) do |chunk|
      yield chunk
    end
  end
end

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



574
575
576
577
578
579
580
581
582
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 574

def (credentials, opts={})
  s3_client = new_client(credentials, :s3)
  blob_meta = {}
  safely do
    the_bucket, s3_client = get_bucket_with_endpoint(s3_client, credentials, opts['bucket'])
    the_blob = the_bucket.key(opts[:id], true)
    blob_meta = the_blob.meta_headers
  end
end

#blob_segment_id(request, response) ⇒ Object



560
561
562
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 560

def blob_segment_id(request, response)
  response["etag"].gsub("\"", "")
end

#blob_stream_connection(params) ⇒ Object

params: :user,:password,:bucket,:blob,:content_type,:content_length,:metadata



605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 605

def blob_stream_connection(params)
  #canonicalise metadata:
  #http://docs.amazonwebservices.com/AmazonS3/latest/dev/index.html?RESTAuthentication.html
   = params[:metadata] || {}
  signature_meta_string = ""
  BlobHelper::(, 'x-amz-meta-')
  keys_array = .keys.sort!
  keys_array.each {|k| signature_meta_string << "#{k}:#{[k]}\n"}
  provider = "https://#{endpoint_for_service(:s3)}"
  uri = URI.parse(provider)
  http = Net::HTTP.new("#{params[:bucket]}.#{uri.host}", uri.port )
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  timestamp = Time.now.httpdate
  string_to_sign =
    "PUT\n\n#{params[:content_type]}\n#{timestamp}\n#{signature_meta_string}/#{params[:bucket]}/#{params[:blob]}"
  if BlobHelper.segmented_blob_op_type(params[:context]) == "segment"
    partNumber = BlobHelper.segment_order(params[:context])
    uploadId = BlobHelper.segmented_blob_id(params[:context])
    segment_string = "?partNumber=#{partNumber}&uploadId=#{uploadId}"
    string_to_sign << segment_string
    request = Net::HTTP::Put.new("/#{params[:blob]}#{segment_string}")
  else
    request = Net::HTTP::Put.new("/#{params[:blob]}")
  end
  auth_string = Aws::Utils::sign(params[:password], string_to_sign)
  request['Host'] = "#{params[:bucket]}.#{uri.host}"
  request['Date'] = timestamp
  request['Content-Type'] = params[:content_type]
  request['Content-Length'] = params[:content_length]
  request['Authorization'] = "AWS #{params[:user]}:#{auth_string}"
  request['Expect'] = "100-continue"
  .each{|k,v| request[k] = v}
  return http, request
end

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



499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 499

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

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



459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 459

def buckets(credentials, opts={})
  buckets = []
  safely do
    s3_client = new_client(credentials, :s3)
    unless (opts[:id].nil?)
      bucket, s3_client = get_bucket_with_endpoint(s3_client, credentials, opts[:id])
      buckets << convert_bucket(bucket)
    else
      bucket_list = s3_client.buckets
      bucket_list.each do |current|
        buckets << Bucket.new({:name => current.name, :id => current.name})
      end
    end
  end
  filter_on(buckets, :id, opts)
end

#configured_providersObject



844
845
846
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 844

def configured_providers
  Deltacloud::Drivers::driver_config[:ec2][:entrypoints]["ec2"].keys
end

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



735
736
737
738
739
740
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 735

def create_address(credentials, opts={})
  ec2 = new_client(credentials)
  safely do
    Address.new(:id => ec2.allocate_address)
  end
end

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

– Create Blob - NON Streaming way (i.e. was called with POST html multipart form data) – also called for segmented blobs - as final call with blob manifest



520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 520

def create_blob(credentials, bucket_id, blob_id, data = nil, opts = {})
  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
  if(opts[:segment_manifest])
    safely do
      s3_client.interface.complete_multipart(bucket_id, blob_id, opts[:segmented_blob_id], opts[:segment_manifest])
    end
  else
    # File stream needs to be reopened in binary mode
    file = File::open(data[:tempfile].path, 'rb')
    #insert ec2-specific header for user metadata ... x-amz-meta-KEY = VALUE
    BlobHelper::(opts, 'x-amz-meta-')
    opts["Content-Type"] = data[:type]
    safely do
      s3_client.interface.put(bucket_id,
                                  blob_id,
                                  file,
                                  opts)
    end
  end
  #create a new Blob object and return that
  Blob.new( { :id => blob_id,
              :bucket => bucket_id,
              :content_length => ((data && data[:tempfile]) ? data[:tempfile].length : nil),
              :content_type => ((data && data[:type]) ? data[:type] : nil),
              :last_modified => '',
              :user_metadata => opts.select{|k,v| k.match(/^x-amz-meta-/i)}
            }
          )
end

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



476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 476

def create_bucket(credentials, name, opts={})
  bucket = nil
  safely do
    s3_client = new_client(credentials, :s3)
    bucket_location = opts['location']
    if (bucket_location && bucket_location.size >0 &&
                                       (not bucket_location.eql?(DEFAULT_REGION)) )
      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_firewall(credentials, opts = {}) ⇒ Object

– Create firewall –



794
795
796
797
798
799
800
801
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 794

def create_firewall(credentials, opts={})
  ec2 = new_client(credentials)
  safely do
    ec2.create_security_group(opts["name"], opts["description"])
  end
  Firewall.new( { :id=>opts["name"], :name=>opts["name"],
                  :description => opts["description"], :owner_id => "", :rules => [] } )
end

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



815
816
817
818
819
820
821
822
823
824
825
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 815

def create_firewall_rule(credentials, opts={})
  ec2 = new_client(credentials)
  groups = []
  opts['groups'].each do |k,v|
    groups << {"group_name" => k, "owner" =>v}
  end
  safely do
    ec2.manage_security_group_ingress(opts['id'], opts['port_from'], opts['port_to'], opts['protocol'],
      "authorize", opts['addresses'], groups)
  end
end

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



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

def create_image(credentials, opts={})
  ec2 = new_client(credentials)
  instance = instance(credentials, :id => opts[:id])
  safely do
    new_image_id = ec2.create_image(instance.id, opts[:name], opts[:description])
    image(credentials, :id => new_image_id)
  end
end

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



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 262

def create_instance(credentials, image_id, opts={})
  ec2 = new_client(credentials)
  instance_options = {}
  if opts[:user_data]
    instance_options[:user_data] = Base64::decode64(opts[:user_data])
  end
  if opts[:metrics] and !opts[:metrics].empty?
    instance_options[:monitoring_enabled] = true
  end
  if opts[:realm_id]
    az, sn = opts[:realm_id].split(":")
    if sn
      instance_options[:subnet_id] = sn
    else
      instance_options[:availability_zone] = az
    end
  end
  instance_options[:key_name] = opts[:keyname] if opts[:keyname]
  instance_options[:instance_type] = opts[:hwp_id] if opts[:hwp_id] && opts[:hwp_id].length > 0
  firewalls = opts.inject([]){|res, (k,v)| res << v if k =~ /firewalls\d+$/; res}
  instance_options[:group_ids] = firewalls unless firewalls.empty?
  if opts[:instance_count] and opts[:instance_count].length != 0
    instance_options[:min_count] = opts[:instance_count]
    instance_options[:max_count] = opts[:instance_count]
  end
  if opts[:snapshot_id] and opts[:device_name]
    instance_options[:block_device_mappings] = [{
      :snapshot_id => opts[:snapshot_id],
      :device_name => opts[:device_name]
    }]
  end
  safely do
    new_instances = ec2.launch_instances(image_id, instance_options).collect do |i|
      convert_instance(i)
    end
    if new_instances.size == 1
      new_instances.first
    else
      new_instances
    end
  end
end

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



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

def create_key(credentials, opts={})
  ec2 = new_client(credentials)
  safely do
    if (opts[:public_key] && opts[:public_key].length >0)
      convert_key(ec2.import_key_pair(opts[:key_name], opts[:public_key]))
    else
      convert_key(ec2.create_key_pair(opts[:key_name]))
    end
  end
end

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



423
424
425
426
427
428
429
430
431
432
433
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 423

def create_load_balancer(credentials, opts={})
  ec2 = new_client( credentials, :elb )
  safely do
    ec2.create_load_balancer(opts['name'], [opts['realm_id']],
      [{:load_balancer_port => opts['listener_balancer_port'],
        :instance_port => opts['listener_instance_port'],
        :protocol => opts['listener_protocol']}]
    )
    return load_balancer(credentials, :id => opts['name'])
  end
end

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



704
705
706
707
708
709
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 704

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



651
652
653
654
655
656
657
658
659
660
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 651

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 = {}) ⇒ Object

– Delete Blob –



567
568
569
570
571
572
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 567

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

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



491
492
493
494
495
496
497
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 491

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

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

– Delete firewall –



806
807
808
809
810
811
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 806

def delete_firewall(credentials, opts={})
  ec2 = new_client(credentials)
  safely do
    ec2.delete_security_group(opts["id"])
  end
end

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



829
830
831
832
833
834
835
836
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 829

def delete_firewall_rule(credentials, opts={})
  ec2 = new_client(credentials)
  firewall = opts[:firewall]
  protocol, from_port, to_port, addresses, groups = firewall_rule_params(opts[:rule_id])
  safely do
    ec2.manage_security_group_ingress(firewall, from_port, to_port, protocol, "revoke", addresses, groups)
  end
end

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



742
743
744
745
746
747
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 742

def destroy_address(credentials, opts={})
  ec2 = new_client(credentials)
  safely do
    ec2.release_address(opts[:id])
  end
end

#destroy_image(credentials, image_id) ⇒ Object



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

def destroy_image(credentials, image_id)
  ec2 = new_client(credentials)
  safely do
    unless ec2.deregister_image(image_id)
      raise "ERROR: Unable to deregister AMI"
    end
  end
end

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



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

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

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



395
396
397
398
399
400
401
402
403
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 395

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_load_balancer(credentials, id) ⇒ Object



435
436
437
438
439
440
441
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 435

def destroy_load_balancer(credentials, id)
  ec2 = new_client( credentials, :elb )
  return 'InvalidLoadBalancer' if load_balancer(credentials, :id => id).nil?
  safely do
    ec2.delete_load_balancer(id)
  end
end

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



711
712
713
714
715
716
717
718
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 711

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

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



662
663
664
665
666
667
668
669
670
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 662

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, :id => opts[:id])
  end
end

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



679
680
681
682
683
684
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 679

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

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



760
761
762
763
764
765
766
767
768
769
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 760

def disassociate_address(credentials, opts={})
  ec2 = new_client(credentials)
  safely do
    if ec2.disassociate_address(opts[:id])
      Address.new(:id => opts[:id])
    else
      raise "ERROR: Cannot disassociate an IP address from the Instance"
    end
  end
end

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

– FIREWALLS - ec2 security groups –



774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 774

def firewalls(credentials, opts={})
  ec2 = new_client(credentials)
  the_firewalls = []
  groups = []
  safely do
    if opts[:id]
      groups = ec2.describe_security_groups([opts[:id]])
    else
      groups = ec2.describe_security_groups()
    end
  end
  groups.each do |security_group|
    the_firewalls << convert_security_group(security_group)
  end
  the_firewalls
end

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



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 145

def images(credentials, opts={})
  ec2 = new_client(credentials)
  img_arr = []
  opts ||= {}
  profiles = hardware_profiles(nil)
  if opts[:id]
    safely do
      begin
        img_arr = ec2.describe_images([opts[:id]]).collect do |image|
          convert_image(image, profiles)
        end
      rescue => e
        raise e unless e.message =~ /Invalid id/ or e.message =~ /does not exist/
        img_arr = []
      end
    end
    return img_arr
  end
  owner_id = opts[:owner_id] || default_image_owner
  safely do
    img_arr = ec2.describe_images_by_owner([owner_id], default_image_type).collect do |image|
      convert_image(image, profiles)
    end
  end
  img_arr = filter_on( img_arr, :architecture, opts )
  img_arr.sort_by { |e| [e.owner_id, e.name] }
end

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



552
553
554
555
556
557
558
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 552

def init_segmented_blob(credentials, opts={})
  s3_client = new_client(credentials, :s3)
  safely do
    s3_client.interface.initiate_multipart(opts[:bucket],opts[:id])
  end

end

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



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 226

def instance(credentials, opts={})
  ec2 = new_client(credentials)
  safely do
    begin
      ec2_inst = ec2.describe_instances([opts[:id]]).first
    rescue => e
      raise e unless e.message =~ /Invalid id/ or e.message =~ /NotFound/
      ec2_inst = nil
    end
    return if ec2_inst.nil?
    instance = convert_instance(ec2_inst)
    return nil unless instance
    if ec2_inst[:aws_platform] == 'windows'
      console_output = ec2.get_console_output(instance.id)
      windows_password = console_output[:aws_output][%r{<Password>(.+)</Password>}m] && $1
      if windows_password
        instance.username = 'Administrator'
        instance.password = windows_password
      end
    end
    instance
  end
end

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



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

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

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



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

def keys(credentials, opts={})
  ec2 = new_client(credentials)
  opts ||= {}
  safely do
    begin
      ec2.describe_key_pairs(opts[:id] ? [opts[:id]] : nil).collect do |key|
        convert_key(key)
      end
    rescue => e
      raise e unless e.message =~ /does not exist/
      []
    end
  end
end

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



443
444
445
446
447
448
449
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 443

def lb_register_instance(credentials, opts={})
  ec2 = new_client( credentials, :elb)
  safely do
    ec2.register_instances_with_load_balancer(opts['id'], [opts['instance_id']])
    load_balancer(credentials, :id => opts[:id])
  end
end

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



451
452
453
454
455
456
457
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 451

def lb_unregister_instance(credentials, opts={})
  ec2 = new_client( credentials, :elb)
  safely do
    ec2.deregister_instances_from_load_balancer(opts['id'], [opts['instance_id']])
    load_balancer(credentials, :id => opts['id'])
  end
end

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



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

def load_balancer(credentials, opts={})
  load_balancers(credentials, {
    :names => [opts[:id]]
  }).first
end

#load_balancers(credentials, opts = nil) ⇒ Object



411
412
413
414
415
416
417
418
419
420
421
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 411

def load_balancers(credentials, opts=nil)
  ec2 = new_client( credentials, :elb )
  result = []
  safely do
    loadbalancers = ec2.describe_load_balancers(opts || {})
    loadbalancers.each do |loadbalancer|
      result << convert_load_balancer(credentials, loadbalancer)
    end
  end
  return result
end

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



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

def metric(credentials, opts={})
  cw = new_client(credentials, :mon)
  m = metrics(credentials, :id => opts[:id])
  return [] if m.empty?
  m = m.first
  # Get statistics from last 1 hour
  start_time = (Time.now - 3600).utc.iso8601.to_s
  end_time = Time.now.utc.iso8601.to_s
  m.properties.each do |p|
    p.values = cw.get_metric_statistics(p.name,  ['Minimum', 'Maximum', 'Average'],
                start_time, end_time, metric_unit_for(p.name), { m.entity => opts[:id]})
  end
  m
end

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



339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 339

def metrics(credentials, opts={})
  cw = new_client(credentials, :mon)
  metrics_arr = []
  cw.list_metrics( :namespace => 'AWS/EC2' ).each do |metric|
    if metrics_arr.any? { |m| m.id == metric[:value] }
      i = metrics_arr.index { |m| m.id == metric[:value] }
      metrics_arr[i] = metrics_arr[i].add_property(metric[:measure_name])
    else
      metrics_arr << convert_metric(metric)
    end
  end
  metrics_arr.reject! { |m| m.unknown? }
  filter_on(metrics_arr, :id, opts)
end

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



838
839
840
841
842
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 838

def providers(credentials, opts={})
  ec2 = new_client(credentials)
  @providers ||= ec2.describe_regions.map{|r| Provider.new( {:id=>r, :name=>r,
   :url=>"#{ENV['API_HOST']}:#{ENV['API_PORT']}#{Deltacloud.default_frontend.root_url}\;provider=#{r}" }) }
end

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



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 173

def realms(credentials, opts={})
  # We have two different kinds of realms:
  #  (1) Availability Zones
  #  (2) Subnets in VPC's (scoped to an AZ)
  # For the latter, the ID is AZ:SUBNET, and we can tell that we
  # are looking at such a realm by checking if the id contains a colon
  ec2 = new_client(credentials)
  realms = []
  safely do
    if opts[:id] and !opts[:id].empty?
      az, sn = opts[:id].split(":")
      begin
        if sn
          subnet = ec2.describe_subnets(sn).first
          realms << convert_realm(subnet) if subnet
        else
          ec2.describe_availability_zones([az]).collect do |realm|
            realms << convert_realm(realm) unless realm.empty?
          end
        end
      rescue => e
        raise e unless e.message =~ /Invalid availability zone/
        realms = []
      end
    else
      realms = ec2.describe_availability_zones.collect do |realm|
        convert_realm(realm) unless realm.empty?
      end
      realms = realms +
        ec2.describe_subnets.map { |sn| convert_realm(sn) }
    end
  end
  realms
end

#reboot_instance(credentials, instance_id) ⇒ Object



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

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

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



305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 305

def run_on_instance(credentials, opts={})
  target = instance(credentials, :id => opts[:id])
  param = {}
  param[:credentials] = {
    :username => (opts[:username]) ? opts[:username] : 'root', # Default for EC2 Linux instances
  }
  param[:port] = opts[:port] || '22'
  param[:ip] = opts[:ip] || target.public_addresses.first.address
  param[:private_key] = (opts[:private_key].length > 1) ? opts[:private_key] : nil
  safely do
    Deltacloud::Runner.execute(opts[:cmd], param)
  end
end

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



686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 686

def storage_snapshots(credentials, opts={})
  ec2 = new_client(credentials)
  snapshot_list = opts[:id] ? [opts[:id]] : []
  safely do
    begin
    ec2.describe_snapshots(snapshot_list).collect do |snapshot|
      convert_snapshot(snapshot)
    end
    rescue => e
      if e.message =~ /NotFound/
        []
      else
        raise e
      end
    end
  end
end

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



641
642
643
644
645
646
647
648
649
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 641

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

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



584
585
586
587
588
589
590
591
592
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 584

def (credentials, opts={})
  s3_client = new_client(credentials, :s3)
  meta_hash = BlobHelper::(opts['meta_hash'], '')
  safely do
    the_bucket, s3_client = get_bucket_with_endpoint(s3_client, credentials, opts['bucket'])
    the_blob = the_bucket.key(opts[:id])
    the_blob.save_meta(meta_hash)
  end
end

#valid_credentials?(credentials) ⇒ Boolean

Returns:

  • (Boolean)


848
849
850
851
852
853
854
855
856
857
858
# File 'lib/deltacloud/drivers/ec2/ec2_driver.rb', line 848

def valid_credentials?(credentials)
  begin
    realms(credentials) && true
  rescue => e
    if e.class.name =~ /AuthFailure/
      false
    else
      safely { raise e }
    end
  end
end