From 0dacd78b107cb84a30266ac3a6f408dc22207240 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 10 Jul 2011 22:41:35 -0700 Subject: [PATCH] Add `forwarded_port_destination` to find SSH port by that instead of key. [closes GH-375] --- CHANGELOG.md | 3 +++ config/default.rb | 1 + lib/vagrant/config/ssh.rb | 1 + lib/vagrant/ssh.rb | 18 ++++++++++++++---- test/vagrant/action/builder_test.rb | 4 ++++ test/vagrant/action/warden_test.rb | 20 +++++++++++++------- 6 files changed, 36 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6191648f0..c17484921 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,9 @@ - Vagrantfile can be lowercase now. [GH-399] - Only one copy of Vagrant may be running at any given time. [GH-364] - Default home directory for Vagrant moved to `~/.vagrant.d` [GH-333] + - Specify a `forwarded_port_destination` for SSH configuration and + SSH port searching will fall back to that if it can't find any + other port. [GH-375] ## 0.7.6 (July 2, 2011) diff --git a/config/default.rb b/config/default.rb index b977c99ba..2254050aa 100644 --- a/config/default.rb +++ b/config/default.rb @@ -6,6 +6,7 @@ Vagrant::Config.run do |config| config.ssh.username = "vagrant" config.ssh.host = "127.0.0.1" config.ssh.forwarded_port_key = "ssh" + config.ssh.forwarded_port_destination = 22 config.ssh.max_tries = 10 config.ssh.timeout = 30 config.ssh.private_key_path = File.expand_path("keys/vagrant", Vagrant.source_root) diff --git a/lib/vagrant/config/ssh.rb b/lib/vagrant/config/ssh.rb index d6a5bb627..091ea31c0 100644 --- a/lib/vagrant/config/ssh.rb +++ b/lib/vagrant/config/ssh.rb @@ -6,6 +6,7 @@ module Vagrant attr_accessor :username attr_accessor :host attr_accessor :forwarded_port_key + attr_accessor :forwarded_port_destination attr_accessor :max_tries attr_accessor :timeout attr_writer :private_key_path diff --git a/lib/vagrant/ssh.rb b/lib/vagrant/ssh.rb index c55236c09..bccb2c532 100644 --- a/lib/vagrant/ssh.rb +++ b/lib/vagrant/ssh.rb @@ -182,16 +182,26 @@ module Vagrant return env.config.ssh.port if env.config.ssh.port # Check if we have an SSH forwarded port - pnum = nil + pnum_by_name = nil + pnum_by_destination = nil env.vm.vm.network_adapters.each do |na| - pnum = na.nat_driver.forwarded_ports.detect do |fp| + # Look for the port number by name... + pnum_by_name = na.nat_driver.forwarded_ports.detect do |fp| fp.name == env.config.ssh.forwarded_port_key end - break if pnum + # Look for the port number by destination... + pnum_by_destination = na.nat_driver.forwarded_ports.detect do |fp| + fp.guestport == env.config.ssh.forwarded_port_destination + end + + # pnum_by_name is what we're looking for here, so break early + # if we have it. + break if pnum_by_name end - return pnum.hostport if pnum + return pnum_by_name.hostport if pnum_by_name + return pnum_by_destination.hostport if pnum_by_destination # This should NEVER happen. raise Errors::SSHPortNotDetected diff --git a/test/vagrant/action/builder_test.rb b/test/vagrant/action/builder_test.rb index 1530b8da3..cc0e350e2 100644 --- a/test/vagrant/action/builder_test.rb +++ b/test/vagrant/action/builder_test.rb @@ -1,4 +1,5 @@ require "test_helper" +require "logger" class ActionBuilderTest < Test::Unit::TestCase setup do @@ -64,6 +65,7 @@ class ActionBuilderTest < Test::Unit::TestCase context "flatten" do should "return the flattened format of the builder" do env = Vagrant::Action::Environment.new(nil) + env["logger"] = Logger.new(nil) env.expects(:foo).once func = lambda { |x| x.foo } @@ -157,6 +159,7 @@ class ActionBuilderTest < Test::Unit::TestCase context "converting to an app" do should "make non-classes lambdas" do env = Vagrant::Action::Environment.new(nil) + env["logger"] = Logger.new(nil) env.expects(:foo).once func = lambda { |x| x.foo } @@ -193,6 +196,7 @@ class ActionBuilderTest < Test::Unit::TestCase end env = Vagrant::Action::Environment.new(nil) + env["logger"] = Logger.new(nil) env[:key] = :value @instance.use(mw) diff --git a/test/vagrant/action/warden_test.rb b/test/vagrant/action/warden_test.rb index 41a42231f..7a6615745 100644 --- a/test/vagrant/action/warden_test.rb +++ b/test/vagrant/action/warden_test.rb @@ -1,4 +1,5 @@ require "test_helper" +require "logger" class ActionWardenTest < Test::Unit::TestCase setup do @@ -8,11 +9,12 @@ class ActionWardenTest < Test::Unit::TestCase context "initializing" do should "finalize the middleware" do + env = new_env middleware = [1,2,3] middleware.each do |m| - @klass.any_instance.expects(:finalize_action).with(m, {}).returns(m) + @klass.any_instance.expects(:finalize_action).with(m, env).returns(m) end - @warden = @klass.new(middleware, new_env) + @warden = @klass.new(middleware, env) assert_equal @warden.actions, [1,2,3] end end @@ -48,10 +50,11 @@ class ActionWardenTest < Test::Unit::TestCase end should "call the next action" do + env = new_env action = mock('action') - action.expects(:call).with({}) + action.expects(:call).with(env) @instance.actions << action - @instance.call(new_env) + @instance.call(env) end should "begin recovery sequence when the called action raises an exception" do @@ -97,18 +100,21 @@ class ActionWardenTest < Test::Unit::TestCase context "recover" do should "call recover on all items in the stack" do + env = new_env seq = sequence("sequence") @instance.stack = [rescueable_mock("action"), rescueable_mock("another")] @instance.stack.each do |action| - action.expects(:recover).with(new_env).in_sequence(seq) + action.expects(:recover).with(env).in_sequence(seq) end - @instance.begin_rescue(new_env) + @instance.begin_rescue(env) end end def new_env - Vagrant::Action::Environment.new(nil) + env = Vagrant::Action::Environment.new(nil) + env["logger"] = Logger.new(nil) + env end def rescueable_mock(name)