Class: CloudProviders::Ec2
- Inherits:
-
CloudProvider
- Object
- CloudProvider
- CloudProviders::Ec2
- 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/aws.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/ec2/aws.yml', caching = true) ⇒ Object
Load the yaml file containing keys.
Instance Method Summary collapse
-
#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.
-
#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/aws.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 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/aws.yml if the environment variable is not set
29 30 31 |
# File 'lib/cloud_providers/ec2/ec2.rb', line 29 def self.default_access_key ENV['EC2_ACCESS_KEY'] || load_keys_from_file[:access_key] end |
.default_cert ⇒ Object
41 42 43 |
# File 'lib/cloud_providers/ec2/ec2.rb', line 41 def self.default_cert ENV['EC2_CERT'] || load_keys_from_file[:cert] end |
.default_cloud_cert ⇒ Object
57 58 59 |
# File 'lib/cloud_providers/ec2/ec2.rb', line 57 def self.default_cloud_cert ENV['CLOUD_CERT'] || ENV['EUCALYPTUS_CERT'] || load_keys_from_file[:cloud_cert] end |
.default_ec2_url ⇒ Object
49 50 51 |
# File 'lib/cloud_providers/ec2/ec2.rb', line 49 def self.default_ec2_url ENV['EC2_URL'] || load_keys_from_file[:ec2_url] end |
.default_private_key ⇒ Object
37 38 39 |
# File 'lib/cloud_providers/ec2/ec2.rb', line 37 def self.default_private_key ENV['EC2_PRIVATE_KEY'] || load_keys_from_file[:private_key] end |
.default_s3_url ⇒ Object
53 54 55 |
# File 'lib/cloud_providers/ec2/ec2.rb', line 53 def self.default_s3_url ENV['S3_URL'] || load_keys_from_file[:s3_url] end |
.default_secret_access_key ⇒ Object
33 34 35 |
# File 'lib/cloud_providers/ec2/ec2.rb', line 33 def self.default_secret_access_key ENV['EC2_SECRET_KEY'] || load_keys_from_file[:access_key] end |
.default_user_id ⇒ Object
45 46 47 |
# File 'lib/cloud_providers/ec2/ec2.rb', line 45 def self.default_user_id ENV['EC2_USER_ID'] || load_keys_from_file[:user_id] end |
.load_keys_from_file(filename = '/etc/poolparty/ec2/aws.yml', caching = true) ⇒ Object
Load the yaml file containing keys. If the file does not exist, return an empty hash
62 63 64 65 66 |
# File 'lib/cloud_providers/ec2/ec2.rb', line 62 def self.load_keys_from_file(filename='/etc/poolparty/ec2/aws.yml', caching=true) return @aws_yml if @aws_yml && caching==true return {} unless File.exists?(filename) @aws_yml = YAML::load( open(filename).read ) end |
Instance Method Details
#amazon? ⇒ Boolean
Are we running on amazon?
149 150 151 152 153 154 |
# File 'lib/cloud_providers/ec2/ec2.rb', line 149 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.
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/cloud_providers/ec2/ec2.rb', line 171 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 |
#create_keypair(kname, path = '~/.ec2') ⇒ Object
shortcut to ec2-add-keypair name > ~./.ec2/kname
192 193 194 |
# File 'lib/cloud_providers/ec2/ec2.rb', line 192 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
124 125 126 |
# File 'lib/cloud_providers/ec2/ec2.rb', line 124 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
129 130 131 132 133 134 |
# File 'lib/cloud_providers/ec2/ec2.rb', line 129 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
95 96 97 |
# File 'lib/cloud_providers/ec2/ec2.rb', line 95 def ec2(o={}) @ec2 ||= Rightscale::Ec2.new(access_key, secret_access_key, o.merge(:logger => PoolParty::PoolPartyLog)) end |
#run_instance(o = {}) ⇒ Object
Start a new instance with the given options
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/cloud_providers/ec2/ec2.rb', line 100 def run_instance(o={}) 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, max_count, 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 #FIXME: This needs to deal with the case when an array is returned if max_instances > 1 instances.first end |
#save_aws_env_to_yml(filename = '/etc/poolparty/aws.yml') ⇒ Object
Save aws keys and env variables to a yaml file
164 165 166 |
# File 'lib/cloud_providers/ec2/ec2.rb', line 164 def save_aws_env_to_yml(filename='/etc/poolparty/aws.yml') File.open(filename, 'w') {|f| f<<YAML::dump(aws_hash) } 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.
157 158 159 160 161 |
# File 'lib/cloud_providers/ec2/ec2.rb', line 157 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
137 138 139 140 141 142 |
# File 'lib/cloud_providers/ec2/ec2.rb', line 137 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 |