Add old ruby support to inet6_prefix

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
This commit is contained in:
Tobias Urdin 2020-03-31 13:41:15 +02:00
parent 60b6455598
commit efffd27a41
1 changed files with 10 additions and 1 deletions

View File

@ -8,6 +8,15 @@ 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)
@ -20,7 +29,7 @@ Puppet::Functions.create_function(:inet6_prefix) do
ip = "inet6:[#{ip_parts.shift}]#{ip_parts.join}"
end
end
rescue IPAddr::AddressFamilyError, IPAddr::Error, IPAddr::InvalidAddressError, IPAddr::InvalidPrefixError, ArgumentError, NoMethodError => e
rescue *exp
# ignore it
end
result << ip