Sha256: f3cd94830622ff103cf7b8dde57fa6a08989b1bd0f9a68fd707db4d7e7ff0a0f

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

require 'net/ssh/stream/version'

require 'net/ssh'

module Net
  module SSH
    class Stream

      def initialize(*args)
        @session ||= Net::SSH.start *args
      end

      def close
        if @session && !@session.closed?
          @session.close
          @session = nil
        end
      end

      def exec(command, stdout: STDOUT, stderr: STDERR)
        exit_code = nil
        
        @session.open_channel do |channel|
          channel.exec(command) do |ch, success|
            unless success
              abort "FAILED: couldn't execute command (ssh.channel.exec)"
            end
            
            channel.on_data do |ch, data|
              stdout << data
            end

            channel.on_extended_data do |ch, type, data|
              stderr << data
            end

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

        @session.loop
        
        exit_code
      end

      def exec!(command, stdout: STDOUT, stderr: STDERR)
        exit_code = self.exec command, stdout: stdout, stderr: stderr
        raise "Command fail - Exit code #{exit_code} - #{command}" if exit_code != 0
      end

      def self.start(*args)
        stream = self.new *args
        yield stream
      ensure
        stream.close
        nil
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
net-ssh-stream-0.2.0 lib/net/ssh/stream.rb