Class: Ridley::SSH::Worker Private

Inherits:
Object
  • Object
show all
Includes:
Celluloid, Celluloid::Logger
Defined in:
lib/ridley/ssh/worker.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Author:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Worker

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Worker


15
16
17
18
19
20
21
# File 'lib/ridley/ssh/worker.rb', line 15

def initialize(options = {})
  @options = options.deep_symbolize_keys
  @sudo    = @options[:sudo]
  @user    = @options[:user]

  @options[:paranoid] = false
end

Instance Attribute Details

#optionsHashie::Mash (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


12
13
14
# File 'lib/ridley/ssh/worker.rb', line 12

def options
  @options
end

#sudoObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


9
10
11
# File 'lib/ridley/ssh/worker.rb', line 9

def sudo
  @sudo
end

#userObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


10
11
12
# File 'lib/ridley/ssh/worker.rb', line 10

def user
  @user
end

Instance Method Details

#run(host, command) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ridley/ssh/worker.rb', line 27

def run(host, command)
  response = Response.new(host)
  debug "Running SSH command: '#{command}' on: '#{host}' as: '#{user}'"

  Net::SSH.start(host, user, options.slice(*Net::SSH::VALID_OPTIONS)) do |ssh|
    ssh.open_channel do |channel|
      if self.sudo
        channel.request_pty
      end

      channel.exec(command) do |ch, success|
        unless success
          raise "FAILURE: could not execute command"
        end

        channel.on_data do |ch, data|
          response.stdout += data
          info "NODE[#{host}] #{data}" if data.present? and data != "\r\n"
        end

        channel.on_extended_data do |ch, type, data|
          response.stderr += data
          info "NODE[#{host}] #{data}" if data.present? and data != "\r\n"
        end

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

        channel.on_request("exit-signal") do |ch, data|
          response.exit_signal = data.read_string
        end
      end
    end

    ssh.loop
  end

  case response.exit_code
  when 0
    debug "Successfully ran SSH command on: '#{host}' as: '#{user}'"
    [ :ok, response ]
  else
    error "Successfully ran SSH command on: '#{host}' as: '#{user}', but it failed"
    error response.stdout
    [ :error, response ]
  end
rescue => e
  error "Failed to run SSH command on: '#{host}' as: '#{user}'"
  error "#{e.class}: #{e.message}"
  response.exit_code = -1
  response.stderr = e.message
  [ :error, response ]
end