Module: OmfCommon::Command

Defined in:
omf_common/lib/omf_common/command.rb

Class Method Summary (collapse)

Class Method Details

+ (Object) execute(*cmd, &block)

Execute a system command and use Open3 to capture exit status, stdout, stderr

Examples:


OmfCommon::Command.execute("uname -a")


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'omf_common/lib/omf_common/command.rb', line 14

def self.execute(*cmd, &block)
  result = nil
  begin
    Open3.popen3(*cmd) do |stdin, stdout, stderr, wait_thread|
      case wait_thread.value.exitstatus
      when 0
        # Exit status 0, all good, read stdout
        result = stdout.read.chomp
      when 1
        # Exit status 1, log minor error as warning
        logger.warn stderr.read.chomp
      when 2
        # Exit status 2, log standard error
        logger.error stderr.read.chomp
      else
        # Exit status greater than 2, log fatal error
        logger.fatal stderr.read.chomp
      end
    end
  rescue Errno::ENOENT => e
    logger.fatal e.message
  end
  result
end