Port forwarding definitions with the same guest port will override earlier

This commit is contained in:
Mitchell Hashimoto 2012-01-05 22:56:59 -08:00
parent 40a6dfaf9d
commit 79e8a6ef0a
1 changed files with 24 additions and 6 deletions

View File

@ -12,19 +12,37 @@ module Vagrant
def call(env) def call(env)
@env = env @env = env
# Get the ports we're forwarding
ports = forward_port_definitions
# Warn if we're port forwarding to any privileged ports... # Warn if we're port forwarding to any privileged ports...
threshold_check threshold_check(ports)
env[:ui].info I18n.t("vagrant.actions.vm.forward_ports.forwarding") env[:ui].info I18n.t("vagrant.actions.vm.forward_ports.forwarding")
forward_ports(env[:vm]) forward_ports(ports)
@app.call(env) @app.call(env)
end end
# This returns an array of forwarded ports with overrides properly
# squashed.
def forward_port_definitions
# Get all the port mappings in the order they're defined and
# organize them by their guestport, taking the "last one wins"
# approach.
guest_port_mapping = {}
@env[:vm].config.vm.forwarded_ports.each do |options|
guest_port_mapping[options[:guestport]] = options
end
# Return the values, since the order doesn't really matter
guest_port_mapping.values
end
# This method checks for any forwarded ports on the host below # This method checks for any forwarded ports on the host below
# 1024, which causes the forwarded ports to fail. # 1024, which causes the forwarded ports to fail.
def threshold_check def threshold_check(ports)
@env[:vm].config.vm.forwarded_ports.each do |options| ports.each do |options|
if options[:hostport] <= 1024 if options[:hostport] <= 1024
@env[:ui].warn I18n.t("vagrant.actions.vm.forward_ports.privileged_ports") @env[:ui].warn I18n.t("vagrant.actions.vm.forward_ports.privileged_ports")
return return
@ -32,12 +50,12 @@ module Vagrant
end end
end end
def forward_ports(vm) def forward_ports(mappings)
ports = [] ports = []
interfaces = @env[:vm].driver.read_network_interfaces interfaces = @env[:vm].driver.read_network_interfaces
@env[:vm].config.vm.forwarded_ports.each do |options| mappings.each do |options|
message_attributes = { message_attributes = {
:guest_port => options[:guestport], :guest_port => options[:guestport],
:host_port => options[:hostport], :host_port => options[:hostport],