Class: VagrantPlugins::Utm::Action::ForwardPorts

Inherits:
Object
  • Object
show all
Includes:
Util::CompileForwardedPorts
Defined in:
lib/vagrant_utm/action/forward_ports.rb

Overview

This action forwards ports on VM based on user configuration.

Instance Method Summary collapse

Methods included from Util::CompileForwardedPorts

#compile_forwarded_ports

Constructor Details

#initialize(app, _env) ⇒ ForwardPorts

Returns a new instance of ForwardPorts.



13
14
15
# File 'lib/vagrant_utm/action/forward_ports.rb', line 13

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

Instance Method Details

#call(env) ⇒ Object


Execution




20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/vagrant_utm/action/forward_ports.rb', line 20

def call(env)
  @env = env

  # Get the ports we're forwarding
  env[:forwarded_ports] ||= compile_forwarded_ports(env[:machine].config)

  # Warn if we're port forwarding to any privileged ports...
  env[:forwarded_ports].each do |fp|
    if fp.host_port <= 1024
      env[:ui].warn I18n.t("vagrant.actions.vm.forward_ports.privileged_ports")
      break
    end
  end

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

  @app.call(env)
end

#forward_portsObject

rubocop:disable Metrics/AbcSize,Metrics/MethodLength



40
41
42
43
44
45
46
47
48
49
50
51
52
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
# File 'lib/vagrant_utm/action/forward_ports.rb', line 40

def forward_ports # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  ports = []

  interfaces = @env[:machine].provider.driver.read_network_interfaces

  @env[:forwarded_ports].each do |fp| # rubocop:disable Metrics/BlockLength
    message_attributes = {
      adapter: fp.adapter,
      guest_port: fp.guest_port,
      host_port: fp.host_port
    }

    @env[:ui].detail(I18n.t("vagrant.actions.vm.forward_ports.forwarding_entry", **message_attributes))

    # Verify we have the network interface to attach to
    unless 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

    # Port forwarding requires the network interface to be a 'Emulated VLAN' interface,
    # so verify that that is the case.
    if interfaces[fp.adapter][:type] != :emulated
      @env[:ui].detail(I18n.t("vagrant_utm.actions.vm.forward_ports.non_emulated",
                              **message_attributes))
      next
    end

    # Add the options to the ports array to send to the driver later
    ports << {
      adapter: fp.adapter,
      guestip: fp.guest_ip,
      guestport: fp.guest_port,
      hostip: fp.host_ip,
      hostport: fp.host_port,
      name: fp.id,
      protocol: fp.protocol
    }
  end

  # We only need to forward ports if there are any to forward
  return if ports.empty?

  @env[:machine].provider.driver.forward_ports(ports)
end