diff options
Diffstat (limited to 'lib')
23 files changed, 69 insertions, 93 deletions
diff --git a/lib/puppet/parser/functions/bool2num.rb b/lib/puppet/parser/functions/bool2num.rb index 9a07a8a..6ad6cf4 100644 --- a/lib/puppet/parser/functions/bool2num.rb +++ b/lib/puppet/parser/functions/bool2num.rb @@ -14,30 +14,7 @@ module Puppet::Parser::Functions raise(Puppet::ParseError, "bool2num(): Wrong number of arguments " + "given (#{arguments.size} for 1)") if arguments.size < 1 - value = arguments[0] - klass = value.class - - # We can have either true or false, or string which resembles boolean ... - unless [FalseClass, TrueClass, String].include?(klass) - raise(Puppet::ParseError, 'bool2num(): Requires either ' + - 'boolean or string to work with') - end - - if value.is_a?(String) - # We consider all the yes, no, y, n and so on too ... - value = case value - # - # This is how undef looks like in Puppet ... - # We yield 0 (or false if you wish) in this case. - # - when /^$/, '' then false # Empty string will be false ... - when /^(1|t|y|true|yes)$/ then true - when /^(0|f|n|false|no)$/ then false - when /^(undef|undefined)$/ then false # This is not likely to happen ... - else - raise(Puppet::ParseError, 'bool2num(): Unknown type of boolean given') - end - end + value = function_str2bool([arguments[0]]) # We have real boolean values as well ... result = value ? 1 : 0 diff --git a/lib/puppet/parser/functions/capitalize.rb b/lib/puppet/parser/functions/capitalize.rb index 640d00b..98b2d16 100644 --- a/lib/puppet/parser/functions/capitalize.rb +++ b/lib/puppet/parser/functions/capitalize.rb @@ -13,9 +13,8 @@ module Puppet::Parser::Functions "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'capitalize(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/chomp.rb b/lib/puppet/parser/functions/chomp.rb index 4564a00..c55841e 100644 --- a/lib/puppet/parser/functions/chomp.rb +++ b/lib/puppet/parser/functions/chomp.rb @@ -14,9 +14,8 @@ module Puppet::Parser::Functions "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'chomp(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/chop.rb b/lib/puppet/parser/functions/chop.rb index f242af3..b24ab78 100644 --- a/lib/puppet/parser/functions/chop.rb +++ b/lib/puppet/parser/functions/chop.rb @@ -16,9 +16,8 @@ module Puppet::Parser::Functions "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'chop(): Requires either an ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/concat.rb b/lib/puppet/parser/functions/concat.rb index 6c86382..0d35b07 100644 --- a/lib/puppet/parser/functions/concat.rb +++ b/lib/puppet/parser/functions/concat.rb @@ -28,11 +28,7 @@ Would result in: raise(Puppet::ParseError, 'concat(): Requires array to work with') end - if b.is_a?(Array) - result = a.concat(b) - else - result = a << b - end + result = a + Array(b) return result end diff --git a/lib/puppet/parser/functions/downcase.rb b/lib/puppet/parser/functions/downcase.rb index 4066d21..040b84f 100644 --- a/lib/puppet/parser/functions/downcase.rb +++ b/lib/puppet/parser/functions/downcase.rb @@ -12,9 +12,8 @@ Converts the case of a string or all strings in an array to lower case. "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'downcase(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/empty.rb b/lib/puppet/parser/functions/empty.rb index 80ebb86..cca620f 100644 --- a/lib/puppet/parser/functions/empty.rb +++ b/lib/puppet/parser/functions/empty.rb @@ -12,9 +12,8 @@ Returns true if the variable is empty. "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, Hash, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(Hash) || value.is_a?(String) raise(Puppet::ParseError, 'empty(): Requires either ' + 'array, hash or string to work with') end diff --git a/lib/puppet/parser/functions/fqdn_rotate.rb b/lib/puppet/parser/functions/fqdn_rotate.rb index 6558206..7f4d37d 100644 --- a/lib/puppet/parser/functions/fqdn_rotate.rb +++ b/lib/puppet/parser/functions/fqdn_rotate.rb @@ -12,10 +12,9 @@ Rotates an array a random number of times based on a nodes fqdn. "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class require 'digest/md5' - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'fqdn_rotate(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/getvar.rb b/lib/puppet/parser/functions/getvar.rb index 1621149..fb336b6 100644 --- a/lib/puppet/parser/functions/getvar.rb +++ b/lib/puppet/parser/functions/getvar.rb @@ -19,7 +19,10 @@ module Puppet::Parser::Functions raise Puppet::ParseError, ("getvar(): wrong number of arguments (#{args.length}; must be 1)") end - self.lookupvar("#{args[0]}") + begin + self.lookupvar("#{args[0]}") + rescue Puppet::ParseError # Eat the exception if strict_variables = true is set + end end diff --git a/lib/puppet/parser/functions/has_interface_with.rb b/lib/puppet/parser/functions/has_interface_with.rb index d5dbe47..00e405d 100644 --- a/lib/puppet/parser/functions/has_interface_with.rb +++ b/lib/puppet/parser/functions/has_interface_with.rb @@ -34,8 +34,7 @@ has_interface_with("lo") => true end kind, value = args - kind.downcase! - + if lookupvar(kind) == value return true end @@ -43,7 +42,12 @@ has_interface_with("lo") => true result = false interfaces.each do |iface| iface.downcase! - if value == lookupvar("#{kind}_#{iface}") + factval = nil + begin + factval = lookupvar("#{kind}_#{iface}") + rescue Puppet::ParseError # Eat the exception if strict_variables = true is set + end + if value == factval result = true break end diff --git a/lib/puppet/parser/functions/lstrip.rb b/lib/puppet/parser/functions/lstrip.rb index 3a64de3..624e4c8 100644 --- a/lib/puppet/parser/functions/lstrip.rb +++ b/lib/puppet/parser/functions/lstrip.rb @@ -12,9 +12,8 @@ Strips leading spaces to the left of a string. "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'lstrip(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/private.rb b/lib/puppet/parser/functions/private.rb new file mode 100644 index 0000000..60210d3 --- /dev/null +++ b/lib/puppet/parser/functions/private.rb @@ -0,0 +1,29 @@ +# +# private.rb +# + +module Puppet::Parser::Functions + newfunction(:private, :doc => <<-'EOS' + Sets the current class or definition as private. + Calling the class or definition from outside the current module will fail. + EOS + ) do |args| + + raise(Puppet::ParseError, "private(): Wrong number of arguments "+ + "given (#{args.size}}) for 0 or 1)") if args.size > 1 + + scope = self + if scope.lookupvar('module_name') != scope.lookupvar('caller_module_name') + message = nil + if args[0] and args[0].is_a? String + message = args[0] + else + manifest_name = scope.source.name + manifest_type = scope.source.type + message = (manifest_type.to_s == 'hostclass') ? 'Class' : 'Definition' + message += " #{manifest_name} is private" + end + raise(Puppet::ParseError, message) + end + end +end diff --git a/lib/puppet/parser/functions/reverse.rb b/lib/puppet/parser/functions/reverse.rb index fe04869..7f1018f 100644 --- a/lib/puppet/parser/functions/reverse.rb +++ b/lib/puppet/parser/functions/reverse.rb @@ -12,9 +12,8 @@ Reverses the order of a string or array. "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'reverse(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/rstrip.rb b/lib/puppet/parser/functions/rstrip.rb index 29b0998..0cf8d22 100644 --- a/lib/puppet/parser/functions/rstrip.rb +++ b/lib/puppet/parser/functions/rstrip.rb @@ -12,9 +12,8 @@ Strips leading spaces to the right of the string. "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'rstrip(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/shuffle.rb b/lib/puppet/parser/functions/shuffle.rb index 18134ab..30c663d 100644 --- a/lib/puppet/parser/functions/shuffle.rb +++ b/lib/puppet/parser/functions/shuffle.rb @@ -12,9 +12,8 @@ Randomizes the order of a string or array elements. "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'shuffle(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/strip.rb b/lib/puppet/parser/functions/strip.rb index 5f4630d..3fac47d 100644 --- a/lib/puppet/parser/functions/strip.rb +++ b/lib/puppet/parser/functions/strip.rb @@ -19,9 +19,8 @@ Would result in: "aaa" "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'strip(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/swapcase.rb b/lib/puppet/parser/functions/swapcase.rb index b9e6632..eb7fe13 100644 --- a/lib/puppet/parser/functions/swapcase.rb +++ b/lib/puppet/parser/functions/swapcase.rb @@ -18,9 +18,8 @@ Would result in: "AbCd" "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'swapcase(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/unique.rb b/lib/puppet/parser/functions/unique.rb index 8844a74..cf770f3 100644 --- a/lib/puppet/parser/functions/unique.rb +++ b/lib/puppet/parser/functions/unique.rb @@ -28,9 +28,8 @@ This returns: "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'unique(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/upcase.rb b/lib/puppet/parser/functions/upcase.rb index fe6cadc..4302b29 100644 --- a/lib/puppet/parser/functions/upcase.rb +++ b/lib/puppet/parser/functions/upcase.rb @@ -20,9 +20,8 @@ Will return: "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'upcase(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/uriescape.rb b/lib/puppet/parser/functions/uriescape.rb index 0d81de5..a486eee 100644 --- a/lib/puppet/parser/functions/uriescape.rb +++ b/lib/puppet/parser/functions/uriescape.rb @@ -14,9 +14,8 @@ module Puppet::Parser::Functions "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'uriescape(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/validate_string.rb b/lib/puppet/parser/functions/validate_string.rb index e667794..c841f6a 100644 --- a/lib/puppet/parser/functions/validate_string.rb +++ b/lib/puppet/parser/functions/validate_string.rb @@ -13,9 +13,14 @@ module Puppet::Parser::Functions validate_string(true) validate_string([ 'some', 'array' ]) - $undefined = undef - validate_string($undefined) - + + Note: validate_string(undef) will not fail in this version of the + functions API (incl. current and future parser). Instead, use: + + if $var == undef { + fail('...') + } + ENDHEREDOC unless args.length > 0 then diff --git a/lib/puppet/parser/functions/zip.rb b/lib/puppet/parser/functions/zip.rb index 2b56e9c..3074f28 100644 --- a/lib/puppet/parser/functions/zip.rb +++ b/lib/puppet/parser/functions/zip.rb @@ -27,33 +27,7 @@ Would result in: raise(Puppet::ParseError, 'zip(): Requires array to work with') end - flatten = arguments[2] if arguments[2] - - if flatten - klass = flatten.class - - # We can have either true or false, or string which resembles boolean ... - unless [FalseClass, TrueClass, String].include?(klass) - raise(Puppet::ParseError, 'zip(): Requires either ' + - 'boolean or string to work with') - end - - if flatten.is_a?(String) - # We consider all the yes, no, y, n and so on too ... - flatten = case flatten - # - # This is how undef looks like in Puppet ... - # We yield false in this case. - # - when /^$/, '' then false # Empty string will be false ... - when /^(1|t|y|true|yes)$/ then true - when /^(0|f|n|false|no)$/ then false - when /^(undef|undefined)$/ then false # This is not likely to happen ... - else - raise(Puppet::ParseError, 'zip(): Unknown type of boolean given') - end - end - end + flatten = function_str2bool([arguments[2]]) if arguments[2] result = a.zip(b) result = flatten ? result.flatten : result diff --git a/lib/puppet/type/file_line.rb b/lib/puppet/type/file_line.rb index 323fc4c..9dbe43c 100644 --- a/lib/puppet/type/file_line.rb +++ b/lib/puppet/type/file_line.rb @@ -21,6 +21,9 @@ Puppet::Type.newtype(:file_line) do In this example, Puppet will ensure both of the specified lines are contained in the file /etc/sudoers. + **Autorequires:** If Puppet is managing the file that will contain the line + being managed, the file_line resource will autorequire that file. + EOT ensurable do |