Use provided ssh port for machine (#1641)

To allow for a different ssh port to be used when connecting to a
machine for NFS setup, use the port provided in the ssh_info hash
with a fallback to the default ssh port.

This may allow NFS mounting into Windows guests once support is added to
vagrant itself to handle NFS installation.

Fixes: #1640
This commit is contained in:
Darragh Bailey
2022-10-13 13:28:25 +01:00
committed by GitHub
parent 305232ff11
commit e87de89855
2 changed files with 31 additions and 12 deletions

View File

@@ -52,8 +52,8 @@ module VagrantPlugins
# @return [String]
def read_machine_ip(machine)
# check host only ip
ssh_host = machine.ssh_info[:host]
return ssh_host if ping(ssh_host)
ssh_info = machine.ssh_info
return ssh_info[:host] if ping(ssh_info[:host], ssh_info[:port])
# check other ips
command = "ip=$(which ip); ${ip:-/sbin/ip} addr show | grep -i 'inet ' | grep -v '127.0.0.1' | tr -s ' ' | cut -d' ' -f3 | cut -d'/' -f 1"
@@ -65,17 +65,19 @@ module VagrantPlugins
ips = result.chomp.split("\n").uniq
@logger.info("guest IPs: #{ips.join(', ')}")
ips.each do |ip|
next if ip == ssh_host
next if ip == ssh_info[:host]
return ip if ping(ip)
end
nil
end
private
# Check if we can open a connection to the host
def ping(host, timeout = 3)
def ping(host, port=nil, timeout = 3)
::Timeout.timeout(timeout) do
s = TCPSocket.new(host, 'ssh')
s = TCPSocket.new(host, port || 'ssh')
s.close
end
true

View File

@@ -34,7 +34,6 @@ describe VagrantPlugins::ProviderLibvirt::Action::PrepareNFSSettings do
let(:udp_socket) { double('udp_socket') }
before do
allow(::TCPSocket).to receive(:new).and_return(socket)
allow(socket).to receive(:close)
allow(::UDPSocket).to receive(:open).and_return(udp_socket)
@@ -42,17 +41,35 @@ describe VagrantPlugins::ProviderLibvirt::Action::PrepareNFSSettings do
end
it 'should retrieve the guest IP address' do
times_called = 0
expect(::TCPSocket).to receive(:new) do
# force reaching later code
times_called += 1
times_called < 2 ? raise("StandardError") : socket
end
expect(::TCPSocket).to receive(:new).with('192.168.1.2', 'ssh').and_raise(StandardError)
expect(::TCPSocket).to receive(:new).with('192.168.2.2', 'ssh').and_return(socket)
expect(machine).to receive(:ssh_info).and_return({:host => '192.168.1.2'})
expect(communicator).to receive(:execute).and_yield(:stdout, "192.168.1.2\n192.168.2.2")
expect(subject.call(env)).to be_nil
end
it 'should use the ip if connection refused' do
expect(::TCPSocket).to receive(:new).with('192.168.1.2', 'ssh').and_raise(Errno::ECONNREFUSED)
expect(machine).to receive(:ssh_info).and_return({:host => '192.168.1.2'})
expect(subject.call(env)).to be_nil
end
it 'should use the ssh port defined' do
expect(::TCPSocket).to receive(:new).with('192.168.1.2', '2022').and_return(socket)
expect(machine).to receive(:ssh_info).and_return({:host => '192.168.1.2', :port => '2022'})
expect(subject.call(env)).to be_nil
end
it 'should raise an exception if machine ip not found' do
expect(::TCPSocket).to receive(:new).with('192.168.1.2', 'ssh').and_raise(StandardError)
expect(machine).to receive(:ssh_info).and_return({:host => '192.168.1.2'})
expect(communicator).to receive(:execute).and_yield(:stdout, "192.168.1.2")
expect { subject.call(env) }.to raise_error(::Vagrant::Errors::NFSNoHostonlyNetwork)
end
end
end
end