Class: Symphony::Task::SSH

Inherits:
Symphony::Task
  • Object
show all
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
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

CONFIG_DEFAULTS =

SSH default options.

{
	:path => '/usr/bin/ssh',
	:opts => [
		'-e', 'none',
		'-T',
		'-x',
		'-q',
		'-o', 'CheckHostIP=no',
		'-o', 'BatchMode=yes',
		'-o', 'StrictHostKeyChecking=no'
	],
	:user => 'root',
	:key  => nil
}
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/

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.keyObject (readonly)

An absolute path to a password-free ssh private key.



85
86
87
# File 'lib/symphony/tasks/ssh.rb', line 85

def key
  @key
end

.optsObject (readonly)

A default set of ssh client options when connecting to remote hosts.



79
80
81
# File 'lib/symphony/tasks/ssh.rb', line 79

def opts
  @opts
end

.pathObject (readonly)

The full path to the ssh binary.



75
76
77
# File 'lib/symphony/tasks/ssh.rb', line 75

def path
  @path
end

.userObject (readonly)

The default user to use when connecting. If unset, ‘root’ is used.



82
83
84
# File 'lib/symphony/tasks/ssh.rb', line 82

def user
  @user
end

Class Method Details

.configure(config = nil) ⇒ Object

Configurability API.



90
91
92
93
94
95
96
97
# File 'lib/symphony/tasks/ssh.rb', line 90

def self::configure( config=nil )
	config = Symphony::Task::SSH.defaults.merge( config || {} )
	@path  = config.delete( :path )
	@opts  = config.delete( :opts )
	@user  = config.delete( :user )
	@key   = config.delete( :key )
	super
end

Instance Method Details

#work(payload, metadata) ⇒ Object

Perform the ssh connection, passing the command to the pipe and retreiving any output from the remote end.

Raises:

  • (ArgumentError)


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/symphony/tasks/ssh.rb', line 103

def work( payload,  )
	command = payload[ 'command' ]
	raise ArgumentError, "Missing required option 'command'" unless 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