Class: AutoNetwork::PoolManager
- Inherits:
-
Object
- Object
- AutoNetwork::PoolManager
- 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
-
#address_for(machine) ⇒ IPAddr?
Look up the address assigned to a given machine.
-
#initialize(path) ⇒ PoolManager
constructor
Create a new
PoolManager
instance with persistent storage. -
#release(machine) ⇒ nil
Release an IP address associated with a machine.
-
#request(machine) ⇒ IPAddr
Allocate an IP address for the given machine.
-
#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.
Constructor Details
#initialize(path) ⇒ PoolManager
Create a new PoolManager
instance with persistent storage.
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.
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.
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.
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.
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 |