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

Commit

Permalink
Read IP from guest even if there's just 1 private network
Browse files Browse the repository at this point in the history
In case we want to use the public network's address
  • Loading branch information
njam committed Aug 17, 2016
1 parent 8983ae4 commit d0bf44b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 22 deletions.
25 changes: 11 additions & 14 deletions lib/landrush/action/setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ def call(env)
end

def host_ip_address
static_private_network_ip || machine.guest.capability(:read_host_visible_ip_address)
if private_network_ips.include? machine.config.landrush.host_ip_address
machine.config.landrush.host_ip_address
else
machine.guest.capability(:read_host_visible_ip_address)
end
end

private
Expand Down Expand Up @@ -97,19 +101,12 @@ 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 statically 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
# @return [Array<String] IPv4 addresses of all private networks
def private_network_ips
# 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
machine.config.vm.networks.select { |network| :private_network == network[0] && !network[1][:ip].nil? }
.map { |network| network[1][:ip] }
end
end
end
Expand Down
8 changes: 0 additions & 8 deletions test/landrush/action/setup_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,6 @@ 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
setup = Setup.new(app, nil)
env[:machine].config.vm.network :private_network, ip: '42.42.42.42'

setup.call(env)
Store.hosts.get('somehost.vagrant.test').must_equal '42.42.42.42'
end

it "for multiple private network IPs host visible IP cant be retrieved if host_ip_address is set" do
setup = Setup.new(app, nil)

Expand Down

0 comments on commit d0bf44b

Please sign in to comment.