Project

General

Profile

Download (1.17 KB) Statistics
| Branch: | Tag: | Revision:

foreman_pipeline / app / lib / actions / foreman_pipeline / mixins / ssh_extension.rb @ be52ca64

1
module Actions
2
  module ForemanPipeline
3
    module Mixins
4
      module SshExtension
5
        extend ActiveSupport::Concern
6

    
7
        # http://stackoverflow.com/questions/3386233/how-to-get-exit-status-with-rubys-netssh-library
8
        def ssh_exec!(ssh, command)
9
          stdout_data = ""
10
          stderr_data = ""
11
          exit_code = nil
12
          exit_signal = nil
13
          ssh.open_channel do |channel|
14
            channel.exec(command) do |ch, success|
15
              unless success
16
                abort "FAILED: couldn't execute command (ssh.channel.exec)"
17
              end
18
              channel.on_data do |ch, data|
19
                stdout_data += data
20
              end
21

    
22
              channel.on_extended_data do |ch, type, data|
23
                stderr_data += data
24
              end
25

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

    
30
              channel.on_request("exit-signal") do |ch, data|
31
                exit_signal = data.read_long
32
              end
33
            end
34
          end
35
          ssh.loop
36
          [stdout_data, stderr_data, exit_code, exit_signal]
37
        end
38
      end
39
    end
40
  end
41
end