From e68e4f8f052e96fc7f6cd9554355ed13de87317f Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Sun, 21 Jul 2013 22:45:03 -0300 Subject: [PATCH] Support for specifying host interface/ip for binding redir Closes #76 --- lib/vagrant-lxc/action/forward_ports.rb | 14 +++++++++----- spec/unit/action/forward_ports_spec.rb | 4 +++- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/vagrant-lxc/action/forward_ports.rb b/lib/vagrant-lxc/action/forward_ports.rb index 1e408df..772490a 100644 --- a/lib/vagrant-lxc/action/forward_ports.rb +++ b/lib/vagrant-lxc/action/forward_ports.rb @@ -35,8 +35,6 @@ module Vagrant end def forward_ports - @container_ip = @env[:machine].provider.driver.assigned_ip - @env[:forwarded_ports].each do |fp| message_attributes = { # TODO: Add support for multiple adapters @@ -49,7 +47,12 @@ module Vagrant @env[:ui].info(I18n.t("vagrant.actions.vm.forward_ports.forwarding_entry", message_attributes)) - redir_pid = redirect_port(fp[:host], fp[:guest]) + redir_pid = redirect_port( + fp[:host_ip] || "127.0.0.1", + fp[:host], + fp[:guest_ip] || @env[:machine].provider.driver.assigned_ip, + fp[:guest] + ) store_redir_pid(fp[:host], redir_pid) end end @@ -68,8 +71,9 @@ module Vagrant mappings.values end - def redirect_port(host, guest) - redir_cmd = "sudo redir --laddr=127.0.0.1 --lport=#{host} --cport=#{guest} --caddr=#{@container_ip} 2>/dev/null" + def redirect_port(host_ip, host_port, guest_ip, guest_port) + host_ip = "--laddr=#{host_ip}" unless host_ip.empty? + redir_cmd = "sudo redir #{host_ip} --lport=#{host_port} --caddr=#{guest_ip} --cport=#{guest_port} 2>/dev/null" @logger.debug "Forwarding port with `#{redir_cmd}`" spawn redir_cmd diff --git a/spec/unit/action/forward_ports_spec.rb b/spec/unit/action/forward_ports_spec.rb index 987c221..118464e 100644 --- a/spec/unit/action/forward_ports_spec.rb +++ b/spec/unit/action/forward_ports_spec.rb @@ -1,5 +1,7 @@ require 'unit_helper' +require 'tmpdir' +require 'vagrant-lxc/errors' require 'vagrant-lxc/action/forward_ports' describe Vagrant::LXC::Action::ForwardPorts do @@ -31,7 +33,7 @@ describe Vagrant::LXC::Action::ForwardPorts do subject.stub(system: true) subject.call(env) subject.should have_received(:spawn).with( - "sudo redir --laddr=127.0.0.1 --lport=#{host_port} --cport=#{guest_port} --caddr=#{container_ip} 2>/dev/null" + "sudo redir --laddr=127.0.0.1 --lport=#{host_port} --caddr=#{container_ip} --cport=#{guest_port} 2>/dev/null" ) end