Class: AutoNetwork::Pool

Inherits:
Object
  • Object
show all
Defined in:
lib/auto_network/pool.rb

Overview

The Pool is a class that manages a range of IP addresses and manages the allocation of specific addresses to individual Vagrant machines.

Defined Under Namespace

Classes: PoolExhaustedError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(network_range) ⇒ Pool

Create a new Pool object that manages a range of IP addresses.

Parameters:

  • network_range (String)

    The network address range to use as the address pool.



23
24
25
26
# File 'lib/auto_network/pool.rb', line 23

def initialize(network_range)
  @network_range = network_range
  generate_pool
end

Instance Attribute Details

#network_rangeString (readonly)

Returns The address range manged by this Pool instance.

Returns:

  • (String)

    The address range manged by this Pool instance.



17
18
19
# File 'lib/auto_network/pool.rb', line 17

def network_range
  @network_range
end

Instance Method Details

#address_for(machine) ⇒ IPAddr?

Look up the address assigned to a given machine.

Parameters:

  • machine (Vagrant::Machine)

Returns:

  • (IPAddr)

    the IP address assigned to the machine.

  • (nil)

    if the machine has no address assigned.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/auto_network/pool.rb', line 63

def address_for(machine)
  machine_id = id_for(machine)
  addr, _ = @pool.find do |(addr, id)|
    if id.is_a?(String)
      # Check for old-style UUID values. These should eventually cycle out
      # as machines are destroyed.
      id == machine.id
    else
      id == machine_id
    end
  end

  addr
end

#id_for(machine) ⇒ Hash{String=>String}

Compute the value that will be used to identify a machine. This value will be associated with IP addresses allocated to the machine.

Parameters:

  • machine (Vagrant::Machine)

Returns:

  • (Hash{String=>String})

    A hash containing the path to the directory containing the Vagrantfile that defined the machine and the machine name.

Since:

  • 1.0.0



88
89
90
91
92
93
# File 'lib/auto_network/pool.rb', line 88

def id_for(machine)
  {
    'path' => machine.env.root_path.to_s,
    'name' => machine.name.to_s,
  }
end

#release(machine) ⇒ nil

Release an IP address associated with a machine.

Parameters:

  • machine (Vagrant::Machine)

Returns:

  • (nil)


52
53
54
55
56
# File 'lib/auto_network/pool.rb', line 52

def release(machine)
  if (address = address_for(machine))
    @pool[address] = nil
  end
end

#request(machine) ⇒ IPAddr

Allocate an IP address for the given machine. If a machine already has an IP address allocated, then return that.

Parameters:

  • machine (Vagrant::Machine)

Returns:

  • (IPAddr)

    the IP address assigned to the machine.

Raises:

  • (PoolExhaustedError)

    if no allocatable addresses remain in the range managed by the pool.



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/auto_network/pool.rb', line 35

def request(machine)
  if (address = address_for(machine))
    return address
  elsif (address = next_available_lease)
    @pool[address] = id_for(machine)
    return address
  else
    raise PoolExhaustedError,
      :name    => machine.name,
      :network => @network_range
  end
end