Skip to content

Commit

Permalink
Merge pull request #440 from Parallels/fix-ip-detection
Browse files Browse the repository at this point in the history
driver: Fix the detection of VM IP using prlctl command
  • Loading branch information
legal90 committed Mar 23, 2023
2 parents 5892f8e + 696be2c commit 858dd51
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions lib/vagrant-parallels/driver/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ def read_forwarded_ports(global = false)
# Returns an IP of the virtual machine fetched from the DHCP lease file.
# It requires that Shared network adapter is configured for this VM
# and it obtains an IP via DHCP.
# Returns an empty string if the IP coudn't be determined this way.
#
# @return [String] IP address leased by DHCP server in "Shared" network
def read_guest_ip_dhcp
Expand All @@ -432,12 +433,13 @@ def read_guest_ip_dhcp
end

# Returns an IP of the virtual machine fetched from prlctl.
# Returns an empty string if the IP coudn't be determined this way.
#
# @return [String] IP address returned by `prlctl list -f` command
def read_guest_ip_prlctl
vm_info = json { execute_prlctl('list', @uuid, '--full', '--json') }
ip = vm_info.first.fetch('ip_configured', nil)
ip == '-' ? nil : ip
ip = vm_info.first.fetch('ip_configured', '')
ip == '-' ? '' : ip
end

# Returns path to the Parallels Tools ISO file.
Expand Down Expand Up @@ -768,13 +770,25 @@ def share_folders(folders)
end

# Reads the SSH IP of this VM from DHCP lease file or from `prlctl list`
# command - whatever returns a non-empty result.
# command - whatever returns a non-empty result first.
# The method with DHCP does not work for *.macvm VMs on Apple M-series Macs,
# so we try both sources here.
#
# @return [String] IP address to use for SSH connection to the VM.
def ssh_ip
read_guest_ip_dhcp || read_guest_ip_prlctl
5.times do
ip = read_guest_ip_dhcp
return ip unless ip.empty?

ip = read_guest_ip_prlctl
return ip unless ip.empty?

sleep 2
end

# We didn't manage to determine IP - return nil and
# expect SSH client to do a retry
return nil
end

# Reads the SSH port of this VM.
Expand Down

0 comments on commit 858dd51

Please sign in to comment.