Class: VagrantPlugins::Utm::Action::PrepareForwardedPortCollisionParams

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant_utm/action/prepare_forwarded_port_collision_params.rb

Overview

This middleware class is responsible for preparing forwarded port

Instance Method Summary collapse

Constructor Details

#initialize(app, _env) ⇒ PrepareForwardedPortCollisionParams

Returns a new instance of PrepareForwardedPortCollisionParams.



11
12
13
# File 'lib/vagrant_utm/action/prepare_forwarded_port_collision_params.rb', line 11

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

Instance Method Details

#call(env) ⇒ Object

rubocop:disable Metrics/AbcSize



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/vagrant_utm/action/prepare_forwarded_port_collision_params.rb', line 15

def call(env) # rubocop:disable Metrics/AbcSize
  # Get the forwarded ports used by other virtual machines and
  # consider those in use as well.
  env[:port_collision_extra_in_use] = env[:machine].provider.driver.read_used_ports

  # Build the remap for any existing collision detections
  remap = {}
  env[:port_collision_remap] = remap
  env[:machine].provider.driver.read_forwarded_ports.each do |_nic, name, hostport, _guestport|
    env[:machine].config.vm.networks.each do |type, options|
      next if type != :forwarded_port

      # UTM port forwarding does not have name field
      # We use the host port as the name (key) since that is what should be unique
      # If the host port matches the name(host port) of the forwarded port, then
      # remap.
      if options[:host] == name
        remap[options[:host]] = hostport
        break
      end
    end
  end

  @app.call(env)
end