diff --git a/plugins/providers/virtualbox/action/forward_ports.rb b/plugins/providers/virtualbox/action/forward_ports.rb index 0afc71115..1910df6cc 100644 --- a/plugins/providers/virtualbox/action/forward_ports.rb +++ b/plugins/providers/virtualbox/action/forward_ports.rb @@ -27,23 +27,35 @@ module VagrantPlugins # 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[:machine].config.vm.forwarded_ports.each do |options| - guest_port_mapping[options[:guestport]] = options + # 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 - guest_port_mapping.values + 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 |options| - if options[:hostport] <= 1024 + ports.each do |port| + if port.host_port <= 1024 @env[:ui].warn I18n.t("vagrant.actions.vm.forward_ports.privileged_ports") return end @@ -55,11 +67,11 @@ module VagrantPlugins interfaces = @env[:machine].provider.driver.read_network_interfaces - mappings.each do |options| + mappings.each do |port| message_attributes = { - :guest_port => options[:guestport], - :host_port => options[:hostport], - :adapter => options[:adapter] + :guest_port => port.guest_port, + :host_port => port.host_port, + :adapter => port.adapter } # Assuming the only reason to establish port forwarding is @@ -71,14 +83,20 @@ module VagrantPlugins # Port forwarding requires the network interface to be a NAT interface, # so verify that that is the case. - if interfaces[options[:adapter]][:type] != :nat + if interfaces[port.adapter][:type] != :nat @env[:ui].info(I18n.t("vagrant.actions.vm.forward_ports.non_nat", message_attributes)) next end # Add the options to the ports array to send to the driver later - ports << options.merge(:name => options[:name], :adapter => options[:adapter]) + ports << { + :adapter => port.adapter, + :guestport => port.guest_port, + :hostport => port.host_port, + :name => port.id, + :protocol => port.protocol + } end if !ports.empty? diff --git a/plugins/providers/virtualbox/model/forwarded_port.rb b/plugins/providers/virtualbox/model/forwarded_port.rb new file mode 100644 index 000000000..d3f759504 --- /dev/null +++ b/plugins/providers/virtualbox/model/forwarded_port.rb @@ -0,0 +1,44 @@ +module VagrantPlugins + module ProviderVirtualBox + module Model + # Represents a single forwarded port for VirtualBox. This has various + # helpers and defaults for a forwarded port. + class ForwardedPort + # The NAT adapter on which to attach the forwarded port. + # + # @return [Integer] + attr_reader :adapter + + # The unique ID for the forwarded port. + # + # @return [String] + attr_reader :id + + # The protocol to forward. + # + # @return [String] + attr_reader :protocol + + # The port on the guest to be exposed on the host. + # + # @return [Integer] + attr_reader :guest_port + + # The port on the host used to access the port on the guest. + # + # @return [Integer] + attr_reader :host_port + + def initialize(id, host_port, guest_port, options) + @id = id + @guest_port = guest_port + @host_port = host_port + + options ||= {} + @adapter = options[:adapter] || 1 + @protocol = options[:protocol] || "tcp" + end + end + end + end +end diff --git a/plugins/providers/virtualbox/plugin.rb b/plugins/providers/virtualbox/plugin.rb index 3c876c340..7a0c7fb6e 100644 --- a/plugins/providers/virtualbox/plugin.rb +++ b/plugins/providers/virtualbox/plugin.rb @@ -30,5 +30,9 @@ module VagrantPlugins autoload :Version_4_1, File.expand_path("../driver/version_4_1", __FILE__) autoload :Version_4_2, File.expand_path("../driver/version_4_2", __FILE__) end + + module Model + autoload :ForwardedPort, File.expand_path("../model/forwarded_port", __FILE__) + end end end