Module: Performa::ShellHelper

Included in:
ContainerRegistry, Coordinator, Images
Defined in:
lib/performa/shell_helper.rb

Instance Method Summary collapse

Instance Method Details

#run_capture_command(command) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/performa/shell_helper.rb', line 31

def run_capture_command(command)
  exit_status = nil
  result_str = +""

  Open3.popen2e(command) do |_stdin, stdout_and_stderr, wait_thr|
    stdout_and_stderr.each do |line|
      result_str << line
    end
    exit_status = wait_thr.value
  end
  exit_status.success? ? LOG.info_success(result_str) : LOG.info_error(result_str)

  [exit_status, result_str]
end

#run_command(command, success_only: true, no_capture: false) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/performa/shell_helper.rb', line 9

def run_command(command, success_only: true, no_capture: false)
  LOG.info("Running `#{command.colorize(:light_yellow)}` ...")

  exit_status, result_str = no_capture ? run_no_capture_command(command) : run_capture_command(command)
  raise "(non-zero exit code: #{exit_status.exitstatus})" if success_only && !exit_status.success?

  CommandResult.new(result_str).tap do |result|
    result.success = exit_status.success?
  end
rescue StandardError => e
  raise Error, <<~MSG
    Error running the command `#{command}`:
    => error: #{e.message}
    => command output: #{result_str}
  MSG
end

#run_container_command(container_id, command, **options) ⇒ Object



46
47
48
# File 'lib/performa/shell_helper.rb', line 46

def run_container_command(container_id, command, **options)
  run_command("docker container exec #{container_id} sh -c #{Shellwords.escape(command)}", options)
end

#run_no_capture_command(command) ⇒ Object



26
27
28
29
# File 'lib/performa/shell_helper.rb', line 26

def run_no_capture_command(command)
  system(command)
  [$CHILD_STATUS, ""]
end