Class: Bosh::Aws::EC2

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

Instance Method Summary collapse

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_ipsObject (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_ipObject



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(options)
  aws_ec2.instances.create(options)
end

#create_internet_gatewayObject



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(options)
  name = options["name"]
  key_pair = select_key_pair_for_instance(name, options["key_name"])


  instance_options = {
      image_id: NAT_AMI_ID[aws_provider.region],
      instance_type: options.fetch("instance_type", "m1.small"),
      subnet: options["subnet_id"],
      private_ip_address: options["ip"],
      security_groups: [options["security_group"]],
      key_name: key_pair
  }

  create_instance(instance_options).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_groupsObject



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.ingress_ip_permissions.map(&:revoke)
    sg.egress_ip_permissions.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.attachments.map(&:delete)
    gw.delete
  end
end

#delete_volumesObject



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_optionsObject



31
32
33
# File 'lib/bosh_cli_plugin_aws/ec2.rb', line 31

def dhcp_options
  aws_ec2.dhcp_options
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.tags["Name"] == name && instance.status == :running }
  raise "More than one running instance with name '#{name}'." if instances.count > 1
  instances.first
end

#instance_namesObject



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.tags["Name"] || '<unnamed instance>'
    memo
  end
end

#instances_countObject



23
24
25
# File 'lib/bosh_cli_plugin_aws/ec2.rb', line 23

def instances_count
  terminatable_instances.size
end

#internet_gateway_idsObject



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_pairsObject



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_ipsObject



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_pairsObject



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_namesObject



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.tags["Name"]
    memo
  end
end

#terminate_instancesObject



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_countObject



146
147
148
# File 'lib/bosh_cli_plugin_aws/ec2.rb', line 146

def volume_count
  unattached_volumes.count
end

#vpcsObject



27
28
29
# File 'lib/bosh_cli_plugin_aws/ec2.rb', line 27

def vpcs
  aws_ec2.vpcs
end