Class: Vagrant::Action::Builtin::HandleForwardedPortCollisions

Inherits:
Object
  • Object
show all
Includes:
Util::IPv4Interfaces, Util::IsPortOpen
Defined in:
lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb

Overview

This middleware class will detect and handle collisions with forwarded ports, whether that means raising an error or repairing them automatically.

Parameters it takes from the environment hash:

  • :port_collision_repair - If true, it will attempt to repair port collisions. If false, it will raise an exception when there is a collision.

  • :port_collision_extra_in_use - An array of ports that are considered in use.

  • :port_collision_remap - A hash remapping certain host ports to other host ports.

Instance Method Summary collapse

Methods included from Util::IPv4Interfaces

#ipv4_interfaces

Methods included from Util::IsPortOpen

#is_port_open?

Constructor Details

#initialize(app, env) ⇒ HandleForwardedPortCollisions

Returns a new instance of HandleForwardedPortCollisions.



35
36
37
38
# File 'lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb', line 35

def initialize(app, env)
  @app    = app
  @logger = Log4r::Logger.new("vagrant::action::builtin::handle_port_collisions")
end

Instance Method Details

#call(env) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb', line 40

def call(env)
  @leased  = []
  @machine = env[:machine]

  # Acquire a process-level lock so that we don't choose a port
  # that someone else also chose.
  begin
    env[:machine].env.lock("fpcollision") do
      handle(env)
    end
  rescue Errors::EnvironmentLockedError
    sleep 1
    retry
  end

  @app.call(env)

  # Always run the recover method so that we release leases
  recover(env)
end

#recover(env) ⇒ Object



61
62
63
# File 'lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb', line 61

def recover(env)
  lease_release
end