From 76d0ec471d074a07cc2cece3963dd9c8c2d18aeb Mon Sep 17 00:00:00 2001 From: Emerson Prado Date: Fri, 3 Feb 2017 17:00:03 -0200 Subject: Include routine to converge ensure values 'present' and 'installed' If user declares ensure_package concurrently with ensure values 'present' and 'installed', function fails as if values were different Change causes function to interpret ensure => 'installed' as 'present', effectively elliminating the error Also works if user doesn't specify ensure value, since 'present' is the default --- lib/puppet/parser/functions/ensure_packages.rb | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/ensure_packages.rb b/lib/puppet/parser/functions/ensure_packages.rb index 439af1e..93dd4fb 100644 --- a/lib/puppet/parser/functions/ensure_packages.rb +++ b/lib/puppet/parser/functions/ensure_packages.rb @@ -20,6 +20,9 @@ third argument to the ensure_resource() function. if arguments[0].is_a?(Hash) if arguments[1] defaults = { 'ensure' => 'present' }.merge(arguments[1]) + if defaults['ensure'] == 'installed' + defaults['ensure'] = 'present' + end else defaults = { 'ensure' => 'present' } end @@ -31,6 +34,9 @@ third argument to the ensure_resource() function. if arguments[1] defaults = { 'ensure' => 'present' }.merge(arguments[1]) + if defaults['ensure'] == 'installed' + defaults['ensure'] = 'present' + end else defaults = { 'ensure' => 'present' } end -- cgit v1.2.3 From ec6cd56576953c74b48ebbba477d89e138e91ace Mon Sep 17 00:00:00 2001 From: Reid Vandewiele Date: Wed, 28 Jun 2017 13:37:55 -0700 Subject: (MODULES-5113) Make line support Sensitive Do this by making it a property. The idea is to get rid of the warning that appears when a Sensitive value is passed to the line attribute. --- lib/puppet/type/file_line.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'lib/puppet') diff --git a/lib/puppet/type/file_line.rb b/lib/puppet/type/file_line.rb index e82b246..3d691bf 100644 --- a/lib/puppet/type/file_line.rb +++ b/lib/puppet/type/file_line.rb @@ -104,8 +104,16 @@ Puppet::Type.newtype(:file_line) do ' This is also takes a regex.' end - newparam(:line) do + # The line property never changes; the type only ever performs a create() or + # destroy(). line is a property in order to allow it to correctly handle + # Sensitive type values. Because it is a property which will never change, + # it should never be considered out of sync. + newproperty(:line) do desc 'The line to be appended to the file or used to replace matches found by the match attribute.' + + def retrieve + @resource[:line] + end end newparam(:path) do -- cgit v1.2.3 From 740ca7dc8053be93e43392ca61b2f308c0596d19 Mon Sep 17 00:00:00 2001 From: Reid Vandewiele Date: Fri, 7 Apr 2017 15:13:59 -0700 Subject: (FACT-932) Add new function, fact() The fact() function allows dot-notation reference to facts. It is an alternative to using $facts directly with array-indexing. Array-indexing is often onerous to use since it doesn't align with how structured facts are accessed elsewhere in the ecosystem and if any element in a multi-step path doesn't exist, array indexing can cause a compilation failure. Example usage: fact('os.family') --- lib/puppet/functions/fact.rb | 58 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 lib/puppet/functions/fact.rb (limited to 'lib/puppet') diff --git a/lib/puppet/functions/fact.rb b/lib/puppet/functions/fact.rb new file mode 100644 index 0000000..dfb048b --- /dev/null +++ b/lib/puppet/functions/fact.rb @@ -0,0 +1,58 @@ +# Digs into the facts hash using dot-notation +# +# Example usage: +# +# fact('osfamily') +# fact('os.architecture') +# +# Array indexing: +# +# fact('mountpoints."/dev".options.1') +# +# Fact containing a "." in the name: +# +# fact('vmware."VRA.version"') +# +Puppet::Functions.create_function(:fact) do + dispatch :fact do + param 'String', :fact_name + end + + def to_dot_syntax(array_path) + array_path.map do |string| + string.include?('.') ? %Q{"#{string}"} : string + end.join('.') + end + + def fact(fact_name) + facts = closure_scope['facts'] + + # Transform the dot-notation string into an array of paths to walk. Make + # sure to correctly extract double-quoted values containing dots as single + # elements in the path. + path = fact_name.scan(/([^."]+)|(?:")([^"]+)(?:")/).map {|x| x.compact.first } + + walked_path = [] + path.reduce(facts) do |d, k| + return nil if d.nil? || k.nil? + + case + when d.is_a?(Array) + begin + result = d[Integer(k)] + rescue ArgumentError => e + Puppet.warning("fact request for #{fact_name} returning nil: '#{to_dot_syntax(walked_path)}' is an array; cannot index to '#{k}'") + result = nil + end + when d.is_a?(Hash) + result = d[k] + else + Puppet.warning("fact request for #{fact_name} returning nil: '#{to_dot_syntax(walked_path)}' is not a collection; cannot walk to '#{k}'") + result = nil + end + + walked_path << k + result + end + end +end -- cgit v1.2.3 From 409a974095a3f5b637e091494b5d14b451c5de78 Mon Sep 17 00:00:00 2001 From: Reid Vandewiele Date: Fri, 30 Jun 2017 14:01:41 -0700 Subject: (FACT-932) Allow use of fact() on other hashes Because sometimes people want to use an alternative data set, but treat it like it's a set of facts. --- lib/puppet/functions/fact.rb | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'lib/puppet') diff --git a/lib/puppet/functions/fact.rb b/lib/puppet/functions/fact.rb index dfb048b..48d0d3c 100644 --- a/lib/puppet/functions/fact.rb +++ b/lib/puppet/functions/fact.rb @@ -18,22 +18,27 @@ Puppet::Functions.create_function(:fact) do param 'String', :fact_name end - def to_dot_syntax(array_path) - array_path.map do |string| - string.include?('.') ? %Q{"#{string}"} : string - end.join('.') + dispatch :alternative do + param 'Hash', :fact_hash + param 'String', :fact_name end def fact(fact_name) - facts = closure_scope['facts'] + dot_dig(closure_scope['facts'], fact_name) + end + + def alternative(alternative_hash, fact_name) + dot_dig(alternative_hash, fact_name) + end + def dot_dig(fact_hash, fact_name) # Transform the dot-notation string into an array of paths to walk. Make # sure to correctly extract double-quoted values containing dots as single # elements in the path. path = fact_name.scan(/([^."]+)|(?:")([^"]+)(?:")/).map {|x| x.compact.first } walked_path = [] - path.reduce(facts) do |d, k| + path.reduce(fact_hash) do |d, k| return nil if d.nil? || k.nil? case @@ -55,4 +60,10 @@ Puppet::Functions.create_function(:fact) do result end end + + def to_dot_syntax(array_path) + array_path.map do |string| + string.include?('.') ? %Q{"#{string}"} : string + end.join('.') + end end -- cgit v1.2.3 From 58131d8ff1886352e964aafef58ffeb65c5d2cdd Mon Sep 17 00:00:00 2001 From: tphoney Date: Mon, 3 Jul 2017 17:08:09 +0100 Subject: (MODULES-5003) file_line fix all broken lines --- lib/puppet/provider/file_line/ruby.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'lib/puppet') diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb index 42f287a..1d27424 100644 --- a/lib/puppet/provider/file_line/ruby.rb +++ b/lib/puppet/provider/file_line/ruby.rb @@ -1,16 +1,21 @@ Puppet::Type.type(:file_line).provide(:ruby) do def exists? + found = true if resource[:replace].to_s != 'true' and count_matches(match_regex) > 0 - true + found = true else lines.find do |line| if resource[:ensure].to_s == 'absent' and resource[:match_for_absence].to_s == 'true' - line.chomp =~ Regexp.new(resource[:match]) + found = line.chomp =~ Regexp.new(resource[:match]) else - line.chomp == resource[:line].chomp + found = line.chomp == resource[:line].chomp + end + if found == false then + break end end end + found end def create -- cgit v1.2.3 From 0f35700487368357adec8a535b5c50437b208264 Mon Sep 17 00:00:00 2001 From: Reid Vandewiele Date: Mon, 3 Jul 2017 11:33:15 -0700 Subject: Revert "Allow use of fact() on other hashes" This reverts commit 409a974095a3f5b637e091494b5d14b451c5de78. After thinking about this, use case, and function naming, I think it's probably best for now to keep things simple and let `fact()` be single-purpose for looking up facts with no potentially confusing extensions. The only use case where the name makes sense is where it's being used on the `$facts` hash, and I think we'd rather in that circumstance promote the raw use of `fact()`. --- lib/puppet/functions/fact.rb | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) (limited to 'lib/puppet') diff --git a/lib/puppet/functions/fact.rb b/lib/puppet/functions/fact.rb index 48d0d3c..dfb048b 100644 --- a/lib/puppet/functions/fact.rb +++ b/lib/puppet/functions/fact.rb @@ -18,27 +18,22 @@ Puppet::Functions.create_function(:fact) do param 'String', :fact_name end - dispatch :alternative do - param 'Hash', :fact_hash - param 'String', :fact_name + def to_dot_syntax(array_path) + array_path.map do |string| + string.include?('.') ? %Q{"#{string}"} : string + end.join('.') end def fact(fact_name) - dot_dig(closure_scope['facts'], fact_name) - end - - def alternative(alternative_hash, fact_name) - dot_dig(alternative_hash, fact_name) - end + facts = closure_scope['facts'] - def dot_dig(fact_hash, fact_name) # Transform the dot-notation string into an array of paths to walk. Make # sure to correctly extract double-quoted values containing dots as single # elements in the path. path = fact_name.scan(/([^."]+)|(?:")([^"]+)(?:")/).map {|x| x.compact.first } walked_path = [] - path.reduce(fact_hash) do |d, k| + path.reduce(facts) do |d, k| return nil if d.nil? || k.nil? case @@ -60,10 +55,4 @@ Puppet::Functions.create_function(:fact) do result end end - - def to_dot_syntax(array_path) - array_path.map do |string| - string.include?('.') ? %Q{"#{string}"} : string - end.join('.') - end end -- cgit v1.2.3 From fe7ccd8b89556cc6cc1f5ea7f58b5ac2aedb23ec Mon Sep 17 00:00:00 2001 From: Frank de Jong Date: Sat, 8 Jul 2017 09:43:40 +0200 Subject: Add validate_domain_name function --- .../parser/functions/validate_domain_name.rb | 39 ++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 lib/puppet/parser/functions/validate_domain_name.rb (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/validate_domain_name.rb b/lib/puppet/parser/functions/validate_domain_name.rb new file mode 100644 index 0000000..c3fad78 --- /dev/null +++ b/lib/puppet/parser/functions/validate_domain_name.rb @@ -0,0 +1,39 @@ +module Puppet::Parser::Functions + newfunction(:validate_domain_name, :doc => <<-ENDHEREDOC + Validate that all values passed are syntactically correct domain names. + Fail compilation if any value fails this check. + + The following values will pass: + + $my_domain_name = 'server.domain.tld' + validate_domain_name($my_domain_name) + validate_domain_name('domain.tld', 'puppet.com', $my_domain_name) + + The following values will fail, causing compilation to abort: + + validate_domain_name(1) + validate_domain_name(true) + validate_domain_name('invalid domain') + validate_domain_name('-foo.example.com') + validate_domain_name('www.example.2com') + + ENDHEREDOC + ) do |args| + + rescuable_exceptions = [ArgumentError] + + if args.empty? + raise Puppet::ParseError, "validate_domain_name(): wrong number of arguments (#{args.length}; must be > 0)" + end + + args.each do |arg| + raise Puppet::ParseError, "#{arg.inspect} is not a string." unless arg.is_a?(String) + + begin + raise Puppet::ParseError, "#{arg.inspect} is not a syntactically correct domain name" unless function_is_domain_name([arg]) + rescue *rescuable_exceptions + raise Puppet::ParseError, "#{arg.inspect} is not a syntactically correct domain name" + end + end + end +end -- cgit v1.2.3 From 81d7d35fd78917ee1a5e66cb7459ea341bc452ea Mon Sep 17 00:00:00 2001 From: tkishel Date: Thu, 13 Jul 2017 15:16:04 -0700 Subject: (MODULES-5003) file_line_does_not_change_multiple_lines_when_one_matches The exists? method is called to determine the need to call the create and destroy methods. When the ensure, match, and multiple attributes are defined, the exists? method needs to return false unless all the lines that match the match attribute equal the line attribute. The first commit is minimal, and implements this change. The second commit normalizes the code, which I needed for comprehension. --- lib/puppet/provider/file_line/ruby.rb | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) (limited to 'lib/puppet') diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb index 1d27424..aa99986 100644 --- a/lib/puppet/provider/file_line/ruby.rb +++ b/lib/puppet/provider/file_line/ruby.rb @@ -1,18 +1,21 @@ Puppet::Type.type(:file_line).provide(:ruby) do def exists? - found = true - if resource[:replace].to_s != 'true' and count_matches(match_regex) > 0 - found = true + found = false + lines_count = 0 + lines.each do |line| + found = line.chomp == resource[:line] + if found + lines_count += 1 + end + end + if resource[:match] == nil + found = lines_count > 0 else - lines.find do |line| - if resource[:ensure].to_s == 'absent' and resource[:match_for_absence].to_s == 'true' - found = line.chomp =~ Regexp.new(resource[:match]) - else - found = line.chomp == resource[:line].chomp - end - if found == false then - break - end + match_count = count_matches(match_regex) + if resource[:replace].to_s == 'true' + found = lines_count > 0 && lines_count == match_count + else + found = match_count > 0 end end found -- cgit v1.2.3 From 4d764068ffe456366a9c823e15581e9a9649898e Mon Sep 17 00:00:00 2001 From: tkishel Date: Thu, 13 Jul 2017 15:30:54 -0700 Subject: (MODULES-5003) file_line_does_not_change_multiple_lines_when_one_matches The exists? method is called to determine the need to call the create and destroy methods. When the ensure, match, and multiple attributes are defined, the exists? method needs to return false unless all the lines that match the match attribute equal the line attribute. The first commit is minimal, and implements this change. The second commit normalizes the code, which I needed for comprehension. --- lib/puppet/provider/file_line/ruby.rb | 79 ++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 38 deletions(-) (limited to 'lib/puppet') diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb index aa99986..2f6c8ef 100644 --- a/lib/puppet/provider/file_line/ruby.rb +++ b/lib/puppet/provider/file_line/ruby.rb @@ -11,7 +11,7 @@ Puppet::Type.type(:file_line).provide(:ruby) do if resource[:match] == nil found = lines_count > 0 else - match_count = count_matches(match_regex) + match_count = count_matches(new_match_regex) if resource[:replace].to_s == 'true' found = lines_count > 0 && lines_count == match_count else @@ -22,19 +22,19 @@ Puppet::Type.type(:file_line).provide(:ruby) do end def create - unless resource[:replace].to_s != 'true' and count_matches(match_regex) > 0 + unless resource[:replace].to_s != 'true' && count_matches(new_match_regex) > 0 if resource[:match] handle_create_with_match elsif resource[:after] handle_create_with_after else - append_line + handle_append_line end end end def destroy - if resource[:match_for_absence].to_s == 'true' and resource[:match] + if resource[:match_for_absence].to_s == 'true' && resource[:match] handle_destroy_with_match else handle_destroy_line @@ -42,6 +42,7 @@ Puppet::Type.type(:file_line).provide(:ruby) do end private + def lines # If this type is ever used with very large files, we should # write this in a different way, using a temp @@ -56,25 +57,34 @@ Puppet::Type.type(:file_line).provide(:ruby) do end end - def match_regex + def new_after_regex + resource[:after] ? Regexp.new(resource[:after]) : nil + end + + def new_match_regex resource[:match] ? Regexp.new(resource[:match]) : nil end + def count_matches(regex) + lines.select{ |line| line.match(regex) }.size + end + def handle_create_with_match() - regex_after = resource[:after] ? Regexp.new(resource[:after]) : nil - match_count = count_matches(match_regex) + after_regex = new_after_regex + match_regex = new_match_regex + match_count = count_matches(new_match_regex) if match_count > 1 && resource[:multiple].to_s != 'true' raise Puppet::Error, "More than one line in file '#{resource[:path]}' matches pattern '#{resource[:match]}'" end File.open(resource[:path], 'w') do |fh| - lines.each do |l| - fh.puts(match_regex.match(l) ? resource[:line] : l) - if (match_count == 0 and regex_after) - if regex_after.match(l) + lines.each do |line| + fh.puts(match_regex.match(line) ? resource[:line] : line) + if match_count == 0 && after_regex + if after_regex.match(line) fh.puts(resource[:line]) - match_count += 1 #Increment match_count to indicate that the new line has been inserted. + match_count += 1 # Increment match_count to indicate that the new line has been inserted. end end end @@ -86,32 +96,29 @@ Puppet::Type.type(:file_line).provide(:ruby) do end def handle_create_with_after - regex = Regexp.new(resource[:after]) - count = count_matches(regex) + after_regex = new_after_regex + after_count = count_matches(after_regex) - if count > 1 && resource[:multiple].to_s != 'true' - raise Puppet::Error, "#{count} lines match pattern '#{resource[:after]}' in file '#{resource[:path]}'. One or no line must match the pattern." + if after_count > 1 && resource[:multiple].to_s != 'true' + raise Puppet::Error, "#{after_count} lines match pattern '#{resource[:after]}' in file '#{resource[:path]}'. One or no line must match the pattern." end - File.open(resource[:path], 'w') do |fh| - lines.each do |l| - fh.puts(l) - if regex.match(l) then + File.open(resource[:path],'w') do |fh| + lines.each do |line| + fh.puts(line) + if after_regex.match(line) fh.puts(resource[:line]) end end - end - if (count == 0) # append the line to the end of the file - append_line + if (after_count == 0) + fh.puts(resource[:line]) + end end end - def count_matches(regex) - lines.select{|l| l.match(regex)}.size - end - def handle_destroy_with_match + match_regex = new_match_regex match_count = count_matches(match_regex) if match_count > 1 && resource[:multiple].to_s != 'true' raise Puppet::Error, "More than one line in file '#{resource[:path]}' matches pattern '#{resource[:match]}'" @@ -119,27 +126,23 @@ Puppet::Type.type(:file_line).provide(:ruby) do local_lines = lines File.open(resource[:path],'w') do |fh| - fh.write(local_lines.reject{|l| match_regex.match(l) }.join('')) + fh.write(local_lines.reject{ |line| match_regex.match(line) }.join('')) end end def handle_destroy_line local_lines = lines File.open(resource[:path],'w') do |fh| - fh.write(local_lines.reject{|l| l.chomp == resource[:line] }.join('')) + fh.write(local_lines.reject{ |line| line.chomp == resource[:line] }.join('')) end end - ## - # append the line to the file. - # - # @api private - def append_line - File.open(resource[:path], 'w') do |fh| - lines.each do |l| - fh.puts(l) + def handle_append_line + File.open(resource[:path],'w') do |fh| + lines.each do |line| + fh.puts(line) end - fh.puts resource[:line] + fh.puts(resource[:line]) end end end -- cgit v1.2.3 From 33922a4ec7a8c204a17e0c3017eea21faa220f39 Mon Sep 17 00:00:00 2001 From: tphoney Date: Fri, 14 Jul 2017 10:14:04 +0100 Subject: FM-6239 clean up for puppet 5.tests/deprecation --- lib/puppet/parser/functions/unique.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/unique.rb b/lib/puppet/parser/functions/unique.rb index b57431d..1e2a895 100644 --- a/lib/puppet/parser/functions/unique.rb +++ b/lib/puppet/parser/functions/unique.rb @@ -24,6 +24,10 @@ This returns: EOS ) do |arguments| + if Puppet::Util::Package.versioncmp(Puppet.version, '5.0.0') >= 0 + function_deprecation([:unique, 'This method is deprecated, please use the core puppet unique function. There is further documentation for the function in the release notes of Puppet 5.0.']) + end + raise(Puppet::ParseError, "unique(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] -- cgit v1.2.3 From 42d4ea7af9197f77a3062727eb166c719ca6296a Mon Sep 17 00:00:00 2001 From: Eric Putnam Date: Fri, 7 Jul 2017 17:30:17 -0700 Subject: (MODULES-4908) adds support for sensitive data type to pw_hash Also includes a small fix to integer_spec so tests pass. --- lib/puppet/parser/functions/pw_hash.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/pw_hash.rb b/lib/puppet/parser/functions/pw_hash.rb index d99ee5b..0deeb3a 100644 --- a/lib/puppet/parser/functions/pw_hash.rb +++ b/lib/puppet/parser/functions/pw_hash.rb @@ -27,6 +27,13 @@ Puppet::Parser::Functions::newfunction( environment contains several different operating systems, ensure that they are compatible before using this function.") do |args| raise ArgumentError, "pw_hash(): wrong number of arguments (#{args.size} for 3)" if args.size != 3 + args.map! do |arg| + if arg.is_a? Puppet::Pops::Types::PSensitiveType::Sensitive + arg.unwrap + else + arg + end + end raise ArgumentError, "pw_hash(): first argument must be a string" unless args[0].is_a? String or args[0].nil? raise ArgumentError, "pw_hash(): second argument must be a string" unless args[1].is_a? String hashes = { 'md5' => '1', -- cgit v1.2.3 From 703fa777e1daee97e2aea529064d773662ef1fe0 Mon Sep 17 00:00:00 2001 From: "asif.shaikh" Date: Wed, 1 Feb 2017 20:14:27 -0800 Subject: Add new file_line option append_on_no_match --- lib/puppet/provider/file_line/ruby.rb | 4 +++- lib/puppet/type/file_line.rb | 8 +++++++- 2 files changed, 10 insertions(+), 2 deletions(-) (limited to 'lib/puppet') diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb index 2f6c8ef..16f2709 100644 --- a/lib/puppet/provider/file_line/ruby.rb +++ b/lib/puppet/provider/file_line/ruby.rb @@ -12,7 +12,9 @@ Puppet::Type.type(:file_line).provide(:ruby) do found = lines_count > 0 else match_count = count_matches(new_match_regex) - if resource[:replace].to_s == 'true' + if resource[:append_on_no_match].to_s == 'false' + found = true + elsif resource[:replace].to_s == 'true' found = lines_count > 0 && lines_count == match_count else found = match_count > 0 diff --git a/lib/puppet/type/file_line.rb b/lib/puppet/type/file_line.rb index 3d691bf..b2357b8 100644 --- a/lib/puppet/type/file_line.rb +++ b/lib/puppet/type/file_line.rb @@ -58,7 +58,7 @@ Puppet::Type.newtype(:file_line) do encoding => "iso-8859-1", } - Files with special characters that are not valid UTF-8 will give the + Files with special characters that are not valid UTF-8 will give the error message "invalid byte sequence in UTF-8". In this case, determine the correct file encoding and specify the correct encoding using the encoding attribute, the value of which needs to be a valid Ruby character @@ -136,6 +136,12 @@ Puppet::Type.newtype(:file_line) do defaultto 'UTF-8' end + newparam(:append_on_no_match) do + desc 'If true, append line if match is not found. If false, do not append line if a match is not found' + newvalues(true, false) + defaultto true + end + # Autorequire the file resource if it's being managed autorequire(:file) do self[:path] -- cgit v1.2.3 From 772a2d2f406b65ef65161a04455865b37a1c8456 Mon Sep 17 00:00:00 2001 From: Eric Putnam Date: Thu, 27 Jul 2017 09:24:52 -0700 Subject: (maint) move/rewrite round() as ruby function --- lib/puppet/parser/functions/round.rb | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 lib/puppet/parser/functions/round.rb (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/round.rb b/lib/puppet/parser/functions/round.rb new file mode 100644 index 0000000..489c301 --- /dev/null +++ b/lib/puppet/parser/functions/round.rb @@ -0,0 +1,33 @@ +# +# round.rb +# + +module Puppet::Parser::Functions + newfunction(:round, :type => :rvalue, :doc => <<-EOS + Rounds a number to the nearest integer + + *Examples:* + + round(2.9) + + returns: 3 + + round(2.4) + + returns: 2 + + EOS + ) do |args| + + raise Puppet::ParseError, "round(): Wrong number of arguments given #{args.size} for 1" if args.size != 1 + raise Puppet::ParseError, "round(): Expected a Numeric, got #{args[0].class}" unless args[0].is_a? Numeric + + value = args[0] + + if value >= 0 + Integer(value + 0.5) + else + Integer(value - 0.5) + end + end +end -- cgit v1.2.3