summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG3
-rw-r--r--lib/puppet/parser/functions.rb35
-rwxr-xr-xtest/language/functions.rb47
3 files changed, 85 insertions, 0 deletions
diff --git a/CHANGELOG b/CHANGELOG
index 37b076cc1..4b7ad9ddd 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,4 +1,7 @@
0.22.2 (grover)
+ Added a generate() command, which sets values to the result of an external
+ command. (#541)
+
Added a file() command to read in files with no interpolation. The first
found file has its content returned.
diff --git a/lib/puppet/parser/functions.rb b/lib/puppet/parser/functions.rb
index cef694b85..b02ea4308 100644
--- a/lib/puppet/parser/functions.rb
+++ b/lib/puppet/parser/functions.rb
@@ -252,6 +252,41 @@ module Functions
vals.join(", ")
end
end
+
+ newfunction(:generate, :type => :rvalue,
+ :doc => "Calls an external command and returns the results of the
+ command. Any arguments are passed to the external command as
+ arguments. If the generator does not exit with return code of 0,
+ the generator is considered to have failed and a parse error is
+ thrown. Generators can only have file separators, alphanumerics, dashes,
+ and periods in them. This function will attempt to protect you from
+ malicious generator calls (e.g., those with '..' in them), but it can
+ never be entirely safe. No subshell is used to execute
+ generators, so all shell metacharacters are passed directly to
+ the generator.") do |args|
+
+ unless args[0] =~ /^#{File::SEPARATOR}/
+ raise Puppet::ParseError, "Generators must be fully qualified"
+ end
+
+ unless args[0] =~ /^[-#{File::SEPARATOR}\w.]+$/
+ raise Puppet::ParseError,
+ "Generators can only contain alphanumerics, file separators, and dashes"
+ end
+
+ if args[0] =~ /\.\./
+ raise Puppet::ParseError,
+ "Can not use generators with '..' in them."
+ end
+
+ begin
+ output = Puppet::Util.execute(args)
+ rescue Puppet::ExecutionFailure => detail
+ raise Puppet::ParseError, "Failed to execute generator %s: %s" %
+ [args[0], detail]
+ end
+ output
+ end
end
end
diff --git a/test/language/functions.rb b/test/language/functions.rb
index 941f56c2c..ad6aa2761 100755
--- a/test/language/functions.rb
+++ b/test/language/functions.rb
@@ -473,6 +473,53 @@ class TestLangFunctions < Test::Unit::TestCase
val = scope.function_file([file1, file3])
end
end
+
+ def test_generate
+ command = tempfile
+ sh = %x{which sh}
+ File.open(command, "w") do |f|
+ f.puts %{#!#{sh}
+ if [ -n "$1" ]; then
+ echo "yay-$1"
+ else
+ echo yay
+ fi
+ }
+ end
+ File.chmod(0755, command)
+ assert_equal("yay\n", %x{#{command}}, "command did not work")
+ assert_equal("yay-foo\n", %x{#{command} foo}, "command did not work")
+
+ interp = mkinterp
+ scope = mkscope(:interp => interp)
+
+ val = nil
+ assert_nothing_raised("Could not call generator with no args") do
+ val = scope.function_generate([command])
+ end
+ assert_equal("yay\n", val, "generator returned wrong results")
+
+ assert_nothing_raised("Could not call generator with args") do
+ val = scope.function_generate([command, "foo"])
+ end
+ assert_equal("yay-foo\n", val, "generator returned wrong results")
+
+ assert_raise(Puppet::ParseError, "Did not fail with an unqualified path") do
+ val = scope.function_generate([File.basename(command), "foo"])
+ end
+
+ assert_raise(Puppet::ParseError, "Did not fail when command failed") do
+ val = scope.function_generate([%x{which touch}.chomp, "/this/dir/does/not/exist"])
+ end
+
+ fake = File.join(File.dirname(command), "..")
+ dir = File.dirname(command)
+ dirname = File.basename(dir)
+ bad = File.join(dir, "..", dirname, File.basename(command))
+ assert_raise(Puppet::ParseError, "Did not fail when command failed") do
+ val = scope.function_generate([bad])
+ end
+ end
end
# $Id$