From bc3e19545cbee580ececc404e268e1922b4ee04e Mon Sep 17 00:00:00 2001 From: Hardy Ferentschik Date: Wed, 22 Jun 2016 14:54:02 +0200 Subject: [PATCH] Issue #189 Improve host sisible IP detection. In the case of a single private network with static IP the default IP detection meachanism can be bypassed --- lib/landrush/action/setup.rb | 24 +++++++++++++++++++--- test/landrush/action/setup_test.rb | 33 ++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/lib/landrush/action/setup.rb b/lib/landrush/action/setup.rb index e0620a4..7cd0e62 100644 --- a/lib/landrush/action/setup.rb +++ b/lib/landrush/action/setup.rb @@ -54,7 +54,7 @@ def start_server def setup_static_dns config.hosts.each do |hostname, dns_value| - dns_value ||= machine.guest.capability(:read_host_visible_ip_address) + dns_value ||= host_ip_address unless Store.hosts.has?(hostname, dns_value) info "adding static entry: #{hostname} => #{dns_value}" Store.hosts.set hostname, dns_value @@ -64,8 +64,7 @@ def setup_static_dns end def record_machine_dns_entry - ip_address = machine.config.landrush.host_ip_address || - machine.guest.capability(:read_host_visible_ip_address) + ip_address = machine.config.landrush.host_ip_address || host_ip_address unless machine_hostname.match(config.tld) log :error, "hostname #{machine_hostname} does not match the configured TLD: #{config.tld}" @@ -79,9 +78,28 @@ def record_machine_dns_entry end end + def host_ip_address + static_private_network_ip || machine.guest.capability(:read_host_visible_ip_address) + end + def private_network_exists? machine.config.vm.networks.any? { |type, _| type == :private_network } end + + # machine.config.vm.networks is an array of two elements. The first containing the type as symbol, the second is a + # hash containing other config data which varies between types + def static_private_network_ip + # select all staticlly defined private network ip + private_networks = machine.config.vm.networks.select {|network| :private_network == network[0] && !network[1][:ip].nil?} + .map {|network| network[1][:ip]} + if machine.config.landrush.host_ip_address.nil? + private_networks[0] if private_networks.length == 1 + elsif private_networks.include? machine.config.landrush.host_ip_address + machine.config.landrush.host_ip_address + end + # If there is more than one private network or there is no match between config.landrush.host_ip_address + # and the discovered addresses we will pass on to read_host_visible_ip_address capability + end end end end diff --git a/test/landrush/action/setup_test.rb b/test/landrush/action/setup_test.rb index 13b4a8e..b11e79b 100644 --- a/test/landrush/action/setup_test.rb +++ b/test/landrush/action/setup_test.rb @@ -68,6 +68,39 @@ module Action DependentVMs.list.must_equal [] end + it "for single private network IP host visible IP can be retrieved w/o starting the VM" do + app = proc {} + setup = Setup.new(app, nil) + env = fake_environment + env[:machine].config.vm.network :private_network, ip: '42.42.42.42' + + setup.call(env) + setup.static_private_network_ip.must_equal '42.42.42.42' + end + + it "for multiple private network IPs host visible IP cannot be retrieved w/o starting the VM" do + app = proc {} + setup = Setup.new(app, nil) + env = fake_environment + env[:machine].config.vm.network :private_network, ip: '42.42.42.41' + env[:machine].config.vm.network :private_network, ip: '42.42.42.42' + + setup.call(env) + setup.static_private_network_ip.must_be_nil + end + + it "for multiple private network IPs host visible IP cant be retrieved if host_ip_address is set" do + app = proc {} + setup = Setup.new(app, nil) + env = fake_environment + + env[:machine].config.vm.network :private_network, ip: '42.42.42.41' + env[:machine].config.vm.network :private_network, ip: '42.42.42.42' + env[:machine].config.landrush.host_ip_address = '42.42.42.42' + setup.call(env) + setup.static_private_network_ip.must_equal '42.42.42.42' + end + describe 'after boot' do it "stores the machine's hostname => ip address" do app = proc {}