Add `forwarded_port_destination` to find SSH port by that instead of key. [closes GH-375]

This commit is contained in:
Mitchell Hashimoto 2011-07-10 22:41:35 -07:00
parent 0515d9a61e
commit 0dacd78b10
6 changed files with 36 additions and 11 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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
return pnum.hostport if pnum
# 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_by_name.hostport if pnum_by_name
return pnum_by_destination.hostport if pnum_by_destination
# This should NEVER happen.
raise Errors::SSHPortNotDetected

View File

@ -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)

View File

@ -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)