Remove forwarded_port_key/destination from config, replace with guest_port

This commit is contained in:
Mitchell Hashimoto 2012-01-06 01:03:22 -08:00
parent 00a31ff27b
commit acaabd5aa3
5 changed files with 41 additions and 26 deletions

View File

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

View File

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

View File

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

View File

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

View File

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