diff --git a/lib/vagrant-aws/action/run_instance.rb b/lib/vagrant-aws/action/run_instance.rb index 0eede22b..9a7fe3d9 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, :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'] 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