Class: CloudProviders::Ec2

Inherits:
CloudProvider show all
Includes:
Ec2Helpers
Defined in:
lib/cloud_providers/ec2/ec2.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Ec2Helpers

#associate_address, #attach_volume, #next_unused_elastic_ip, #next_unused_volume, #security_groups

Methods inherited from CloudProvider

inherited, #initialize, #keypair, #keypair=, #nodes

Methods included from Callbacks

included

Constructor Details

This class inherits a constructor from CloudProviders::CloudProvider

Class Method Details

.default_access_keyObject

Set the aws keys from the environment, or load from /etc/poolparty/env.yml if the environment variable is not set



32
33
34
# File 'lib/cloud_providers/ec2/ec2.rb', line 32

def self.default_access_key
  ENV['EC2_ACCESS_KEY'] || load_keys_from_file[:access_key]
end

.default_certObject



44
45
46
# File 'lib/cloud_providers/ec2/ec2.rb', line 44

def self.default_cert
  ENV['EC2_CERT'] || load_keys_from_file[:cert]
end

.default_cloud_certObject



60
61
62
# File 'lib/cloud_providers/ec2/ec2.rb', line 60

def self.default_cloud_cert
 ENV['CLOUD_CERT'] || ENV['EUCALYPTUS_CERT'] || load_keys_from_file[:cloud_cert]
end

.default_ec2_urlObject



52
53
54
# File 'lib/cloud_providers/ec2/ec2.rb', line 52

def self.default_ec2_url
  ENV['EC2_URL'] || load_keys_from_file[:ec2_url]
end

.default_private_keyObject



40
41
42
# File 'lib/cloud_providers/ec2/ec2.rb', line 40

def self.default_private_key
  ENV['EC2_PRIVATE_KEY'] || load_keys_from_file[:private_key]
end

.default_s3_urlObject



56
57
58
# File 'lib/cloud_providers/ec2/ec2.rb', line 56

def self.default_s3_url
  ENV['S3_URL'] || load_keys_from_file[:s3_url]
end

.default_secret_access_keyObject



36
37
38
# File 'lib/cloud_providers/ec2/ec2.rb', line 36

def self.default_secret_access_key
  ENV['EC2_SECRET_KEY'] || load_keys_from_file[:secret_access_key]
end

.default_user_idObject



48
49
50
# File 'lib/cloud_providers/ec2/ec2.rb', line 48

def self.default_user_id
  ENV['EC2_USER_ID'] || load_keys_from_file[:user_id]
end

.load_keys_from_file(filename = '/etc/poolparty/env.yml', caching = true) ⇒ Object

Load the yaml file containing keys. If the file does not exist, return an empty hash



65
66
67
68
69
70
# File 'lib/cloud_providers/ec2/ec2.rb', line 65

def self.load_keys_from_file(filename='/etc/poolparty/env.yml', caching=true)
  return @aws_yml if @aws_yml && caching==true
  return {} unless File.exists?(filename)
  ddputs("Reading keys from file: #{filename}")
  @aws_yml = YAML::load( open(filename).read ) || {}
end

Instance Method Details

#after_compile(cld) ⇒ Object



167
168
169
# File 'lib/cloud_providers/ec2/ec2.rb', line 167

def after_compile(cld)
  save_aws_env_to_yml(cld.tmp_path/"etc"/"poolparty"/"env.yml") rescue nil
end

#after_run_instance(instances_list) ⇒ Object

Run after all the instances are run



172
173
174
175
176
177
# File 'lib/cloud_providers/ec2/ec2.rb', line 172

def after_run_instance(instances_list)
  instances_list.each do |inst|
    associate_address(inst.instance_id) if next_unused_elastic_ip
    attach_volume(inst.instance_id) if next_unused_volume
  end
end

#amazon?Boolean

Are we running on amazon?

Returns:

  • (Boolean)


156
157
158
159
160
161
# File 'lib/cloud_providers/ec2/ec2.rb', line 156

def amazon?
  !['https://ec2.amazonaws.com', 
   'https://us-east-1.ec2.amazonaws.com', 
   'https://eu-west-1.ec2.amazonaws.com'
   ].include?(ec2_url)
end

#aws_hash(opts = {}, base_dir = nil) ⇒ Object

Return a hash of the aws keys and environment variables If base_dir string is provided as second argument, replace path to file based variables, such as cert, with the base_dir.



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/cloud_providers/ec2/ec2.rb', line 195

