diff options
Diffstat (limited to 'lib/puppet/util.rb')
-rw-r--r-- | lib/puppet/util.rb | 54 |
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 |