Forwarded ports use the new high-level configuration

This commit is contained in:
Mitchell Hashimoto 2013-01-11 14:51:49 -08:00
parent 7e5f175d2c
commit da7f227fff
3 changed files with 26 additions and 53 deletions

View File

@ -17,7 +17,9 @@ Vagrant.configure("2") do |config|
config.vm.guest = :linux
# Share SSH locally by default
config.vm.network :forwarded_port, 2222, 22, :id => "ssh", :auto => true
config.vm.network :forwarded_port, 22, 2222,
:id => "ssh",
:auto_correct => true
# Share the root folder. This can then be overridden by
# other Vagrantfiles, if they wish.

View File

@ -23,6 +23,7 @@ module VagrantPlugins
autoload :DestroyUnusedNetworkInterfaces, File.expand_path("../action/destroy_unused_network_interfaces", __FILE__)
autoload :DiscardState, File.expand_path("../action/discard_state", __FILE__)
autoload :Export, File.expand_path("../action/export", __FILE__)
autoload :ForwardPorts, File.expand_path("../action/forward_ports", __FILE__)
autoload :Halt, File.expand_path("../action/halt", __FILE__)
autoload :HostName, File.expand_path("../action/host_name", __FILE__)
autoload :Import, File.expand_path("../action/import", __FILE__)
@ -65,6 +66,7 @@ module VagrantPlugins
b.use ShareFolders
b.use ClearNetworkInterfaces
b.use Network
b.use ForwardPorts
b.use HostName
b.use SaneDefaults
b.use Customize

View File

@ -2,6 +2,8 @@ module VagrantPlugins
module ProviderVirtualBox
module Action
class ForwardPorts
include Util::CompileForwardedPorts
def initialize(app, env)
@app = app
end
@ -13,65 +15,32 @@ module VagrantPlugins
@env = env
# Get the ports we're forwarding
ports = forward_port_definitions
env[:forwarded_ports] ||= compile_forwarded_ports(env[:machine].config)
# Warn if we're port forwarding to any privileged ports...
threshold_check(ports)
env[:forwarded_ports].each do |fp|
if fp.host_port <= 1024
env[:ui].warn I18n.t("vagrant.actions.vm.forward_ports.privileged_ports")
return
end
end
env[:ui].info I18n.t("vagrant.actions.vm.forward_ports.forwarding")
forward_ports(ports)
forward_ports
@app.call(env)
end
# This returns an array of forwarded ports with overrides properly
# squashed.
def forward_port_definitions
# Get all of the forwarded port definitions in the network and
# convert it to a forwarded port model for use in the rest of
# the action.
#
# Duplicate forward port definitions are treated as "last one wins"
# where the last host port definition wins.
fp_mapping = {}
@env[:machine].config.vm.networks.each do |type, args|
# We only care about forwarded ports currently
if type == :forwarded_port
options = args[2] || {}
host_port = args[0].to_i
guest_port = args[1].to_i
id = options[:id] || "#{guest_port.to_s(32)}-#{host_port.to_s(32)}"
fp_mapping[host_port] =
Model::ForwardedPort.new(id, host_port, guest_port, options)
end
end
# Return the values, since the order doesn't really matter
fp_mapping.values
end
# This method checks for any forwarded ports on the host below
# 1024, which causes the forwarded ports to fail.
def threshold_check(ports)
ports.each do |port|
if port.host_port <= 1024
@env[:ui].warn I18n.t("vagrant.actions.vm.forward_ports.privileged_ports")
return
end
end
end
def forward_ports(mappings)
def forward_ports
ports = []
interfaces = @env[:machine].provider.driver.read_network_interfaces
mappings.each do |port|
@env[:forwarded_ports].each do |fp|
message_attributes = {
:guest_port => port.guest_port,
:host_port => port.host_port,
:adapter => port.adapter
:adapter => fp.adapter,
:guest_port => fp.guest_port,
:host_port => fp.host_port
}
# Assuming the only reason to establish port forwarding is
@ -83,7 +52,7 @@ module VagrantPlugins
# Port forwarding requires the network interface to be a NAT interface,
# so verify that that is the case.
if interfaces[port.adapter][:type] != :nat
if interfaces[fp.adapter][:type] != :nat
@env[:ui].info(I18n.t("vagrant.actions.vm.forward_ports.non_nat",
message_attributes))
next
@ -91,11 +60,11 @@ module VagrantPlugins
# Add the options to the ports array to send to the driver later
ports << {
:adapter => port.adapter,
:guestport => port.guest_port,
:hostport => port.host_port,
:name => port.id,
:protocol => port.protocol
:adapter => fp.adapter,
:guestport => fp.guest_port,
:hostport => fp.host_port,
:name => fp.id,
:protocol => fp.protocol
}
end