1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
|
#
# Copyright CEA/DAM/DIF (2007, 2008, 2009, 2010, 2011)
# Contributor: Stephane THIELL <stephane.thiell@cea.fr>
#
# This file is part of the ClusterShell library.
#
# This software is governed by the CeCILL-C license under French law and
# abiding by the rules of distribution of free software. You can use,
# modify and/ or redistribute the software under the terms of the CeCILL-C
# license as circulated by CEA, CNRS and INRIA at the following URL
# "http://www.cecill.info".
#
# As a counterpart to the access to the source code and rights to copy,
# modify and redistribute granted by the license, users are provided only
# with a limited warranty and the software's author, the holder of the
# economic rights, and the successive licensors have only limited
# liability.
#
# In this respect, the user's attention is drawn to the risks associated
# with loading, using, modifying and/or developing or reproducing the
# software by the user in light of its specific status of free software,
# that may mean that it is complicated to manipulate, and that also
# therefore means that it is reserved for developers and experienced
# professionals having in-depth computer knowledge. Users are therefore
# encouraged to load and test the software's suitability as regards their
# requirements in conditions enabling the security of their systems and/or
# data to be ensured and, more generally, to use and operate it in the
# same conditions as regards security.
#
# The fact that you are presently reading this means that you have had
# knowledge of the CeCILL-C license and that you accept its terms.
"""
ClusterShell worker interface.
A worker is a generic object which provides "grouped" work in a specific task.
"""
from ClusterShell.Worker.EngineClient import EngineClient
from ClusterShell.NodeSet import NodeSet
import os
class WorkerException(Exception):
"""Generic worker exception."""
class WorkerError(WorkerException):
"""Generic worker error."""
# DEPRECATED: WorkerBadArgumentError exception is deprecated as of 1.4,
# use ValueError instead.
WorkerBadArgumentError = ValueError
class Worker(object):
"""
Worker is an essential base class for the ClusterShell library. The goal
of a worker object is to execute a common work on a single or several
targets (abstract notion) in parallel. Concret targets and also the notion
of local or distant targets are managed by Worker's subclasses (for
example, see the DistantWorker base class).
A configured Worker object is associated to a specific ClusterShell Task,
which can be seen as a single-threaded Worker supervisor. Indeed, the work
to be done is executed in parallel depending on other Workers and Task's
current paramaters, like current fanout value.
ClusterShell is designed to write event-driven applications, and the Worker
class is key here as Worker objects are passed as parameter of most event
handlers (see the ClusterShell.Event.EventHandler class).
The following public object variables are defined on some events, so you
may find them useful in event handlers:
- worker.current_node [ev_read,ev_error,ev_hup]
node/key concerned by event
- worker.current_msg [ev_read]
message just read (from stdout)
- worker.current_errmsg [ev_error]
error message just read (from stderr)
- worker.current_rc [ev_hup]
return code just received
Example of use:
>>> from ClusterShell.Event import EventHandler
>>> class MyOutputHandler(EventHandler):
... def ev_read(self, worker):
... node = worker.current_node
... line = worker.current_msg
... print "%s: %s" % (node, line)
...
"""
def __init__(self, handler):
"""
Initializer. Should be called from derived classes.
"""
# Associated EventHandler object
self.eh = handler
# Parent task (once bound)
self.task = None
self.started = False
self.metaworker = None
self.metarefcnt = 0
# current_x public variables (updated at each event accordingly)
self.current_node = None
self.current_msg = None
self.current_errmsg = None
self.current_rc = 0
def _set_task(self, task):
"""
Bind worker to task. Called by task.schedule()
"""
if self.task is not None:
# one-shot-only schedule supported for now
raise WorkerError("worker has already been scheduled")
self.task = task
def _task_bound_check(self):
if not self.task:
raise WorkerError("worker is not task bound")
def _engine_clients(self):
"""
Return a list of underlying engine clients.
"""
raise NotImplementedError("Derived classes must implement.")
def _on_start(self):
"""
Starting worker.
"""
if not self.started:
self.started = True
if self.eh:
self.eh.ev_start(self)
# Base getters
def last_read(self):
"""
Get last read message from event handler.
[DEPRECATED] use current_msg
"""
raise NotImplementedError("Derived classes must implement.")
def last_error(self):
"""
Get last error message from event handler.
[DEPRECATED] use current_errmsg
"""
raise NotImplementedError("Derived classes must implement.")
def did_timeout(self):
"""
Return whether this worker has aborted due to timeout.
"""
self._task_bound_check()
return self.task._num_timeout_by_worker(self) > 0
# Base actions
def abort(self):
"""
Abort processing any action by this worker.
"""
raise NotImplementedError("Derived classes must implement.")
def flush_buffers(self):
"""
Flush any messages associated to this worker.
"""
self._task_bound_check()
self.task._flush_buffers_by_worker(self)
def flush_errors(self):
"""
Flush any error messages associated to this worker.
"""
self._task_bound_check()
self.task._flush_errors_by_worker(self)
class DistantWorker(Worker):
"""
Base class DistantWorker, which provides a useful set of setters/getters
to use with distant workers like ssh or pdsh.
"""
def _on_node_msgline(self, node, msg):
"""
Message received from node, update last* stuffs.
"""
# Maxoptimize this method as it might be called very often.
task = self.task
handler = self.eh
self.current_node = node
self.current_msg = msg
if task._msgtree is not None: # don't waste time
task._msg_add((self, node), msg)
if handler is not None:
handler.ev_read(self)
def _on_node_errline(self, node, msg):
"""
Error message received from node, update last* stuffs.
"""
task = self.task
handler = self.eh
self.current_node = node
self.current_errmsg = msg
if task._errtree is not None:
task._errmsg_add((self, node), msg)
if handler is not None:
handler.ev_error(self)
def _on_node_rc(self, node, rc):
"""
Return code received from a node, update last* stuffs.
"""
self.current_node = node
self.current_rc = rc
self.task._rc_set((self, node), rc)
if self.eh:
self.eh.ev_hup(self)
def _on_node_timeout(self, node):
"""
Update on node timeout.
"""
# Update current_node to allow node resolution after ev_timeout.
self.current_node = node
self.task._timeout_add((self, node))
def last_node(self):
"""
Get last node, useful to get the node in an EventHandler
callback like ev_read().
[DEPRECATED] use current_node
"""
return self.current_node
def last_read(self):
"""
Get last (node, buffer), useful in an EventHandler.ev_read()
[DEPRECATED] use (current_node, current_msg)
"""
return self.current_node, self.current_msg
def last_error(self):
"""
Get last (node, error_buffer), useful in an EventHandler.ev_error()
[DEPRECATED] use (current_node, current_errmsg)
"""
return self.current_node, self.current_errmsg
def last_retcode(self):
"""
Get last (node, rc), useful in an EventHandler.ev_hup()
[DEPRECATED] use (current_node, current_rc)
"""
return self.current_node, self.current_rc
def node_buffer(self, node):
"""
Get specific node buffer.
"""
self._task_bound_check()
return self.task._msg_by_source((self, node))
def node_error(self, node):
"""
Get specific node error buffer.
"""
self._task_bound_check()
return self.task._errmsg_by_source((self, node))
node_error_buffer = node_error
def node_retcode(self, node):
"""
Get specific node return code. Raises a KeyError if command on
node has not yet finished (no return code available), or is
node is not known by this worker.
"""
self._task_bound_check()
try:
rc = self.task._rc_by_source((self, node))
except KeyError:
raise KeyError(node)
return rc
node_rc = node_retcode
def iter_buffers(self, match_keys=None):
"""
Returns an iterator over available buffers and associated
NodeSet. If the optional parameter match_keys is defined, only
keys found in match_keys are returned.
"""
self._task_bound_check()
for msg, keys in self.task._call_tree_matcher( \
self.task._msgtree.walk, match_keys, self):
yield msg, NodeSet.fromlist(keys)
def iter_errors(self, match_keys=None):
"""
Returns an iterator over available error buffers and associated
NodeSet. If the optional parameter match_keys is defined, only
keys found in match_keys are returned.
"""
self._task_bound_check()
for msg, keys in self.task._call_tree_matcher( \
self.task._errtree.walk, match_keys, self):
yield msg, NodeSet.fromlist(keys)
def iter_node_buffers(self, match_keys=None):
"""
Returns an iterator over each node and associated buffer.
"""
self._task_bound_check()
return self.task._call_tree_matcher(self.task._msgtree.items,
match_keys, self)
def iter_node_errors(self, match_keys=None):
"""
Returns an iterator over each node and associated error buffer.
"""
self._task_bound_check()
return self.task._call_tree_matcher(self.task._errtree.items,
match_keys, self)
def iter_retcodes(self, match_keys=None):
"""
Returns an iterator over return codes and associated NodeSet.
If the optional parameter match_keys is defined, only keys
found in match_keys are returned.
"""
self._task_bound_check()
for rc, keys in self.task._rc_iter_by_worker(self, match_keys):
yield rc, NodeSet.fromlist(keys)
def iter_node_retcodes(self):
"""
Returns an iterator over each node and associated return code.
"""
self._task_bound_check()
return self.task._krc_iter_by_worker(self)
def num_timeout(self):
"""
Return the number of timed out "keys" (ie. nodes) for this worker.
"""
self._task_bound_check()
return self.task._num_timeout_by_worker(self)
def iter_keys_timeout(self):
"""
Iterate over timed out keys (ie. nodes) for a specific worker.
"""
self._task_bound_check()
return self.task._iter_keys_timeout_by_worker(self)
class WorkerSimple(EngineClient, Worker):
"""
Implements a simple Worker being itself an EngineClient.
"""
def __init__(self, file_reader, file_writer, file_error, key, handler,
stderr=False, timeout=-1, autoclose=False):
"""
Initialize worker.
"""
Worker.__init__(self, handler)
EngineClient.__init__(self, self, stderr, timeout, autoclose)
if key is None: # allow key=0
self.key = self
else:
self.key = key
if file_reader:
self.fd_reader = file_reader.fileno()
if file_error:
self.fd_error = file_error.fileno()
if file_writer:
self.fd_writer = file_writer.fileno()
def _engine_clients(self):
"""
Return a list of underlying engine clients.
"""
return [self]
def set_key(self, key):
"""
Source key for this worker is free for use. Use this method to
set the custom source key for this worker.
"""
self.key = key
def _start(self):
"""
Called on EngineClient start.
"""
# call Worker._on_start()
self._on_start()
return self
def _read(self, size=65536):
"""
Read data from process.
"""
return EngineClient._read(self, size)
def _readerr(self, size=65536):
"""
Read error data from process.
"""
return EngineClient._readerr(self, size)
def _close(self, abort, flush, timeout):
"""
Close client. See EngineClient._close().
"""
if flush and self._rbuf:
# We still have some read data available in buffer, but no
# EOL. Generate a final message before closing.
self.worker._on_msgline(self._rbuf)
if self.fd_reader:
os.close(self.fd_reader)
if self.fd_error:
os.close(self.fd_error)
if self.fd_writer:
os.close(self.fd_writer)
if timeout:
assert abort, "abort flag not set on timeout"
self._on_timeout()
if self.eh:
self.eh.ev_close(self)
def _handle_read(self):
"""
Engine is telling us there is data available for reading.
"""
# Local variables optimization
task = self.worker.task
msgline = self._on_msgline
debug = task.info("debug", False)
if debug:
print_debug = task.info("print_debug")
for msg in self._readlines():
print_debug(task, "LINE %s" % msg)
msgline(msg)
else:
for msg in self._readlines():
msgline(msg)
def _handle_error(self):
"""
Engine is telling us there is error available for reading.
"""
task = self.worker.task
errmsgline = self._on_errmsgline
debug = task.info("debug", False)
if debug:
print_debug = task.info("print_debug")
for msg in self._readerrlines():
print_debug(task, "LINE@STDERR %s" % msg)
errmsgline(msg)
else:
for msg in self._readerrlines():
errmsgline(msg)
def last_read(self):
"""
Read last msg, useful in an EventHandler.
"""
return self.current_msg
def last_error(self):
"""
Get last error message from event handler.
"""
return self.current_errmsg
def _on_msgline(self, msg):
"""
Add a message.
"""
# add last msg to local buffer
self.current_msg = msg
# update task
self.task._msg_add((self, self.key), msg)
if self.eh:
self.eh.ev_read(self)
def _on_errmsgline(self, msg):
"""
Add a message.
"""
# add last msg to local buffer
self.current_errmsg = msg
# update task
self.task._errmsg_add((self, self.key), msg)
if self.eh:
self.eh.ev_error(self)
def _on_rc(self, rc):
"""
Set return code received.
"""
self.current_rc = rc
self.task._rc_set((self, self.key), rc)
if self.eh:
self.eh.ev_hup(self)
def _on_timeout(self):
"""
Update on timeout.
"""
self.task._timeout_add((self, self.key))
# trigger timeout event
if self.eh:
self.eh.ev_timeout(self)
def read(self):
"""
Read worker buffer.
"""
self._task_bound_check()
for key, msg in self.task._call_tree_matcher(self.task._msgtree.items,
worker=self):
assert key == self.key
return str(msg)
def error(self):
"""
Read worker error buffer.
"""
self._task_bound_check()
for key, msg in self.task._call_tree_matcher(self.task._errtree.items,
worker=self):
assert key == self.key
return str(msg)
def write(self, buf):
"""
Write to worker.
"""
self._write(buf)
def set_write_eof(self):
"""
Tell worker to close its writer file descriptor once flushed. Do not
perform writes after this call.
"""
self._set_write_eof()
|