class Shipper::Executor

Attributes

host_bash[RW]
path[RW]

Public Class Methods

new(host_bash = nil, path = nil) click to toggle source
# File lib/shipper/executor.rb, line 10
def initialize(host_bash = nil, path = nil)
  @host_bash = host_bash
  @path = path || Dir.pwd
end

Public Instance Methods

cd(new_location) click to toggle source
# File lib/shipper/executor.rb, line 19
def cd(new_location)
  @path = new_location
end
exec(cmd) click to toggle source
# File lib/shipper/executor.rb, line 15
def exec(cmd)
  host_bash ? exec_host(cmd) : exec_local(cmd)
end

Private Instance Methods

exec_host(cmd) click to toggle source
# File lib/shipper/executor.rb, line 25
def exec_host(cmd)
  logger.bold("Exec host '#{cmd}'")

  host_bash.exec!("cd #{path}; #{cmd}") do |_channel, _stream, data|
    logger.puts(data)
  end
end
exec_local(cmd) click to toggle source
# File lib/shipper/executor.rb, line 33
def exec_local(cmd)
  logger.bold("Exec local '#{cmd}'")

  Dir.chdir(path) do
    status = Open3.popen2e(cmd) do |_stdin, stdout, wait_thread|
      stdout.each_line { |line| logger.puts(line) }

      wait_thread.value
    end
    fall_down!(cmd) unless status.exitstatus.zero?
  end
end
fall_down!(failed_cmd) click to toggle source
# File lib/shipper/executor.rb, line 50
def fall_down!(failed_cmd)
  logger.error 'Command finished with non-zero code:'
  logger.puts "'#{failed_cmd}'"
  logger.puts 'halt.'
  exit
end
logger() click to toggle source
# File lib/shipper/executor.rb, line 46
def logger
  ::Shipper::Logger.instance
end