X-Git-Url: https://git.openstreetmap.org./chef.git/blobdiff_plain/3a4b1c2078cd515e93a5f1bf8967f380fbd1646f..31f1fa863c0b208a44e10ef3ae1571024ef0f0ae:/cookbooks/networking/libraries/ipaddresses.rb diff --git a/cookbooks/networking/libraries/ipaddresses.rb b/cookbooks/networking/libraries/ipaddresses.rb index 5eb54265e..5f746c9aa 100644 --- a/cookbooks/networking/libraries/ipaddresses.rb +++ b/cookbooks/networking/libraries/ipaddresses.rb @@ -1,25 +1,55 @@ -class Chef - class Node - def ipaddresses(options = {}, &block) - addresses = [] - - interfaces(options).each do |interface| - if block.nil? - addresses << interface[:address] - else - block.call(interface[:address]) +require "ipaddr" + +module OpenStreetMap + module Mixin + module IPAddresses + class Address + attr_reader :address, :prefix, :gateway, :network, :netmask + + def initialize(address) + @address = address[:public_address] || address[:address] + @prefix = address[:prefix] + @gateway = address[:gateway] + + ip = IPAddr.new(address[:address]).mask(address[:prefix]) + + @network = ip.to_s + @netmask = ip.netmask + end + + def <=>(other) + address <=> other.address + end + + def to_s + address + end + + def to_str + address + end + + def subnet + "#{@network}/#{@prefix}" end end - addresses - end + def ipaddresses(role: nil, family: nil) + interfaces(:role => role).each_with_object([]) do |interface, addresses| + addresses << Address.new(interface[:inet]) if interface[:inet] && (family.nil? || family == :inet) + addresses << Address.new(interface[:inet6]) if interface[:inet6] && (family.nil? || family == :inet6) + end + end - def internal_ipaddress - ipaddresses(:role => :internal).first - end + def internal_ipaddress(family: nil) + ipaddresses(:role => :internal, :family => family).first + end - def external_ipaddress - ipaddresses(:role => :external).first + def external_ipaddress(family: nil) + ipaddresses(:role => :external, :family => family).first + end end end end + +Chef::Node.include(OpenStreetMap::Mixin::IPAddresses)