aboutsummaryrefslogtreecommitdiff
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/array_del.rb11
-rw-r--r--lib/puppet/parser/functions/gsub.rb14
-rw-r--r--lib/puppet/parser/functions/hostname.rb13
-rw-r--r--lib/puppet/parser/functions/join.rb10
-rw-r--r--lib/puppet/parser/functions/multi_source_template.rb29
-rw-r--r--lib/puppet/parser/functions/prefix_with.rb14
-rw-r--r--lib/puppet/parser/functions/slash_escape.rb7
-rw-r--r--lib/puppet/parser/functions/strlength.rb6
-rw-r--r--lib/puppet/parser/functions/substitute.rb20
-rw-r--r--lib/puppet/parser/functions/uniq_flatten.rb10
10 files changed, 113 insertions, 21 deletions
diff --git a/lib/puppet/parser/functions/array_del.rb b/lib/puppet/parser/functions/array_del.rb
new file mode 100644
index 0000000..e604916
--- /dev/null
+++ b/lib/puppet/parser/functions/array_del.rb
@@ -0,0 +1,11 @@
+Puppet::Parser::Functions::newfunction(
+ :array_del,
+ :type => :rvalue,
+ :doc => "Deletes items from an array
+
+ Example: array_del(['a','b'],'b') -> ['a']"
+) do |args|
+ raise Puppet::ParseError, 'array_del() needs two arguments' if args.length != 2
+ (res=args[0].dup).to_a.delete(args[1])
+ res
+end
diff --git a/lib/puppet/parser/functions/gsub.rb b/lib/puppet/parser/functions/gsub.rb
index 27e6192..e2410ff 100644
--- a/lib/puppet/parser/functions/gsub.rb
+++ b/lib/puppet/parser/functions/gsub.rb
@@ -1,13 +1,9 @@
-#
-# A thin wrapper around the ruby gsub function.
-#
-# gsub($string, $pattern, $replacement)
-#
-# will replace all occurrences of $pattern in $string with $replacement.
-# $string can be either a single value or an array. In the latter case, each
-# element of the array will be processed in turn.
-#
module Puppet::Parser::Functions
+ # thin wrapper around the ruby gsub function
+ # gsub($string, $pattern, $replacement) will replace all occurrences of
+ # $pattern in $string with $replacement. $string can be either a singel
+ # value or an array. In the latter case, each element of the array will
+ # be processed in turn.
newfunction(:gsub, :type => :rvalue) do |args|
if args[0].is_a?(Array)
args[0].collect do |val|
diff --git a/lib/puppet/parser/functions/hostname.rb b/lib/puppet/parser/functions/hostname.rb
new file mode 100644
index 0000000..7bc477f
--- /dev/null
+++ b/lib/puppet/parser/functions/hostname.rb
@@ -0,0 +1,13 @@
+# get an uniq array of ipaddresses for a hostname
+require 'resolv'
+
+module Puppet::Parser::Functions
+ newfunction(:hostname, :type => :rvalue) do |args|
+ res = Array.new
+ Resolv::DNS.new.each_address(args[0]){ |addr|
+ res << addr
+ }
+ res.uniq
+ end
+end
+
diff --git a/lib/puppet/parser/functions/join.rb b/lib/puppet/parser/functions/join.rb
new file mode 100644
index 0000000..95b664c
--- /dev/null
+++ b/lib/puppet/parser/functions/join.rb
@@ -0,0 +1,10 @@
+Puppet::Parser::Functions::newfunction(
+ :join,
+ :type => :rvalue,
+ :doc => "Joins the values of the array in arg1 with the string in arg2
+
+ Example: join(['a','b'],',') -> 'a,b'"
+) do |args|
+ raise Puppet::ParseError, 'join() needs two arguments' if args.length != 2
+ args[0].to_a.join(args[1])
+end
diff --git a/lib/puppet/parser/functions/multi_source_template.rb b/lib/puppet/parser/functions/multi_source_template.rb
new file mode 100644
index 0000000..e075320
--- /dev/null
+++ b/lib/puppet/parser/functions/multi_source_template.rb
@@ -0,0 +1,29 @@
+module Puppet::Parser::Functions
+ require 'erb'
+
+ newfunction(:multi_source_template, :type => :rvalue) do |args|
+ contents = nil
+ environment = compiler.environment
+ sources = args
+
+ sources.each do |file|
+ Puppet.debug("Looking for #{file} in #{environment}")
+ if filename = Puppet::Parser::Files.find_template(file, environment.to_s)
+ wrapper = Puppet::Parser::TemplateWrapper.new(self)
+ wrapper.file = file
+
+ begin
+ contents = wrapper.result
+ rescue => detail
+ raise Puppet::ParseError, "Failed to parse template %s: %s" % [file, detail]
+ end
+
+ break
+ end
+ end
+
+ raise Puppet::ParseError, "multi_source_template: No match found for files: #{sources.join(', ')}" if contents == nil
+
+ contents
+ end
+end
diff --git a/lib/puppet/parser/functions/prefix_with.rb b/lib/puppet/parser/functions/prefix_with.rb
index 5a12986..6e64a4a 100644
--- a/lib/puppet/parser/functions/prefix_with.rb
+++ b/lib/puppet/parser/functions/prefix_with.rb
@@ -1,15 +1,5 @@
-# Prefixes arguments 2..n with first argument.
-#
-# prefix_with(string prefix, string[] arguments) : string[]
-#
-# Example:
-#
-# prefix_with("php-", [ "blah", "foo" ])
-#
-# will result in this array:
-#
-# [ "php-blah", "php-foo" ]
-#
+# prefix arguments 2..n with first argument
+
module Puppet::Parser::Functions
newfunction(:prefix_with, :type => :rvalue) do |args|
prefix = args.shift
diff --git a/lib/puppet/parser/functions/slash_escape.rb b/lib/puppet/parser/functions/slash_escape.rb
new file mode 100644
index 0000000..04d3b95
--- /dev/null
+++ b/lib/puppet/parser/functions/slash_escape.rb
@@ -0,0 +1,7 @@
+# escape slashes in a String
+module Puppet::Parser::Functions
+ newfunction(:slash_escape, :type => :rvalue) do |args|
+ args[0].gsub(/\//, '\\/')
+ end
+end
+
diff --git a/lib/puppet/parser/functions/strlength.rb b/lib/puppet/parser/functions/strlength.rb
new file mode 100644
index 0000000..147b24a
--- /dev/null
+++ b/lib/puppet/parser/functions/strlength.rb
@@ -0,0 +1,6 @@
+module Puppet::Parser::Functions
+ newfunction(:strlength, :type => :rvalue) do |args|
+ args[0].to_s.length
+ end
+end
+
diff --git a/lib/puppet/parser/functions/substitute.rb b/lib/puppet/parser/functions/substitute.rb
new file mode 100644
index 0000000..4c97def
--- /dev/null
+++ b/lib/puppet/parser/functions/substitute.rb
@@ -0,0 +1,20 @@
+# subsititute($string, $regex, $replacement) : $string
+# subsititute($string[], $regex, $replacement) : $string[]
+#
+# Replace all ocurrences of $regex in $string by $replacement.
+# $regex is interpreted as Ruby regular expression.
+#
+# For long-term portability it is recommended to refrain from using Ruby's
+# extended RE features.
+module Puppet::Parser::Functions
+ newfunction(:substitute, :type => :rvalue) do |args|
+ if args[0].is_a?(Array)
+ args[0].collect do |val|
+ val.gsub(/#{args[1]}/, args[2])
+ end
+ else
+ args[0].gsub(/#{args[1]}/, args[2])
+ end
+ end
+end
+
diff --git a/lib/puppet/parser/functions/uniq_flatten.rb b/lib/puppet/parser/functions/uniq_flatten.rb
new file mode 100644
index 0000000..4841c4d
--- /dev/null
+++ b/lib/puppet/parser/functions/uniq_flatten.rb
@@ -0,0 +1,10 @@
+Puppet::Parser::Functions::newfunction(
+ :uniq_flatten,
+ :type => :rvalue,
+ :doc => "Flattens an array and make it uniq
+
+ Example: uniq_flatten([['a','b'],'a']) -> ['a','b']"
+) do |args|
+ raise Puppet::ParseError, 'uniq_flatten() needs one arguments' if args.length != 1
+ args[0].to_a.flatten.collect(&:to_s).uniq
+end