| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
""" |
| 21 |
WorkerPdsh |
| 22 |
|
| 23 |
ClusterShell worker for executing commands with LLNL pdsh. |
| 24 |
""" |
| 25 |
|
| 26 |
import errno |
| 27 |
import os |
| 28 |
import shlex |
| 29 |
|
| 30 |
from ClusterShell.NodeSet import NodeSet |
| 31 |
from ClusterShell.Worker.EngineClient import EngineClientError |
| 32 |
from ClusterShell.Worker.EngineClient import EngineClientNotSupportedError |
| 33 |
from ClusterShell.Worker.Worker import WorkerError |
| 34 |
from ClusterShell.Worker.Exec import ExecWorker, ExecClient, CopyClient |
| 35 |
|
| 36 |
|
| 37 |
class PdshClient(ExecClient): |
| 38 |
"""EngineClient which run 'pdsh'""" |
| 39 |
|
| 40 |
MODE = 'pdsh' |
| 41 |
|
| 42 |
def __init__(self, node, command, worker, stderr, timeout, autoclose=False, |
| 43 |
rank=None): |
| 44 |
ExecClient.__init__(self, node, command, worker, stderr, timeout, |
| 45 |
autoclose, rank) |
| 46 |
self._closed_nodes = NodeSet() |
| 47 |
|
| 48 |
def _build_cmd(self): |
| 49 |
""" |
| 50 |
Build the shell command line to start the command. |
| 51 |
Return an array of command and arguments. |
| 52 |
""" |
| 53 |
task = self.worker.task |
| 54 |
pdsh_env = {} |
| 55 |
|
| 56 |
|
| 57 |
path = task.info("pdsh_path") or "pdsh" |
| 58 |
cmd_l = [os.path.expanduser(pathc) for pathc in shlex.split(path)] |
| 59 |
cmd_l.append("-b") |
| 60 |
|
| 61 |
fanout = task.info("fanout", 0) |
| 62 |
if fanout > 0: |
| 63 |
cmd_l.append("-f %d" % fanout) |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
connect_timeout = task.info("connect_timeout", 0) |
| 69 |
if connect_timeout > 0: |
| 70 |
pdsh_env['PDSH_SSH_ARGS_APPEND'] = "-o ConnectTimeout=%d" % \ |
| 71 |
connect_timeout |
| 72 |
|
| 73 |
command_timeout = task.info("command_timeout", 0) |
| 74 |
if command_timeout > 0: |
| 75 |
cmd_l.append("-u %d" % command_timeout) |
| 76 |
|
| 77 |
cmd_l.append("-w %s" % self.key) |
| 78 |
cmd_l.append("%s" % self.command) |
| 79 |
|
| 80 |
return (cmd_l, pdsh_env) |
| 81 |
|
| 82 |
def _close(self, abort, timeout): |
| 83 |
"""Close client. See EngineClient._close().""" |
| 84 |
if abort: |
| 85 |
|
| 86 |
prc = self.popen.poll() |
| 87 |
|
| 88 |
if prc is None: |
| 89 |
try: |
| 90 |
self.popen.kill() |
| 91 |
except OSError: |
| 92 |
pass |
| 93 |
prc = self.popen.wait() |
| 94 |
|
| 95 |
if prc > 0: |
| 96 |
raise WorkerError("Cannot run pdsh (error %d)" % prc) |
| 97 |
|
| 98 |
self.streams.clear() |
| 99 |
self.invalidate() |
| 100 |
|
| 101 |
if timeout: |
| 102 |
assert abort, "abort flag not set on timeout" |
| 103 |
for node in (self.key - self._closed_nodes): |
| 104 |
self.worker._on_node_timeout(node) |
| 105 |
else: |
| 106 |
for node in (self.key - self._closed_nodes): |
| 107 |
self.worker._on_node_close(node, 0) |
| 108 |
|
| 109 |
self.worker._check_fini() |
| 110 |
|
| 111 |
def _parse_line(self, line, sname): |
| 112 |
""" |
| 113 |
Parse Pdsh line syntax. |
| 114 |
""" |
| 115 |
if line.startswith(b"pdsh@") or \ |
| 116 |
line.startswith(b"pdcp@") or \ |
| 117 |
line.startswith(b"sending "): |
| 118 |
try: |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
words = line.split() |
| 132 |
|
| 133 |
if self.MODE == 'pdsh': |
| 134 |
if len(words) == 4 and words[2] == b"command" and \ |
| 135 |
words[3] == b"timeout": |
| 136 |
pass |
| 137 |
elif len(words) == 8 and words[3] == b"exited" and \ |
| 138 |
words[7].isdigit(): |
| 139 |
nodename = words[1][:-1].decode() |
| 140 |
self._closed_nodes.add(nodename) |
| 141 |
self.worker._on_node_close(nodename, int(words[7])) |
| 142 |
elif self.MODE == 'pdcp': |
| 143 |
nodename = words[1][:-1].decode() |
| 144 |
self._closed_nodes.add(nodename) |
| 145 |
self.worker._on_node_close(nodename, errno.ENOENT) |
| 146 |
|
| 147 |
except Exception as exc: |
| 148 |
raise EngineClientError("Pdsh parser error: %s" % exc) |
| 149 |
else: |
| 150 |
|
| 151 |
nodename, msg = line.split(b': ', 1) |
| 152 |
self.worker._on_node_msgline(nodename.decode(), msg, sname) |
| 153 |
|
| 154 |
def _flush_read(self, sname): |
| 155 |
"""Called at close time to flush stream read buffer.""" |
| 156 |
pass |
| 157 |
|
| 158 |
def _handle_read(self, sname): |
| 159 |
"""Engine is telling us a read is available.""" |
| 160 |
debug = self.worker.task.info("debug", False) |
| 161 |
if debug: |
| 162 |
print_debug = self.worker.task.info("print_debug") |
| 163 |
|
| 164 |
suffix = "" |
| 165 |
if sname == 'stderr': |
| 166 |
suffix = "@STDERR" |
| 167 |
|
| 168 |
for msg in self._readlines(sname): |
| 169 |
if debug: |
| 170 |
print_debug(self.worker.task, "PDSH%s: %s" % (suffix, msg)) |
| 171 |
self._parse_line(msg, sname) |
| 172 |
|
| 173 |
|
| 174 |
class PdcpClient(CopyClient, PdshClient): |
| 175 |
"""EngineClient when pdsh is run to copy file, using pdcp.""" |
| 176 |
|
| 177 |
MODE = 'pdcp' |
| 178 |
|
| 179 |
def __init__(self, node, source, dest, worker, stderr, timeout, autoclose, |
| 180 |
preserve, reverse, rank=None): |
| 181 |
CopyClient.__init__(self, node, source, dest, worker, stderr, timeout, |
| 182 |
autoclose, preserve, reverse, rank) |
| 183 |
PdshClient.__init__(self, node, None, worker, stderr, timeout, |
| 184 |
autoclose, rank) |
| 185 |
|
| 186 |
def _build_cmd(self): |
| 187 |
|
| 188 |
cmd_l = [] |
| 189 |
|
| 190 |
|
| 191 |
if self.reverse: |
| 192 |
path = self.worker.task.info("rpdcp_path") or "rpdcp" |
| 193 |
else: |
| 194 |
path = self.worker.task.info("pdcp_path") or "pdcp" |
| 195 |
cmd_l = [os.path.expanduser(pathc) for pathc in shlex.split(path)] |
| 196 |
cmd_l.append("-b") |
| 197 |
|
| 198 |
fanout = self.worker.task.info("fanout", 0) |
| 199 |
if fanout > 0: |
| 200 |
cmd_l.append("-f %d" % fanout) |
| 201 |
|
| 202 |
connect_timeout = self.worker.task.info("connect_timeout", 0) |
| 203 |
if connect_timeout > 0: |
| 204 |
cmd_l.append("-t %d" % connect_timeout) |
| 205 |
|
| 206 |
cmd_l.append("-w %s" % self.key) |
| 207 |
|
| 208 |
if self.isdir: |
| 209 |
cmd_l.append("-r") |
| 210 |
|
| 211 |
if self.preserve: |
| 212 |
cmd_l.append("-p") |
| 213 |
|
| 214 |
cmd_l.append(self.source) |
| 215 |
cmd_l.append(self.dest) |
| 216 |
|
| 217 |
return (cmd_l, None) |
| 218 |
|
| 219 |
|
| 220 |
class WorkerPdsh(ExecWorker): |
| 221 |
""" |
| 222 |
ClusterShell pdsh-based worker Class. |
| 223 |
|
| 224 |
Remote Shell (pdsh) usage example: |
| 225 |
>>> worker = WorkerPdsh(nodeset, handler=MyEventHandler(), |
| 226 |
... timeout=30, command="/bin/hostname") |
| 227 |
>>> task.schedule(worker) # schedule worker for execution |
| 228 |
>>> task.resume() # run |
| 229 |
|
| 230 |
Remote Copy (pdcp) usage example: |
| 231 |
>>> worker = WorkerPdsh(nodeset, handler=MyEventHandler(), |
| 232 |
... timeout=30, source="/etc/my.conf", |
| 233 |
... dest="/etc/my.conf") |
| 234 |
>>> task.schedule(worker) # schedule worker for execution |
| 235 |
>>> task.resume() # run |
| 236 |
|
| 237 |
Known limitations: |
| 238 |
- write() is not supported by WorkerPdsh |
| 239 |
- return codes == 0 are not guaranteed when a timeout is used (rc > 0 |
| 240 |
are fine) |
| 241 |
""" |
| 242 |
|
| 243 |
SHELL_CLASS = PdshClient |
| 244 |
COPY_CLASS = PdcpClient |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
def _create_clients(self, **kwargs): |
| 251 |
self._add_client(self.nodes, **kwargs) |
| 252 |
|
| 253 |
def write(self, buf): |
| 254 |
""" |
| 255 |
Write data to process. Not supported with Pdsh worker. |
| 256 |
""" |
| 257 |
raise EngineClientNotSupportedError("writing to stdin is not " |
| 258 |
"supported by pdsh worker") |
| 259 |
|
| 260 |
def set_write_eof(self): |
| 261 |
""" |
| 262 |
Tell worker to close its writer file descriptor once flushed. Do not |
| 263 |
perform writes after this call. |
| 264 |
|
| 265 |
Not supported by PDSH Worker. |
| 266 |
""" |
| 267 |
raise EngineClientNotSupportedError("writing to stdin is not " |
| 268 |
"supported by pdsh worker") |
| 269 |
|
| 270 |
WORKER_CLASS = WorkerPdsh |