Module: Meroku::Tunnelable

Included in:
Node
Defined in:
lib/meroku/tunnelable.rb

Overview

Gives object the ability to execute bash commands on itself via ssh

Instance Method Summary collapse

Instance Method Details

#tunnel_run(cmd) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/MethodLength



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/meroku/tunnelable.rb', line 5

def tunnel_run(cmd)
  retries = 0
  begin
    code = nil
    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
  end
end