From fcc5bae22cb2a0286975cfc0bcab52a2a7f7973d Mon Sep 17 00:00:00 2001 From: luke Date: Tue, 5 Sep 2006 23:20:45 +0000 Subject: 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 --- lib/puppet/type/exec.rb | 39 ++++++++++++++++++++++++++++++++++++++- lib/puppet/util/execution.rb | 1 + 2 files changed, 39 insertions(+), 1 deletion(-) (limited to 'lib') 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 -- cgit