Skip to content

Commit 5ef9f1f

Browse files
author
Madhurranjan Mohaan
committed
Elastic ip modifications
1 parent 808f7c5 commit 5ef9f1f

File tree

3 files changed

+75
-11
lines changed

3 files changed

+75
-11
lines changed

lib/vagrant-aws/action/run_instance.rb

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def call(env)
3737
user_data = region_config.user_data
3838
block_device_mapping = region_config.block_device_mapping
3939
elastic_ip = region_config.elastic_ip
40+
allocate_elastic_ip = region_config.allocate_elastic_ip
4041
terminate_on_shutdown = region_config.terminate_on_shutdown
4142
iam_instance_profile_arn = region_config.iam_instance_profile_arn
4243
iam_instance_profile_name = region_config.iam_instance_profile_name
@@ -47,7 +48,7 @@ def call(env)
4748
end
4849

4950
# If there is a subnet ID then warn the user
50-
if subnet_id && !elastic_ip
51+
if subnet_id and not elastic_ip and not allocate_elastic_ip
5152
env[:ui].warn(I18n.t("vagrant_aws.launch_vpc_warning"))
5253
end
5354

@@ -62,7 +63,8 @@ def call(env)
6263
env[:ui].info(" -- IAM Instance Profile ARN: #{iam_instance_profile_arn}") if iam_instance_profile_arn
6364
env[:ui].info(" -- IAM Instance Profile Name: #{iam_instance_profile_name}") if iam_instance_profile_name
6465
env[:ui].info(" -- Private IP: #{private_ip_address}") if private_ip_address
65-
env[:ui].info(" -- Elastic IP: #{elastic_ip}") if elastic_ip
66+
env[:ui].info(" -- Elastic IP: #{elastic_ip.inspect}") if elastic_ip
67+
env[:ui].info(" -- Allocate Elastic IP: #{allocate_elastic_ip.inspect}") if allocate_elastic_ip
6668
env[:ui].info(" -- User Data: yes") if user_data
6769
env[:ui].info(" -- Security Groups: #{security_groups.inspect}") if !security_groups.empty?
6870
env[:ui].info(" -- User Data: #{user_data}") if user_data
@@ -80,6 +82,8 @@ def call(env)
8082
:iam_instance_profile_name => iam_instance_profile_name,
8183
:tags => tags,
8284
:user_data => user_data,
85+
:elastic_ip => elastic_ip,
86+
:allocate_elastic_ip => allocate_elastic_ip,
8387
:block_device_mapping => block_device_mapping,
8488
:instance_initiated_shutdown_behavior => terminate_on_shutdown == true ? "terminate" : nil
8589
}
@@ -138,9 +142,14 @@ def call(env)
138142
@logger.info("Time to instance ready: #{env[:metrics]["instance_ready_time"]}")
139143

140144
# Allocate and associate an elastic IP if requested
145+
#if elastic_ip
146+
# domain = subnet_id ? 'vpc' : 'standard'
147+
# do_elastic_ip(env, domain, server)
148+
#end
141149
if elastic_ip
142-
domain = subnet_id ? 'vpc' : 'standard'
143-
do_elastic_ip(env, domain, server)
150+
associate_elastic_ip(env,elastic_ip)
151+
elsif allocate_elastic_ip
152+
allocate_and_associate_elastic_ip(env,allocate_elastic_ip)
144153
end
145154

