From ad41c445a4fbd62737d8b4f0285d00b32dcd31f3 Mon Sep 17 00:00:00 2001
From: Darrell Hamilton <darrell.noice@gmail.com>
Date: Sun, 14 Jul 2013 22:42:49 -0700
Subject: [PATCH] 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.
---
 lib/vagrant-lxc/action/forward_ports.rb | 37 +++++++++++++++----------
 lib/vagrant-lxc/errors.rb               |  3 ++
 locales/en.yml                          |  3 ++
 3 files changed, 29 insertions(+), 14 deletions(-)

diff --git a/lib/vagrant-lxc/action/forward_ports.rb b/lib/vagrant-lxc/action/forward_ports.rb
index df150c3..4e6e466 100644
--- a/lib/vagrant-lxc/action/forward_ports.rb
+++ b/lib/vagrant-lxc/action/forward_ports.rb
@@ -26,28 +26,33 @@ module Vagrant
 
           if @env[:forwarded_ports].any?
             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
 
         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
-              :adapter    => 'eth0',
-              :guest_port => fp[:guest],
-              :host_port  => fp[:host]
-            }
+            @env[:forwarded_ports].each do |fp|
+              message_attributes = {
+                # TODO: Add support for multiple adapters
+                :adapter    => 'eth0',
+                :guest_port => fp[:guest],
+                :host_port  => fp[:host]
+              }
 
-            # TODO: Remove adapter from logging
-            @env[:ui].info(I18n.t("vagrant.actions.vm.forward_ports.forwarding_entry",
-                                  message_attributes))
+              # TODO: Remove adapter from logging
+              @env[:ui].info(I18n.t("vagrant.actions.vm.forward_ports.forwarding_entry",
+                                    message_attributes))
 
-            redir_pid = redirect_port(fp[:host], fp[:guest])
-            store_redir_pid(fp[:host], redir_pid)
-          end
+              redir_pid = redirect_port(fp[:host], fp[:guest])
+              store_redir_pid(fp[:host], redir_pid)
+            end
         end
 
         private
@@ -79,6 +84,10 @@ module Vagrant
             pid_file.write(redir_pid)
           end
         end
+
+        def redir_installed?
+          system "sudo which redir"
+        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 50edd3d..b7aa5bf 100644
--- a/locales/en.yml
+++ b/locales/en.yml
@@ -40,3 +40,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.