Class: Workers::System

Inherits:
Object
  • Object
show all
Extended by:
Resque::Plugins::Result
Defined in:
lib/workers/quorum.rb

Class Method Summary collapse

Class Method Details

.perform(meta_id, cmd, remote, ssh_host, ssh_user, ssh_options = {}, stdout = false) ⇒ Object

Resque worker method.



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
52
53
# File 'lib/workers/quorum.rb', line 10

def self.perform(meta_id, cmd, remote, ssh_host, ssh_user, ssh_options = {}, stdout = false)
  unless ssh_options.empty?
    # Convert each key in ssh_options to a symbol.
    ssh_options = ssh_options.inject({}) do |memo, (k, v)|
      memo[k.to_sym] = v
    memo
    end
  end

  exit_status = 1
  out         = ""

  if remote
    # Execute command on remote machine.
    Net::SSH.start(ssh_host, ssh_user, ssh_options) do |ssh|
      ssh.open_channel do |ch|
        ch.exec(cmd) do |ch, success|
          if success
            # Capture STDOUT from ch.exec()
            if stdout
              ch.on_data do |ch, data|
                out = data
              end
            end
            # Read the exit status of the remote process.
            ch.on_request("exit-status") do |ch, data|
              exit_status = data.read_long
            end
          else
            Rails.logger.warn "Channel Net::SSH exec() failed. :'("
          end
        end
      end
      ssh.loop
    end
  else
    out         = `#{cmd}`
    exit_status = $?.exitstatus
  end
  if exit_status > 0
    raise "Worker failed :'(. See quorum/log/quorum.log for more information."
  end
  out if stdout
end