Class: Fleetctl::Runner::SSH

Inherits:
Runner
  • Object
show all
Defined in:
lib/fleetctl/runner/ssh.rb

Instance Attribute Summary

Attributes inherited from Runner

#command, #exit_code, #exit_signal, #status, #stderr_data, #stdout_data

Instance Method Summary collapse

Methods inherited from Runner

#initialize, #output

Constructor Details

This class inherits a constructor from Fleetctl::Runner::Runner

Instance Method Details

#run(host: Fleetctl.options.fleet_host, user: Fleetctl.options.fleet_user, ssh_options: {}) ⇒ Object



4
5
6
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
# File 'lib/fleetctl/runner/ssh.rb', line 4

def run(host: Fleetctl.options.fleet_host, user: Fleetctl.options.fleet_user, ssh_options: {})
  begin
    ssh_options = Fleetctl.options.ssh_options.merge(ssh_options)
    # return @output if @output
    Fleetctl.logger.info "#{self.class.name} #{user}@#{host} RUNNING: #{command.inspect}"
    Net::SSH.start(host, user, ssh_options) do |ssh|
      @stdout_data = ''
      @stderr_data = ''
      @exit_code = nil
      @exit_signal = nil
      ssh.open_channel do |channel|
        channel.exec(command) do |ch, success|
          unless success
            abort "FAILED: couldn't execute command (ssh.channel.exec)"
          end
          channel.on_data do |ch,data|
            @stdout_data+=data
          end

          channel.on_extended_data do |ch,type,data|
            @stderr_data+=data
          end

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

          channel.on_request('exit-signal') do |ch, data|
            @exit_signal = data.read_long
          end
        end
      end
      ssh.loop
      @output = @stdout_data
    end
    Fleetctl.logger.info "EXIT CODE!: #{exit_code.inspect}"
    Fleetctl.logger.info "STDOUT: #{@output.inspect}"
    @output
  rescue => e
    Fleetctl.logger.error 'ERROR in Runner#run'
    Fleetctl.logger.error e.message
    Fleetctl.logger.error e.backtrace.join("\n")
    raise e
  end
end