Class: Kiel::Cloud::AWS
- Inherits:
-
Object
- Object
- Kiel::Cloud::AWS
- Defined in:
- lib/kiel/cloud/aws.rb
Overview
Implements the connection to the Amazon Web Services (AWS). The current implementation works for one configured region. The default server is a small EC2 instance.
Instance Method Summary collapse
- #all_images_by_tags(tags) ⇒ Object
-
#delete_image(image_name) ⇒ Object
deletes the given image.
- #dns_name(instance) ⇒ Object
-
#exists?(tags) ⇒ Boolean
returns true, if an image with the given tags exists.
-
#initialize(options = {}) ⇒ AWS
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.
Constructor Details
#initialize(options = {}) ⇒ AWS
The contructor takes the following configuration options:
- :region
-
A string naming the region to be used. If no region is given, the default region is used.
- :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
-
Options that are applied to EC2::InstanceCollection.create (siehe aws-sdk for more details). The aws_tests.rb uses the :key_name and :security_groups options to set the name of the used ssh key and a security group, where ssh is enabled.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/kiel/cloud/aws.rb', line 24 def initialize = {} require 'aws/ec2' .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 ::AWS.config( [ :credentials ] ) @ec2 = ::AWS::EC2.new @ec2 = @ec2.regions[ [ :region ] ] if .key? :region end end |
Instance Method Details
#all_images_by_tags(tags) ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/kiel/cloud/aws.rb', line 44 def images = @ec2.images.with_owner('self').tagged( .first.first ) images = images.select do | image | = image..to_h .merge( ) == end images end |
#delete_image(image_name) ⇒ Object
deletes the given image
159 160 |
# File 'lib/kiel/cloud/aws.rb', line 159 def delete_image image_name end |
#dns_name(instance) ⇒ Object
154 155 156 |
# File 'lib/kiel/cloud/aws.rb', line 154 def dns_name instance instance.dns_name end |
#exists?(tags) ⇒ Boolean
returns true, if an image with the given tags exists
148 149 150 151 152 |
# File 'lib/kiel/cloud/aws.rb', line 148 def exists? raise ArgumentError, "AWS.exists? with empty tags" if .empty? 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’ }
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/kiel/cloud/aws.rb', line 91 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.id } end ) instance = @ec2.instances.create( ) begin wait_for_ec2 instance puts "ec2 instance \'#{instance.dns_name}\' started." rescue instance.terminate raise end instance end |
#stop_instance(instance) ⇒ Object
stops the given instance.
140 141 142 143 144 145 |
# File 'lib/kiel/cloud/aws.rb', line 140 def stop_instance instance begin instance.terminate rescue end end |
#store_image(instance, tags) ⇒ Object
store the given instance
and add the hash of tags
to the image.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/kiel/cloud/aws.rb', line 116 def store_image instance, begin puts "waiting 2 minutes before starting to take the image..." sleep 120 puts "creating images..." image = @ec2.images.create( :instance_id => instance.id, :no_reboot => true, :description => "automaticaly created #{[ 'image_type' ]} image", :name => "#{[ 'image_type' ]} #{Digest::SHA1.hexdigest .inspect}" ) wait_for_image image .each do | key, value | image.add_tag( key, :value => value ) end ensure stop_instance instance end end |