From a7e2af30033a47adfaa935365b331224320eeef9 Mon Sep 17 00:00:00 2001 From: Guillaume Giamarchi Date: Sat, 20 May 2017 15:49:26 +0200 Subject: [PATCH] Fix floating IP assignement Depending on the latency within Neutron to actually assign the floating IP, the plugin doesn't manage to resolve the IP when it tries to access the machine immediately after the assignment request. A race condition is caused by a missing assignment into the machine config object. --- source/lib/vagrant-openstack-provider/config_resolver.rb | 5 +++++ .../vagrant-openstack-provider/config_resolver_spec.rb | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/source/lib/vagrant-openstack-provider/config_resolver.rb b/source/lib/vagrant-openstack-provider/config_resolver.rb index e1b3b88..5f367d8 100644 --- a/source/lib/vagrant-openstack-provider/config_resolver.rb +++ b/source/lib/vagrant-openstack-provider/config_resolver.rb @@ -37,12 +37,17 @@ def resolve_floating_ip(env) config = env[:machine].provider_config nova = env[:openstack_client].nova return config.floating_ip if config.floating_ip + fail Errors::UnableToResolveFloatingIP if config.floating_ip_pool.nil? || config.floating_ip_pool.empty? + @logger.debug 'Searching for available ips' free_ip = search_free_ip(config, nova, env) + config.floating_ip = free_ip return free_ip unless free_ip.nil? + @logger.debug 'Allocate new ip anyway' allocated_ip = allocate_ip(config, nova, env) + config.floating_ip = allocated_ip return allocated_ip unless allocated_ip.nil? end diff --git a/source/spec/vagrant-openstack-provider/config_resolver_spec.rb b/source/spec/vagrant-openstack-provider/config_resolver_spec.rb index 3a25366..5818046 100644 --- a/source/spec/vagrant-openstack-provider/config_resolver_spec.rb +++ b/source/spec/vagrant-openstack-provider/config_resolver_spec.rb @@ -249,6 +249,7 @@ FloatingIP.new('80.81.82.85', 'pool-1', nil) end config.stub(:floating_ip_pool) { ['pool-1'] } + config.stub(:floating_ip=) { nil } @action.resolve_floating_ip(env).should eq('80.81.82.85') end end @@ -261,6 +262,7 @@ FloatingIP.new('80.81.82.83', 'pool-1', nil)] end config.stub(:floating_ip_pool) { ['pool-1'] } + config.stub(:floating_ip=) { nil } @action.resolve_floating_ip(env).should eq('80.81.82.83') end end @@ -275,6 +277,7 @@ FloatingIP.new('80.81.82.84', 'pool-1', nil) end config.stub(:floating_ip_pool) { ['pool-1'] } + config.stub(:floating_ip=) { nil } @action.resolve_floating_ip(env).should eq('80.81.82.84') end end @@ -294,6 +297,7 @@ FloatingIP.new('80.81.82.85', 'pool-1', nil) end config.stub(:floating_ip_pool) { %w(pool-1 pool-2) } + config.stub(:floating_ip=) { nil } @action.resolve_floating_ip(env).should eq('80.81.82.85') end end @@ -306,6 +310,7 @@ FloatingIP.new('80.81.82.83', 'pool-2', nil)] end config.stub(:floating_ip_pool) { %w(pool-1 pool-2) } + config.stub(:floating_ip=) { nil } @action.resolve_floating_ip(env).should eq('80.81.82.83') end end @@ -322,6 +327,7 @@ FloatingIP.new('80.81.82.84', 'pool-1', nil) end config.stub(:floating_ip_pool) { %w(pool-1 pool-2) } + config.stub(:floating_ip=) { nil } @action.resolve_floating_ip(env).should eq('80.81.82.84') end end @@ -337,6 +343,7 @@ FloatingIP.new('80.81.82.84', 'pool-2', nil) end config.stub(:floating_ip_pool) { %w(pool-1 pool-2) } + config.stub(:floating_ip=) { nil } @action.resolve_floating_ip(env).should eq('80.81.82.84') end end @@ -350,6 +357,7 @@ nova.stub(:allocate_floating_ip).with(env, 'pool-1').and_raise Errors::VagrantOpenstackError, message: 'error', code: 404 nova.stub(:allocate_floating_ip).with(env, 'pool-2').and_raise Errors::VagrantOpenstackError, message: 'error', code: 404 config.stub(:floating_ip_pool) { %w(pool-1 pool-2) } + config.stub(:floating_ip=) { nil } expect { @action.resolve_floating_ip(env) }.to raise_error(Errors::VagrantOpenstackError) end end