Sha256: 2fd867ba45119f1d9f1bc802503230ab1d7545f5eed84021d7e231d59416b71d

Contents?: true

Size: 1.3 KB

Versions: 2

Compression:

Stored size: 1.3 KB

Contents

module Meroku
  # Gives object the ability to execute bash commands on itself via ssh
  module Tunnelable
    # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
    def tunnel_run(cmd)
      retries = 0
      begin
        code = nil
        puts "DB8 tunnel_ip #{@tunnel_ip}"
        puts "DB8 @tunnel_username #{@tunnel_username}"
        puts "DB8 @tunnel_key_name #{@tunnel_key_name}"
        puts "DB8 #{`ls -la ~/.meroku/`}"
        Net::SSH.start(@tunnel_ip,
                       @tunnel_username,
                       keys: @tunnel_key_name,
                       verify_host_key: false) do |ssh|
          ssh.open_channel do |channel|
            puts cmd
            channel.exec cmd do |ch, success|
              raise "could not execute command" unless success
              ch.on_data { |_c, data| print data }
              ch.on_extended_data { |_c, _type, data| print data }
              ch.on_request("exit-status") { |_ch, data| code = data.read_long }
            end
          end.wait
        end
        abort "#{cmd} returned #{code} !!" if code != 0
      rescue Net::SSH::ConnectionTimeout
        puts "Net::SSH::ConnectionTimeout" && retry if (retries += 1) < 3
      rescue Errno::ECONNREFUSED
        puts "Net::SSH::ConnectionTimeout" && retry if (retries += 1) < 3
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
meroku-2.0.21 lib/meroku/tunnelable.rb
meroku-2.0.20 lib/meroku/tunnelable.rb