Class: Kiel::Cloud::RightAWS
- Inherits:
-
Object
- Object
- Kiel::Cloud::RightAWS
- Includes:
- AWSBase
- Defined in:
- lib/kiel/cloud/right_aws.rb
Overview
Implements the connection to the Amazon Web Services (AWS) using the Right-AWS gem.
Constant Summary collapse
- RECOGNIZED_OPTIONS =
[ :region, :credentials, :instance, :start_options ]
Constants included from AWSBase
AWSBase::INSTANCE_STARTUP_TIMEOUT
Instance Method Summary collapse
-
#all_images_by_tags(tags) ⇒ Object
Finds all images where the given tags apply.
-
#delete_image(tags) ⇒ Object
deletes the given images by tags.
-
#dns_name(instance) ⇒ Object
the public dns name of the given instance.
-
#initialize(options = {}) ⇒ RightAWS
constructor
The contructor takes the following configuration options:.
-
#start_instance(image_name) ⇒ Object
starts a server instance in the cloud, returning a handle to that instance.
-
#stop_instance(instance) ⇒ Object
stops the given instance.
-
#store_image(instance, tags) ⇒ Object
store the given
instance
and add the hash oftags
to the image.
Methods included from AWSBase
Constructor Details
#initialize(options = {}) ⇒ RightAWS
The contructor takes the following configuration options:
- :region
-
A string naming the region to be used. If no region is given, North America is used by default.
- :credentials
-
A hash containing the fields ‘access_key_id’ and ‘secret_access_key’ with the credential information to your amazon account.
- :instance
-
An instance of the AWS::EC2. If that instance is given, no
credentials:
should be given. Kiel::Cloud::AWS will instead use this instance. - :start_options
-
Possible options are :key_name and :security_groups to set the name of the used ssh key and a security group, where ssh is enabled.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/kiel/cloud/right_aws.rb', line 23 def initialize = {} require 'right_aws' .each_key do | key | raise ArgumentError, "unrecognized option \'#{key}\'" unless RECOGNIZED_OPTIONS.include? key end @ec2 = [ :instance ] @start_options = [ :start_options ] || {} if @ec2 puts "\'credentials\' ignored as an instance was given too" if .key? :credentials else raise ArgumentError, 'no credentials given' unless .key? :credentials credentials = [ :credentials ] raise ArgumentError, 'no :access_key_id given within credentials' unless credentials.key? :access_key_id raise ArgumentError, 'no :secret_access_key given within credentials' unless credentials.key? :secret_access_key params = .key?( :region ) ? { region: [ :region ] } : {} @ec2 = RightAws::Ec2.new credentials[ :access_key_id ], credentials[ :secret_access_key ], params end end |
Instance Method Details
#all_images_by_tags(tags) ⇒ Object
Finds all images where the given tags apply
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/kiel/cloud/right_aws.rb', line 48 def images = @ec2.describe_images_by_owner 'self' images = images.select do | image | = image[ :tags ] .merge( ) == end images end |
#delete_image(tags) ⇒ Object
deletes the given images by tags. For now this function is used just for cleanup during tests.
151 152 153 154 155 |
# File 'lib/kiel/cloud/right_aws.rb', line 151 def delete_image ( ).each do | image | @ec2.deregister_image image[ :aws_id ] end end |
#dns_name(instance) ⇒ Object
the public dns name of the given instance
146 147 148 |
# File 'lib/kiel/cloud/right_aws.rb', line 146 def dns_name instance @ec2.describe_instances( [ instance[ :aws_instance_id ] ] )[ 0 ][ :dns_name ] end |
#start_instance(image_name) ⇒ Object
starts a server instance in the cloud, returning a handle to that instance. the image is either named by an image id :id => ‘image_id’ or by a set of tags that match for just one image :tags => { ‘image_type’ => ‘application’, ‘base’ => ‘34’ }
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/kiel/cloud/right_aws.rb', line 90 def start_instance image_name = @start_options.merge( if image_name.key?( :id ) { image_id: image_name[ :id ] } else = image_name[ :tags ] image = raise RuntimeError, "no image with tags: \'#{}\' found to start an instance" unless image { image_id: image[ :aws_id ] } end ) instance = @ec2.launch_instances( [ :image_id ], image_id: [ :image_id ], key_name: [ :key_name ], group_names: [ :security_groups ] ) instance = instance[ 0 ] begin wait_for_ec2 instance puts "ec2 instance \'#{dns_name instance}\' started." rescue stop_instance instance raise end instance end |
#stop_instance(instance) ⇒ Object
stops the given instance.
141 142 143 |
# File 'lib/kiel/cloud/right_aws.rb', line 141 def stop_instance instance @ec2.terminate_instances( [ instance[ :aws_instance_id ] ] ) end |
#store_image(instance, tags) ⇒ Object
store the given instance
and add the hash of tags
to the image.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/kiel/cloud/right_aws.rb', line 117 def store_image instance, begin puts "waiting 2 minutes before starting to take the image..." sleep 120 puts "creating image..." image = @ec2.create_image( instance[ :aws_instance_id ], :no_reboot => true, :description => "automaticaly created #{[ 'image_type' ]} image", :name => "#{[ 'image_type' ]} #{Digest::SHA1.hexdigest .inspect}" ) puts "waiting for image to get ready..." wait_for_image image puts "image was created, adding tags..." @ec2.( image, ) puts "tags added." ensure stop_instance instance end end |