Class: Bosh::Aws::EC2
- Inherits:
-
Object
- Object
- Bosh::Aws::EC2
- Defined in:
- lib/bosh_cli_plugin_aws/ec2.rb
Constant Summary collapse
- NAT_AMI_ID =
{ 'us-east-1' => 'ami-f619c29f', # ami-vpc-nat-1.1.0-beta 'us-west-1' => 'ami-3bcc9e7e', # ami-vpc-nat-1.0.0-beta 'us-west-2' => 'ami-52ff7262', # ami-vpc-nat-1.0.0-beta 'eu-west-1' => 'ami-e5e2d991', # ami-vpc-nat-1.1.0-beta 'ap-southeast-1' => 'ami-02eb9350', # ami-vpc-nat-1.0.0-beta 'ap-northeast-1' => 'ami-14d86d15', # ami-vpc-nat-1.0.0-beta 'ap-southeast-2' => 'ami-ab990e91', # ami-vpc-nat-1.0.0-beta 'sa-east-1' => 'ami-0039e61d', # ami-vpc-nat-1.0.0-beta }
Instance Attribute Summary collapse
-
#elastic_ips ⇒ Object
readonly
Returns the value of attribute elastic_ips.
Instance Method Summary collapse
- #add_key_pair(name, path_to_public_private_key) ⇒ Object
- #allocate_elastic_ip ⇒ Object
- #allocate_elastic_ips(count) ⇒ Object
- #create_instance(options) ⇒ Object
- #create_internet_gateway ⇒ Object
- #create_nat_instance(options) ⇒ Object
- #delete_all_security_groups ⇒ Object
- #delete_internet_gateways(ids) ⇒ Object
- #delete_volumes ⇒ Object
- #dhcp_options ⇒ Object
- #disable_src_dest_checking(instance_id) ⇒ Object
- #force_add_key_pair(name, path_to_public_private_key) ⇒ Object
- #get_running_instance_by_name(name) ⇒ Object
-
#initialize(credentials) ⇒ EC2
constructor
A new instance of EC2.
- #instance_names ⇒ Object
- #instances_count ⇒ Object
- #internet_gateway_ids ⇒ Object
- #key_pair_by_name(name) ⇒ Object
- #key_pairs ⇒ Object
- #release_all_elastic_ips ⇒ Object
- #release_elastic_ips(ips) ⇒ Object
- #remove_all_key_pairs ⇒ Object
- #remove_key_pair(name) ⇒ Object
- #terminatable_instance_names ⇒ Object
- #terminate_instances ⇒ Object
- #volume_count ⇒ Object
- #vpcs ⇒ Object
Constructor Details
#initialize(credentials) ⇒ EC2
Returns a new instance of EC2.
18 19 20 21 |
# File 'lib/bosh_cli_plugin_aws/ec2.rb', line 18 def initialize(credentials) @aws_provider = AwsProvider.new(credentials) @elastic_ips = [] end |
Instance Attribute Details
#elastic_ips ⇒ Object (readonly)
Returns the value of attribute elastic_ips.
16 17 18 |
# File 'lib/bosh_cli_plugin_aws/ec2.rb', line 16 def elastic_ips @elastic_ips end |
Instance Method Details
#add_key_pair(name, path_to_public_private_key) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/bosh_cli_plugin_aws/ec2.rb', line 150 def add_key_pair(name, path_to_public_private_key) private_key_path = path_to_public_private_key.gsub(/\.pub$/, '') public_key_path = "#{private_key_path}.pub" if !File.exist?(private_key_path) system "ssh-keygen", "-q", '-N', "", "-t", "rsa", "-f", private_key_path end unless key_pair_by_name(name).nil? err "Key pair #{name} already exists on AWS" end aws_ec2.key_pairs.import(name, File.read(public_key_path)) end |
#allocate_elastic_ip ⇒ Object
42 43 44 |
# File 'lib/bosh_cli_plugin_aws/ec2.rb', line 42 def allocate_elastic_ip aws_ec2.elastic_ips.allocate(vpc: true) end |
#allocate_elastic_ips(count) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/bosh_cli_plugin_aws/ec2.rb', line 35 def allocate_elastic_ips(count) count.times do @elastic_ips << allocate_elastic_ip.public_ip end #say "\tallocated #{eip.public_ip}".make_green end |
#create_instance(options) ⇒ Object
70 71 72 |
# File 'lib/bosh_cli_plugin_aws/ec2.rb', line 70 def create_instance() aws_ec2.instances.create() end |
#create_internet_gateway ⇒ Object
54 55 56 |
# File 'lib/bosh_cli_plugin_aws/ec2.rb', line 54 def create_internet_gateway aws_ec2.internet_gateways.create end |
#create_nat_instance(options) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/bosh_cli_plugin_aws/ec2.rb', line 74 def create_nat_instance() name = ["name"] key_pair = select_key_pair_for_instance(name, ["key_name"]) = { image_id: NAT_AMI_ID[aws_provider.region], instance_type: .fetch("instance_type", "m1.small"), subnet: ["subnet_id"], private_ip_address: ["ip"], security_groups: [["security_group"]], key_name: key_pair } create_instance().tap do |instance| Bosh::AwsCloud::ResourceWait.for_instance(instance: instance, state: :running) instance.add_tag("Name", {value: name}) elastic_ip = allocate_elastic_ip Bosh::Common.retryable(tries: 30, on: AWS::EC2::Errors::InvalidAddress::NotFound) do instance.associate_elastic_ip(elastic_ip) true end disable_src_dest_checking(instance.id) end end |
#delete_all_security_groups ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/bosh_cli_plugin_aws/ec2.rb', line 194 def delete_all_security_groups dsg = deletable_security_groups # Revoke all permissions before deleting because a permission can reference # another security group, causing a delete to fail dsg.each do |sg| sg..map(&:revoke) sg..map(&:revoke) end dsg.each do |sg| sg.delete unless (sg.name == "default" && !sg.vpc_id) end end |
#delete_internet_gateways(ids) ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/bosh_cli_plugin_aws/ec2.rb', line 62 def delete_internet_gateways(ids) Array(ids).each do |id| gw = aws_ec2.internet_gateways[id] gw..map(&:delete) gw.delete end end |
#delete_volumes ⇒ Object
136 137 138 139 140 141 142 143 144 |
# File 'lib/bosh_cli_plugin_aws/ec2.rb', line 136 def delete_volumes unattached_volumes.each do |vol| begin vol.delete rescue AWS::EC2::Errors::InvalidVolume::NotFound # ignored end end end |
#dhcp_options ⇒ Object
31 32 33 |
# File 'lib/bosh_cli_plugin_aws/ec2.rb', line 31 def aws_ec2. end |
#disable_src_dest_checking(instance_id) ⇒ Object
100 101 102 103 104 105 |
# File 'lib/bosh_cli_plugin_aws/ec2.rb', line 100 def disable_src_dest_checking(instance_id) aws_ec2.client.modify_instance_attribute( :instance_id => instance_id, :source_dest_check => {:value => false} ) end |
#force_add_key_pair(name, path_to_public_private_key) ⇒ Object
173 174 175 176 |
# File 'lib/bosh_cli_plugin_aws/ec2.rb', line 173 def force_add_key_pair(name, path_to_public_private_key) remove_key_pair(name) add_key_pair(name, path_to_public_private_key) end |
#get_running_instance_by_name(name) ⇒ Object
123 124 125 126 127 |
# File 'lib/bosh_cli_plugin_aws/ec2.rb', line 123 def get_running_instance_by_name(name) instances = aws_ec2.instances.select { |instance| instance.["Name"] == name && instance.status == :running } raise "More than one running instance with name '#{name}'." if instances.count > 1 instances.first end |
#instance_names ⇒ Object
116 117 118 119 120 121 |
# File 'lib/bosh_cli_plugin_aws/ec2.rb', line 116 def instance_names terminatable_instances.inject({}) do |memo, instance| memo[instance.instance_id] = instance.["Name"] || '<unnamed instance>' memo end end |
#instances_count ⇒ Object
23 24 25 |
# File 'lib/bosh_cli_plugin_aws/ec2.rb', line 23 def instances_count terminatable_instances.size end |
#internet_gateway_ids ⇒ Object
58 59 60 |
# File 'lib/bosh_cli_plugin_aws/ec2.rb', line 58 def internet_gateway_ids aws_ec2.internet_gateways.map(&:id) end |
#key_pair_by_name(name) ⇒ Object
165 166 167 |
# File 'lib/bosh_cli_plugin_aws/ec2.rb', line 165 def key_pair_by_name(name) key_pairs.detect { |kp| kp.name == name } end |
#key_pairs ⇒ Object
169 170 171 |
# File 'lib/bosh_cli_plugin_aws/ec2.rb', line 169 def key_pairs aws_ec2.key_pairs.to_a end |
#release_all_elastic_ips ⇒ Object
50 51 52 |
# File 'lib/bosh_cli_plugin_aws/ec2.rb', line 50 def release_all_elastic_ips releasable_elastic_ips.map(&:release) end |
#release_elastic_ips(ips) ⇒ Object
46 47 48 |
# File 'lib/bosh_cli_plugin_aws/ec2.rb', line 46 def release_elastic_ips(ips) aws_ec2.elastic_ips.each { |ip| ip.release if ips.include? ip.public_ip } end |
#remove_all_key_pairs ⇒ Object
186 187 188 189 190 191 192 |
# File 'lib/bosh_cli_plugin_aws/ec2.rb', line 186 def remove_all_key_pairs aws_ec2.key_pairs.each(&:delete) Bosh::Common.retryable(tries: 10) do aws_ec2.key_pairs.to_a.empty? end end |
#remove_key_pair(name) ⇒ Object
178 179 180 181 182 183 184 |
# File 'lib/bosh_cli_plugin_aws/ec2.rb', line 178 def remove_key_pair(name) key_pair = key_pair_by_name(name) key_pair.delete unless key_pair.nil? Bosh::Common.retryable(tries: 15) do key_pair_by_name(name).nil? end end |
#terminatable_instance_names ⇒ Object
129 130 131 132 133 134 |
# File 'lib/bosh_cli_plugin_aws/ec2.rb', line 129 def terminatable_instance_names terminatable_instances.inject({}) do |memo, instance| memo[instance.instance_id] = instance.["Name"] memo end end |
#terminate_instances ⇒ Object
107 108 109 110 111 112 113 114 |
# File 'lib/bosh_cli_plugin_aws/ec2.rb', line 107 def terminate_instances terminatable_instances.each(&:terminate) 1.upto(100) do break if terminatable_instances.empty? sleep 4 end terminatable_instances.empty? end |
#volume_count ⇒ Object
146 147 148 |
# File 'lib/bosh_cli_plugin_aws/ec2.rb', line 146 def volume_count unattached_volumes.count end |
#vpcs ⇒ Object
27 28 29 |
# File 'lib/bosh_cli_plugin_aws/ec2.rb', line 27 def vpcs aws_ec2.vpcs end |