lib/symphony/tasks/sshscript.rb
author Mahlon E. Smith <mahlon@martini.nu>
Wed, 15 Jul 2020 15:35:06 -0700
changeset 19 f31d60b04f8a
parent 11 ffa70066522c
permissions -rw-r--r--
Multiple changes to help support Windows native ssh with sshscript workers. Defaults are to assume a non-hostile posix environment. - Don't hardcode the tempdir separator character. - Allow overrides in the payload of any exposed net-ssh options. - Allow setting of the "delete" command for script cleanup (ie, 'del') - Allow running the script via a specific interpreter. An example payload to make this work with Windows native ssh/powershell to execute a ruby script: payload = { 'compression' => false, 'delete_cmd' => 'del', 'run_binary' => 'ruby', 'tempdir' => '' }
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     1
#!/usr/bin/env ruby
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     2
# vim: set nosta noet ts=4 sw=4:
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     3
11
ffa70066522c Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
     4
require 'securerandom'
0
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     5
require 'net/ssh'
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     6
require 'net/sftp'
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     7
require 'inversion'
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     8
require 'symphony'
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     9
require 'symphony/task'
11
ffa70066522c Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    10
require 'symphony/tasks/ssh'
0
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    11
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    12
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    13
### A base class for connecting to a remote host, then uploading and
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    14
### executing an Inversion templated script.
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    15
###
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    16
### This isn't designed to be used directly.  To use this in your
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    17
### environment, you'll want to subclass it, add the behaviors
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    18
### that make sense for you, then super() back to the parent in the
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    19
### #work method.
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    20
###
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    21
### It expects the payload to contain the following keys:
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    22
###
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    23
###    host:       (required) The hostname to connect to
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    24
###    template:   (required) A path to the Inversion templated script
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    25
###    port:       (optional) The port to connect to (defaults to 22)
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    26
###    user:       (optional) The user to connect as (defaults to root)
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    27
###    key:        (optional) The path to an SSH private key
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    28
###    attributes: (optional) Additional data to attach to the template
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    29
###    nocleanup:  (optional) Leave the remote script after execution? (default to false)
19
f31d60b04f8a Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents: 11
diff changeset
    30
###    delete_cmd: (optional) The command to delete the remote script.  (default to 'rm')
f31d60b04f8a Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents: 11
diff changeset
    31
###    run_binary: (optional) Windows doesn't allow direct execution of scripts, this is prefixed to the remote command if present.
f31d60b04f8a Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents: 11
diff changeset
    32
###    tempdir:    (optional) The destination temp directory.  (defaults to /tmp/, needs to include the separator character)
0
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    33
###
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    34
###
11
ffa70066522c Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    35
### Additionally, this class responds to the 'symphony.ssh' configurability
0
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    36
### key.  Currently, you can override the default ssh user and private key.
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    37
###
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    38
### Textual output of the command is stored in the @output instance variable.
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    39
###
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    40
###
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    41
###    require 'symphony'
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    42
###    require 'symphony/tasks/sshscript'
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    43
###
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    44
###    class YourTask < Symphony::Task::SSHScript
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    45
###        timeout 30
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    46
###        subscribe_to 'ssh.script.*'
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    47
###
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    48
###        def work( payload, metadata )
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    49
###            status = super
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    50
###            puts "Remote script said: %s" % [ @output ]
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    51
###            return status.success?
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    52
###        end
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    53
###    end
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    54
###
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    55
class Symphony::Task::SSHScript < Symphony::Task
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    56
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    57
	# Template config
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    58
	#
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    59
	TEMPLATE_OPTS = {
11
ffa70066522c Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    60
		ignore_unknown_tags: false,
ffa70066522c Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    61
		on_render_error:     :propagate,
ffa70066522c Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    62
		strip_tag_lines:     true
0
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    63
	}
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    64
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    65
	# The defaults to use when connecting via SSH
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    66
	#
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    67
	DEFAULT_SSH_OPTIONS = {
11
ffa70066522c Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    68
		auth_methods:            [ 'publickey' ],
ffa70066522c Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    69
		compression:             true,
ffa70066522c Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    70
		config:                  false,
ffa70066522c Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    71
		keys_only:               true,
ffa70066522c Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    72
		verify_host_key:         :never,
ffa70066522c Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    73
		global_known_hosts_file: '/dev/null',
ffa70066522c Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    74
		user_known_hosts_file:   '/dev/null'
0
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    75
	}
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    76
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    77
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    78
	### Perform the ssh connection, render the template, send it, and
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    79
	### execute it.
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    80
	###
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    81
	def work( payload, metadata )
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    82
		template   = payload[ 'template' ]
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    83
		attributes = payload[ 'attributes' ] || {}
11
ffa70066522c Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    84
		user       = payload[ 'user' ]    || Symphony::Task::SSH.user
ffa70066522c Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    85
		key        = payload[ 'key'  ]    || Symphony::Task::SSH.key
19
f31d60b04f8a Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents: 11
diff changeset
    86
		tempdir    = payload[ 'tempdir' ] || '/tmp/'
0
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    87
4
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents: 3
diff changeset
    88
		raise ArgumentError, "Missing required option 'template'" unless template
