Class: Fragrant::AddressManager

Inherits:
Object
  • Object
show all
Defined in:
lib/fragrant/address_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_location, address_range) ⇒ AddressManager

Returns a new instance of AddressManager.



11
12
13
14
15
16
17
# File 'lib/fragrant/address_manager.rb', line 11

def initialize(data_location, address_range)
  self.data_location = data_location
  self.address_range = address_range
  self.address_map = {}
  self.allocated_addresses = []
  load_address_data
end

Instance Attribute Details

#address_mapObject

Returns the value of attribute address_map.



9
10
11
# File 'lib/fragrant/address_manager.rb', line 9

def address_map
  @address_map
end

#address_rangeObject

Returns the value of attribute address_range.



9
10
11
# File 'lib/fragrant/address_manager.rb', line 9

def address_range
  @address_range
end

#allocated_addressesObject

Returns the value of attribute allocated_addresses.



8
9
10
# File 'lib/fragrant/address_manager.rb', line 8

def allocated_addresses
  @allocated_addresses
end

#data_locationObject

Returns the value of attribute data_location.



8
9
10
# File 'lib/fragrant/address_manager.rb', line 8

def data_location
  @data_location
end

Instance Method Details

#address_dataObject



32
33
34
35
36
# File 'lib/fragrant/address_manager.rb', line 32

def address_data
  {:address_range => address_range,
   :allocated_addresses => allocated_addresses,
   :address_map => address_map}
end

#claim_address(environment_id) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/fragrant/address_manager.rb', line 54

def claim_address(environment_id)
  if address_map.key?(environment_id)
    raise "#{environment_id} already has an address"
  end
  address = first_available_address
  address_map[environment_id] = address
  allocated_addresses << address
  persist
  address
end

#first_available_addressObject



38
39
40
41
42
43
44
# File 'lib/fragrant/address_manager.rb', line 38

def first_available_address
  ip = IPAddr.new(address_range).to_range.detect do |ip|
    !allocated_addresses.include?(ip.to_s)
  end
  return ip.to_s if ip
  raise AddressRangeExhausted, "No more addresses available in range #{address_range}"
end

#load_address_dataObject



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fragrant/address_manager.rb', line 19

def load_address_data
  return unless File.exist?(data_location)
  unless File.writable?(data_location)
    raise "Unable to access IP address config file at #{data_location}"
  end
  File.open(data_location, 'rb') do |f|
    data = JSON.parse(f.read)
    self.address_range = data['address_range']
    self.address_map = data['address_map']
    self.allocated_addresses = data['allocated_addresses']
  end
end

#persistObject



46
47
48
49
50
51
52
# File 'lib/fragrant/address_manager.rb', line 46

def persist
  dir = File.dirname(data_location)
  FileUtils.mkdir_p(dir) unless File.directory?(dir)
  File.open(data_location, 'wb') do |f|
    f.write(address_data.to_json)
  end
end

#release_addresses(environment_id) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/fragrant/address_manager.rb', line 65

def release_addresses(environment_id)
  unless address_map.key?(environment_id)
    raise "No addresses registered to environment #{environment_id}"
  end

  address = address_map[environment_id]
  allocated_addresses.delete address
  address_map.delete environment_id
  persist
end