Module: Dopi::Connector::Local

Included in:
Dopi::Command::Custom, Ssh
Defined in:
lib/dopi/connector/local.rb

Instance Method Summary collapse

Instance Method Details

#local_command(env, command_string) ⇒ Object

The command method executes the command of the step. Returns an array with stdio, sterror and exit code.



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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/dopi/connector/local.rb', line 15

def local_command(env, command_string)
  master, slave = PTY.open
  stdout_r, stdout_w = IO.pipe
  stderr_r, stderr_w = IO.pipe
  cmd_stdout = ''
  cmd_stderr = ''
  options = {
    :pgroup          => true,
    :unsetenv_others => true,
    :in              => slave,
    :out             => stdout_w,
    :err             => stderr_w,
  }
  log(:debug, "Executing #{command_string} for command #{name}")
  log(:debug, "Environment: #{env.to_s}")

  pid = Process.spawn(merged_env(env), command_string, options)
  slave.close
  stdout_w.close
  stderr_w.close

  signal_handler = Proc.new do |signal|
    case signal
    when :abort then Process.kill(:TERM, pid)
    when :kill  then Process.kill(:KILL, pid)
    end
  end

  on_signal(signal_handler)
  stdout_thread = Thread.new do
    until ( line = stdout_r.gets ).nil? do
      cmd_stdout << line
      log(:debug, line.gsub("\n", '').gsub("\r", ''))
    end
  end

  stderr_thread = Thread.new do
    until ( line = stderr_r.gets ).nil? do
      cmd_stderr << line
      log(:error, line.gsub("\n", '').gsub("\r", ''))
    end
  end

  _, status = Process.wait2(pid)
  stdout_thread.join
  stderr_thread.join
  delete_on_signal(signal_handler)
  [ cmd_stdout, cmd_stderr, status.exitstatus ]
end