Class: Symphony::Task::SSHScript

Inherits:
Symphony::Task
  • Object
show all
Defined in:
lib/symphony/tasks/sshscript.rb

Overview

A base class for connecting to a remote host, then uploading and executing an Inversion templated script.

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
template:   (required) A path to the Inversion templated script
port:       (optional) The port to connect to (defaults to 22)
user:       (optional) The user to connect as (defaults to root)
key:        (optional) The path to an SSH private key
attributes: (optional) Additional data to attach to the template
nocleanup:  (optional) Leave the remote script after execution? (default to false)
tempdir:    (optional) The destination temp directory.  (defaults to /tmp)

Additionally, this class responds to the ‘symphony.ssh’ configurability key. Currently, you can override the default ssh user and private key.

Textual output of the command is stored in the @output instance variable.

require 'symphony'
require 'symphony/tasks/sshscript'

class YourTask < Symphony::Task::SSHScript
    timeout 30
    subscribe_to 'ssh.script.*'

    def work( payload,  )
        status = super
        puts "Remote script said: %s" % [ @output ]
        return status.success?
    end
end

Constant Summary collapse

TEMPLATE_OPTS =

Template config

{
	ignore_unknown_tags: false,
	on_render_error:     :propagate,
	strip_tag_lines:     true
}
DEFAULT_SSH_OPTIONS =

The defaults to use when connecting via SSH

{
	auth_methods:            [ 'publickey' ],
	compression:             true,
	config:                  false,
	keys_only:               true,
	verify_host_key:         :never,
	global_known_hosts_file: '/dev/null',
	user_known_hosts_file:   '/dev/null'
}

Instance Method Summary collapse

Instance Method Details

#work(payload, metadata) ⇒ Object

Perform the ssh connection, render the template, send it, and execute it.

Raises:

  • (ArgumentError)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/symphony/tasks/sshscript.rb', line 79

def work( payload,  )
	template   = payload[ 'template' ]
	attributes = payload[ 'attributes' ] || {}
	port       = payload[ 'port' ]    || 22
	user       = payload[ 'user' ]    || Symphony::Task::SSH.user
	key        = payload[ 'key'  ]    || Symphony::Task::SSH.key
	nocleanup  = payload[ 'nocleanup' ]
	tempdir    = payload[ 'tempdir' ] || '/tmp'

	raise ArgumentError, "Missing required option 'template'" unless template
	raise ArgumentError, "Missing required option 'host'"     unless payload[ 'host' ]

	remote_filename = self.make_remote_filename( template, tempdir )
	source = self.generate_script( template, attributes )

	ssh_options = DEFAULT_SSH_OPTIONS.merge( port: port, keys: Array(key) )
	ssh_options.merge!(
		logger: Loggability[ Net::SSH ],
		verbose: :debug
	) if payload[ 'debug' ]

	Net::SSH.start( payload['host'], user, ssh_options ) do |conn|
		self.log.debug "Uploading script (%d bytes) to %s:%s." %
			[ source.bytesize, payload['host'], remote_filename ]
		self.upload_script( conn, source, remote_filename )
		self.log.debug "  done with the upload."

		self.run_script( conn, remote_filename, nocleanup )
		self.log.debug "Output was:\n#{@output}"
	end

	return true
end