Skip to content
This repository has been archived by the owner on Jun 25, 2023. It is now read-only.

Issue #189 Use static private network configuration to find routable host IP #192

Merged
merged 1 commit into from
Jun 22, 2016
Merged
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
24 changes: 21 additions & 3 deletions lib/landrush/action/setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}"
Expand All @@ -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
33 changes: 33 additions & 0 deletions test/landrush/action/setup_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down