
When running an older version of Ruby that has an old version of ipaddr this function fails with a uninitialized constant IPAddr::AddressFamilyError error. This can happen if you are for example running this code from inside a Puppetserver that runs under JRuby that has an older version of ipaddr. This adds a check if one of the classes that was introduced to replace the ArgumentError exception is present and customizes the exceptions we catch based on that. Change-Id: I9948d4dc22195300cf1c0a48fcc0694bcf991698
41 lines
1.3 KiB
Ruby
41 lines
1.3 KiB
Ruby
# Add inet6 prefix if the argument is an IPv6 address.
|
|
#
|
|
# This is useful for services relying on python-memcached which require
|
|
# the inet6:[<ip_address]:<port> format.
|
|
#
|
|
# Returns the argument untouched otherwise.
|
|
Puppet::Functions.create_function(:inet6_prefix) do
|
|
def inet6_prefix(*args)
|
|
require 'ipaddr'
|
|
|
|
# NOTE(tobias-urdin): Add support for older version of the ipaddr
|
|
# lib where ArgumentError hasn't been replaced yet.
|
|
if IPAddr.const_defined?('InvalidAddressError')
|
|
exp = [IPAddr::AddressFamilyError, IPAddr::Error, IPAddr::InvalidAddressError,
|
|
IPAddr::InvalidPrefixError, ArgumentError, NoMethodError]
|
|
else
|
|
exp = [ArgumentError, NoMethodError]
|
|
end
|
|
|
|
result = []
|
|
args = args[0] if args[0].kind_of?(Array)
|
|
args = [args] unless args.kind_of?(Array)
|
|
args.each do |ip|
|
|
begin
|
|
unless ip.match(/^inet6:.+/)
|
|
ip_parts = ip.split(/\s|\[|\]/).reject { |c| c.empty? }
|
|
if IPAddr.new(ip_parts[0]).ipv6?
|
|
Puppet.debug("#{ip} is changed to inet6:[#{ip}]")
|
|
ip = "inet6:[#{ip_parts.shift}]#{ip_parts.join}"
|
|
end
|
|
end
|
|
rescue *exp
|
|
# ignore it
|
|
end
|
|
result << ip
|
|
end
|
|
return result[0] if args.size == 1
|
|
result
|
|
end
|
|
end
|