module Ohai::Mixin::Command

Public Class Methods

shell_out(cmd, **options) click to toggle source

DISCLAIMER: Logging only works in the context of a plugin!! accept a command and any of the mixlib-shellout options

# File lib/ohai/mixin/command.rb, line 29
def shell_out(cmd, **options)
  options = options.dup
  # unless specified by the caller timeout after 30 seconds
  options[:timeout] ||= 30
  unless RUBY_PLATFORM =~ /mswin|mingw32|windows/
    options[:env] = options.key?(:env) ? options[:env].dup : {}
    options[:env]["PATH"] ||= ((ENV["PATH"] || "").split(":") + %w{/usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin}).join(":")
  end
  so = Mixlib::ShellOut.new(cmd, options)
  begin
    so.run_command
    Ohai::Log.debug("Plugin #{name}: ran '#{cmd}' and returned #{so.exitstatus}")
    so
  rescue Errno::ENOENT => e
    Ohai::Log.debug("Plugin #{name}: ran '#{cmd}' and failed #{e.inspect}")
    raise Ohai::Exceptions::Exec, e
  rescue Mixlib::ShellOut::CommandTimeout => e
    Ohai::Log.debug("Plugin #{name}: ran '#{cmd}' and timed out after #{options[:timeout]} seconds")
    raise Ohai::Exceptions::Exec, e
  end
end

Private Instance Methods

shell_out(cmd, **options) click to toggle source

DISCLAIMER: Logging only works in the context of a plugin!! accept a command and any of the mixlib-shellout options

# File lib/ohai/mixin/command.rb, line 29
def shell_out(cmd, **options)
  options = options.dup
  # unless specified by the caller timeout after 30 seconds
  options[:timeout] ||= 30
  unless RUBY_PLATFORM =~ /mswin|mingw32|windows/
    options[:env] = options.key?(:env) ? options[:env].dup : {}
    options[:env]["PATH"] ||= ((ENV["PATH"] || "").split(":") + %w{/usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin}).join(":")
  end
  so = Mixlib::ShellOut.new(cmd, options)
  begin
    so.run_command
    Ohai::Log.debug("Plugin #{name}: ran '#{cmd}' and returned #{so.exitstatus}")
    so
  rescue Errno::ENOENT => e
    Ohai::Log.debug("Plugin #{name}: ran '#{cmd}' and failed #{e.inspect}")
    raise Ohai::Exceptions::Exec, e
  rescue Mixlib::ShellOut::CommandTimeout => e
    Ohai::Log.debug("Plugin #{name}: ran '#{cmd}' and timed out after #{options[:timeout]} seconds")
    raise Ohai::Exceptions::Exec, e
  end
end