mirror of
https://github.com/vagrant-libvirt/vagrant-libvirt.git
synced 2025-02-25 18:55:27 -06:00
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:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user