11
ffa70066522c Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    89
		raise ArgumentError, "Missing required option 'host'"     unless payload[ 'host' ]
0
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    90
11
ffa70066522c Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    91
		remote_filename = self.make_remote_filename( template, tempdir )
0
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    92
		source = self.generate_script( template, attributes )
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    93
19
f31d60b04f8a Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents: 11
diff changeset
    94
		# Map any configuration parameters in the payload to ssh
f31d60b04f8a Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents: 11
diff changeset
    95
		# options, for potential per-message behavior overrides.
f31d60b04f8a Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents: 11
diff changeset
    96
		ssh_opts_override = payload.
f31d60b04f8a Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents: 11
diff changeset
    97
			slice( *DEFAULT_SSH_OPTIONS.keys.map( &:to_s ) ).
f31d60b04f8a Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents: 11
diff changeset
    98
			transform_keys{|k| k.to_sym }
f31d60b04f8a Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents: 11
diff changeset
    99
f31d60b04f8a Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents: 11
diff changeset
   100
		ssh_options = DEFAULT_SSH_OPTIONS.dup.merge!(
f31d60b04f8a Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents: 11
diff changeset
   101
			ssh_opts_override,
f31d60b04f8a Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents: 11
diff changeset
   102
			port: payload[ 'port' ] || 22,
f31d60b04f8a Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents: 11
diff changeset
   103
			keys: Array( key )
f31d60b04f8a Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents: 11
diff changeset
   104
		)
0
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   105
		ssh_options.merge!(
11
ffa70066522c Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   106
			logger: Loggability[ Net::SSH ],
ffa70066522c Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   107
			verbose: :debug
0
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   108
		) if payload[ 'debug' ]
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   109
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   110
		Net::SSH.start( payload['host'], user, ssh_options ) do |conn|
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   111
			self.log.debug "Uploading script (%d bytes) to %s:%s." %
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   112
				[ source.bytesize, payload['host'], remote_filename ]
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   113
			self.upload_script( conn, source, remote_filename )
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   114
			self.log.debug "  done with the upload."
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   115
19
f31d60b04f8a Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents: 11
diff changeset
   116
			self.run_script( conn, remote_filename, payload )
0
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   117
			self.log.debug "Output was:\n#{@output}"
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   118
		end
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   119
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   120
		return true
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   121
	end
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   122
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   123
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   124
	#########
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   125
	protected
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   126
	#########
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   127
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   128
	### Generate a unique filename for the script on the remote host,
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   129
	### based on +template+ name.
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   130
	###
19
f31d60b04f8a Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents: 11
diff changeset
   131
	def make_remote_filename( template, tempdir="/tmp/" )
0
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   132
		basename = File.basename( template, File.extname(template) )
19
f31d60b04f8a Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents: 11
diff changeset
   133
		tmpname  = "%s%s-%s" % [
11
ffa70066522c Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   134
			tempdir,
ffa70066522c Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   135
			basename,
ffa70066522c Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   136
			SecureRandom.hex( 6 )
ffa70066522c Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   137
		]
0
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   138
11
ffa70066522c Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   139
		return tmpname
0
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   140
	end
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   141
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   142
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   143
	### Generate a script by loading the script +template+, populating it with
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   144
	### +attributes+, and returning the rendered output.
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   145
	###
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   146
	def generate_script( template, attributes )
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   147
		tmpl = Inversion::Template.load( template, TEMPLATE_OPTS )
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   148
		tmpl.attributes.merge!( attributes )
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   149
		tmpl.task = self
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   150
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   151
		return tmpl.render
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   152
	end
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   153
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   154
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   155
	### Upload the templated +source+ via the ssh +conn+ to an
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   156
	### executable file named +remote_filename+.
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   157
	###
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   158
	def upload_script( conn, source, remote_filename )
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   159
		conn.sftp.file.open( remote_filename, "w", 0755 ) do |fh|
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   160
			fh.print( source )
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   161
		end
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   162
	end
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   163
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   164
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   165
	### Run the +remote_filename+ via the ssh +conn+.  The script
19
f31d60b04f8a Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents: 11
diff changeset
   166
	### will be deleted automatically unless +nocleanup+ is set
f31d60b04f8a Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents: 11
diff changeset
   167
	### in the payload.
0
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   168
	###
19
f31d60b04f8a Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents: 11
diff changeset
   169
	def run_script( conn, remote_filename, payload )
f31d60b04f8a Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents: 11
diff changeset
   170
		delete_cmd = payload[ 'delete_cmd' ] || 'rm'
f31d60b04f8a Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents: 11
diff changeset
   171
		command    = remote_filename
f31d60b04f8a Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents: 11
diff changeset
   172
		command    = "%s %s" % [ payload['run_binary'], remote_filename ] if payload[ 'run_binary' ]
f31d60b04f8a Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents: 11
diff changeset
   173
f31d60b04f8a Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents: 11
diff changeset
   174
		@output = conn.exec!( command )
f31d60b04f8a Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents: 11
diff changeset
   175
		conn.exec!( "#{delete_cmd} #{remote_filename}" ) unless payload[ 'nocleanup' ]
0
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   176
	end
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   177
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   178
end # Symphony::Task::SSHScript
aef8f9f4a788 Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   179