diff options
author | Luke Kanies <luke@madstop.com> | 2005-07-11 17:10:56 +0000 |
---|---|---|
committer | Luke Kanies <luke@madstop.com> | 2005-07-11 17:10:56 +0000 |
commit | 600f272c7f61bc645d6f0ab4920c692e8190b789 (patch) | |
tree | fd17d5ad0cb3e4aab5816e80ff2da59f18a0e612 | |
parent | 4157fa01f499bdcdba19a0a6096f14cb1dc710ab (diff) | |
download | puppet-600f272c7f61bc645d6f0ab4920c692e8190b789.tar.gz puppet-600f272c7f61bc645d6f0ab4920c692e8190b789.tar.xz puppet-600f272c7f61bc645d6f0ab4920c692e8190b789.zip |
adding cwd parameter to exec
git-svn-id: https://reductivelabs.com/svn/puppet/library/trunk@350 980ebf18-57e1-0310-9a29-db15c13687c0
-rwxr-xr-x | lib/puppet/type/exec.rb | 52 | ||||
-rwxr-xr-x | test/types/tc_exec.rb | 19 |
2 files changed, 62 insertions, 9 deletions
diff --git a/lib/puppet/type/exec.rb b/lib/puppet/type/exec.rb index 421a1089a..dec7370eb 100755 --- a/lib/puppet/type/exec.rb +++ b/lib/puppet/type/exec.rb @@ -3,7 +3,6 @@ # $Id$ require 'puppet/type/state' -require 'puppet/type/pfile' module Puppet # okay, how do we deal with parameters that don't have operations @@ -11,6 +10,8 @@ module Puppet class State # this always runs class Returns < Puppet::State + attr_reader :output + @name = :returns # because this command always runs, @@ -52,23 +53,48 @@ module Puppet end def sync + olddir = nil + unless self.parent[:cwd].nil? + Puppet.debug "Resetting cwd to %s" % self.parent[:cwd] + olddir = Dir.getwd + begin + Dir.chdir(self.parent[:cwd]) + rescue => detail + raise "Failed to set cwd: %s" % detail + end + end + tmppath = ENV["PATH"] ENV["PATH"] = self.parent[:path] # capture both stdout and stderr - output = %x{#{self.parent[:command]} 2>&1} + @output = %x{#{self.parent[:command]} 2>&1} status = $? - ENV["PATH"] = tmppath - + loglevel = :info if status.exitstatus.to_s != self.should.to_s Puppet.err("%s returned %s" % [self.parent[:command],status.exitstatus]) - # and log - output.split(/\n/).each { |line| - Puppet.info("%s: %s" % [self.parent[:command],line]) - } + # if we've had a failure, up the log level + loglevel = :err + end + + # and log + @output.split(/\n/).each { |line| + Puppet.send(loglevel, "%s: %s" % [self.parent[:command],line]) + } + + # reset things to how we found them + ENV["PATH"] = tmppath + + unless olddir.nil? + begin + Dir.chdir(olddir) + rescue => detail + Puppet.err("Could not reset cwd to %s: %s" % + [olddir,detail]) + end end return :executed_command @@ -88,6 +114,7 @@ module Puppet @parameters = [ :path, :user, + :cwd, :command ] @@ -109,7 +136,6 @@ module Puppet hash[:returns] = "0" end - super if self[:command].nil? @@ -127,6 +153,14 @@ module Puppet end end end + + def output + if self.state(:returns).nil? + return nil + else + return self.state(:returns).output + end + end end end end diff --git a/test/types/tc_exec.rb b/test/types/tc_exec.rb index bc6b7dfaf..1eada111b 100755 --- a/test/types/tc_exec.rb +++ b/test/types/tc_exec.rb @@ -119,4 +119,23 @@ class TestExec < Test::Unit::TestCase ) } end + + def test_xcwdsettings + command = nil + assert_nothing_raised { + command = Puppet::Type::Exec.new( + :command => "pwd", + :cwd => "/tmp", + :path => "/usr/bin:/bin:/usr/sbin:/sbin", + :returns => 0 + ) + } + assert_nothing_raised { + command.retrieve + } + assert_nothing_raised { + command.sync + } + assert_equal("/tmp\n",command.output) + end end |