From 07391048afd9e7a26ebaf633cb4a4baceef63c2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Bl=C3=BCmel?= Date: Tue, 11 Jun 2019 18:36:10 +0200 Subject: [PATCH] Enable forward ssh-port to host Provides an option to disable the forwarding in case it causes issues. However based on original PRs adding the forwarded behaviour, it does not appear to be intentional to exclude it permanently. Closes: #1011 Closes: #1012 --- README.md | 19 +++++++++++ lib/vagrant-libvirt/action/forward_ports.rb | 2 +- lib/vagrant-libvirt/config.rb | 7 ++++ spec/unit/action/forward_ports_spec.rb | 38 +++++++++++++++++++++ 4 files changed, 65 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c8cf967..44e24f1 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ can help a lot :-) * [No box and PXE boot](#no-box-and-pxe-boot) * [SSH Access To VM](#ssh-access-to-vm) * [Forwarded Ports](#forwarded-ports) + * [Forwarding the ssh-port](#forwarding-the-ssh-port) * [Synced Folders](#synced-folders) * [QEMU Session Support](#qemu-session-support) * [Customized Graphics](#customized-graphics) @@ -1416,6 +1417,24 @@ Default is `eth0`. `config.vm.network :forwarded_port, guest: 80, host: 2000, host_ip: "0.0.0.0"` +### Forwarding the ssh-port + +By default vagrant-libvirt now allows the standard ssh-port to be forwarded to +the localhost to allow for consistent provisioning steps/ports used when +defining across multiple providers. + +Previously by default libvirt skipped the forwarding of the ssh-port because +you can access the machine directly. To return to this behaviour set the +option `forward_ssh_port` to `false` in the Vagrantfile. + +```ruby +Vagrant.configure("2") do |config| + config.vm.provider :libvirt do |libvirt| + # Disable forwarding of forwarded_port with id 'ssh'. + libvirt.forward_ssh_port = false + end +``` + ## Synced Folders Vagrant automatically syncs the project folder on the host to `/vagrant` in diff --git a/lib/vagrant-libvirt/action/forward_ports.rb b/lib/vagrant-libvirt/action/forward_ports.rb index 14e49c5..d0335ef 100644 --- a/lib/vagrant-libvirt/action/forward_ports.rb +++ b/lib/vagrant-libvirt/action/forward_ports.rb @@ -71,7 +71,7 @@ module VagrantPlugins next end - next unless type == :forwarded_port && options[:id] != 'ssh' + next if type != :forwarded_port || ( options[:id] == 'ssh' && !env[:machine].provider_config.forward_ssh_port ) if options.fetch(:host_ip, '').to_s.strip.empty? options.delete(:host_ip) end diff --git a/lib/vagrant-libvirt/config.rb b/lib/vagrant-libvirt/config.rb index e7de501..255a5a6 100644 --- a/lib/vagrant-libvirt/config.rb +++ b/lib/vagrant-libvirt/config.rb @@ -39,6 +39,9 @@ module VagrantPlugins attr_accessor :proxy_command + # Forward port with id 'ssh' + attr_accessor :forward_ssh_port + # Libvirt storage pool name, where box image and instance snapshots will # be stored. attr_accessor :storage_pool_name @@ -196,6 +199,7 @@ module VagrantPlugins @id_ssh_key_file = UNSET_VALUE @socket = UNSET_VALUE @proxy_command = UNSET_VALUE + @forward_ssh_port = UNSET_VALUE # forward port with id 'ssh' @storage_pool_name = UNSET_VALUE @snapshot_pool_name = UNSET_VALUE @random_hostname = UNSET_VALUE @@ -784,6 +788,9 @@ module VagrantPlugins finalize_from_uri finalize_proxy_command + # forward port with id 'ssh' + @forward_ssh_port = true if @forward_ssh_port == UNSET_VALUE + @storage_pool_name = 'default' if @storage_pool_name == UNSET_VALUE @snapshot_pool_name = @storage_pool_name if @snapshot_pool_name == UNSET_VALUE @storage_pool_path = nil if @storage_pool_path == UNSET_VALUE diff --git a/spec/unit/action/forward_ports_spec.rb b/spec/unit/action/forward_ports_spec.rb index 39b6617..2677eef 100644 --- a/spec/unit/action/forward_ports_spec.rb +++ b/spec/unit/action/forward_ports_spec.rb @@ -12,11 +12,14 @@ describe VagrantPlugins::ProviderLibvirt::Action::ForwardPorts do let(:machine_config) { double("machine_config") } let(:vm_config) { double("vm_config") } + let(:provider_config) { double("provider_config") } before (:each) do allow(machine).to receive(:config).and_return(machine_config) + allow(machine).to receive(:provider_config).and_return(provider_config) allow(machine_config).to receive(:vm).and_return(vm_config) allow(vm_config).to receive(:networks).and_return([]) + allow(provider_config).to receive(:forward_ssh_port).and_return(true) end describe '#call' do @@ -67,6 +70,40 @@ describe VagrantPlugins::ProviderLibvirt::Action::ForwardPorts do expect(subject.call(env)).to be_nil end end + + context 'when default ssh port forward provided' do + let(:networks){ [ + [:private_network, {:ip=>"10.20.30.40", :protocol=>"tcp", :id=>"6b8175ed-3220-4b63-abaf-0bb8d7cdd723"}], + [:forwarded_port, {guest: 80, host: 8080}], + [:forwarded_port, {guest: 22, host: 2222, host_ip: '127.0.0.1', id: 'ssh'}], + ]} + + context 'with default config' do + it 'should forward the port' do + expect(vm_config).to receive(:networks).and_return(networks) + expect(subject).to receive(:forward_ports) + + expect(subject.call(env)).to be_nil + + expect(env[:forwarded_ports]).to eq(networks.drop(1).map { |_, opts| opts }) + end + end + + context 'with forward_ssh_port disabled' do + before do + allow(provider_config).to receive(:forward_ssh_port).and_return(false) + end + + it 'should not forward the port' do + expect(vm_config).to receive(:networks).and_return(networks) + expect(subject).to receive(:forward_ports) + + expect(subject.call(env)).to be_nil + + expect(env[:forwarded_ports]).to eq([networks[1][1]]) + end + end + end end describe '#forward_ports' do @@ -82,6 +119,7 @@ describe VagrantPlugins::ProviderLibvirt::Action::ForwardPorts do :private_key_path => ["/home/test/.ssh/id_rsa"], } ) + allow(provider_config).to receive(:proxy_command).and_return(nil) end context 'with port to forward' do