Skip to content
This repository has been archived by the owner on Feb 11, 2022. It is now read-only.

add associate elastic ip support #248

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 25 additions & 20 deletions lib/vagrant-aws/action/run_instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def call(env)
# Allocate and associate an elastic IP if requested
if elastic_ip
domain = subnet_id ? 'vpc' : 'standard'
do_elastic_ip(env, domain, server)
do_elastic_ip(env, domain, server,elastic_ip)
end

if !env[:interrupted]
Expand Down Expand Up @@ -198,26 +198,31 @@ def allows_ssh_port?(env, test_sec_groups, is_vpc)
!rules.select { |r| (r["fromPort"]..r["toPort"]).include?(port) }.empty?
end

def do_elastic_ip(env, domain, server)
begin
allocation = env[:aws_compute].allocate_address(domain)
rescue
@logger.debug("Could not allocate Elastic IP.")
terminate(env)
raise Errors::FogError,
:message => "Could not allocate Elastic IP."
end
@logger.debug("Public IP #{allocation.body['publicIp']}")

# Associate the address and save the metadata to a hash
if domain == 'vpc'
# VPC requires an allocation ID to assign an IP
association = env[:aws_compute].associate_address(server.id, nil, nil, allocation.body['allocationId'])
h = { :allocation_id => allocation.body['allocationId'], :association_id => association.body['associationId'], :public_ip => allocation.body['publicIp'] }
def do_elastic_ip(env, domain, server, elastic_ip)
if(elastic_ip.kind_of?(String))
association = env[:aws_compute].associate_address(server.id, elastic_ip)
h = { :public_ip => elastic_ip, :is_ip_retain => true }
else
# Standard EC2 instances only need the allocated IP address
association = env[:aws_compute].associate_address(server.id, allocation.body['publicIp'])
h = { :public_ip => allocation.body['publicIp'] }
begin
allocation = env[:aws_compute].allocate_address(domain)
rescue
@logger.debug("Could not allocate Elastic IP.")
terminate(env)
raise Errors::FogError,
:message => "Could not allocate Elastic IP."
end
@logger.debug("Public IP #{allocation.body['publicIp']}")

# Associate the address and save the metadata to a hash
if domain == 'vpc'
# VPC requires an allocation ID to assign an IP
association = env[:aws_compute].associate_address(server.id, nil, nil, allocation.body['allocationId'])
h = { :allocation_id => allocation.body['allocationId'], :association_id => association.body['associationId'], :public_ip => allocation.body['publicIp'] }
else
# Standard EC2 instances only need the allocated IP address
association = env[:aws_compute].associate_address(server.id, allocation.body['publicIp'])
h = { :public_ip => allocation.body['publicIp'] ,:is_ip_retain => false }
end
end

unless association.body['return']
Expand Down
6 changes: 5 additions & 1 deletion lib/vagrant-aws/action/terminate_instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@ def call(env)
# Release an elastic IP address
def release_address(env,eip)
h = JSON.parse(eip)
p h
# Use association_id and allocation_id for VPC, use public IP for EC2
if h['association_id']
env[:aws_compute].disassociate_address(nil,h['association_id'])
env[:aws_compute].release_address(h['allocation_id'])
else
env[:aws_compute].disassociate_address(h['public_ip'])
env[:aws_compute].release_address(h['public_ip'])
# Retain public IP
if(!h['is_ip_retain'])
env[:aws_compute].release_address(h['public_ip'])
end
end
end
end
Expand Down