]> git.openstreetmap.org Git - chef.git/blob - cookbooks/networking/libraries/ipaddresses.rb
nominatim: disable luajit
[chef.git] / cookbooks / networking / libraries / ipaddresses.rb
1 require "ipaddr"
2
3 module OpenStreetMap
4   module Mixin
5     module IPAddresses
6       class Address
7         attr_reader :address, :prefix, :gateway, :network, :netmask
8
9         def initialize(address)
10           @address = address[:public_address] || address[:address]
11           @prefix = address[:prefix]
12           @gateway = address[:gateway]
13
14           ip = IPAddr.new(address[:address]).mask(address[:prefix])
15
16           @network = ip.to_s
17           @netmask = ip.netmask
18         end
19
20         def <=>(other)
21           address <=> other.address
22         end
23
24         def to_s
25           address
26         end
27
28         def to_str
29           address
30         end
31
32         def subnet
33           "#{@network}/#{@prefix}"
34         end
35       end
36
37       def ipaddresses(role: nil, family: nil)
38         interfaces(:role => role).each_with_object([]) do |interface, addresses|
39           addresses << Address.new(interface[:inet]) if interface[:inet] && (family.nil? || family == :inet)
40           addresses << Address.new(interface[:inet6]) if interface[:inet6] && (family.nil? || family == :inet6)
41         end
42       end
43
44       def internal_ipaddress(family: nil)
45         ipaddresses(:role => :internal, :family => family).first
46       end
47
48       def external_ipaddress(family: nil)
49         ipaddresses(:role => :external, :family => family).first
50       end
51     end
52   end
53 end
54
55 Chef::Node.include(OpenStreetMap::Mixin::IPAddresses)