summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-09-05 23:20:45 +0000
committerluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-09-05 23:20:45 +0000
commitfcc5bae22cb2a0286975cfc0bcab52a2a7f7973d (patch)
tree6ebe72a068a216cd56f6938b4d90fe0eaa86671b
parent8310c9d18a89e499b63a10e7890d836dcfc86f46 (diff)
downloadpuppet-fcc5bae22cb2a0286975cfc0bcab52a2a7f7973d.tar.gz
puppet-fcc5bae22cb2a0286975cfc0bcab52a2a7f7973d.tar.xz
puppet-fcc5bae22cb2a0286975cfc0bcab52a2a7f7973d.zip
Adding an "env" parameter to exec, for providing extra environment settings, as requested in #236.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1568 980ebf18-57e1-0310-9a29-db15c13687c0
-rwxr-xr-xlib/puppet/type/exec.rb39
-rw-r--r--lib/puppet/util/execution.rb1
-rwxr-xr-xtest/types/exec.rb40
-rwxr-xr-xtest/util/execution.rb4
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