diff options
author | Gilles Dubreuil <gilles@redhat.com> | 2015-11-16 16:55:28 +1100 |
---|---|---|
committer | Gilles Dubreuil <gilles@redhat.com> | 2015-12-15 17:07:57 +1100 |
commit | 11a0619a290d64f3caa45435a23f3e503530087a (patch) | |
tree | c0e462d7f91537538b42b5587ab1feac8c440fdb /lib/facter | |
parent | b94e93d8c822c0940e89ebfe47818351634f8cc2 (diff) |
Adds IPv6 support for interface_for_ip function
Proper interface matching when an IPv6 address is provided.
If Facter version used is < 3 then it adds the netmask6 facts as custom facts.
Fix bugs https://bugzilla.redhat.com/show_bug.cgi?id=1280523
Change-Id: Ide26ca1740dc12ea5f47a28f4cecacd6ef0b18f9
Diffstat (limited to 'lib/facter')
-rw-r--r-- | lib/facter/netmask_ipv6.rb | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/lib/facter/netmask_ipv6.rb b/lib/facter/netmask_ipv6.rb new file mode 100644 index 0000000..5261485 --- /dev/null +++ b/lib/facter/netmask_ipv6.rb @@ -0,0 +1,47 @@ +require 'ipaddr' + +def netmask6(value) + if value + ip = IPAddr.new('::0').mask(value) + ip.inspect.split('/')[1].gsub('>', '') + end +end + +if Facter.value('facterversion')[0].to_i < 3 + Facter::Util::IP.get_interfaces.each do |interface| + Facter.add('netmask6_' + Facter::Util::IP.alphafy(interface)) do + setcode do + tmp = [] + regex = %r{inet6\s+.*\s+(?:prefixlen)\s+(\d+)}x + output_int = Facter::Util::IP.get_output_for_interface_and_label(interface, 'netmask6') + + output_int.each_line do |line| + prefixlen = nil + matches = line.match(regex) + prefixlen = matches[1] if matches + + if prefixlen + value = netmask6(prefixlen) + tmp.push(value) + end + end + + tmp.shift if tmp + end + end + end + + Facter.add('netmask6') do + setcode do + prefixlen = nil + regex = %r{#{Facter.value(:ipaddress6)}.*?(?:prefixlen)\s*(\d+)}x + + String(Facter::Util::IP.exec_ifconfig(['2>/dev/null'])).split(/\n/).collect do |line| + matches = line.match(regex) + prefixlen = matches[1] if matches + end + + netmask6(prefixlen) if prefixlen + end + end +end |