From 0d11bde5073d2976e9f5d9625757a7a71ddab6c4 Mon Sep 17 00:00:00 2001 From: Mathias Klette Date: Fri, 10 Oct 2014 22:54:22 +0200 Subject: ensure_resource: be more verbose in debug mode helps discovering duplication issues, especially when figthing boolean vs. string arguments --- lib/puppet/parser/functions/ensure_resource.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/ensure_resource.rb b/lib/puppet/parser/functions/ensure_resource.rb index 05e5593..1ba6a44 100644 --- a/lib/puppet/parser/functions/ensure_resource.rb +++ b/lib/puppet/parser/functions/ensure_resource.rb @@ -36,8 +36,9 @@ ENDOFDOC items.each do |item| Puppet::Parser::Functions.function(:defined_with_params) if function_defined_with_params(["#{type}[#{item}]", params]) - Puppet.debug("Resource #{type}[#{item}] not created because it already exists") + Puppet.debug("Resource #{type}[#{item}] with params #{params} not created because it already exists") else + Puppet.debug("Create new resource #{type}[#{item}] with params #{params}") Puppet::Parser::Functions.function(:create_resources) function_create_resources([type.capitalize, { item => params }]) end -- cgit v1.2.3 From 89995e4db0eacf55260cf5ca85e715e2e45dfce5 Mon Sep 17 00:00:00 2001 From: Oliver Bertuch Date: Tue, 25 Nov 2014 12:45:23 +0100 Subject: Allow array of pathes in validate_absolute_path --- .../parser/functions/validate_absolute_path.rb | 61 +++++++++++++--------- 1 file changed, 37 insertions(+), 24 deletions(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/validate_absolute_path.rb b/lib/puppet/parser/functions/validate_absolute_path.rb index fe27974..b696680 100644 --- a/lib/puppet/parser/functions/validate_absolute_path.rb +++ b/lib/puppet/parser/functions/validate_absolute_path.rb @@ -5,15 +5,20 @@ module Puppet::Parser::Functions The following values will pass: - $my_path = "C:/Program Files (x86)/Puppet Labs/Puppet" + $my_path = 'C:/Program Files (x86)/Puppet Labs/Puppet' validate_absolute_path($my_path) - $my_path2 = "/var/lib/puppet" + $my_path2 = '/var/lib/puppet' validate_absolute_path($my_path2) - + $my_path3 = ['C:/Program Files (x86)/Puppet Labs/Puppet','C:/Program Files/Puppet Labs/Puppet'] + validate_absolute_path($my_path3) + $my_path4 = ['/var/lib/puppet','/usr/share/puppet'] + validate_absolute_path($my_path4) The following values will fail, causing compilation to abort: validate_absolute_path(true) + validate_absolute_path('../var/lib/puppet') + validate_absolute_path('var/lib/puppet') validate_absolute_path([ 'var/lib/puppet', '/var/foo' ]) validate_absolute_path([ '/var/lib/puppet', 'var/foo' ]) $undefined = undef @@ -28,28 +33,36 @@ module Puppet::Parser::Functions end args.each do |arg| - # This logic was borrowed from - # [lib/puppet/file_serving/base.rb](https://github.com/puppetlabs/puppet/blob/master/lib/puppet/file_serving/base.rb) - - # Puppet 2.7 and beyond will have Puppet::Util.absolute_path? Fall back to a back-ported implementation otherwise. - if Puppet::Util.respond_to?(:absolute_path?) then - unless Puppet::Util.absolute_path?(arg, :posix) or Puppet::Util.absolute_path?(arg, :windows) - raise Puppet::ParseError, ("#{arg.inspect} is not an absolute path.") + # put arg to candidate var to be able to replace it + candidates = arg + # if arg is just a string with a path to test, convert it to an array + # to avoid test code duplication + unless arg.is_a?(Array) then + candidates = Array.new(1,arg) + end + # iterate over all pathes within the candidates array + candidates.each do |path| + # This logic was borrowed from + # [lib/puppet/file_serving/base.rb](https://github.com/puppetlabs/puppet/blob/master/lib/puppet/file_serving/base.rb) + # Puppet 2.7 and beyond will have Puppet::Util.absolute_path? Fall back to a back-ported implementation otherwise. + if Puppet::Util.respond_to?(:absolute_path?) then + unless Puppet::Util.absolute_path?(path, :posix) or Puppet::Util.absolute_path?(path, :windows) + raise Puppet::ParseError, ("#{path.inspect} is not an absolute path.") + end + else + # This code back-ported from 2.7.x's lib/puppet/util.rb Puppet::Util.absolute_path? + # Determine in a platform-specific way whether a path is absolute. This + # defaults to the local platform if none is specified. + # Escape once for the string literal, and once for the regex. + slash = '[\\\\/]' + name = '[^\\\\/]+' + regexes = { + :windows => %r!^(([A-Z]:#{slash})|(#{slash}#{slash}#{name}#{slash}#{name})|(#{slash}#{slash}\?#{slash}#{name}))!i, + :posix => %r!^/!, + } + rval = (!!(path =~ regexes[:posix])) || (!!(path =~ regexes[:windows])) + rval or raise Puppet::ParseError, ("#{path.inspect} is not an absolute path.") end - else - # This code back-ported from 2.7.x's lib/puppet/util.rb Puppet::Util.absolute_path? - # Determine in a platform-specific way whether a path is absolute. This - # defaults to the local platform if none is specified. - # Escape once for the string literal, and once for the regex. - slash = '[\\\\/]' - name = '[^\\\\/]+' - regexes = { - :windows => %r!^(([A-Z]:#{slash})|(#{slash}#{slash}#{name}#{slash}#{name})|(#{slash}#{slash}\?#{slash}#{name}))!i, - :posix => %r!^/!, - } - - rval = (!!(arg =~ regexes[:posix])) || (!!(arg =~ regexes[:windows])) - rval or raise Puppet::ParseError, ("#{arg.inspect} is not an absolute path.") end end end -- cgit v1.2.3 From 75a6186512b0d4d02593453fcc9ffe5e1b253107 Mon Sep 17 00:00:00 2001 From: Peter Souter Date: Thu, 4 Dec 2014 14:32:23 +0000 Subject: (MODULES-444) Update docs with new functionality --- lib/puppet/parser/functions/concat.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/concat.rb b/lib/puppet/parser/functions/concat.rb index 0d35b07..4b53f59 100644 --- a/lib/puppet/parser/functions/concat.rb +++ b/lib/puppet/parser/functions/concat.rb @@ -4,15 +4,15 @@ module Puppet::Parser::Functions newfunction(:concat, :type => :rvalue, :doc => <<-EOS -Appends the contents of array 2 onto array 1. +Appends the contents of multiple arrays into array 1. *Example:* - concat(['1','2','3'],['4','5','6']) + concat(['1','2','3'],['4','5','6'],['7','8','9']) Would result in: - ['1','2','3','4','5','6'] + ['1','2','3','4','5','6','7','8','9'] EOS ) do |arguments| -- cgit v1.2.3 From 594c2dd38dc35a4f458ce511be9b7dd875915b44 Mon Sep 17 00:00:00 2001 From: Peter Souter Date: Thu, 4 Dec 2014 14:33:23 +0000 Subject: (MODULES-444) Change argument restriction to < 2 --- lib/puppet/parser/functions/concat.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/concat.rb b/lib/puppet/parser/functions/concat.rb index 4b53f59..8400f7b 100644 --- a/lib/puppet/parser/functions/concat.rb +++ b/lib/puppet/parser/functions/concat.rb @@ -16,9 +16,9 @@ Would result in: EOS ) do |arguments| - # Check that 2 arguments have been given ... + # Check that more than 2 arguments have been given ... raise(Puppet::ParseError, "concat(): Wrong number of arguments " + - "given (#{arguments.size} for 2)") if arguments.size != 2 + "given (#{arguments.size} for < 2)") if arguments.size < 2 a = arguments[0] b = arguments[1] -- cgit v1.2.3 From 84bd98645f248a1dc3e0ed791e3af6f2ba9996fa Mon Sep 17 00:00:00 2001 From: Peter Souter Date: Thu, 4 Dec 2014 14:34:25 +0000 Subject: (MODULES-444) - Real meat of the change This is the core change, we now go through the array and add it to the first element, instead of just two arguments. --- lib/puppet/parser/functions/concat.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/concat.rb b/lib/puppet/parser/functions/concat.rb index 8400f7b..618e62d 100644 --- a/lib/puppet/parser/functions/concat.rb +++ b/lib/puppet/parser/functions/concat.rb @@ -21,14 +21,18 @@ Would result in: "given (#{arguments.size} for < 2)") if arguments.size < 2 a = arguments[0] - b = arguments[1] # Check that the first parameter is an array unless a.is_a?(Array) raise(Puppet::ParseError, 'concat(): Requires array to work with') end - result = a + Array(b) + result = a + arguments.shift + + arguments.each do |x| + result = result + Array(x) + end return result end -- cgit v1.2.3 From ef3d42f7bbdf95b21f46e580de309298cad300ea Mon Sep 17 00:00:00 2001 From: Rob Fugina Date: Mon, 17 Nov 2014 16:01:42 -0600 Subject: Added basename() based on Ruby's File.basename Based on dirname code. Includes RSpec tests and docs. --- lib/puppet/parser/functions/basename.rb | 34 +++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 lib/puppet/parser/functions/basename.rb (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/basename.rb b/lib/puppet/parser/functions/basename.rb new file mode 100644 index 0000000..f7e4438 --- /dev/null +++ b/lib/puppet/parser/functions/basename.rb @@ -0,0 +1,34 @@ +module Puppet::Parser::Functions + newfunction(:basename, :type => :rvalue, :doc => <<-EOS + Strips directory (and optional suffix) from a filename + EOS + ) do |arguments| + + if arguments.size < 1 then + raise(Puppet::ParseError, "basename(): No arguments given") + elsif arguments.size > 2 then + raise(Puppet::ParseError, "basename(): Too many arguments given (#{arguments.size})") + else + + unless arguments[0].is_a?(String) + raise(Puppet::ParseError, 'basename(): Requires string as first argument') + end + + if arguments.size == 1 then + rv = File.basename(arguments[0]) + elsif arguments.size == 2 then + + unless arguments[1].is_a?(String) + raise(Puppet::ParseError, 'basename(): Requires string as second argument') + end + + rv = File.basename(arguments[0], arguments[1]) + end + + end + + return rv + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 165caa8be195590bfd445b87db5fe2d0227d99ad Mon Sep 17 00:00:00 2001 From: Peter Souter Date: Tue, 9 Dec 2014 14:20:31 +0000 Subject: (MODULES-1582) Initial spike for % placeholder This simply `gsub`'s the file path into where the % placeholder is. --- lib/puppet/parser/functions/validate_cmd.rb | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/validate_cmd.rb b/lib/puppet/parser/functions/validate_cmd.rb index c6136a5..7290265 100644 --- a/lib/puppet/parser/functions/validate_cmd.rb +++ b/lib/puppet/parser/functions/validate_cmd.rb @@ -6,9 +6,9 @@ module Puppet::Parser::Functions Perform validation of a string with an external command. The first argument of this function should be a string to test, and the second argument should be a path to a test command - taking a file as last argument. If the command, launched against - a tempfile containing the passed string, returns a non-null value, - compilation will abort with a parse error. + taking a % as a placeholder for the file path (will default to the end). + If the command, launched against a tempfile containing the passed string, + returns a non-null value, compilation will abort with a parse error. If a third argument is specified, this will be the error message raised and seen by the user. @@ -17,8 +17,12 @@ module Puppet::Parser::Functions Example: + # Defaults to end of path validate_cmd($sudoerscontent, '/usr/sbin/visudo -c -f', 'Visudo failed to validate sudoers content') + # % as file location + validate_cmd($haproxycontent, '/usr/sbin/haproxy -f % -c', 'Haproxy failed to validate config content') + ENDHEREDOC if (args.length < 2) or (args.length > 3) then raise Puppet::ParseError, ("validate_cmd(): wrong number of arguments (#{args.length}; must be 2 or 3)") @@ -34,10 +38,17 @@ module Puppet::Parser::Functions begin tmpfile.write(content) tmpfile.close + + if checkscript.include?('%') + check_with_correct_location = checkscript.gsub(/%/,tmpfile.path) + else + check_with_correct_location = "#{checkscript} #{tmpfile.path}" + end + if Puppet::Util::Execution.respond_to?('execute') - Puppet::Util::Execution.execute("#{checkscript} #{tmpfile.path}") + Puppet::Util::Execution.execute(check_with_correct_location) else - Puppet::Util.execute("#{checkscript} #{tmpfile.path}") + Puppet::Util.execute(check_with_correct_location) end rescue Puppet::ExecutionFailure => detail msg += "\n#{detail}" -- cgit v1.2.3 From b3d007f1daa9bd7c8c02f372494df3a6e6ff6acf Mon Sep 17 00:00:00 2001 From: Peter Souter Date: Thu, 18 Dec 2014 23:08:13 +0000 Subject: (MODULES-1582) Improve % detection Avoids any validate commands that have %'s in them other than "... % ..." --- lib/puppet/parser/functions/validate_cmd.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/validate_cmd.rb b/lib/puppet/parser/functions/validate_cmd.rb index 7290265..5df3c60 100644 --- a/lib/puppet/parser/functions/validate_cmd.rb +++ b/lib/puppet/parser/functions/validate_cmd.rb @@ -39,7 +39,7 @@ module Puppet::Parser::Functions tmpfile.write(content) tmpfile.close - if checkscript.include?('%') + if checkscript =~ /\s%(\s|$)/ check_with_correct_location = checkscript.gsub(/%/,tmpfile.path) else check_with_correct_location = "#{checkscript} #{tmpfile.path}" -- cgit v1.2.3 From 8ec6f8dbfdaaa4b34030cbe1f4764c42c629240e Mon Sep 17 00:00:00 2001 From: Travis Fields Date: Fri, 19 Dec 2014 10:26:01 -0800 Subject: MODULES-1606 add ability to pass array to delete for items to delete --- lib/puppet/parser/functions/delete.rb | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/delete.rb b/lib/puppet/parser/functions/delete.rb index d03a293..a1e39ed 100644 --- a/lib/puppet/parser/functions/delete.rb +++ b/lib/puppet/parser/functions/delete.rb @@ -19,25 +19,25 @@ string, or key from a hash. delete('abracadabra', 'bra') Would return: 'acada' - EOS + EOS ) do |arguments| if (arguments.size != 2) then raise(Puppet::ParseError, "delete(): Wrong number of arguments "+ - "given #{arguments.size} for 2.") + "given #{arguments.size} for 2.") end collection = arguments[0].dup - item = arguments[1] - - case collection - when Array, Hash - collection.delete item - when String - collection.gsub! item, '' - else - raise(TypeError, "delete(): First argument must be an Array, " + - "String, or Hash. Given an argument of class #{collection.class}.") + Array(arguments[1]).each do |item| + case collection + when Array, Hash + collection.delete item + when String + collection.gsub! item, '' + else + raise(TypeError, "delete(): First argument must be an Array, " + + "String, or Hash. Given an argument of class #{collection.class}.") + end end collection end -- cgit v1.2.3 From f6e20d20684f315d746e99ff554a0bdc714f96a3 Mon Sep 17 00:00:00 2001 From: Travis Fields Date: Fri, 19 Dec 2014 10:41:07 -0800 Subject: Update docs to reflect new behavior of delete function taking array in second argument --- lib/puppet/parser/functions/delete.rb | 3 +++ 1 file changed, 3 insertions(+) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/delete.rb b/lib/puppet/parser/functions/delete.rb index a1e39ed..f548b44 100644 --- a/lib/puppet/parser/functions/delete.rb +++ b/lib/puppet/parser/functions/delete.rb @@ -17,6 +17,9 @@ string, or key from a hash. delete({'a'=>1,'b'=>2,'c'=>3}, 'b') Would return: {'a'=>1,'c'=>3} + delete({'a'=>1,'b'=>2,'c'=>3}, ['b','c']) + Would return: {'a'=>1} + delete('abracadabra', 'bra') Would return: 'acada' EOS -- cgit v1.2.3 From 7c8ae311cade65e84df1054779a039ff906e630c Mon Sep 17 00:00:00 2001 From: Hunter Haugen Date: Mon, 15 Dec 2014 16:11:10 -0800 Subject: (MODULES-1473) Deprecate type() function for new parser The `type()` function will cease to work on the new parser because 'type' is a reserved keyword. The `type3x()` function may be used to continue similar functionality, but will be deprecated in favor of the built-in typing system. The `type_of()` function has been included to introspect types in the new parser. --- lib/puppet/parser/functions/type.rb | 43 +++++------------------------ lib/puppet/parser/functions/type3x.rb | 51 +++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 37 deletions(-) create mode 100644 lib/puppet/parser/functions/type3x.rb (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/type.rb b/lib/puppet/parser/functions/type.rb index 8d85f11..016529b 100644 --- a/lib/puppet/parser/functions/type.rb +++ b/lib/puppet/parser/functions/type.rb @@ -4,46 +4,15 @@ module Puppet::Parser::Functions newfunction(:type, :type => :rvalue, :doc => <<-EOS -Returns the type when passed a variable. Type can be one of: - -* string -* array -* hash -* float -* integer -* boolean + DEPRECATED: This function will cease to function on Puppet 4; please use type3x() before upgrading to puppet 4 for backwards-compatibility, or migrate to the new parser's typing system. EOS - ) do |arguments| - - raise(Puppet::ParseError, "type(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - value = arguments[0] - - klass = value.class - - if not [TrueClass, FalseClass, Array, Bignum, Fixnum, Float, Hash, String].include?(klass) - raise(Puppet::ParseError, 'type(): Unknown type') - end - - klass = klass.to_s # Ugly ... + ) do |args| - # We note that Integer is the parent to Bignum and Fixnum ... - result = case klass - when /^(?:Big|Fix)num$/ then 'integer' - when /^(?:True|False)Class$/ then 'boolean' - else klass + warning("type() DEPRECATED: This function will cease to function on Puppet 4; please use type3x() before upgrading to puppet 4 for backwards-compatibility, or migrate to the new parser's typing system.") + if ! Puppet::Parser::Functions.autoloader.loaded?(:type3x) + Puppet::Parser::Functions.autoloader.load(:type3x) end - - if result == "String" then - if value == value.to_i.to_s then - result = "Integer" - elsif value == value.to_f.to_s then - result = "Float" - end - end - - return result.downcase + function_type3x(args + [false]) end end diff --git a/lib/puppet/parser/functions/type3x.rb b/lib/puppet/parser/functions/type3x.rb new file mode 100644 index 0000000..0800b4a --- /dev/null +++ b/lib/puppet/parser/functions/type3x.rb @@ -0,0 +1,51 @@ +# +# type3x.rb +# + +module Puppet::Parser::Functions + newfunction(:type3x, :type => :rvalue, :doc => <<-EOS +DEPRECATED: This function will be removed when puppet 3 support is dropped; please migrate to the new parser's typing system. + +Returns the type when passed a value. Type can be one of: + +* string +* array +* hash +* float +* integer +* boolean + EOS + ) do |args| + raise(Puppet::ParseError, "type3x(): Wrong number of arguments " + + "given (#{args.size} for 1)") if args.size < 1 + + value = args[0] + + klass = value.class + + if not [TrueClass, FalseClass, Array, Bignum, Fixnum, Float, Hash, String].include?(klass) + raise(Puppet::ParseError, 'type3x(): Unknown type') + end + + klass = klass.to_s # Ugly ... + + # We note that Integer is the parent to Bignum and Fixnum ... + result = case klass + when /^(?:Big|Fix)num$/ then 'integer' + when /^(?:True|False)Class$/ then 'boolean' + else klass + end + + if result == "String" then + if value == value.to_i.to_s then + result = "Integer" + elsif value == value.to_f.to_s then + result = "Float" + end + end + + return result.downcase + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3