From acaabd5aa3059c1a23d5790dd845fb97689beed5 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 6 Jan 2012 01:03:22 -0800 Subject: [PATCH] Remove forwarded_port_key/destination from config, replace with guest_port --- config/default.rb | 3 +-- lib/vagrant/config/ssh.rb | 34 +++++++++++++++++++++++++------- lib/vagrant/driver/virtualbox.rb | 10 ++++++++++ lib/vagrant/ssh.rb | 18 ++--------------- lib/vagrant/vm.rb | 2 +- 5 files changed, 41 insertions(+), 26 deletions(-) diff --git a/config/default.rb b/config/default.rb index 9b256336a..4ea69bd06 100644 --- a/config/default.rb +++ b/config/default.rb @@ -6,8 +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.guest_port = 22 config.ssh.max_tries = 100 config.ssh.timeout = 10 config.ssh.forward_agent = false diff --git a/lib/vagrant/config/ssh.rb b/lib/vagrant/config/ssh.rb index 166a01f93..dd5b7276a 100644 --- a/lib/vagrant/config/ssh.rb +++ b/lib/vagrant/config/ssh.rb @@ -3,24 +3,44 @@ module Vagrant class SSHConfig < Base attr_accessor :username attr_accessor :host - attr_accessor :forwarded_port_key - attr_accessor :forwarded_port_destination + attr_accessor :port + attr_accessor :guest_port attr_accessor :max_tries attr_accessor :timeout attr_accessor :private_key_path attr_accessor :forward_agent attr_accessor :forward_x11 attr_accessor :shell - attr_accessor :port def initialize - @shell = "bash" - @port = nil - @forward_agent = false - @forward_x11 = false + @shell = "bash" + @port = nil + @guest_port = nil + @forward_agent = false + @forward_x11 = false @private_key_path = nil end + def forwarded_port_key=(value) + raise Errors::DeprecationError, :message => <<-MESSAGE +`config.ssh.forwarded_port_key` is now gone. You must now use +`config.ssh.guest_port` which is expected to be the port on the +guest that SSH is listening on. Vagrant will automatically scan +the forwarded ports to look for a forwarded port from this port +and use it. + MESSAGE + end + + def forwarded_port_destination=(value) + raise Errors::DeprecationError, :message => <<-MESSAGE +`config.ssh.forwarded_port_destination` is now gone. You must now use +`config.ssh.guest_port` which is expected to be the port on the +guest that SSH is listening on. Vagrant will automatically scan +the forwarded ports to look for a forwarded port from this port +and use it. + MESSAGE + end + def validate(env, errors) [:username, :host, :forwarded_port_key, :max_tries, :timeout].each do |field| errors.add(I18n.t("vagrant.config.common.error_empty", :field => field)) if !instance_variable_get("@#{field}".to_sym) diff --git a/lib/vagrant/driver/virtualbox.rb b/lib/vagrant/driver/virtualbox.rb index 570955757..21690c940 100644 --- a/lib/vagrant/driver/virtualbox.rb +++ b/lib/vagrant/driver/virtualbox.rb @@ -434,6 +434,16 @@ module Vagrant end end + # This reads the SSH port from the VM. + def ssh_port(expected_port) + # Look for the forwarded port only by comparing the guest port + read_forwarded_ports.each do |_, _, hostport, guestport| + return hostport if guestport == expected_port + end + + nil + end + # Starts the virtual machine in the given mode. # # @param [String] mode Mode to boot the VM: either "headless" or "gui" diff --git a/lib/vagrant/ssh.rb b/lib/vagrant/ssh.rb index 9dc983aa8..0ca1ee2df 100644 --- a/lib/vagrant/ssh.rb +++ b/lib/vagrant/ssh.rb @@ -188,26 +188,12 @@ module Vagrant return @vm.config.ssh.port if @vm.config.ssh.port # Check if we have an SSH forwarded port - pnum_by_name = nil - pnum_by_destination = nil @vm.driver.read_forwarded_ports.each do |_, name, hostport, guestport| - # Record the forwarded port if we find it by name - if name == @vm.config.ssh.forwarded_port_key - pnum_by_name = hostport + if guestport == @vm.config.ssh.guest_port + return hostport end - - if guestport == @vm.config.ssh.forwarded_port_destination - pnum_by_destination = hostport - 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_by_name if pnum_by_name - return pnum_by_destination if pnum_by_destination - # This should NEVER happen. raise Errors::SSHPortNotDetected end diff --git a/lib/vagrant/vm.rb b/lib/vagrant/vm.rb index 1c1407205..7747f9689 100644 --- a/lib/vagrant/vm.rb +++ b/lib/vagrant/vm.rb @@ -81,7 +81,7 @@ module Vagrant def ssh_info results = { :host => config.ssh.host, - :port => config.ssh.port || driver.ssh_port, + :port => config.ssh.port || driver.ssh_port(config.ssh.guest_port), :username => config.ssh.username, :forward_agent => config.ssh.forward_agent, :forward_x11 => config.ssh.forward_x11