Class: Vagrant::Action::VM::ForwardPorts

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/action/vm/forward_ports.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ ForwardPorts

Returns a new instance of ForwardPorts.



5
6
7
# File 'lib/vagrant/action/vm/forward_ports.rb', line 5

def initialize(app,env)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object


Execution



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/vagrant/action/vm/forward_ports.rb', line 12

def call(env)
  @env = env

  # Get the ports we're forwarding
  ports = forward_port_definitions

  # Warn if we're port forwarding to any privileged ports...
  threshold_check(ports)

  env[:ui].info I18n.t("vagrant.actions.vm.forward_ports.forwarding")
  forward_ports(ports)

  @app.call(env)
end

#forward_port_definitionsObject

This returns an array of forwarded ports with overrides properly squashed.



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/vagrant/action/vm/forward_ports.rb', line 29

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

#forward_ports(mappings) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/vagrant/action/vm/forward_ports.rb', line 53

def forward_ports(mappings)
  ports = []

  interfaces = @env[:vm].driver.read_network_interfaces

  mappings.each do |options|
    message_attributes = {
      :guest_port => options[:guestport],
      :host_port => options[:hostport],
      :adapter => options[:adapter]
    }

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

    # Port forwarding requires the network interface to be a NAT interface,
    # so verify that that is the case.
    if interfaces[options[: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])
  end

  if !ports.empty?
    # We only need to forward ports if there are any to forward
    @env[:vm].driver.forward_ports(ports)
  end
end

#threshold_check(ports) ⇒ Object

This method checks for any forwarded ports on the host below 1024, which causes the forwarded ports to fail.



44
45
46
47
48
49
50
51
# File 'lib/vagrant/action/vm/forward_ports.rb', line 44

def threshold_check(ports)
  ports.each do |options|
    if options[:hostport] <= 1024
      @env[:ui].warn I18n.t("vagrant.actions.vm.forward_ports.privileged_ports")
      return
    end
  end
end