Skip to content

Commit

Permalink
Merge pull request #434 from Parallels/wip-vagrant-ssh
Browse files Browse the repository at this point in the history
Reimplement 'vagrant ssh' using guest-tools
  • Loading branch information
legal90 committed Mar 22, 2023
2 parents 96bde1f + 73dbd9e commit eb92ec5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/vagrant-parallels/action/prepare_nfs_settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def call(env)
# The ! indicates that this method modifies its argument.
def add_ips_to_env!(env)
host_ip = @machine.provider.driver.read_shared_interface[:ip]
guest_ip = @machine.provider.driver.read_guest_ip
guest_ip = @machine.provider.driver.ssh_ip

# If we couldn't determine either guest's or host's IP, then
# it is probably a bug. Display an appropriate error message.
Expand Down
23 changes: 18 additions & 5 deletions lib/vagrant-parallels/driver/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,12 @@ def read_forwarded_ports(global = false)
end
end

# Returns an IP of the virtual machine. It requires that Shared network
# adapter is configured for this VM and it obtains an IP via DHCP.
# 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.
#
# @return [String] IP address leased by DHCP server in "Shared" network
def read_guest_ip
def read_guest_ip_dhcp
mac_addr = read_mac_address.downcase
leases_file = '/Library/Preferences/Parallels/parallels_dhcp_leases'
leases = {}
Expand All @@ -430,6 +431,15 @@ def read_guest_ip
leases.max_by { |_ip, lease_time| lease_time }.first
end

# Returns an IP of the virtual machine fetched from prlctl.
#
# @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
end

# Returns path to the Parallels Tools ISO file.
#
# @param [String] guest_os Guest os type: "linux", "darwin" or "windows"
Expand Down Expand Up @@ -757,11 +767,14 @@ def share_folders(folders)
end
end

# Reads the SSH IP of this VM.
# Reads the SSH IP of this VM from DHCP lease file or from `prlctl list`
# command - whatever returns a non-empty result.
# 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
read_guest_ip_dhcp || read_guest_ip_prlctl
end

# Reads the SSH port of this VM.
Expand Down
3 changes: 2 additions & 1 deletion lib/vagrant-parallels/driver/meta.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ def initialize(uuid=nil)
:read_bridged_interfaces,
:read_current_snapshot,
:read_forwarded_ports,
:read_guest_ip,
:read_guest_ip_dhcp,
:read_guest_ip_prlctl,
:read_guest_tools_state,
:read_guest_tools_iso_path,
:read_host_only_interfaces,
Expand Down

0 comments on commit eb92ec5

Please sign in to comment.