author | Mahlon E. Smith <mahlon@martini.nu> |
Fri, 16 May 2014 08:28:27 -0700 | |
changeset 5 | 6177a734f764 |
parent 4 | 3972315383b3 |
child 6 | 5afabb9d8a2c |
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 'shellwords' |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
5 |
require 'symphony/task' unless defined?( Symphony::Task ) |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
6 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
7 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
8 |
### A base class for connecting to remote hosts, running arbitrary |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
9 |
### commands, and collecting output. |
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 |
### 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
|
12 |
### 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
|
13 |
### 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
|
14 |
### #work method. |
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 |
### 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
|
17 |
### |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
18 |
### 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
|
19 |
### command: (required) The command to run on the remote host |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
20 |
### 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
|
21 |
### opts: (optional) Explicit SSH client options |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
22 |
### 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
|
23 |
### 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
|
24 |
### |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
25 |
### |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
26 |
### 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
|
27 |
### key. Currently, you can set the 'path' argument, which is the |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
28 |
### full path to the local ssh binary (defaults to '/usr/bin/ssh') and |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
29 |
### override the default ssh user, key, and client opts. |
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 |
### 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
|
32 |
### |
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 |
### require 'symphony' |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
35 |
### require 'symphony/tasks/ssh' |
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 |
### class YourTask < Symphony::Task::SSH |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
38 |
### timeout 5 |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
39 |
### subscribe_to 'ssh.command' |
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 |
### def work( payload, metadata ) |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
42 |
### status = super |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
43 |
### puts "Remote host said: %s" % [ @output ] |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
44 |
### return status.success? |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
45 |
### end |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
46 |
### end |
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 |
class Symphony::Task::SSH < Symphony::Task |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
49 |
extend Configurability |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
50 |
config_key :symphony_ssh |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
51 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
52 |
# SSH default options. |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
53 |
# |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
54 |
CONFIG_DEFAULTS = { |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
55 |
:path => '/usr/bin/ssh', |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
56 |
:opts => [ |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
57 |
'-e', 'none', |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
58 |
'-T', |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
59 |
'-x', |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
60 |
'-q', |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
61 |
'-o', 'CheckHostIP=no', |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
62 |
'-o', 'BatchMode=yes', |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
63 |
'-o', 'StrictHostKeyChecking=no' |
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 |
:user => 'root', |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
66 |
:key => nil |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
67 |
} |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
68 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
69 |
# SSH "informative" stdout output that should be cleaned from the |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
70 |
# command output. |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
71 |
SSH_CLEANUP = %r/Warning: no access to tty|Thus no job control in this shell/ |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
72 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
73 |
class << self |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
74 |
# The full path to the ssh binary. |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
75 |
attr_reader :path |
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 |
# A default set of ssh client options when connecting |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
78 |
# to remote hosts. |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
79 |
attr_reader :opts |
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 |
# 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
|
82 |
attr_reader :user |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
83 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
84 |
# 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
|
85 |
attr_reader :key |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
86 |
end |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
87 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
88 |
### Configurability API. |
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 |
def self::configure( config=nil ) |
3
62196065e9ea
Use full class paths with configurability, so additional subclassing
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
91 |
config = Symphony::Task::SSH.defaults.merge( config || {} ) |
0
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
92 |
@path = config.delete( :path ) |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
93 |
@opts = config.delete( :opts ) |
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, passing the command to the pipe |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
101 |
### and retreiving any output from the remote end. |
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 |
command = payload[ 'command' ] |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
105 |
raise ArgumentError, "Missing required option 'command'" unless command |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
106 |
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
|
107 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
108 |
exitcode = self.open_connection( payload, metadata ) do |reader, writer| |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
109 |
self.log.debug "Writing command #{command}..." |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
110 |
writer.puts( command ) |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
111 |
self.log.debug " closing child's writer." |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
112 |
writer.close |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
113 |
self.log.debug " reading from child." |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
114 |
reader.read |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
115 |
end |
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 |
self.log.debug "SSH exited: %d" % [ exitcode ] |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
118 |
return exitcode |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
119 |
end |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
120 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
121 |
|
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 |
protected |
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 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
126 |
### Call ssh and yield the remote IO objects to the caller, |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
127 |
### cleaning up afterwards. |
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 |
def open_connection( payload, metadata=nil ) |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
130 |
raise LocalJumpError, "no block given" unless block_given? |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
131 |
@output = '' |
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 |
port = payload[ 'port' ] || 22 |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
134 |
opts = payload[ 'opts' ] || Symphony::Task::SSH.opts |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
135 |
user = payload[ 'user' ] || Symphony::Task::SSH.user |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
136 |
key = payload[ 'key' ] || Symphony::Task::SSH.key |
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 |
cmd = [] |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
139 |
cmd << Symphony::Task::SSH.path |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
140 |
cmd += Symphony::Task::SSH.opts |
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 |
cmd << '-p' << port.to_s |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
143 |
cmd << '-i' << key if key |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
144 |
cmd << '-l' << user |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
145 |
cmd << payload[ 'host' ] |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
146 |
cmd.flatten! |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
147 |
self.log.debug "Running SSH command with: %p" % [ Shellwords.shelljoin(cmd) ] |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
148 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
149 |
parent_reader, child_writer = IO.pipe |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
150 |
child_reader, parent_writer = IO.pipe |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
151 |
|
4
3972315383b3
Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
3
diff
changeset
|
152 |
pid = Process.spawn( *cmd, :out => child_writer, :in => child_reader, :close_others => true ) |
0
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
153 |
child_writer.close |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
154 |
child_reader.close |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
155 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
156 |
self.log.debug "Yielding back to the run block." |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
157 |
@output = yield( parent_reader, parent_writer ) |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
158 |
@output = @output.split("\n").reject{|l| l =~ SSH_CLEANUP }.join |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
159 |
self.log.debug " run block done." |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
160 |
|
5
6177a734f764
Zombie killer. Make sure we reap our children, even if they are
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
161 |
status = nil |
6177a734f764
Zombie killer. Make sure we reap our children, even if they are
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
162 |
|
6177a734f764
Zombie killer. Make sure we reap our children, even if they are
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
163 |
ensure |
6177a734f764
Zombie killer. Make sure we reap our children, even if they are
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
164 |
pid, status = Process.waitpid2( pid ) if pid |
0
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
165 |
return status |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
166 |
end |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
167 |
|
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
168 |
end # Symphony::Task::SSH |
aef8f9f4a788
Initial commit -- ssh base classes, converted from GroundControl and
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
169 |