Class: AutoNetwork::PoolManager

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

Overview

The PoolManager class manages the mapping between providers and pools of IP addresses. Each PoolManager instance is backed by a file that persists state and attempts to prevent race conditions between multiple Vagrant processes.

Once created, PoolManager instances proxy the public interface of the Pool instances they manage. New pools will be allocated as needed and all pool operations are wrapped in transactions that ensure state is synced to the file system.

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ PoolManager

Create a new PoolManager instance with persistent storage.

Parameters:

  • path (String, Pathname)

    the location at which to persist the state of AutoNetwork pools.

Since:

  • 1.0.0



26
27
28
29
30
# File 'lib/auto_network/pool_manager.rb', line 26

def initialize(path)
  # Ensure newly created files start with a skeleton data structure.
  AutoNetwork::PoolStorage.init(path) unless File.file?(path)
  @pool_storage = AutoNetwork::PoolStorage.new(path)
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.

See Also:

Since:

  • 1.0.0



73
74
75
76
77
# File 'lib/auto_network/pool_manager.rb', line 73

def address_for(machine)
  with_pool_for(machine, read_only=true) do |pool|
    pool.address_for(machine)
  end
end

#release(machine) ⇒ nil

Release an IP address associated with a machine.

Parameters:

  • machine (Vagrant::Machine)

Returns:

  • (nil)

See Also:

Since:

  • 1.0.0



63
64
65
66
67
# File 'lib/auto_network/pool_manager.rb', line 63

def release(machine)
  with_pool_for(machine) do |pool|
    pool.release(machine)
  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.

See Also:

Since:

  • 1.0.0



53
54
55
56
57
# File 'lib/auto_network/pool_manager.rb', line 53

def request(machine)
  with_pool_for(machine) do |pool|
    pool.request(machine)
  end
end

#with_pool_for(machine, read_only = false) {|pool| ... } ⇒ Object

Looks up the pool associated with the provider for a given machine and sets up a transaction where the state of the pool can be safely inspected or modified. If a pool does not exist for the machine provider, one will automatically be created.

Parameters:

  • machine (Vagrant::Machine)
  • read_only (Boolean) (defaults to: false)

    whether to create a read_only transaction.

Yield Parameters:

Since:

  • 1.0.0



40
41
42
43
44
45
46
47
# File 'lib/auto_network/pool_manager.rb', line 40

def with_pool_for(machine, read_only=false)
  @pool_storage.transaction(read_only) do
    pool = lookup_pool_for(machine)
    pool ||= generate_pool_for(machine)

    yield pool
  end
end