Class: AutoNetwork::Pool
- Inherits:
-
Object
- Object
- AutoNetwork::Pool
- 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
-
#network_range ⇒ String
readonly
The address range manged by this Pool instance.
Instance Method Summary collapse
-
#address_for(machine) ⇒ IPAddr?
Look up the address assigned to a given machine.
-
#id_for(machine) ⇒ Hash{String=>String}
Compute the value that will be used to identify a machine.
-
#initialize(network_range) ⇒ Pool
constructor
Create a new Pool object that manages a range of IP addresses.
-
#release(machine) ⇒ nil
Release an IP address associated with a machine.
-
#request(machine) ⇒ IPAddr
Allocate an IP address for the given machine.
Constructor Details
#initialize(network_range) ⇒ Pool
Create a new Pool object that manages a range of IP addresses.
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_range ⇒ String (readonly)
Returns 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.
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.
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.
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.
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 |