class Jisota::SSHSession

Public Class Methods

new(session = nil, options = {}) click to toggle source
# File lib/jisota/ssh_session.rb, line 8
def initialize(session = nil, options = {})
  @session = session
  @scp_engine = options.fetch(:scp_engine) { Net::SCP }
end

Public Instance Methods

command(command, logger = NilOutput.new) click to toggle source
# File lib/jisota/ssh_session.rb, line 13
def command(command, logger = NilOutput.new)
  exit_code = nil
  exit_signal = nil
  error_message = ""

  logger.system_message("Executing #{command}")

  @session.open_channel do |channel|
    channel.exec(command) do
      channel.on_data { |_, data| logger.info(data) }
      channel.on_extended_data { |_, _, data| logger.warn(data); error_message << data }
      channel.on_request("exit-status") { |_, data| exit_code = data.read_long }
      channel.on_request("exit-signal") { |_, data| exit_signal = data.read_long }
    end
  end
  @session.loop

  if exit_code > 0
    logger.error("Error running #{command}:")
    logger.error(error_message)
  end

  exit_code
end
upload(from: , to: ) click to toggle source
# File lib/jisota/ssh_session.rb, line 38
def upload(from: , to: )
  raise FileNotFoundError, "Upload file not found: #{from}" unless File.exist?(from)
  @scp_engine.new(@session).upload!(from, to, recursive: false)
end