Sha256: 238179aaabc51448b6ad0d0470455976247e057b37839d36c8b5b338a3830ec0

Contents?: true

Size: 1.23 KB

Versions: 3

Compression:

Stored size: 1.23 KB

Contents

require 'net/ssh'

class KuberKit::Shell::SshSession
  SshSessionError = Class.new(KuberKit::Error)

  attr_reader :session, :host, :user, :port

  def initialize(host:, user:, port:)
    @host = host
    @user = user
    @port = port
    @session = Net::SSH.start(host, user, {port: port})
  end

  def connected?
    !!@session
  end
  
  def disconnect
    return unless connected?
    @session.close
    @session = nil
  end

  def exec!(command)
    stdout_data = ''
    stderr_data = ''
    exit_code = nil
    channel = session.open_channel do |ch|
      ch.exec(command) do |ch, success|
        if !success
          raise SshSessionError, "Shell command failed: #{command}\r\n#{stdout_data}\r\n#{stderr_data}"
        end

        channel.on_data do |ch,data|
          stdout_data += data
        end

        channel.on_extended_data do |ch,type,data|
          stderr_data += data
        end

        channel.on_request('exit-status') do |ch,data|
          exit_code = data.read_long
        end
      end
    end

    channel.wait
    session.loop

    stdout_data = stdout_data.chomp.strip

    if exit_code != 0
      raise SshSessionError, "Shell command failed: #{command}\r\n#{stdout_data}\r\n#{stderr_data}"
    end

    stdout_data
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
kuber_kit-0.2.1 lib/kuber_kit/shell/ssh_session.rb
kuber_kit-0.2.0 lib/kuber_kit/shell/ssh_session.rb
kuber_kit-0.1.9 lib/kuber_kit/shell/ssh_session.rb