Hi,
I replaced RightAWS gem with Amazon-EC2.
Reasons:
- Amazon-EC2 is well supported by Amazon - RightAWS was pretty outdated and dunno if it's maintained - Amazon-EC2 is much faster in getting images / filtering compared to Right one. - Amazon-EC2 has some functionality which Right doesn't support: - Uploading userdata in Base64 form - Filtering images (kernel/ramdisk/public/own...) - Add keypair when starting an instance
I tested this 'new' driver against real EC2 and everything is working fine. Let me known if it works for you as well and if there is some reason why we want to keep right-aws gem.
- Michal
--- server/lib/deltacloud/drivers/ec2/ec2_driver.rb | 174 ++++++++++------------- 1 files changed, 78 insertions(+), 96 deletions(-)
diff --git a/server/lib/deltacloud/drivers/ec2/ec2_driver.rb b/server/lib/deltacloud/drivers/ec2/ec2_driver.rb index 7f73666..4777501 100644 --- a/server/lib/deltacloud/drivers/ec2/ec2_driver.rb +++ b/server/lib/deltacloud/drivers/ec2/ec2_driver.rb @@ -17,7 +17,7 @@
require 'deltacloud/base_driver' -require 'right_aws' +require 'AWS'
module Deltacloud module Drivers @@ -121,35 +121,19 @@ class EC2Driver < Deltacloud::BaseDriver # Images #
- def images(credentials, opts=nil ) - puts(opts) - ec2 = new_client( credentials ) - images = [] + def images(credentials, opts={} ) + ec2 = new_client(credentials) + img_arr = [] + config = { :owner_id => "amazon" } + config.merge!({ :owner_id => opts[:owner_id] }) if opts and opts[:owner_id] + config.merge!({ :image_id => opts[:id] }) if opts and opts[:id] safely do - if ( opts && opts[:id] ) - ec2.describe_images(opts[:id]).each do |ec2_image| - if ( ec2_image[:aws_id] =~ /^ami-/ ) - images << convert_image( ec2_image ) - end - end - filter_on( images, :owner_id, opts ) - elsif ( opts && opts[:owner_id] ) - ec2.describe_images_by_owner( opts[:owner_id] ).each do |ec2_image| - if ( ec2_image[:aws_id] =~ /^ami-/ ) - images << convert_image( ec2_image ) - end - end - else - ec2.describe_images().each do |ec2_image| - if ( ec2_image[:aws_id] =~ /^ami-/ ) - images << convert_image( ec2_image ) - end - end + ec2.describe_images(config).imagesSet.item.each do |image| + img_arr << convert_image(image) end end - - images = filter_on( images, :architecture, opts ) - images.sort_by{|e| [e.owner_id,e.description]} + img_arr = filter_on( img_arr, :architecture, opts ) + img_arr.sort_by{|e| [e.owner_id, e.name]} end
# @@ -160,7 +144,7 @@ class EC2Driver < Deltacloud::BaseDriver ec2 = new_client(credentials) realms = [] safely do - ec2.describe_availability_zones.each do |ec2_realm| + ec2.describe_availability_zones.availabilityZoneInfo.item.each do |ec2_realm| realms << convert_realm( ec2_realm ) end end @@ -176,8 +160,11 @@ class EC2Driver < Deltacloud::BaseDriver instances = [] safely do param = opts.nil? ? nil : opts[:id] - ec2.describe_instances( param ).each do |ec2_instance| - instances << convert_instance( ec2_instance ) + ec2.describe_instances.reservationSet.item.each do |item| + item.instancesSet.item.each do |ec2_instance| + puts ec2_instance.inspect + instances << convert_instance( ec2_instance, item.ownerId ) + end end end instances = filter_on( instances, :id, opts ) @@ -188,45 +175,43 @@ class EC2Driver < Deltacloud::BaseDriver def create_instance(credentials, image_id, opts) ec2 = new_client( credentials ) realm_id = opts[:realm_id] - flavor_id = opts[:flavor_id] - unless ( flavor_id ) - image = image(credentials, :id=>image_id ) - flavor = flavor( credentials, :architecture=>image.architecture ) - ( flavor_id = flavor.id ) if ( flavor ) + image = image(credentials, :id => image_id ) + if opts[:flavor_id] + flavor = flavor( credentials, { :architecture => image.architecture, :id => opts[:flavor_id] }) + else + flavor = flavors.first end - flavor_id.gsub!( /-/, '.' ) if flavor_id ec2_instances = ec2.run_instances( - image_id, - 1,1, - [], - nil, - opts[:user_data], - 'public', - flavor_id, - nil, - nil, - realm_id ) - convert_instance( ec2_instances.first ) + :image_id => image.id, + :user_data => opts[:user_data], + :key_name => opts[:key_name], + :availability_zone => realm_id, + :monitoring_enabled => true, + :instance_type => flavor.id.tr('-','.'), + :disable_api_termination => false, + :instance_initiated_shutdown_behavior => 'terminate' + ) + convert_instance( ec2_instances.instancesSet.item.first ) end
def reboot_instance(credentials, id) ec2 = new_client(credentials) safely do - ec2.reboot_instances( id ) + ec2.reboot_instances( :instance_id => id ) end end
def stop_instance(credentials, id) ec2 = new_client(credentials) safely do - ec2.terminate_instances( id ) + ec2.terminate_instances( :instance_id => id ) end end
def destroy_instance(credentials, id) ec2 = new_client(credentials) safely do - ec2.terminate_instances( id ) + ec2.terminate_instances( :instance_id => id ) end end
@@ -238,11 +223,11 @@ class EC2Driver < Deltacloud::BaseDriver volumes = [] safely do if (opts) - ec2.describe_volumes(opts[:id]).each do |ec2_volume| + ec2.describe_volumes(:volume_id => opts[:id]).volumeSet.item.each do |ec2_volume| volumes << convert_volume( ec2_volume ) end else - ec2.describe_volumes().each do |ec2_volume| + ec2.describe_volumes.volumeSet.item.each do |ec2_volume| volumes << convert_volume( ec2_volume ) end end @@ -259,11 +244,11 @@ class EC2Driver < Deltacloud::BaseDriver snapshots = [] safely do if (opts) - ec2.describe_snapshots(opts[:id]).each do |ec2_snapshot| + ec2.describe_snapshots(:owner => 'self', :snapshot_id => opts[:id]).snapshotSet.item.each do |ec2_snapshot| snapshots << convert_snapshot( ec2_snapshot ) end else - ec2.describe_snapshots(opts).each do |ec2_snapshot| + ec2.describe_snapshots(:owner => 'self').snapshotSet.item.each do |ec2_snapshot| snapshots << convert_snapshot( ec2_snapshot ) end end @@ -274,81 +259,78 @@ class EC2Driver < Deltacloud::BaseDriver private
def new_client(credentials) - RightAws::Ec2.new(credentials.user, credentials.password, :cache=>false ) + AWS::EC2::Base.new( + :access_key_id => credentials.user, + :secret_access_key => credentials.password + ) end
def convert_image(ec2_image) Image.new( { - :id=>ec2_image[:aws_id], - :name=>ec2_image[:aws_location], - :description=>ec2_image[:aws_location], - :owner_id=>ec2_image[:aws_owner], - :architecture=>ec2_image[:aws_architecture], + :id=>ec2_image['imageId'], + :name=>ec2_image['name'] || ec2_image['imageId'], + :description=>ec2_image['description'] || ec2_image['imageLocation'] || '', + :owner_id=>ec2_image['imageOwnerId'], + :architecture=>ec2_image['architecture'], } ) end
def convert_realm(ec2_realm) Realm.new( { - :id=>ec2_realm[:zone_name], - :name=>ec2_realm[:zone_name], - :limit=>:unlimited, - :state=>ec2_realm[:zone_state].upcase, + :id=>ec2_realm['zoneName'], + :name=>ec2_realm['regionName'], + :limit=>ec2_realm['zoneState'].eql?('available') ? :unlimited : 0, + :state=>ec2_realm['zoneState'].upcase, } ) end
- def convert_instance(ec2_instance) - state = ec2_instance[:aws_state].upcase + def convert_instance(ec2_instance, owner_id=nil) + state = ec2_instance['instanceState']['name'].upcase state_key = state.downcase.underscore.to_sym - - realm_id = ec2_instance[:aws_availability_zone] + realm_id = ec2_instance['placement']['availabilityZone'] (realm_id = nil ) if ( realm_id == '' ) + Instance.new( { - :id=>ec2_instance[:aws_instance_id], - :name => ec2_instance[:aws_image_id], - :state=>ec2_instance[:aws_state].upcase, - :image_id=>ec2_instance[:aws_image_id], - :owner_id=>ec2_instance[:aws_owner], + :id=>ec2_instance['instanceId'], + :name => ec2_instance['imageId'], + :state=>state, + :image_id=>ec2_instance['imageId'], + :owner_id=>owner_id, :realm_id=>realm_id, - :public_addresses=>( ec2_instance[:dns_name] == '' ? [] : [ec2_instance[:dns_name]] ), - :private_addresses=>( ec2_instance[:private_dns_name] == '' ? [] : [ec2_instance[:private_dns_name]] ), - :flavor_id=>ec2_instance[:aws_instance_type].gsub( /./, '-'), - :actions=>instance_actions_for( ec2_instance[:aws_state].upcase ), + :public_addresses=>( ec2_instance['dnsName'] == '' ? [] : [ec2_instance['dnsName']] ), + :private_addresses=>( ec2_instance['privateDnsName'] == '' ? [] : [ec2_instance['privateDnsName']] ), + :flavor_id=>ec2_instance['instanceType'].gsub( /./, '-'), + :actions=>instance_actions_for( state ), } ) end
def convert_volume(ec2_volume) StorageVolume.new( { - :id=>ec2_volume[:aws_id], - :created=>ec2_volume[:aws_created_at], - :state=>ec2_volume[:aws_status].upcase, - :capacity=>ec2_volume[:aws_size], - :instance_id=>ec2_volume[:aws_instance_id], - :device=>ec2_volume[:aws_device], + :id=>ec2_volume['volumeId'], + :created=>ec2_volume['createTime'], + :state=>ec2_volume['status'].upcase, + :capacity=>ec2_volume['size'], + :instance_id=>ec2_volume['snapshotId'], + :device=>ec2_volume['attachmentSet'], } ) end
def convert_snapshot(ec2_snapshot) StorageSnapshot.new( { - :id=>ec2_snapshot[:aws_id], - :state=>ec2_snapshot[:aws_status].upcase, - :storage_volume_id=>ec2_snapshot[:aws_volume_id], - :created=>ec2_snapshot[:aws_started_at], + :id=>ec2_snapshot['snapshotId'], + :state=>ec2_snapshot['status'].upcase, + :storage_volume_id=>ec2_snapshot['volumeId'], + :created=>ec2_snapshot['startTime'], } ) end
def safely(&block) begin block.call - rescue RightAws::AwsError => e - if ( e.include?( /SignatureDoesNotMatch/ ) ) - raise Deltacloud::AuthException.new - elsif ( e.include?( /InvalidClientTokenId/ ) ) + rescue AWS::AuthFailure => e raise Deltacloud::AuthException.new - else - e.errors.each do |error| - puts "ERROR #{error.inspect}" - end - end + rescue Exception => e + puts "ERROR: #{e.message}" end end
I'm sorry, there is actually only one patch is this set [1/5 = 1/1].
michal
On Fri, Apr 09, 2010 at 05:26:49PM +0200, mfojtik@redhat.com wrote:
Hi,
I replaced RightAWS gem with Amazon-EC2.
Reasons:
- Amazon-EC2 is well supported by Amazon
- RightAWS was pretty outdated and dunno if it's maintained
- Amazon-EC2 is much faster in getting images / filtering compared to Right one.
- Amazon-EC2 has some functionality which Right doesn't support:
- Uploading userdata in Base64 form
- Filtering images (kernel/ramdisk/public/own...)
- Add keypair when starting an instance
I tested this 'new' driver against real EC2 and everything is working fine. Let me known if it works for you as well and if there is some reason why we want to keep right-aws gem.
Hello,
we'd like to test the GEM with Eucalyptus: is the project at http://github.com/grempe/amazon-ec2?
cheers graziano
- Michal
deltacloud-devel mailing list deltacloud-devel@lists.fedorahosted.org https://fedorahosted.org/mailman/listinfo/deltacloud-devel
On 09/04/10 13:36 -0700, graziano obertelli wrote:
Hi,
Hello,
we'd like to test the GEM with Eucalyptus: is the project at http://github.com/grempe/amazon-ec2?
Yes. There is also a Rubyforge project at http://amazon-ec2.rubyforge.org
(http://developer.amazonwebservices.com/connect/entry.jspa?externalID=1013)
- Michal
deltacloud-devel@lists.fedorahosted.org