Class: CloudProviders::Ec2
- Inherits:
-
CloudProvider
- Object
- CloudProvider
- CloudProviders::Ec2
- Includes:
- Ec2Helpers
- Defined in:
- lib/cloud_providers/ec2/ec2.rb
Class Method Summary collapse
-
.default_access_key ⇒ Object
Set the aws keys from the environment, or load from /etc/poolparty/env.yml if the environment variable is not set.
- .default_cert ⇒ Object
- .default_cloud_cert ⇒ Object
- .default_ec2_url ⇒ Object
- .default_private_key ⇒ Object
- .default_s3_url ⇒ Object
- .default_secret_access_key ⇒ Object
- .default_user_id ⇒ Object
-
.load_keys_from_file(filename = '/etc/poolparty/env.yml', caching = true) ⇒ Object
Load the yaml file containing keys.
Instance Method Summary collapse
- #after_compile(cld) ⇒ Object
-
#after_run_instance(instances_list) ⇒ Object
Run after all the instances are run.
-
#amazon? ⇒ Boolean
Are we running on amazon?.
-
#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.
-
#before_compile(cld) ⇒ Object
Callbacks.
-
#create_keypair(kname, path = '~/.ec2') ⇒ Object
shortcut to ec2-add-keypair name > ~./.ec2/kname.
-
#describe_instance(hash_of_criteria_to_select_instance_against) ⇒ Object
Will select the first instance matching the provided criteria hash.
-
#describe_instances(o = {}) ⇒ Object
Describe instances.
- #ec2(o = {}) ⇒ Object
-
#run_instance(o = {}) ⇒ Object
Start a new instance with the given options.
-
#save_aws_env_to_yml(filename = '/etc/poolparty/env.yml') ⇒ Object
Save aws keys and env variables to a yaml file.
-
#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.
-
#terminate_instance!(o = {}) ⇒ Object
Terminate an instance (or instances) by passing :instance_id and :instance_ids.
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
Constructor Details
This class inherits a constructor from CloudProviders::CloudProvider
Class Method Details
.default_access_key ⇒ Object
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_cert ⇒ Object
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_cert ⇒ Object
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_url ⇒ Object
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_key ⇒ Object
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_url ⇒ Object
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_key ⇒ Object
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_id ⇒ Object
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
173 174 175 |
# File 'lib/cloud_providers/ec2/ec2.rb', line 173 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
178 179 180 181 182 183 |
# File 'lib/cloud_providers/ec2/ec2.rb', line 178 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?
162 163 164 165 166 167 |
# File 'lib/cloud_providers/ec2/ec2.rb', line 162 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.
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/cloud_providers/ec2/ec2.rb', line 201 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
170 171 |
# File 'lib/cloud_providers/ec2/ec2.rb', line 170 def before_compile(cld) end |
#create_keypair(kname, path = '~/.ec2') ⇒ Object
shortcut to ec2-add-keypair name > ~./.ec2/kname
222 223 224 |
# File 'lib/cloud_providers/ec2/ec2.rb', line 222 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
137 138 139 |
# File 'lib/cloud_providers/ec2/ec2.rb', line 137 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
142 143 144 145 146 147 |
# File 'lib/cloud_providers/ec2/ec2.rb', line 142 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(.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
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 129 130 131 132 133 134 |
# File 'lib/cloud_providers/ec2/ec2.rb', line 104 def run_instance(o={}) number_of_instances = o[:number_of_instances] || 1 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 vputs("--- Launching ec2 instances") vputs({ "image_id" => image_id, "security_group" => security_group, "keypair" => keypair.basename }) 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
193 194 195 196 |
# File 'lib/cloud_providers/ec2/ec2.rb', line 193 def save_aws_env_to_yml(filename='/etc/poolparty/env.yml') hsh = aws_hash(, "/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.
186 187 188 189 190 |
# File 'lib/cloud_providers/ec2/ec2.rb', line 186 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} aws end |
#terminate_instance!(o = {}) ⇒ Object
Terminate an instance (or instances) by passing :instance_id and :instance_ids
150 151 152 153 154 155 |
# File 'lib/cloud_providers/ec2/ec2.rb', line 150 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 |