Class: Symphony::Task::SSH
- Inherits:
-
Symphony::Task
- Object
- Symphony::Task
- Symphony::Task::SSH
- Extended by:
- Configurability
- Defined in:
- lib/symphony/tasks/ssh.rb
Overview
A base class for connecting to remote hosts, running arbitrary commands, and collecting output.
This isn’t designed to be used directly. To use this in your environment, you’ll want to subclass it, add the behaviors that make sense for you, then super() back to the parent in the #work method.
It expects the payload to contain the following keys:
host: (required) The hostname to connect to
command: (required) The command to run on the remote host
port: (optional) The port to connect to (defaults to 22)
opts: (optional) Explicit SSH client options
env: (optional) A hash of environment vars to set for the connection.
user: (optional) The user to connect as (defaults to root)
key: (optional) The path to an SSH private key
Additionally, this class responds to the ‘symphony.ssh’ configurability key. Currently, you can set the ‘path’ argument, which is the full path to the local ssh binary (defaults to ‘/usr/bin/ssh’) and override the default ssh user, key, and client opts.
Textual output of the command is stored in the @output instance variable.
require 'symphony'
require 'symphony/tasks/ssh'
class YourTask < Symphony::Task::SSH
timeout 5
subscribe_to 'ssh.command'
def work( payload, )
status = super
puts "Remote host said: %s" % [ @output ]
return status.success?
end
end
Constant Summary collapse
- DEFAULT_SSH_OPTS =
The default set of ssh command line flags.
%w[ -e none -T -x -q -o CheckHostIP=no -o BatchMode=yes -o StrictHostKeyChecking=no ]
- SSH_CLEANUP =
SSH “informative” stdout output that should be cleaned from the command output.
%r/Warning: no access to tty|Thus no job control in this shell/
Instance Method Summary collapse
-
#work(payload, metadata) ⇒ Object
Perform the ssh connection in ‘exec’ mode, and retrieve any output from the remote end.
Instance Method Details
#work(payload, metadata) ⇒ Object
Perform the ssh connection in ‘exec’ mode, and retrieve any output from the remote end.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/symphony/tasks/ssh.rb', line 93 def work( payload, ) raise ArgumentError, "Missing required option 'command'" unless payload[ 'command' ] raise ArgumentError, "Missing required option 'host'" unless payload[ 'host' ] exitcode = self.open_connection( payload, ) do |reader, writer| #self.log.debug "Writing command #{command}..." #writer.puts( command ) self.log.debug " closing child's writer." writer.close self.log.debug " reading from child." reader.read end self.log.debug "SSH exited: %d" % [ exitcode ] return exitcode end |