Skip to content

Commit

Permalink
Merge pull request #121 from hashicorp/address-lookup-order
Browse files Browse the repository at this point in the history
Fix read_ip lookup ordering to use guest tools first
  • Loading branch information
chrisroberts authored Aug 9, 2024
2 parents 9f00c21 + 2df64a3 commit 11fc774
Showing 1 changed file with 34 additions and 33 deletions.
67 changes: 34 additions & 33 deletions lib/vagrant-vmware-desktop/driver/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,40 @@ def scrub_forwarded_ports
def read_ip(enable_vmrun_ip_lookup=true)
@logger.info("Reading an accessible IP for machine...")

if enable_vmrun_ip_lookup
# Try to read the IP using vmrun getGuestIPAddress. This
# won't work if the guest doesn't have guest tools installed or
# is using an old version of VMware.
begin
@logger.info("Trying vmrun getGuestIPAddress...")
result = vmrun("getGuestIPAddress", host_vmx_path)
result = result.stdout.chomp

# If returned address ends with a ".1" do not accept address
# and allow lookup via VMX.
# see: https://github.com/vmware/open-vm-tools/issues/93
if result.end_with?(".1")
@logger.warn("vmrun getGuestIPAddress returned: #{result}. Result resembles address retrieval from wrong " \
"interface. Discarding value and proceeding with VMX based lookup.")
result = nil
else
# Try to parse the IP Address. This will raise an exception
# if it fails, which will halt our attempt to use it.
IPAddr.new(result)
@logger.info("vmrun getGuestIPAddress success: #{result}")
return result
end
rescue Errors::VMRunError
@logger.info("vmrun getGuestIPAddress failed: VMRunError")
# Ignore, try the MAC address way.
rescue IPAddr::InvalidAddressError
@logger.info("vmrun getGuestIPAddress failed: InvalidAddressError for #{result.inspect}")
# Ignore, try the MAC address way.
end
else
@logger.info("Skipping vmrun getGuestIPAddress as requested by config.")
end

# NOTE: Read from DHCP leases first so we can attempt to fetch the address
# for the vmnet8 device first. If multiple networks are defined on the guest
# it will return the address of the last device, which will fail when doing
Expand Down Expand Up @@ -628,39 +662,6 @@ def read_ip(enable_vmrun_ip_lookup=true)
return dhcp_ip if dhcp_ip
end

if enable_vmrun_ip_lookup
# Try to read the IP using vmrun getGuestIPAddress. This
# won't work if the guest doesn't have guest tools installed or
# is using an old version of VMware.
begin
@logger.info("Trying vmrun getGuestIPAddress...")
result = vmrun("getGuestIPAddress", host_vmx_path)
result = result.stdout.chomp

# If returned address ends with a ".1" do not accept address
# and allow lookup via VMX.
# see: https://github.com/vmware/open-vm-tools/issues/93
if result.end_with?(".1")
@logger.warn("vmrun getGuestIPAddress returned: #{result}. Result resembles address retrieval from wrong " \
"interface. Discarding value and proceeding with VMX based lookup.")
result = nil
else
# Try to parse the IP Address. This will raise an exception
# if it fails, which will halt our attempt to use it.
IPAddr.new(result)
@logger.info("vmrun getGuestIPAddress success: #{result}")
return result
end
rescue Errors::VMRunError
@logger.info("vmrun getGuestIPAddress failed: VMRunError")
# Ignore, try the MAC address way.
rescue IPAddr::InvalidAddressError
@logger.info("vmrun getGuestIPAddress failed: InvalidAddressError for #{result.inspect}")
# Ignore, try the MAC address way.
end
else
@logger.info("Skipping vmrun getGuestIPAddress as requested by config.")
end
nil
end

Expand Down

0 comments on commit 11fc774

Please sign in to comment.