Class: Fog::SSH::Real

Inherits:
Object
  • Object
show all
Defined in:
lib/relevance_rails/fog_ext/ssh.rb

Overview

Monkey-patch Fog 1.3.1 to stream SSH output (in real time) to stdout.

Instance Method Summary collapse

Instance Method Details

#run(commands) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/relevance_rails/fog_ext/ssh.rb', line 7

def run(commands)
  commands = [*commands]
  results  = []
  begin
    Net::SSH.start(@address, @username, @options) do |ssh|
      commands.each do |command|
        result = Fog::SSH::Result.new(command)
        ssh.open_channel do |ssh_channel|
          ssh_channel.request_pty
          ssh_channel.exec(command) do |channel, success|
            unless success
              raise "Could not execute command: #{command.inspect}"
            end

            channel.on_data do |ch, data|
              result.stdout << data
              puts data
            end

            channel.on_extended_data do |ch, type, data|
              next unless type == 1
              result.stderr << data
              puts data
            end

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

            channel.on_request('exit-signal') do |ch, data|
              result.status = 255
            end
          end
        end
        ssh.loop
        results << result
      end
    end
  rescue Net::SSH::HostKeyMismatch => exception
    exception.remember_host!
    sleep 0.2
    retry
  end
  results
end