Module: Puppet::Util::Execution

Defined in:
lib/puppet/util/execution.rb

Overview

This module defines methods for execution of system commands. It is intented for inclusion in classes that needs to execute system commands.

Constant Summary

Class Method Summary (collapse)

Class Method Details

+ (Object) execute_posix(command, options, stdin, stdout, stderr)  private

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This is private method.



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/puppet/util/execution.rb', line 190

def self.execute_posix(command, options, stdin, stdout, stderr)
  child_pid = Puppet::Util.safe_posix_fork(stdin, stdout, stderr) do

    # We can't just call Array(command), and rely on it returning
    # things like ['foo'], when passed ['foo'], because
    # Array(command) will call command.to_a internally, which when
    # given a string can end up doing Very Bad Things(TM), such as
    # turning "/tmp/foo;\r\n /bin/echo" into ["/tmp/foo;\r\n", " /bin/echo"]
    command = [command].flatten
    Process.setsid
    begin
      Puppet::Util::SUIDManager.change_privileges(options[:uid], options[:gid], true)

      # if the caller has requested that we override locale environment variables,
      if (options[:override_locale]) then
        # loop over them and clear them
        Puppet::Util::POSIX::LOCALE_ENV_VARS.each { |name| ENV.delete(name) }
        # set LANG and LC_ALL to 'C' so that the command will have consistent, predictable output
        # it's OK to manipulate these directly rather than, e.g., via "withenv", because we are in
        # a forked process.
        ENV['LANG'] = 'C'
        ENV['LC_ALL'] = 'C'
      end

      # unset all of the user-related environment variables so that different methods of starting puppet
      # (automatic start during boot, via 'service', via /etc/init.d, etc.) won't have unexpected side
      # effects relating to user / home dir environment vars.
      # it's OK to manipulate these directly rather than, e.g., via "withenv", because we are in
      # a forked process.
      Puppet::Util::POSIX::USER_ENV_VARS.each { |name| ENV.delete(name) }

      options[:custom_environment] ||= {}
      Puppet::Util.withenv(options[:custom_environment]) do
        Kernel.exec(*command)
      end
    rescue => detail
      Puppet.log_exception(detail, "Could not execute posix command: #{detail}")
      exit!(1)
    end
  end
  child_pid
end

+ (Object) execute_windows(command, options, stdin, stdout, stderr)  private

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This is private method.



239
240
241
242
243
244
245
246
247
248
# File 'lib/puppet/util/execution.rb', line 239

def self.execute_windows(command, options, stdin, stdout, stderr)
  command = command.map do |part|
    part.include?(' ') ? %Q["#{part.gsub(/"/, '\"')}"] : part
  end.join(" ") if command.is_a?(Array)

  options[:custom_environment] ||= {}
  Puppet::Util.withenv(options[:custom_environment]) do
    Puppet::Util::Windows::Process.execute(command, options, stdin, stdout, stderr)
  end
end

+ (Object) wait_for_output(stdout)  private

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This is private method.



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/puppet/util/execution.rb', line 256

def self.wait_for_output(stdout)
  # Make sure the file's actually been written.  This is basically a race
  # condition, and is probably a horrible way to handle it, but, well, oh
  # well.
  # (If this method were treated as private / inaccessible from outside of this file, we shouldn't have to worry
  #  about a race condition because all of the places that we call this from are preceded by a call to "waitpid2",
  #  meaning that the processes responsible for writing the file have completed before we get here.)
  2.times do |try|
    if File.exists?(stdout.path)
      stdout.open
      begin
        return stdout.read
      ensure
        stdout.close
        stdout.unlink
      end
    else
      time_to_sleep = try / 2.0
      Puppet.warning "Waiting for output; will sleep #{time_to_sleep} seconds"
      sleep(time_to_sleep)
    end
  end
  nil
end