Check for redir before forwarding ports

Make a system call out to `which` to see if redir exists on the PATH
before trying to forward ports.  Raises a VagrantError if it does not.
This commit is contained in:
Darrell Hamilton 2013-07-14 22:42:49 -07:00
parent 14c1ebfd7c
commit ad41c445a4
3 changed files with 29 additions and 14 deletions

View file

@ -26,28 +26,33 @@ module Vagrant
if @env[:forwarded_ports].any? if @env[:forwarded_ports].any?
env[:ui].info I18n.t("vagrant.actions.vm.forward_ports.forwarding") env[:ui].info I18n.t("vagrant.actions.vm.forward_ports.forwarding")
forward_ports
if redir_installed?
forward_ports
else
raise Errors::RedirNotInstalled
end
end end
end end
def forward_ports def forward_ports
@container_ip = @env[:machine].provider.driver.assigned_ip @container_ip = @env[:machine].provider.driver.assigned_ip
@env[:forwarded_ports].each do |fp| @env[:forwarded_ports].each do |fp|
message_attributes = { message_attributes = {
# TODO: Add support for multiple adapters # TODO: Add support for multiple adapters
:adapter => 'eth0', :adapter => 'eth0',
:guest_port => fp[:guest], :guest_port => fp[:guest],
:host_port => fp[:host] :host_port => fp[:host]
} }
# TODO: Remove adapter from logging # TODO: Remove adapter from logging
@env[:ui].info(I18n.t("vagrant.actions.vm.forward_ports.forwarding_entry", @env[:ui].info(I18n.t("vagrant.actions.vm.forward_ports.forwarding_entry",
message_attributes)) message_attributes))
redir_pid = redirect_port(fp[:host], fp[:guest]) redir_pid = redirect_port(fp[:host], fp[:guest])
store_redir_pid(fp[:host], redir_pid) store_redir_pid(fp[:host], redir_pid)
end end
end end
private private
@ -79,6 +84,10 @@ module Vagrant
pid_file.write(redir_pid) pid_file.write(redir_pid)
end end
end end
def redir_installed?
system "sudo which redir"
end
end end
end end
end end

View file

@ -17,6 +17,9 @@ module Vagrant
class IncompatibleBox < Vagrant::Errors::VagrantError class IncompatibleBox < Vagrant::Errors::VagrantError
error_key(:lxc_incompatible_box) error_key(:lxc_incompatible_box)
end end
class RedirNotInstalled < Vagrant::Errors::VagrantError
error_key(:lxc_redir_not_installed)
end
end end
end end
end end

View file

@ -40,3 +40,6 @@ en:
lxc_template_file_missing: |- lxc_template_file_missing: |-
The template file used for creating the container was not found for %{name} The template file used for creating the container was not found for %{name}
box. box.
lxc_redir_not_installed: |-
`redir` is not installed or is not accessible on the PATH.