def aws_hash(opts={}, base_dir=nil)
  aws={
    :user_id            => user_id,
    :private_key        => private_key,
    :cert               => cert,
    :access_key         => access_key,
    :secret_access_key  => secret_access_key,
    :ec2_url            => ec2_url,
    :s3_url             => s3_url,
    :cloud_cert    => cloud_cert
  }.merge(opts)
  if base_dir
    aws[:cert] = "#{base_dir}/#{File.basename(cert)}" if cert
    aws[:private_key] = "#{base_dir}/#{File.basename(private_key)}" if private_key
    aws[:cloud_cert] = "#{base_dir}/#{File.basename(cloud_cert)}" if cloud_cert
  end
  aws.reject{|k,v| v.nil?}
end

#before_compile(cld) ⇒ Object

Callbacks



164
165
# File 'lib/cloud_providers/ec2/ec2.rb', line 164

def before_compile(cld)
end

#create_keypair(kname, path = '~/.ec2') ⇒ Object

shortcut to ec2-add-keypair name > ~./.ec2/kname



216
217
218
# File 'lib/cloud_providers/ec2/ec2.rb', line 216

def create_keypair(kname, path='~/.ec2')
  ` ec2-add-keypair #{kname} > #{path}/#{kname} &&  chmod 600 #{path}/#{kname}`
end

#describe_instance(hash_of_criteria_to_select_instance_against) ⇒ Object

Will select the first instance matching the provided criteria hash



131
132
133
# File 'lib/cloud_providers/ec2/ec2.rb', line 131

def describe_instance(hash_of_criteria_to_select_instance_against)
  describe_instances(hash_of_criteria_to_select_instance_against).first
end

#describe_instances(o = {}) ⇒ Object

Describe instances



136
137
138
139
140
141
# File 'lib/cloud_providers/ec2/ec2.rb', line 136

def describe_instances(o={})
  instants = Ec2Response.describe_instances(ec2.describe_instances).select_with_hash(o)
  return [] if instants.empty?
  ec2_instances = instants.collect{|i| Ec2Instance.new(dsl_options.merge(i))}
  ec2_instances.sort {|a,b| a[:launch_time].to_i <=> b[:launch_time].to_i }
end

#ec2(o = {}) ⇒ Object



99
100
101
# File 'lib/cloud_providers/ec2/ec2.rb', line 99

def ec2(o={})
  @ec2 ||= Rightscale::Ec2.new(access_key, secret_access_key, o.merge(:logger => PoolParty::PoolPartyLog, :endpoint_url => ec2_url))
end

#run_instance(o = {}) ⇒ Object

Start a new instance with the given options

Raises:

  • (StandardError)


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/cloud_providers/ec2/ec2.rb', line 104

def run_instance(o={})
  number_of_instances = o[:number_of_instances] || 1
  set_vars_from_options o
  raise StandardError.new("You must pass a keypair to launch an instance, or else you will not be able to login. options = #{o.inspect}") if !keypair_name
  response_array = ec2(o).run_instances(image_id,
                                  min_count,
                                  number_of_instances,
                                  security_group,
                                  keypair.basename,
                                  user_data,
                                  addressing_type,
                                  instance_type,
                                  kernel_id,
                                  ramdisk_id,
                                  availability_zone,
                                  block_device_mappings
                                  )
  instances = response_array.collect do |aws_response_hash|
    Ec2Instance.new( Ec2Response.pp_format(aws_response_hash).merge(o) )
  end
  
  after_run_instance(instances)
  
  instances.first
end

#save_aws_env_to_yml(filename = '/etc/poolparty/env.yml') ⇒ Object

Save aws keys and env variables to a yaml file



187
188
189
190
# File 'lib/cloud_providers/ec2/ec2.rb', line 187

def save_aws_env_to_yml(filename='/etc/poolparty/env.yml')
  hsh = aws_hash(default_options, "/etc/poolparty/ec2")
  File.open(filename, 'w') {|f| f<<YAML::dump(hsh) }
end

#set_aws_env_from_yml_file(filename = '/etc/poolparty/env.yml') ⇒ Object

Read yaml file and use it to set environment variables and local variables.



180
181
182
183
184
# File 'lib/cloud_providers/ec2/ec2.rb', line 180

def set_aws_env_from_yml_file(filename='/etc/poolparty/env.yml')
  aws = self.class.load_keys_from_file(filename)
  aws.each{|k,v| ENV[k.upcase]=v.to_s}
  set_vars_from_options aws
end

#terminate_instance!(o = {}) ⇒ Object

Terminate an instance (or instances) by passing :instance_id and :instance_ids

Raises:

  • (StandardError)


144
145
146
147
148
149
# File 'lib/cloud_providers/ec2/ec2.rb', line 144

def terminate_instance!(o={})
  raise StandardError.new("You must pass an instance_id when terminating an instance with ec2") unless o[:instance_id] || o[:instance_ids]
  instance_ids = o[:instance_ids] || [o[:instance_id]]
  response = ec2.terminate_instances(instance_ids)
  response.collect{|i| Ec2Instance.new(Ec2Response.pp_format(i)) }
end