author | Mahlon E. Smith <mahlon@martini.nu> |
Thu, 12 Jul 2018 15:25:35 -0700 | |
changeset 11 | ffa70066522c |
parent 4 | 3972315383b3 |
child 19 | f31d60b04f8a |
permissions | -rw-r--r-- |
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) |
11
ffa70066522c
Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
30 |
### tempdir: (optional) The destination temp directory. (defaults to /tmp) |
0
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
31 |
### |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
32 |
### |
11
ffa70066522c
Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
33 |
### 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
|
34 |
### 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
|
35 |
### |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
36 |
### 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
|
37 |
### |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
38 |
### |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
39 |
### require 'symphony' |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
40 |
### require 'symphony/tasks/sshscript' |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
41 |
### |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
42 |
### class YourTask < Symphony::Task::SSHScript |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
43 |
### timeout 30 |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
44 |
### subscribe_to 'ssh.script.*' |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
45 |
### |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
46 |
### def work( payload, metadata ) |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
47 |
### status = super |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
48 |
### 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
|
49 |
### return status.success? |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
50 |
### end |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
51 |
### end |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
52 |
### |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
53 |
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
|
54 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
55 |
# Template config |
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_OPTS = { |
11
ffa70066522c
Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
58 |
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
|
59 |
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
|
60 |
strip_tag_lines: true |
0
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
61 |
} |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
62 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
63 |
# 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
|
64 |
# |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
65 |
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
|
66 |
auth_methods: [ 'publickey' ], |
ffa70066522c
Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
67 |
compression: true, |
ffa70066522c
Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
68 |
config: false, |
ffa70066522c
Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
69 |
keys_only: true, |
ffa70066522c
Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
70 |
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
|
71 |
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
|
72 |
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
|
73 |
} |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
74 |
|
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 |
### 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
|
77 |
### execute it. |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
78 |
### |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
79 |
def work( payload, metadata ) |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
80 |
template = payload[ 'template' ] |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
81 |
attributes = payload[ 'attributes' ] || {} |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
82 |
port = payload[ 'port' ] || 22 |
11
ffa70066522c
Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
83 |
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
|
84 |
key = payload[ 'key' ] || Symphony::Task::SSH.key |
0
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
85 |
nocleanup = payload[ 'nocleanup' ] |
11
ffa70066522c
Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
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 |
|
11
ffa70066522c
Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
94 |
ssh_options = DEFAULT_SSH_OPTIONS.merge( port: port, keys: Array(key) ) |
0
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
95 |
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
|
96 |
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
|
97 |
verbose: :debug |
0
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
98 |
) if payload[ 'debug' ] |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
99 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
100 |
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
|
101 |
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
|
102 |
[ 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
|
103 |
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
|
104 |
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
|
105 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
106 |
self.run_script( conn, remote_filename, nocleanup ) |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
107 |
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
|
108 |
end |
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 |
return true |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
111 |
end |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
112 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
113 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
114 |
######### |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
115 |
protected |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
116 |
######### |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
117 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
118 |
### 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
|
119 |
### based on +template+ name. |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
120 |
### |
11
ffa70066522c
Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
121 |
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
|
122 |
basename = File.basename( template, File.extname(template) ) |
11
ffa70066522c
Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
123 |
tmpname = "%s/%s-%s" % [ |
ffa70066522c
Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
124 |
tempdir, |
ffa70066522c
Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
125 |
basename, |
ffa70066522c
Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
126 |
SecureRandom.hex( 6 ) |
ffa70066522c
Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
127 |
] |
0
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
128 |
|
11
ffa70066522c
Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
129 |
return tmpname |
0
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
130 |
end |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
131 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
132 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
133 |
### 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
|
134 |
### +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
|
135 |
### |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
136 |
def generate_script( template, attributes ) |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
137 |
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
|
138 |
tmpl.attributes.merge!( attributes ) |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
139 |
tmpl.task = self |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
140 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
141 |
return tmpl.render |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
142 |
end |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
143 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
144 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
145 |
### 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
|
146 |
### executable file named +remote_filename+. |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
147 |
### |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
148 |
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
|
149 |
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
|
150 |
fh.print( source ) |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
151 |
end |
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 |
### Run the +remote_filename+ via the ssh +conn+. The script |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
156 |
### will be deleted automatically unless +nocleanup+ is true. |
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 run_script( conn, remote_filename, nocleanup=false ) |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
159 |
@output = conn.exec!( remote_filename ) |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
160 |
conn.exec!( "rm #{remote_filename}" ) unless nocleanup |
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 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
163 |
end # Symphony::Task::SSHScript |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
164 |