diff --git a/lib/vagrant-lxc/action/forward_ports.rb b/lib/vagrant-lxc/action/forward_ports.rb index df150c3..1e408df 100644 --- a/lib/vagrant-lxc/action/forward_ports.rb +++ b/lib/vagrant-lxc/action/forward_ports.rb @@ -10,12 +10,13 @@ module Vagrant def call(env) @env = env - # Continue, we need the VM to be booted in order to grab its IP - @app.call env - # Get the ports we're forwarding env[:forwarded_ports] = compile_forwarded_ports(env[:machine].config) + if @env[:forwarded_ports].any? and not redir_installed? + raise Errors::RedirNotInstalled + end + # Warn if we're port forwarding to any privileged ports env[:forwarded_ports].each do |fp| if fp[:host] <= 1024 @@ -24,6 +25,9 @@ module Vagrant end end + # Continue, we need the VM to be booted in order to grab its IP + @app.call env + if @env[:forwarded_ports].any? env[:ui].info I18n.t("vagrant.actions.vm.forward_ports.forwarding") forward_ports @@ -79,6 +83,10 @@ module Vagrant pid_file.write(redir_pid) end end + + def redir_installed? + system "sudo which redir > /dev/null" + end end end end diff --git a/lib/vagrant-lxc/errors.rb b/lib/vagrant-lxc/errors.rb index 169e567..3ec0531 100644 --- a/lib/vagrant-lxc/errors.rb +++ b/lib/vagrant-lxc/errors.rb @@ -17,6 +17,9 @@ module Vagrant class IncompatibleBox < Vagrant::Errors::VagrantError error_key(:lxc_incompatible_box) end + class RedirNotInstalled < Vagrant::Errors::VagrantError + error_key(:lxc_redir_not_installed) + end end end end diff --git a/locales/en.yml b/locales/en.yml index fcb8819..ee5b7b6 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -44,3 +44,6 @@ en: lxc_template_file_missing: |- The template file used for creating the container was not found for %{name} box. + + lxc_redir_not_installed: |- + `redir` is not installed or is not accessible on the PATH. diff --git a/spec/unit/action/forward_ports_spec.rb b/spec/unit/action/forward_ports_spec.rb index c9c6c70..987c221 100644 --- a/spec/unit/action/forward_ports_spec.rb +++ b/spec/unit/action/forward_ports_spec.rb @@ -23,19 +23,27 @@ describe Vagrant::LXC::Action::ForwardPorts do subject.stub(exec: true) subject.stub(spawn: pid) - subject.call(env) end after { FileUtils.rm_rf data_dir.to_s } it 'forwards ports using redir' 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" ) end it "stores redir pids on machine's data dir" do + subject.stub(system: true) + subject.call(env) pid_file = data_dir.join('pids', "redir_#{host_port}.pid").read pid_file.should == pid end + + it 'raises RedirNotInstalled error if `redir` is not installed' do + subject.stub(system: false) + lambda { subject.call(env) }.should raise_error(Vagrant::LXC::Errors::RedirNotInstalled) + end end