Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix read_ip lookup ordering to use guest tools first #121

Merged
merged 1 commit into from
Aug 9, 2024
Merged
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
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