Class: Tomo::SSH::ChildProcess

Inherits:
Object
  • Object
show all
Defined in:
lib/tomo/ssh/child_process.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*command, on_data:) ⇒ ChildProcess

Returns a new instance of ChildProcess.



16
17
18
19
20
21
22
# File 'lib/tomo/ssh/child_process.rb', line 16

def initialize(*command, on_data:)
  @command = *command
  @on_data = on_data
  @stdout_buffer = StringIO.new
  @stderr_buffer = StringIO.new
  Tomo.logger.debug command.map(&:shellescape).join(" ")
end

Class Method Details

.execute(*command, on_data: ->(data) {}) ⇒ Object



10
11
12
13
14
# File 'lib/tomo/ssh/child_process.rb', line 10

def self.execute(*command, on_data: ->(data) {})
  process = new(*command, on_data:)
  process.wait_for_exit
  process.result
end

Instance Method Details

#resultObject



35
36
37
# File 'lib/tomo/ssh/child_process.rb', line 35

def result
  Result.new(exit_status:, stdout: stdout_buffer.string, stderr: stderr_buffer.string)
end

#wait_for_exitObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/tomo/ssh/child_process.rb', line 24

def wait_for_exit
  Open3.popen3(*command) do |stdin, stdout, stderr, wait_thread|
    stdin.close
    stdout_thread = start_io_thread(stdout, stdout_buffer)
    stderr_thread = start_io_thread(stderr, stderr_buffer)
    stdout_thread.join
    stderr_thread.join
    @exit_status = wait_thread.value.exitstatus
  end
end