Module: Ohai::Mixin::Command

Included in:
DSL::Plugin
Defined in:
lib/ohai/mixin/command.rb

Class Method Summary collapse

Class Method Details

.shell_out(cmd, **options) ⇒ Object

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ohai/mixin/command.rb', line 29

def shell_out(cmd, **options)
  options = options.dup
  # unless specified by the caller timeout after configured timeout (default 30 seconds)
  options[:timeout] ||= Ohai::Config.ohai[:shellout_timeout]
  unless RUBY_PLATFORM.match?(/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
    logger.trace("Plugin #{name}: ran '#{cmd}' and returned #{so.exitstatus}")
    so
  rescue Errno::ENOENT => e
    logger.trace("Plugin #{name}: ran '#{cmd}' and failed #{e.inspect}")
    raise Ohai::Exceptions::Exec, e
  rescue Mixlib::ShellOut::CommandTimeout => e
    logger.trace("Plugin #{name}: ran '#{cmd}' and timed out after #{options[:timeout]} seconds")
    raise Ohai::Exceptions::Exec, e
  end
end