Class: Kiel::Cloud::AWS

Inherits:
Object
  • Object
show all
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

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 options = {}
    require 'aws/ec2'

    options.each_key do | key |
        raise ArgumentError, "unrecognized option \'#{key}\'" unless RECOGNIZED_OPTIONS.include? key
    end
                        
    @ec2 = options[ :instance ]
    @start_options = options[ :start_options ] || {}
    
    if @ec2 
        puts "\'credentials\' ignored as an instance was given too" if options.key? :credentials
    else
        ::AWS.config( options[ :credentials ]  )
    
        @ec2 = ::AWS::EC2.new
        @ec2 = @ec2.regions[ options[ :region ] ] if options.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 all_images_by_tags tags
    images = @ec2.images.with_owner('self').tagged( tags.first.first )
    
    images = images.select do | image |
        image_tags = image.tags.to_h
        image_tags.merge( tags ) == image_tags 
    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

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


148
149
150
151
152
# File 'lib/kiel/cloud/aws.rb', line 148

def exists? tags
    raise ArgumentError, "AWS.exists? with empty tags" if tags.empty?
    
    image_by_tags tags 
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
    options = @start_options.merge( if image_name.key?( :id ) 
        { image_id: image_name[ :id ] }
    else
        tags  = image_name[ :tags ]
        image = image_by_tags tags
        raise RuntimeError, "no image with tags: \'#{tags}\' found to start an instance" unless image
        
        { image_id: image.id }
    end )
    
    instance = @ec2.instances.create( options )
    
    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, tags
    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 #{tags[ 'image_type' ]} image",
            :name => "#{tags[ 'image_type' ]} #{Digest::SHA1.hexdigest tags.inspect}" )
           
        wait_for_image image
    
        tags.each do | key, value |
            image.add_tag( key, :value => value )
        end               
    ensure
        stop_instance instance
    end
end