diff options
| -rwxr-xr-x | lib/puppet/type/exec.rb | 39 | ||||
| -rw-r--r-- | lib/puppet/util/execution.rb | 1 | ||||
| -rwxr-xr-x | test/types/exec.rb | 40 | ||||
| -rwxr-xr-x | test/util/execution.rb | 4 |
4 files changed, 81 insertions, 3 deletions
diff --git a/lib/puppet/type/exec.rb b/lib/puppet/type/exec.rb index c5cf81142..442eb311f 100755 --- a/lib/puppet/type/exec.rb +++ b/lib/puppet/type/exec.rb @@ -241,7 +241,9 @@ module Puppet this directory does not exist, the command will fail." validate do |dir| - self.fail("CWD must be a fully qualified path") unless dir + unless dir =~ /^#{File::SEPARATOR}/ + self.fail("CWD must be a fully qualified path") + end end munge do |dir| @@ -264,6 +266,22 @@ module Puppet newvalues(*values) end + newparam(:env) do + desc "Any additional environment variables you want to set for a + command. Note that if you use this to set PATH, it will override + the ``path`` attribute. Multiple environment variables should be + specified as an array." + + validate do |values| + values = [values] unless values.is_a? Array + values.each do |value| + unless value =~ /\w+=/ + raise ArgumentError, "Invalid environment setting '%s'" % value + end + end + end + end + newcheck(:refreshonly) do desc "The command should only be run as a refresh mechanism for when a dependent object is changed. It only @@ -499,6 +517,25 @@ module Puppet env[:PATH] = self[:path].join(":") end + if envlist = self[:env] + envlist = [envlist] unless envlist.is_a? Array + envlist.each do |setting| + if setting =~ /^(\w+)=((.|\n)+)$/ + name = $1 + value = $2 + if env.include? name + warning( + "Overriding environment setting '%s' with '%s'" % + [name, value] + ) + end + env[name] = value + else + warning "Cannot understand env setting '%s'" % setting + end + end + end + withenv env do # The user and group default to nil, which 'asuser' # handlers correctly diff --git a/lib/puppet/util/execution.rb b/lib/puppet/util/execution.rb index 67b5ed692..467cd3f52 100644 --- a/lib/puppet/util/execution.rb +++ b/lib/puppet/util/execution.rb @@ -8,6 +8,7 @@ module Puppet::Util::Execution hash.each do |name, val| name = name.to_s oldvals[name] = ENV[name] + ENV[name] = val end yield diff --git a/test/types/exec.rb b/test/types/exec.rb index 16979c7ce..8f0756b9f 100755 --- a/test/types/exec.rb +++ b/test/types/exec.rb @@ -543,6 +543,46 @@ class TestExec < Test::Unit::TestCase exec.run("/bin/nosuchthingexists") } end + + def test_envparam + exec = Puppet::Type.newexec( + :command => "echo $envtest", + :path => ENV["PATH"], + :env => "envtest=yayness" + ) + + assert(exec, "Could not make exec") + + output = status = nil + assert_nothing_raised { + output, status = exec.run("echo $envtest") + } + + assert_equal("yayness\n", output) + + # Now check whether we can do multiline settings + assert_nothing_raised do + exec[:env] = "envtest=a list of things +and stuff" + end + + output = status = nil + assert_nothing_raised { + output, status = exec.run('echo "$envtest"') + } + assert_equal("a list of things\nand stuff\n", output) + + # Now test arrays + assert_nothing_raised do + exec[:env] = ["funtest=A", "yaytest=B"] + end + + output = status = nil + assert_nothing_raised { + output, status = exec.run('echo "$funtest" "$yaytest"') + } + assert_equal("A B\n", output) + end end # $Id$ diff --git a/test/util/execution.rb b/test/util/execution.rb index 4df606806..ec56c84bb 100755 --- a/test/util/execution.rb +++ b/test/util/execution.rb @@ -16,12 +16,12 @@ class TestPuppetUtilExecution < Test::Unit::TestCase assert_nothing_raised do Puppet::Util::Execution.withenv :testing => "foo" do - $ran = true + $ran = ENV["testing"] end end assert_equal("yay", ENV["testing"]) - assert_equal(true, $ran) + assert_equal("foo", $ran) ENV["rah"] = "yay" assert_raise(ArgumentError) do |
