Module: ReaperMan::Utils::Process

Included in:
PackageList::Processor, Signer
Defined in:
lib/reaper-man/utils/process.rb

Overview

Shellout helper

Defined Under Namespace

Classes: CommandFailed, CommandResult, Timeout

Instance Method Summary collapse

Instance Method Details

#child_process_command(cmd, args) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/reaper-man/utils/process.rb', line 79

def child_process_command(cmd, args)
  s_out = Tempfile.new("stdout")
  s_err = Tempfile.new("stderr")
  s_out.sync
  s_err.sync
  c_proc = ChildProcess.build(*Shellwords.split(cmd))
  c_proc.environment.merge(args.fetch(:environment, {}))
  c_proc.io.stdout = s_out
  c_proc.io.stderr = s_err
  c_proc.start
  begin
    c_proc.poll_for_exit(args[:timeout] || 10)
  rescue ChildProcess::TimeoutError
    c_proc.stop
  ensure
    raise CommandFailed.new("Command failed: #{cmd}", CommandResult.new(c_proc)) if c_proc.crashed?
  end
  c_proc
end

#mixlib_shellout_command(cmd, args) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/reaper-man/utils/process.rb', line 99

def mixlib_shellout_command(cmd, args)
  shlout = nil
  begin
    shlout = Mixlib::ShellOut.new(cmd,
                                  :timeout => args[:timeout] || 10,
                                  :environment => args.fetch(:environment, {}))
    shlout.run_command
    shlout.error!
    shlout
  rescue Mixlib::ShellOut::ShellCommandFailed, CommandFailed, Mixlib::ShellOut::CommandTimeout => e
    raise CommandFailed.new(e, CommandResult.new(shlout))
  end
end

#shellout(cmd, args = {}) ⇒ Object

Simple helper to shell out



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/reaper-man/utils/process.rb', line 58

def shellout(cmd, args = {})
  result = nil
  if defined?(Mixlib)
    cmd_type = :mixlib_shellout
  else
    cmd_type = :childprocess
  end
  com_block = nil
  case cmd_type
  when :childprocess
    com_block = lambda { child_process_command(cmd, args) }
  when :mixlib_shellout
    require "mixlib/shellout"
    com_block = lambda { mixlib_shellout_command(cmd, args) }
  else
    raise ArgumentError.new("Unknown shellout helper provided: #{cmd_type}")
  end
  result = defined?(Bundler) ? Bundler.with_clean_env { com_block.call } : com_block.call
  result == false ? false : CommandResult.new(result)
end