author | Mahlon E. Smith <mahlon@martini.nu> |
Sat, 10 May 2014 17:40:36 -0700 | |
changeset 0 | aef8f9f4a788 |
child 3 | 62196065e9ea |
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 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
4 |
require 'net/ssh' |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
5 |
require 'net/sftp' |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
6 |
require 'tmpdir' |
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' |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
10 |
|
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 |
### 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
|
13 |
### executing an Inversion templated script. |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
14 |
### |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
15 |
### 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
|
16 |
### 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
|
17 |
### 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
|
18 |
### #work method. |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
19 |
### |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
20 |
### 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
|
21 |
### |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
22 |
### 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
|
23 |
### 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
|
24 |
### 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
|
25 |
### 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
|
26 |
### 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
|
27 |
### 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
|
28 |
### nocleanup: (optional) Leave the remote script after execution? (default to false) |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
29 |
### |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
30 |
### |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
31 |
### Additionally, this class responds to the 'symphony_ssh' configurability |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
32 |
### 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
|
33 |
### |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
34 |
### 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
|
35 |
### |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
36 |
### |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
37 |
### require 'symphony' |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
38 |
### require 'symphony/tasks/sshscript' |
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 |
### class YourTask < Symphony::Task::SSHScript |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
41 |
### timeout 30 |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
42 |
### subscribe_to 'ssh.script.*' |
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 |
### def work( payload, metadata ) |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
45 |
### status = super |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
46 |
### 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
|
47 |
### return status.success? |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
48 |
### end |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
49 |
### end |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
50 |
### |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
51 |
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
|
52 |
extend Configurability |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
53 |
config_key :symphony_ssh |
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 = { |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
58 |
:ignore_unknown_tags => false, |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
59 |
:on_render_error => :propagate, |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
60 |
:strip_tag_lines => true |
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 = { |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
66 |
:auth_methods => [ 'publickey' ], |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
67 |
:compression => true, |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
68 |
:config => false, |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
69 |
:keys_only => true, |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
70 |
:paranoid => false, |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
71 |
:global_known_hosts_file => '/dev/null', |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
72 |
:user_known_hosts_file => '/dev/null' |
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 |
# SSH default options. |
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 |
CONFIG_DEFAULTS = { |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
78 |
:user => 'root', |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
79 |
:key => nil |
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 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
82 |
class << self |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
83 |
# The default user to use when connecting. If unset, 'root' is used. |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
84 |
attr_reader :user |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
85 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
86 |
# An absolute path to a password-free ssh private key. |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
87 |
attr_reader :key |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
88 |
end |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
89 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
90 |
### Configurability API. |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
91 |
### |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
92 |
def self::configure( config=nil ) |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
93 |
config = self.defaults.merge( config || {} ) |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
94 |
@user = config.delete( :user ) |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
95 |
@key = config.delete( :key ) |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
96 |
super |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
97 |
end |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
98 |
|
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 |
### 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
|
101 |
### execute it. |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
102 |
### |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
103 |
def work( payload, metadata ) |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
104 |
template = payload[ 'template' ] |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
105 |
attributes = payload[ 'attributes' ] || {} |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
106 |
port = payload[ 'port' ] || 22 |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
107 |
user = payload[ 'user' ] || Symphony::Task::SSHScript.user |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
108 |
key = payload[ 'key' ] || Symphony::Task::SSHScript.key |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
109 |
nocleanup = payload[ 'nocleanup' ] |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
110 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
111 |
raise ArgumentError, "Missing required option 'command'" unless template |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
112 |
raise ArgumentError, "Missing required option 'host'" unless payload[ 'host' ] |
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 |
remote_filename = self.make_remote_filename( template ) |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
115 |
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
|
116 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
117 |
ssh_options = DEFAULT_SSH_OPTIONS.merge( :port => port, :keys => [key] ) |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
118 |
ssh_options.merge!( |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
119 |
:logger => Loggability[ Net::SSH ], |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
120 |
:verbose => :debug |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
121 |
) if payload[ 'debug' ] |
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 |
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
|
124 |
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
|
125 |
[ 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
|
126 |
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
|
127 |
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
|
128 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
129 |
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
|
130 |
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
|
131 |
end |
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 |
return true |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
134 |
end |
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 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
137 |
######### |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
138 |
protected |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
139 |
######### |
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 |
### 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
|
142 |
### based on +template+ name. |
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 |
def make_remote_filename( template ) |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
145 |
basename = File.basename( template, File.extname(template) ) |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
146 |
tmpname = Dir::Tmpname.make_tmpname( basename, rand(10000) ) |
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 |
return "/tmp/#{tmpname}" |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
149 |
end |
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 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
152 |
### 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
|
153 |
### +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
|
154 |
### |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
155 |
def generate_script( template, attributes ) |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
156 |
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
|
157 |
tmpl.attributes.merge!( attributes ) |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
158 |
tmpl.task = self |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
159 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
160 |
return tmpl.render |
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 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
164 |
### 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
|
165 |
### executable file named +remote_filename+. |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
166 |
### |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
167 |
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
|
168 |
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
|
169 |
fh.print( source ) |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
170 |
end |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
171 |
end |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
172 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
173 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
174 |
### 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
|
175 |
### 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
|
176 |
### |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
177 |
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
|
178 |
@output = conn.exec!( remote_filename ) |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
179 |
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
|
180 |
end |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
181 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
182 |
end # Symphony::Task::SSHScript |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
183 |