Module: Puppet::Util

Included in:
Puppet, Interface, Parameter, Parameter, Parser::Functions, Parser::Methods, Parser::TemplateWrapper, Provider, Provider, Transaction, Type, Type, ClassGen
Defined in:
lib/puppet/util.rb,
lib/puppet/util/platform.rb,
lib/puppet/util/run_mode.rb,
lib/puppet/util/command_line.rb,
lib/puppet/util/execution_stub.rb,
lib/puppet/util/constant_inflector.rb,
lib/puppet/util/symbolic_file_mode.rb,
lib/puppet/util/command_line/trollop.rb,
lib/puppet/util/command_line/puppet_option_parser.rb

Defined Under Namespace

Modules: ClassGen, Errors, Execution, Profiler, SSL

Constant Summary

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) exit_on_fail(message, code = 1) { ... }

Executes a block of code, wrapped with some special exception handling. Causes the ruby interpreter to exit if the block throws an exception.

Yields:



487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# File 'lib/puppet/util.rb', line 487

def exit_on_fail(message, code = 1)
  yield
# First, we need to check and see if we are catching a SystemExit error.  These will be raised
#  when we daemonize/fork, and they do not necessarily indicate a failure case.
rescue SystemExit => err
  raise err

# Now we need to catch *any* other kind of exception, because we may be calling third-party
#  code (e.g. webrick), and we have no idea what they might throw.
rescue Exception => err
  ## NOTE: when debugging spec failures, these two lines can be very useful
  #puts err.inspect
  #puts Puppet::Util.pretty_backtrace(err.backtrace)
  Puppet.log_exception(err, "Could not #{message}: #{err}")
  Puppet::Util::Log.force_flushqueue()
  exit(code)
end

+ (String) which(bin)

Resolve a path for an executable to the absolute path. This tries to behave in the same manner as the unix which command and uses the PATH environment variable.



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/puppet/util.rb', line 178

def which(bin)
  if absolute_path?(bin)
    return bin if FileTest.file? bin and FileTest.executable? bin
  else
    ENV['PATH'].split(File::PATH_SEPARATOR).each do |dir|
      begin
        dest = File.expand_path(File.join(dir, bin))
      rescue ArgumentError => e
        # if the user's PATH contains a literal tilde (~) character and HOME is not set, we may get
        # an ArgumentError here.  Let's check to see if that is the case; if not, re-raise whatever error
        # was thrown.
        if e.to_s =~ /HOME/ and (ENV['HOME'].nil? || ENV['HOME'] == "")
          # if we get here they have a tilde in their PATH.  We'll issue a single warning about this and then
          # ignore this path element and carry on with our lives.
          Puppet::Util::Warnings.warnonce("PATH contains a ~ character, and HOME is not set; ignoring PATH element '#{dir}'.")
        elsif e.to_s =~ /doesn't exist|can't find user/
          # ...otherwise, we just skip the non-existent entry, and do nothing.
          Puppet::Util::Warnings.warnonce("Couldn't expand PATH containing a ~ character; ignoring PATH element '#{dir}'.")
        else
          raise
        end
      else
        if Puppet.features.microsoft_windows? && File.extname(dest).empty?
          exts = ENV['PATHEXT']
          exts = exts ? exts.split(File::PATH_SEPARATOR) : %w[.COM .EXE .BAT .CMD]
          exts.each do |ext|
            destext = File.expand_path(dest + ext)
            return destext if FileTest.file? destext and FileTest.executable? destext
          end
        end
        return dest if FileTest.file? dest and FileTest.executable? dest
      end
    end
  end
  nil
end

Instance Method Details

- (Object) exit_on_fail(message, code = 1) { ... } (private)

Executes a block of code, wrapped with some special exception handling. Causes the ruby interpreter to exit if the block throws an exception.

Yields:



487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# File 'lib/puppet/util.rb', line 487

def exit_on_fail(message, code = 1)
  yield
# First, we need to check and see if we are catching a SystemExit error.  These will be raised
#  when we daemonize/fork, and they do not necessarily indicate a failure case.
rescue SystemExit => err
  raise err

# Now we need to catch *any* other kind of exception, because we may be calling third-party
#  code (e.g. webrick), and we have no idea what they might throw.
rescue Exception => err
  ## NOTE: when debugging spec failures, these two lines can be very useful
  #puts err.inspect
  #puts Puppet::Util.pretty_backtrace(err.backtrace)
  Puppet.log_exception(err, "Could not #{message}: #{err}")
  Puppet::Util::Log.force_flushqueue()
  exit(code)
end

- (String) which(bin) (private)

Resolve a path for an executable to the absolute path. This tries to behave in the same manner as the unix which command and uses the PATH environment variable.



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/puppet/util.rb', line 178

def which(bin)
  if absolute_path?(bin)
    return bin if FileTest.file? bin and FileTest.executable? bin
  else
    ENV['PATH'].split(File::PATH_SEPARATOR).each do |dir|
      begin
        dest = File.expand_path(File.join(dir, bin))
      rescue ArgumentError => e
        # if the user's PATH contains a literal tilde (~) character and HOME is not set, we may get
        # an ArgumentError here.  Let's check to see if that is the case; if not, re-raise whatever error
        # was thrown.
        if e.to_s =~ /HOME/ and (ENV['HOME'].nil? || ENV['HOME'] == "")
          # if we get here they have a tilde in their PATH.  We'll issue a single warning about this and then
          # ignore this path element and carry on with our lives.
          Puppet::Util::Warnings.warnonce("PATH contains a ~ character, and HOME is not set; ignoring PATH element '#{dir}'.")
        elsif e.to_s =~ /doesn't exist|can't find user/
          # ...otherwise, we just skip the non-existent entry, and do nothing.
          Puppet::Util::Warnings.warnonce("Couldn't expand PATH containing a ~ character; ignoring PATH element '#{dir}'.")
        else
          raise
        end
      else
        if Puppet.features.microsoft_windows? && File.extname(dest).empty?
          exts = ENV['PATHEXT']
          exts = exts ? exts.split(File::PATH_SEPARATOR) : %w[.COM .EXE .BAT .CMD]
          exts.each do |ext|
            destext = File.expand_path(dest + ext)
            return destext if FileTest.file? destext and FileTest.executable? destext
          end
        end
        return dest if FileTest.file? dest and FileTest.executable? dest
      end
    end
  end
  nil
end