146155
if !env[:interrupted]
@@ -232,6 +241,47 @@ def terminate(env)
232241
destroy_env[:force_confirm_destroy] = true
233242
env[:action_runner].run(Action.action_destroy, destroy_env)
234243
end
244+
245+
def associate_elastic_ip(env,elastic_ip)
246+
begin
247+
eip = env[:aws_compute].addresses.get(elastic_ip)
248+
if eip.nil?
249+
terminate(env)
250+
raise Errors::FogError,
251+
:message => "Elastic IP specified not found: #{elastic_ip}"
252+
end
253+
@logger.info("eip - #{eip}")
254+
env[:aws_compute].associate_address(env[:machine].id,nil,nil,eip.allocation_id)
255+
env[:ui].info(I18n.t("vagrant_aws.elastic_ip_allocated"))
256+
rescue Fog::Compute::AWS::NotFound => e
257+
# Invalid elasticip doesn't have its own error so we catch and
258+
# check the error message here.
259+
if e.message =~ /Elastic IP/
260+
terminate(env)
261+
raise Errors::FogError,
262+
:message => "Elastic IP not found: #{elastic_ip}"
263+
end
264+
raise
265+
end
266+
end
267+
268+
def allocate_and_associate_elastic_ip(env,allocate_elastic_ip)
269+
begin
270+
allocated_eip = env[:aws_compute].allocate_address(allocate_elastic_ip)
271+
associate_elastic_ip(env,allocated_eip[:body]["publicIp"])
272+
rescue Fog::Compute::AWS::NotFound => e
273+
# Invalid computation of elasticip doesn't have its own error so we catch and
274+
# check the error message here.
275+
if e.message =~ /Elastic IP/
276+
terminate(env)
277+
raise Errors::FogError,
278+
:message => "Elastic IP not allocated with allocate_elastic_ip option: #{allocate_elastic_ip}"
279+
end
280+
raise
281+
end
282+
end
283+
284+
235285
end
236286
end
237287
end

lib/vagrant-aws/action/terminate_instance.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,19 @@ def call(env)
1717
# Destroy the server and remove the tracking ID
1818
env[:ui].info(I18n.t("vagrant_aws.terminating"))
1919
server.destroy
20+
elastic_ip = env[:aws_compute].describe_addresses('public-ip' => server.public_ip_address)
21+
if !elastic_ip[:body]["addressesSet"].empty?
22+
env[:aws_compute].disassociate_address(nil,elastic_ip[:body]["addressesSet"][0]["associationId"]) if !elastic_ip[:body]["addressesSet"].empty?
23+
env[:ui].info(I18n.t("vagrant_aws.elastic_ip_deallocated"))
24+
end
2025
env[:machine].id = nil
2126

2227
# Release the elastic IP
23-
ip_file = env[:machine].data_dir.join('elastic_ip')
24-
if ip_file.file?
25-
release_address(env,ip_file.read)
26-
ip_file.delete
27-
end
28+
#ip_file = env[:machine].data_dir.join('elastic_ip')
29+
#if ip_file.file?
30+
# release_address(env,ip_file.read)
31+
# ip_file.delete
32+
#end
2833

2934
@app.call(env)
3035
end

lib/vagrant-aws/config.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,14 @@ class Config < Vagrant.plugin("2", :config)
4141

4242
# Acquire and attach an elastic IP address (VPC).
4343
#
44-
# @return [Boolean]
44+
# @return [String]
4545
attr_accessor :elastic_ip
4646

47+
# Allocate Elastic IP
48+
#
49+
# @return [String]
50+
attr_accessor :allocate_elastic_ip
51+
4752
# The name of the AWS region in which to create the instance.
4853
#
4954
# @return [String]
@@ -142,6 +147,7 @@ def initialize(region_specific=false)
142147
@use_iam_profile = UNSET_VALUE
143148
@block_device_mapping = []
144149
@elastic_ip = UNSET_VALUE
150+
@allocate_elastic_ip = UNSET_VALUE
145151
@iam_instance_profile_arn = UNSET_VALUE
146152
@iam_instance_profile_name = UNSET_VALUE
147153
@terminate_on_shutdown = UNSET_VALUE
@@ -240,9 +246,12 @@ def finalize!
240246
# Default the private IP to nil since VPC is not default
241247
@private_ip_address = nil if @private_ip_address == UNSET_VALUE
242248

243-
# Acquire an elastic IP if requested
249+
# Acquire an elastic IP if requested ; This is the case where eip has to be explicitly allocated
244250
@elastic_ip = nil if @elastic_ip == UNSET_VALUE
245251

252+
# Allocate Elastic ip is nil by default
253+
@allocate_elastic_ip = nil if @allocate_elastic_ip == UNSET_VALUE
254+
246255
# Default region is us-east-1. This is sensible because AWS
247256
# generally defaults to this as well.
248257
@region = "us-east-1" if @region == UNSET_VALUE

0 commit comments

Comments
 (0)