Sha256: 9aea25fb0874fea72efbf0361241ee591b19fee5495d80c8d857831687b97552

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

module Meroku
  class Tunnel

    attr_accessor :ip, :username, :keys, :verify_host_key, :verbose

    def initialize(ip:,username:,keys:,verify_host_key:,verbose:)
      @ip = ip
      @username = username
      @keys = keys
      @verify_host_key = verify_host_key
      @verbose = verbose
    end

    def run(cmd)
      @verbose=true
      retries ||= 0
      exit_code=nil
      Net::SSH.start(@ip,
                     @username,
                     password: 'password',
                     keys: @keys,
                     verify_host_key: @verify_host_key,
                     timeout: 90) do |ssh|
        channel = ssh.open_channel do |ch|
          STDERR.print "#{cmd}\n"
          ch.exec cmd do |ch, success|
            raise "could not execute command" unless success
            ch.on_data do |c, data|
              if @verbose
                $stdout.print data
              else
                $stdout.print "."
              end
            end
            ch.on_extended_data do |c, type, data|
              if @verbose
                $stderr.print data
              else
                $stderr.print "."
              end
            end
            ch.on_request("exit-status") do |ch, data|
              exit_code = data.read_long
            end

            ch.on_close { print "\n" }
          end
        end
        channel.wait
      end
      fail "Ssh command returned non-zero" if status != 0
    rescue Errno::ECONNREFUSED => e
      retry if (retries += 1) < 10
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
meroku-2.0.8 lib/meroku/tunnel.rb