From e87de8985538b3d3567e0234f1d9926165eeef83 Mon Sep 17 00:00:00 2001 From: Darragh Bailey Date: Thu, 13 Oct 2022 13:28:25 +0100 Subject: [PATCH] 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 --- .../action/prepare_nfs_settings.rb | 12 ++++--- spec/unit/action/prepare_nfs_settings_spec.rb | 31 ++++++++++++++----- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/lib/vagrant-libvirt/action/prepare_nfs_settings.rb b/lib/vagrant-libvirt/action/prepare_nfs_settings.rb index 7ad425a..21bb904 100644 --- a/lib/vagrant-libvirt/action/prepare_nfs_settings.rb +++ b/lib/vagrant-libvirt/action/prepare_nfs_settings.rb @@ -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 diff --git a/spec/unit/action/prepare_nfs_settings_spec.rb b/spec/unit/action/prepare_nfs_settings_spec.rb index f361810..b4864ed 100644 --- a/spec/unit/action/prepare_nfs_settings_spec.rb +++ b/spec/unit/action/prepare_nfs_settings_spec.rb @@ -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