Class: Makesure::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/makesure/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd, opts = {}) ⇒ Runner

Returns a new instance of Runner.



7
8
9
10
11
12
# File 'lib/makesure/runner.rb', line 7

def initialize(cmd, opts = {})
  @cmd = cmd
  @opts = {
    :test => "val"
  }.merge(opts)
end

Instance Attribute Details

#cmdObject

Returns the value of attribute cmd.



5
6
7
# File 'lib/makesure/runner.rb', line 5

def cmd
  @cmd
end

Instance Method Details

#run!Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/makesure/runner.rb', line 14

def run!
  io = IO.popen(cmd)
  Makesure.log "Running cmd '#{cmd}' with pid #{io.pid}"

  Process.wait # needed to populate $?
  # seems to grab the exitstatus of the last cmd in a chain, ie:
  #   ls -al | grep -i file-that-exits                        # exits w/ 0
  #   ls -al | grep -i -file-that-does-not-exist              # exits w/ 1
  #   ls -al | grep -i -file-that-does-not-exist | echo "yay" # exits w/ 0
  exitstatus = $?.exitstatus

  Makesure.log "cmd exited with status #{exitstatus}"

  output = io.read

  unless exitstatus == 0
    Makesure.warn "oh shitttt something broke, tell everybody!"
    body = ""
    body << "[#{Time.now.to_s}] #{cmd.inspect} [pid #{io.pid}] exited with status #{exitstatus}, output follows:\n\n"
    body << output
    Makesure.send_alert("ERROR in command alert: #{cmd.inspect}", body)
  end

  if output.empty?
    Makesure.log "no output"
  else
    Makesure.log ">>> begin output"
    output.each_line do |l|
      Makesure.log l
    end
    Makesure.log "<<< end output"
  end
end