summaryrefslogtreecommitdiffstats
path: root/lib/puppet/util.rb
diff options
context:
space:
mode:
authorluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-12-19 22:42:39 +0000
committerluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-12-19 22:42:39 +0000
commit36e8d65bbdf236203741d3a3b0d98a1eb51a75a9 (patch)
treef636bea92fe2fcc101b6541f1f84524c742a00ba /lib/puppet/util.rb
parent115ec095357171381e2af3aa5ebbd61164b40064 (diff)
downloadpuppet-36e8d65bbdf236203741d3a3b0d98a1eb51a75a9.tar.gz
puppet-36e8d65bbdf236203741d3a3b0d98a1eb51a75a9.tar.xz
puppet-36e8d65bbdf236203741d3a3b0d98a1eb51a75a9.zip
Fixing #372 and #374. All is not perfect, since OS X still cannot set UID, but it is much better. There is still plenty of bug-fixing to do on other platforms, I expect.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1954 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'lib/puppet/util.rb')
-rw-r--r--lib/puppet/util.rb54
1 files changed, 48 insertions, 6 deletions
diff --git a/lib/puppet/util.rb b/lib/puppet/util.rb
index 246be2bd0..906184c18 100644
--- a/lib/puppet/util.rb
+++ b/lib/puppet/util.rb
@@ -267,18 +267,60 @@ module Util
end
# Execute the desired command, and return the status and output.
- def execute(command, failonfail = true)
+ def execute(command, failonfail = true, uid = nil, gid = nil)
+ if command.is_a?(Array)
+ command = command.collect { |i| i.to_s }
+ str = command.join(" ")
+ else
+ raise "Must pass an array"
+ str = command
+ end
if respond_to? :debug
- debug "Executing '%s'" % command
+ debug "Executing '%s'" % str
else
- Puppet.debug "Executing '%s'" % command
+ Puppet.debug "Executing '%s'" % str
+ end
+
+ if uid
+ uid = Puppet::SUIDManager.convert_xid(:uid, uid)
+ end
+ if gid
+ gid = Puppet::SUIDManager.convert_xid(:gid, gid)
+ end
+
+ @@os ||= Facter.value(:operatingsystem)
+ output = nil
+ IO.popen("-") do |f|
+ if f
+ output = f.read
+ else
+ begin
+ $stderr.close
+ $stderr = $stdout.dup
+ if gid
+ Process.egid = gid
+ Process.gid = gid unless @@os == "Darwin"
+ end
+ if uid
+ Process.euid = uid
+ Process.uid = uid unless @@os == "Darwin"
+ end
+ if command.is_a?(Array)
+ system(*command)
+ else
+ system(command)
+ end
+ exit!($?.exitstatus)
+ rescue => detail
+ puts detail.to_s
+ exit!(1)
+ end
+ end
end
- command += " 2>&1" unless command =~ />/
- output = %x{#{command}}
if failonfail
unless $? == 0
- raise ExecutionFailure, "Could not execute '%s': %s" % [command, output]
+ raise ExecutionFailure, "Execution of '%s' returned %s: %s" % [str, $?, output]
end
end