Class: Kiel::Cloud::AWS
- Inherits:
-
Object
- Object
- Kiel::Cloud::AWS
- Includes:
- AWSBase
- 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.
Constant Summary collapse
- RECOGNIZED_OPTIONS =
[ :region, :credentials, :instance, :start_options ]
Constants included from AWSBase
Kiel::Cloud::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 = {}) ⇒ 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.
Methods included from AWSBase
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.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/kiel/cloud/aws.rb', line 26 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
Finds all images where the given tags apply
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/kiel/cloud/aws.rb', line 47 def images = @ec2.images.with_owner('self').tagged( .first.first ) images = images.select do | image | = image..to_h .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.
149 150 151 |
# File 'lib/kiel/cloud/aws.rb', line 149 def delete_image ( ).each { | image | image.deregister } end |
#dns_name(instance) ⇒ Object
the public dns name of the given instance
144 145 146 |
# File 'lib/kiel/cloud/aws.rb', line 144 def dns_name instance instance.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’ }
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/kiel/cloud/aws.rb', line 87 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.
136 137 138 139 140 141 |
# File 'lib/kiel/cloud/aws.rb', line 136 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.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/kiel/cloud/aws.rb', line 112 def store_image instance, begin puts "waiting 2 minutes before starting to take the image..." sleep 120 puts "creating image..." 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 |