2012-08-15 04:12:41 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module ProviderVirtualBox
|
|
|
|
module Action
|
|
|
|
class ForwardPorts
|
2013-01-11 22:51:49 +00:00
|
|
|
include Util::CompileForwardedPorts
|
|
|
|
|
2012-08-15 04:12:41 +00:00
|
|
|
def initialize(app, env)
|
|
|
|
@app = app
|
|
|
|
end
|
|
|
|
|
|
|
|
#--------------------------------------------------------------
|
|
|
|
# Execution
|
|
|
|
#--------------------------------------------------------------
|
|
|
|
def call(env)
|
|
|
|
@env = env
|
|
|
|
|
|
|
|
# Get the ports we're forwarding
|
2013-01-11 22:51:49 +00:00
|
|
|
env[:forwarded_ports] ||= compile_forwarded_ports(env[:machine].config)
|
2012-08-15 04:12:41 +00:00
|
|
|
|
|
|
|
# Warn if we're port forwarding to any privileged ports...
|
2013-01-11 22:51:49 +00:00
|
|
|
env[:forwarded_ports].each do |fp|
|
|
|
|
if fp.host_port <= 1024
|
|
|
|
env[:ui].warn I18n.t("vagrant.actions.vm.forward_ports.privileged_ports")
|
2013-03-15 22:15:54 +00:00
|
|
|
break
|
2013-01-05 02:22:38 +00:00
|
|
|
end
|
2012-08-15 04:12:41 +00:00
|
|
|
end
|
|
|
|
|
2013-01-11 22:51:49 +00:00
|
|
|
env[:ui].info I18n.t("vagrant.actions.vm.forward_ports.forwarding")
|
|
|
|
forward_ports
|
2012-08-15 04:12:41 +00:00
|
|
|
|
2013-01-11 22:51:49 +00:00
|
|
|
@app.call(env)
|
2012-08-15 04:12:41 +00:00
|
|
|
end
|
|
|
|
|
2013-01-11 22:51:49 +00:00
|
|
|
def forward_ports
|
2012-08-15 04:12:41 +00:00
|
|
|
ports = []
|
|
|
|
|
|
|
|
interfaces = @env[:machine].provider.driver.read_network_interfaces
|
|
|
|
|
2013-01-11 22:51:49 +00:00
|
|
|
@env[:forwarded_ports].each do |fp|
|
2012-08-15 04:12:41 +00:00
|
|
|
message_attributes = {
|
2013-01-11 22:51:49 +00:00
|
|
|
:adapter => fp.adapter,
|
|
|
|
:guest_port => fp.guest_port,
|
|
|
|
:host_port => fp.host_port
|
2012-08-15 04:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Assuming the only reason to establish port forwarding is
|
|
|
|
# because the VM is using Virtualbox NAT networking. Host-only
|
|
|
|
# bridged networking don't require port-forwarding and establishing
|
|
|
|
# forwarded ports on these attachment types has uncertain behaviour.
|
|
|
|
@env[:ui].info(I18n.t("vagrant.actions.vm.forward_ports.forwarding_entry",
|
|
|
|
message_attributes))
|
|
|
|
|
2013-03-22 17:51:07 +00:00
|
|
|
# Verify we have the network interface to attach to
|
|
|
|
if !interfaces[fp.adapter]
|
|
|
|
raise Vagrant::Errors::ForwardPortAdapterNotFound,
|
|
|
|
:adapter => fp.adapter.to_s,
|
|
|
|
:guest => fp.guest_port.to_s,
|
|
|
|
:host => fp.host_port.to_s
|
|
|
|
end
|
|
|
|
|
2012-08-15 04:12:41 +00:00
|
|
|
# Port forwarding requires the network interface to be a NAT interface,
|
|
|
|
# so verify that that is the case.
|
2013-01-11 22:51:49 +00:00
|
|
|
if interfaces[fp.adapter][:type] != :nat
|
2012-08-15 04:12:41 +00:00
|
|
|
@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
|
2013-01-05 02:22:38 +00:00
|
|
|
ports << {
|
2013-01-11 22:51:49 +00:00
|
|
|
:adapter => fp.adapter,
|
|
|
|
:guestport => fp.guest_port,
|
2013-06-09 22:15:59 +00:00
|
|
|
:hostip => fp.host_ip,
|
2013-01-11 22:51:49 +00:00
|
|
|
:hostport => fp.host_port,
|
|
|
|
:name => fp.id,
|
|
|
|
:protocol => fp.protocol
|
2013-01-05 02:22:38 +00:00
|
|
|
}
|
2012-08-15 04:12:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if !ports.empty?
|
|
|
|
# We only need to forward ports if there are any to forward
|
|
|
|
@env[:machine].provider.driver.forward_ports(ports)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|