From 42ce50dc4a9f34868e85d80a327343269d4d58ac Mon Sep 17 00:00:00 2001 From: "yuma.iwasaki" Date: Wed, 11 Jun 2014 23:41:40 +0900 Subject: [PATCH 1/2] add associate elastic ip support --- lib/vagrant-aws/action/run_instance.rb | 45 ++++++++++++++------------ 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/lib/vagrant-aws/action/run_instance.rb b/lib/vagrant-aws/action/run_instance.rb index 0eede22b..6c4c0506 100644 --- a/lib/vagrant-aws/action/run_instance.rb +++ b/lib/vagrant-aws/action/run_instance.rb @@ -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] @@ -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 } 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'] } + end end unless association.body['return'] From 345addbe5d7115459e2cd398a5ddf50bfd022aa7 Mon Sep 17 00:00:00 2001 From: "yuma.iwasaki" Date: Tue, 17 Jun 2014 18:23:18 +0900 Subject: [PATCH 2/2] added elastic_ip --- lib/vagrant-aws/action/run_instance.rb | 4 ++-- lib/vagrant-aws/action/terminate_instance.rb | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/vagrant-aws/action/run_instance.rb b/lib/vagrant-aws/action/run_instance.rb index 6c4c0506..9a7fe3d9 100644 --- a/lib/vagrant-aws/action/run_instance.rb +++ b/lib/vagrant-aws/action/run_instance.rb @@ -201,7 +201,7 @@ def allows_ssh_port?(env, test_sec_groups, is_vpc) 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 } + h = { :public_ip => elastic_ip, :is_ip_retain => true } else begin allocation = env[:aws_compute].allocate_address(domain) @@ -221,7 +221,7 @@ def do_elastic_ip(env, domain, server, elastic_ip) 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'] } + h = { :public_ip => allocation.body['publicIp'] ,:is_ip_retain => false } end end diff --git a/lib/vagrant-aws/action/terminate_instance.rb b/lib/vagrant-aws/action/terminate_instance.rb index 317a659f..38672d5f 100644 --- a/lib/vagrant-aws/action/terminate_instance.rb +++ b/lib/vagrant-aws/action/terminate_instance.rb @@ -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