summaryrefslogtreecommitdiff
path: root/lib/puppet/parser/functions
diff options
context:
space:
mode:
Diffstat (limited to 'lib/puppet/parser/functions')
-rw-r--r--lib/puppet/parser/functions/clamp.rb30
-rw-r--r--lib/puppet/parser/functions/is_absolute_path.rb50
-rw-r--r--lib/puppet/parser/functions/seeded_rand.rb22
-rw-r--r--lib/puppet/parser/functions/validate_absolute_path.rb24
-rw-r--r--lib/puppet/parser/functions/validate_ip_address.rb50
-rw-r--r--lib/puppet/parser/functions/validate_ipv4_address.rb2
6 files changed, 156 insertions, 22 deletions
diff --git a/lib/puppet/parser/functions/clamp.rb b/lib/puppet/parser/functions/clamp.rb
new file mode 100644
index 0000000..432c7c1
--- /dev/null
+++ b/lib/puppet/parser/functions/clamp.rb
@@ -0,0 +1,30 @@
+#
+# clamp.rb
+#
+
+module Puppet::Parser::Functions
+ newfunction(:clamp, :type => :rvalue, :arity => -2, :doc => <<-EOS
+ Clamps value to a range.
+ EOS
+ ) do |args|
+
+ args.flatten!
+
+ raise(Puppet::ParseError, 'clamp(): Wrong number of arguments, ' +
+ 'need three to clamp') if args.size != 3
+
+ # check values out
+ args.each do |value|
+ case [value.class]
+ when [String]
+ raise(Puppet::ParseError, "clamp(): Required explicit numeric (#{value}:String)") unless value =~ /^\d+$/
+ when [Hash]
+ raise(Puppet::ParseError, "clamp(): The Hash type is not allowed (#{value})")
+ end
+ end
+
+ # convert to numeric each element
+ # then sort them and get a middle value
+ args.map{ |n| n.to_i }.sort[1]
+ end
+end
diff --git a/lib/puppet/parser/functions/is_absolute_path.rb b/lib/puppet/parser/functions/is_absolute_path.rb
new file mode 100644
index 0000000..53a5445
--- /dev/null
+++ b/lib/puppet/parser/functions/is_absolute_path.rb
@@ -0,0 +1,50 @@
+module Puppet::Parser::Functions
+ newfunction(:is_absolute_path, :type => :rvalue, :arity => 1, :doc => <<-'ENDHEREDOC') do |args|
+ Returns boolean true if the string represents an absolute path in the filesystem. This function works
+ for windows and unix style paths.
+
+ The following values will return true:
+
+ $my_path = 'C:/Program Files (x86)/Puppet Labs/Puppet'
+ is_absolute_path($my_path)
+ $my_path2 = '/var/lib/puppet'
+ is_absolute_path($my_path2)
+ $my_path3 = ['C:/Program Files (x86)/Puppet Labs/Puppet']
+ is_absolute_path($my_path3)
+ $my_path4 = ['/var/lib/puppet']
+ is_absolute_path($my_path4)
+
+ The following values will return false:
+
+ is_absolute_path(true)
+ is_absolute_path('../var/lib/puppet')
+ is_absolute_path('var/lib/puppet')
+ $undefined = undef
+ is_absolute_path($undefined)
+
+ ENDHEREDOC
+
+ require 'puppet/util'
+
+ path = args[0]
+ # 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
+ value = (Puppet::Util.absolute_path?(path, :posix) or Puppet::Util.absolute_path?(path, :windows))
+ 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!^/!
+ }
+ value = (!!(path =~ regexes[:posix])) || (!!(path =~ regexes[:windows]))
+ end
+ value
+ end
+end \ No newline at end of file
diff --git a/lib/puppet/parser/functions/seeded_rand.rb b/lib/puppet/parser/functions/seeded_rand.rb
new file mode 100644
index 0000000..44e27b8
--- /dev/null
+++ b/lib/puppet/parser/functions/seeded_rand.rb
@@ -0,0 +1,22 @@
+Puppet::Parser::Functions::newfunction(
+ :seeded_rand,
+ :arity => 2,
+ :type => :rvalue,
+ :doc => <<-EOS
+Usage: `seeded_rand(MAX, SEED)`. MAX must be a positive integer; SEED is any string.
+
+Generates a random whole number greater than or equal to 0 and less
+than MAX, using the value of SEED for repeatable randomness. If SEED
+starts with "$fqdn:", this is behaves the same as `fqdn_rand`.
+
+EOS
+) do |args|
+ require 'digest/md5'
+
+ raise(ArgumentError, "seeded_rand(): first argument must be a positive integer") unless function_is_integer([args[0]]) and args[0].to_i > 0
+ raise(ArgumentError, "seeded_rand(): second argument must be a string") unless args[1].is_a? String
+
+ max = args[0].to_i
+ seed = Digest::MD5.hexdigest(args[1]).hex
+ Puppet::Util.deterministic_rand(seed,max)
+end
diff --git a/lib/puppet/parser/functions/validate_absolute_path.rb b/lib/puppet/parser/functions/validate_absolute_path.rb
index b696680..5f85f72 100644
--- a/lib/puppet/parser/functions/validate_absolute_path.rb
+++ b/lib/puppet/parser/functions/validate_absolute_path.rb
@@ -40,28 +40,10 @@ module Puppet::Parser::Functions
unless arg.is_a?(Array) then
candidates = Array.new(1,arg)
end
- # iterate over all pathes within the candidates array
+ # iterate over all paths 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.")
+ unless function_is_absolute_path([path])
+ raise Puppet::ParseError, ("#{path.inspect} is not an absolute path.")
end
end
end
diff --git a/lib/puppet/parser/functions/validate_ip_address.rb b/lib/puppet/parser/functions/validate_ip_address.rb
new file mode 100644
index 0000000..c0baf82
--- /dev/null
+++ b/lib/puppet/parser/functions/validate_ip_address.rb
@@ -0,0 +1,50 @@
+module Puppet::Parser::Functions
+
+ newfunction(:validate_ip_address, :doc => <<-ENDHEREDOC
+ Validate that all values passed are valid IP addresses,
+ regardless they are IPv4 or IPv6
+ Fail compilation if any value fails this check.
+ The following values will pass:
+ $my_ip = "1.2.3.4"
+ validate_ip_address($my_ip)
+ validate_bool("8.8.8.8", "172.16.0.1", $my_ip)
+
+ $my_ip = "3ffe:505:2"
+ validate_ip_address(1)
+ validate_ip_address($my_ip)
+ validate_bool("fe80::baf6:b1ff:fe19:7507", $my_ip)
+
+ The following values will fail, causing compilation to abort:
+ $some_array = [ 1, true, false, "garbage string", "3ffe:505:2" ]
+ validate_ip_address($some_array)
+ ENDHEREDOC
+ ) do |args|
+
+ require "ipaddr"
+ rescuable_exceptions = [ ArgumentError ]
+
+ if defined?(IPAddr::InvalidAddressError)
+ rescuable_exceptions << IPAddr::InvalidAddressError
+ end
+
+ unless args.length > 0 then
+ raise Puppet::ParseError, ("validate_ip_address(): wrong number of arguments (#{args.length}; must be > 0)")
+ end
+
+ args.each do |arg|
+ unless arg.is_a?(String)
+ raise Puppet::ParseError, "#{arg.inspect} is not a string."
+ end
+
+ begin
+ unless IPAddr.new(arg).ipv4? or IPAddr.new(arg).ipv6?
+ raise Puppet::ParseError, "#{arg.inspect} is not a valid IP address."
+ end
+ rescue *rescuable_exceptions
+ raise Puppet::ParseError, "#{arg.inspect} is not a valid IP address."
+ end
+ end
+
+ end
+
+end
diff --git a/lib/puppet/parser/functions/validate_ipv4_address.rb b/lib/puppet/parser/functions/validate_ipv4_address.rb
index fc02748..97faa57 100644
--- a/lib/puppet/parser/functions/validate_ipv4_address.rb
+++ b/lib/puppet/parser/functions/validate_ipv4_address.rb
@@ -8,7 +8,7 @@ module Puppet::Parser::Functions
$my_ip = "1.2.3.4"
validate_ipv4_address($my_ip)
- validate_bool("8.8.8.8", "172.16.0.1", $my_ip)
+ validate_ipv4_address("8.8.8.8", "172.16.0.1", $my_ip)
The following values will fail, causing compilation to abort: