| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
""" |
| 22 |
Execute cluster commands in parallel |
| 23 |
|
| 24 |
clush is an utility program to run commands on a cluster which benefits |
| 25 |
from the ClusterShell library and its Ssh worker. It features an |
| 26 |
integrated output results gathering system (dshbak-like), can get node |
| 27 |
groups by running predefined external commands and can redirect lines |
| 28 |
read on its standard input to the remote commands. |
| 29 |
|
| 30 |
When no command are specified, clush runs interactively. |
| 31 |
|
| 32 |
""" |
| 33 |
|
| 34 |
from __future__ import print_function |
| 35 |
|
| 36 |
import getpass |
| 37 |
import logging |
| 38 |
import os |
| 39 |
from os.path import abspath, dirname, exists, isdir, join |
| 40 |
import random |
| 41 |
import resource |
| 42 |
import shlex |
| 43 |
import signal |
| 44 |
import sys |
| 45 |
import time |
| 46 |
import threading |
| 47 |
|
| 48 |
|
| 49 |
try: |
| 50 |
raw_input |
| 51 |
except NameError: |
| 52 |
raw_input = input |
| 53 |
|
| 54 |
from ClusterShell.Defaults import DEFAULTS, _load_workerclass |
| 55 |
from ClusterShell.CLI.Config import ClushConfig, ClushConfigError |
| 56 |
from ClusterShell.CLI.Display import Display, sys_stdin |
| 57 |
from ClusterShell.CLI.Display import VERB_QUIET, VERB_STD, VERB_VERB, VERB_DEBUG |
| 58 |
from ClusterShell.CLI.OptionParser import OptionParser |
| 59 |
from ClusterShell.CLI.Error import GENERIC_ERRORS, handle_generic_error |
| 60 |
from ClusterShell.CLI.Utils import bufnodeset_cmpkey, human_bi_bytes_unit, \ |
| 61 |
parse_fold_axis |
| 62 |
|
| 63 |
from ClusterShell.Event import EventHandler |
| 64 |
from ClusterShell.MsgTree import MsgTree |
| 65 |
from ClusterShell.NodeSet import RESOLVER_NOGROUP, set_std_group_resolver_config |
| 66 |
from ClusterShell.NodeSet import NodeSet, NodeSetParseError, std_group_resolver |
| 67 |
from ClusterShell.Task import Task, task_self |
| 68 |
|
| 69 |
|
| 70 |
class UpdatePromptException(Exception): |
| 71 |
"""Exception used by the signal handler""" |
| 72 |
|
| 73 |
class StdInputHandler(EventHandler): |
| 74 |
"""Standard input event handler class.""" |
| 75 |
def __init__(self, worker): |
| 76 |
EventHandler.__init__(self) |
| 77 |
self.master_worker = worker |
| 78 |
|
| 79 |
def ev_msg(self, port, msg): |
| 80 |
"""invoked when a message is received from port object""" |
| 81 |
if not msg: |
| 82 |
self.master_worker.set_write_eof() |
| 83 |
return |
| 84 |
|
| 85 |
self.master_worker.write(msg) |
| 86 |
|
| 87 |
class OutputHandler(EventHandler): |
| 88 |
"""Base class for generic output handlers.""" |
| 89 |
|
| 90 |
def __init__(self, prog=None): |
| 91 |
EventHandler.__init__(self) |
| 92 |
self._runtimer = None |
| 93 |
self._prog = prog if prog else os.path.basename(sys.argv[0]) |
| 94 |
|
| 95 |
def runtimer_init(self, task, ntotal=0): |
| 96 |
"""Init timer for live command-completed progressmeter.""" |
| 97 |
thandler = RunTimer(task, ntotal, prog=self._prog) |
| 98 |
self._runtimer = task.timer(1.33, thandler, interval=1./3., |
| 99 |
autoclose=True) |
| 100 |
|
| 101 |
def _runtimer_clean(self): |
| 102 |
"""Hide runtimer counter""" |
| 103 |
if self._runtimer: |
| 104 |
self._runtimer.eh.erase_line() |
| 105 |
|
| 106 |
def _runtimer_set_dirty(self): |
| 107 |
"""Force redisplay of counter""" |
| 108 |
if self._runtimer: |
| 109 |
self._runtimer.eh.set_dirty() |
| 110 |
|
| 111 |
def _runtimer_finalize(self, worker): |
| 112 |
"""Finalize display of runtimer counter""" |
| 113 |
if self._runtimer: |
| 114 |
self._runtimer.eh.finalize(worker.task.default("USER_interactive")) |
| 115 |
self._runtimer.invalidate() |
| 116 |
self._runtimer = None |
| 117 |
|
| 118 |
def update_prompt(self, worker): |
| 119 |
""" |
| 120 |
If needed, notify main thread to update its prompt by sending |
| 121 |
a SIGUSR1 signal. We use task-specific user-defined variable |
| 122 |
to record current states (prefixed by USER_). |
| 123 |
""" |
| 124 |
worker.task.set_default("USER_running", False) |
| 125 |
if worker.task.default("USER_handle_SIGUSR1"): |
| 126 |
os.kill(os.getpid(), signal.SIGUSR1) |
| 127 |
|
| 128 |
def ev_start(self, worker): |
| 129 |
"""Worker is starting.""" |
| 130 |
if self._runtimer: |
| 131 |
self._runtimer.eh.start_time = time.time() |
| 132 |
|
| 133 |
def ev_written(self, worker, node, sname, size): |
| 134 |
"""Bytes written on worker""" |
| 135 |
if self._runtimer: |
| 136 |
self._runtimer.eh.bytes_written += size |
| 137 |
|
| 138 |
def _ev_routing(self, worker, arg): |
| 139 |
prefix = "clush: " |
| 140 |
self._display.vprint_err(VERB_DEBUG, prefix + "_ev_routing: %s" % arg) |
| 141 |
if "reroute" in arg.get("event", ""): |
| 142 |
info_fmt = "rerouting commands for {targets} due to the failure " \ |
| 143 |
"of gateway {gateway}" |
| 144 |
self._display.vprint_err(VERB_STD, prefix + info_fmt.format(**arg)) |
| 145 |
|
| 146 |
class DirectOutputHandler(OutputHandler): |
| 147 |
"""Direct output event handler class.""" |
| 148 |
|
| 149 |
def __init__(self, display, prog=None): |
| 150 |
OutputHandler.__init__(self, prog=prog) |
| 151 |
self._display = display |
| 152 |
|
| 153 |
def ev_read(self, worker, node, sname, msg): |
| 154 |
if sname == worker.SNAME_STDOUT: |
| 155 |
self._display.print_line(node, msg) |
| 156 |
elif sname == worker.SNAME_STDERR: |
| 157 |
self._display.print_line_error(node, msg) |
| 158 |
|
| 159 |
def ev_hup(self, worker, node, rc): |
| 160 |
if rc > 0: |
| 161 |
verb = VERB_QUIET |
| 162 |
if self._display.maxrc: |
| 163 |
verb = VERB_STD |
| 164 |
self._display.vprint_err(verb, "%s: %s: exited with exit code %d" % |
| 165 |
(self._prog, node, rc)) |
| 166 |
|
| 167 |
def ev_close(self, worker, timedout): |
| 168 |
if timedout: |
| 169 |
nodeset = NodeSet._fromlist1(worker.iter_keys_timeout()) |
| 170 |
self._display.vprint_err(VERB_QUIET, |
| 171 |
"%s: %s: command timeout" % |
| 172 |
(self._prog, nodeset)) |
| 173 |
self.update_prompt(worker) |
| 174 |
|
| 175 |
class DirectOutputDirHandler(DirectOutputHandler): |
| 176 |
"""Direct output files event handler class. pssh style""" |
| 177 |
def __init__(self, display, ns, prog=None): |
| 178 |
DirectOutputHandler.__init__(self, display, prog) |
| 179 |
self._ns = ns |
| 180 |
self._outfiles = {} |
| 181 |
self._errfiles = {} |
| 182 |
if display.outdir: |
| 183 |
for n in self._ns: |
| 184 |
self._outfiles[n] = open(join(display.outdir, n), mode="w") |
| 185 |
if display.errdir: |
| 186 |
for n in self._ns: |
| 187 |
self._errfiles[n] = open(join(display.errdir, n), mode="w") |
| 188 |
|
| 189 |
def ev_read(self, worker, node, sname, msg): |
| 190 |
DirectOutputHandler.ev_read(self, worker, node, sname, msg) |
| 191 |
if sname == worker.SNAME_STDOUT: |
| 192 |
if self._display.outdir: |
| 193 |
self._outfiles[node].write("{}\n".format(msg.decode())) |
| 194 |
elif sname == worker.SNAME_STDERR: |
| 195 |
if self._display.errdir: |
| 196 |
self._errfiles[node].write("{}\n".format(msg.decode())) |
| 197 |
|
| 198 |
def ev_close(self, worker, timedout): |
| 199 |
DirectOutputHandler.ev_close(self, worker, timedout) |
| 200 |
if self._display.outdir: |
| 201 |
for v in self._outfiles.values(): |
| 202 |
v.close() |
| 203 |
if self._display.errdir: |
| 204 |
for v in self._errfiles.values(): |
| 205 |
v.close() |
| 206 |
|
| 207 |
class DirectProgressOutputHandler(DirectOutputHandler): |
| 208 |
"""Direct output event handler class with progress support.""" |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
def ev_read(self, worker, node, sname, msg): |
| 215 |
self._runtimer_clean() |
| 216 |
|
| 217 |
if sname == worker.SNAME_STDOUT: |
| 218 |
self._display.print_line(node, msg) |
| 219 |
elif sname == worker.SNAME_STDERR: |
| 220 |
self._display.print_line_error(node, msg) |
| 221 |
|
| 222 |
def ev_close(self, worker, timedout): |
| 223 |
self._runtimer_clean() |
| 224 |
DirectOutputHandler.ev_close(self, worker, timedout) |
| 225 |
|
| 226 |
class CopyOutputHandler(DirectProgressOutputHandler): |
| 227 |
"""Copy output event handler.""" |
| 228 |
def __init__(self, display, reverse=False, prog=None): |
| 229 |
DirectOutputHandler.__init__(self, display, prog=prog) |
| 230 |
self.reverse = reverse |
| 231 |
|
| 232 |
def ev_close(self, worker, timedout): |
| 233 |
"""A copy worker has finished.""" |
| 234 |
for rc, nodes in worker.iter_retcodes(): |
| 235 |
if rc == 0: |
| 236 |
if self.reverse: |
| 237 |
self._display.vprint(VERB_VERB, "%s:`%s' -> `%s'" % \ |
| 238 |
(nodes, worker.source, worker.dest)) |
| 239 |
else: |
| 240 |
self._display.vprint(VERB_VERB, "`%s' -> %s:`%s'" % \ |
| 241 |
(worker.source, nodes, worker.dest)) |
| 242 |
break |
| 243 |
|
| 244 |
copies = worker.task.default("USER_copies") - 1 |
| 245 |
worker.task.set_default("USER_copies", copies) |
| 246 |
if copies == 0: |
| 247 |
self._runtimer_finalize(worker) |
| 248 |
|
| 249 |
DirectOutputHandler.ev_close(self, worker, timedout) |
| 250 |
|
| 251 |
class GatherOutputHandler(OutputHandler): |
| 252 |
"""Gathered output event handler class (e.g. clush -b).""" |
| 253 |
|
| 254 |
def __init__(self, display, prog=None): |
| 255 |
OutputHandler.__init__(self, prog=prog) |
| 256 |
self._display = display |
| 257 |
|
| 258 |
def ev_read(self, worker, node, sname, msg): |
| 259 |
if sname == worker.SNAME_STDOUT: |
| 260 |
if self._display.verbosity == VERB_VERB: |
| 261 |
self._display.print_line(node, worker.current_msg) |
| 262 |
elif sname == worker.SNAME_STDERR: |
| 263 |
self._runtimer_clean() |
| 264 |
self._display.print_line_error(node, msg) |
| 265 |
self._runtimer_set_dirty() |
| 266 |
|
| 267 |
def ev_close(self, worker, timedout): |
| 268 |
|
| 269 |
self._runtimer_finalize(worker) |
| 270 |
|
| 271 |
nodesetify = lambda v: (v[0], NodeSet._fromlist1(v[1])) |
| 272 |
cleaned = False |
| 273 |
for _rc, nodelist in sorted(worker.iter_retcodes()): |
| 274 |
ns_remain = NodeSet._fromlist1(nodelist) |
| 275 |
|
| 276 |
for buf, nodeset in sorted(map(nodesetify, |
| 277 |
worker.iter_buffers(nodelist)), |
| 278 |
key=bufnodeset_cmpkey): |
| 279 |
if not cleaned: |
| 280 |
|
| 281 |
self._runtimer_clean() |
| 282 |
cleaned = True |
| 283 |
self._display.print_gather(nodeset, buf) |
| 284 |
ns_remain.difference_update(nodeset) |
| 285 |
if ns_remain: |
| 286 |
self._display.print_gather_finalize(ns_remain) |
| 287 |
self._display.flush() |
| 288 |
|
| 289 |
self._close_common(worker) |
| 290 |
|
| 291 |
|
| 292 |
self.update_prompt(worker) |
| 293 |
|
| 294 |
def _close_common(self, worker): |
| 295 |
verbexit = VERB_QUIET |
| 296 |
if self._display.maxrc: |
| 297 |
verbexit = VERB_STD |
| 298 |
|
| 299 |
for rc, nodelist in worker.iter_retcodes(): |
| 300 |
if rc != 0: |
| 301 |
nsdisp = ns = NodeSet._fromlist1(nodelist) |
| 302 |
if self._display.verbosity > VERB_QUIET and len(ns) > 1: |
| 303 |
nsdisp = "%s (%d)" % (ns, len(ns)) |
| 304 |
msgrc = "%s: %s: exited with exit code %d" % (self._prog, nsdisp, rc) |
| 305 |
self._display.vprint_err(verbexit, msgrc) |
| 306 |
|
| 307 |
|
| 308 |
if worker.num_timeout() > 0: |
| 309 |
self._display.vprint_err(verbexit, "%s: %s: command timeout" % \ |
| 310 |
(self._prog, NodeSet._fromlist1(worker.iter_keys_timeout()))) |
| 311 |
|
| 312 |
class SortedOutputHandler(GatherOutputHandler): |
| 313 |
"""Sorted by node output event handler class (e.g. clush -L).""" |
| 314 |
|
| 315 |
def ev_close(self, worker, timedout): |
| 316 |
|
| 317 |
self._runtimer_finalize(worker) |
| 318 |
|
| 319 |
|
| 320 |
for _rc, nodelist in sorted(worker.iter_retcodes()): |
| 321 |
for node in nodelist: |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
for node, msg in worker.iter_node_buffers(match_keys=(node,)): |
| 328 |
self._display.print_gather(node, msg) |
| 329 |
|
| 330 |
self._close_common(worker) |
| 331 |
|
| 332 |
|
| 333 |
self.update_prompt(worker) |
| 334 |
|
| 335 |
class LiveGatherOutputHandler(GatherOutputHandler): |
| 336 |
"""Live line-gathered output event handler class (-bL).""" |
| 337 |
|
| 338 |
def __init__(self, display, nodes, prog=None): |
| 339 |
assert nodes is not None, "cannot gather local command" |
| 340 |
GatherOutputHandler.__init__(self, display, prog=prog) |
| 341 |
self._nodes = NodeSet(nodes) |
| 342 |
self._nodecnt = dict.fromkeys(self._nodes, 0) |
| 343 |
self._mtreeq = [] |
| 344 |
self._offload = 0 |
| 345 |
|
| 346 |
def ev_read(self, worker, node, sname, msg): |
| 347 |
if sname != worker.SNAME_STDOUT: |
| 348 |
GatherOutputHandler.ev_read(self, worker, node, sname, msg) |
| 349 |
return |
| 350 |
|
| 351 |
self._nodecnt[node] += 1 |
| 352 |
cnt = self._nodecnt[node] |
| 353 |
if len(self._mtreeq) < cnt: |
| 354 |
self._mtreeq.append(MsgTree()) |
| 355 |
self._mtreeq[cnt - self._offload - 1].add(node, msg) |
| 356 |
self._live_line(worker) |
| 357 |
|
| 358 |
def ev_hup(self, worker, node, rc): |
| 359 |
if self._mtreeq and node not in self._mtreeq[0]: |
| 360 |
|
| 361 |
|
| 362 |
self._nodes.remove(node) |
| 363 |
self._live_line(worker) |
| 364 |
|
| 365 |
def _live_line(self, worker): |
| 366 |
|
| 367 |
while self._mtreeq and len(self._mtreeq[0]) == len(self._nodes): |
| 368 |
mtree = self._mtreeq.pop(0) |
| 369 |
self._offload += 1 |
| 370 |
self._runtimer_clean() |
| 371 |
nodesetify = lambda v: (v[0], NodeSet.fromlist(v[1])) |
| 372 |
for buf, nodeset in sorted(map(nodesetify, mtree.walk()), |
| 373 |
key=bufnodeset_cmpkey): |
| 374 |
self._display.print_gather(nodeset, buf) |
| 375 |
self._runtimer_set_dirty() |
| 376 |
|
| 377 |
def ev_close(self, worker, timedout): |
| 378 |
|
| 379 |
self._runtimer_finalize(worker) |
| 380 |
|
| 381 |
for mtree in self._mtreeq: |
| 382 |
nodesetify = lambda v: (v[0], NodeSet.fromlist(v[1])) |
| 383 |
for buf, nodeset in sorted(map(nodesetify, mtree.walk()), |
| 384 |
key=bufnodeset_cmpkey): |
| 385 |
self._display.print_gather(nodeset, buf) |
| 386 |
|
| 387 |
self._close_common(worker) |
| 388 |
|
| 389 |
|
| 390 |
self.update_prompt(worker) |
| 391 |
|
| 392 |
class RunTimer(EventHandler): |
| 393 |
"""Running progress timer event handler""" |
| 394 |
def __init__(self, task, total, prog=None): |
| 395 |
EventHandler.__init__(self) |
| 396 |
self.task = task |
| 397 |
self.total = total |
| 398 |
self.cnt_last = -1 |
| 399 |
self.tslen = len(str(self.total)) |
| 400 |
self.wholelen = 0 |
| 401 |
self.started = False |
| 402 |
|
| 403 |
self.start_time = 0 |
| 404 |
self.bytes_written = 0 |
| 405 |
self._prog = prog if prog else os.path.basename(sys.argv[0]) |
| 406 |
|
| 407 |
def ev_timer(self, timer): |
| 408 |
self.update() |
| 409 |
|
| 410 |
def set_dirty(self): |
| 411 |
self.cnt_last = -1 |
| 412 |
|
| 413 |
def erase_line(self): |
| 414 |
if self.wholelen: |
| 415 |
sys.stderr.write(' ' * self.wholelen + '\r') |
| 416 |
self.wholelen = 0 |
| 417 |
|
| 418 |
def update(self): |
| 419 |
"""Update runtime progress info""" |
| 420 |
wrbwinfo = '' |
| 421 |
if self.bytes_written > 0: |
| 422 |
bandwidth = self.bytes_written/(time.time() - self.start_time) |
| 423 |
wrbwinfo = " write: %s/s" % human_bi_bytes_unit(bandwidth) |
| 424 |
|
| 425 |
gwcnt = len(self.task.gateways) |
| 426 |
if gwcnt: |
| 427 |
|
| 428 |
act_targets = set() |
| 429 |
for gw, (chan, metaworkers) in self.task.gateways.items(): |
| 430 |
for mw in metaworkers: |
| 431 |
act_targets.update(mw.gwtargets[gw]) |
| 432 |
cnt = len(act_targets) + len(self.task._engine.clients()) - gwcnt |
| 433 |
gwinfo = ' gw %d' % gwcnt |
| 434 |
else: |
| 435 |
cnt = len(self.task._engine.clients()) |
| 436 |
gwinfo = '' |
| 437 |
if self.bytes_written > 0 or cnt != self.cnt_last: |
| 438 |
self.cnt_last = cnt |
| 439 |
|
| 440 |
towrite = '%s: %*d/%*d%s%s\r' % (self._prog, self.tslen, |
| 441 |
self.total - cnt, self.tslen, |
| 442 |
self.total, gwinfo, wrbwinfo) |
| 443 |
self.wholelen = len(towrite) |
| 444 |
sys.stderr.write(towrite) |
| 445 |
self.started = True |
| 446 |
|
| 447 |
def finalize(self, force_cr): |
| 448 |
"""finalize display of runtimer""" |
| 449 |
if not self.started: |
| 450 |
return |
| 451 |
self.erase_line() |
| 452 |
|
| 453 |
fmt = '%s: %*d/%*d' |
| 454 |
if force_cr: |
| 455 |
fmt += '\n' |
| 456 |
else: |
| 457 |
fmt += '\r' |
| 458 |
sys.stderr.write(fmt % (self._prog, self.tslen, self.total, self.tslen, |
| 459 |
self.total)) |
| 460 |
|
| 461 |
|
| 462 |
def signal_handler(signum, frame): |
| 463 |
"""Signal handler used for main thread notification""" |
| 464 |
if signum == signal.SIGUSR1: |
| 465 |
signal.signal(signal.SIGUSR1, signal.SIG_IGN) |
| 466 |
raise UpdatePromptException() |
| 467 |
|
| 468 |
def get_history_file(): |
| 469 |
"""Turn the history file path""" |
| 470 |
return join(os.environ["HOME"], ".clush_history") |
| 471 |
|
| 472 |
def readline_setup(): |
| 473 |
""" |
| 474 |
Configure readline to automatically load and save a history file |
| 475 |
named .clush_history |
| 476 |
""" |
| 477 |
import readline |
| 478 |
readline.parse_and_bind("tab: complete") |
| 479 |
readline.set_completer_delims("") |
| 480 |
try: |
| 481 |
readline.read_history_file(get_history_file()) |
| 482 |
except IOError: |
| 483 |
pass |
| 484 |
|
| 485 |
def ttyloop(task, nodeset, timeout, display, remote, trytree): |
| 486 |
"""Manage the interactive prompt to run command""" |
| 487 |
readline_avail = False |
| 488 |
interactive = task.default("USER_interactive") |
| 489 |
if interactive: |
| 490 |
try: |
| 491 |
import readline |
| 492 |
readline_setup() |
| 493 |
readline_avail = True |
| 494 |
except ImportError: |
| 495 |
pass |
| 496 |
display.vprint(VERB_STD, \ |
| 497 |
"Enter 'quit' to leave this interactive mode") |
| 498 |
|
| 499 |
rc = 0 |
| 500 |
ns = NodeSet(nodeset) |
| 501 |
ns_info = True |
| 502 |
cmd = "" |
| 503 |
while task.default("USER_running") or \ |
| 504 |
(interactive and cmd.lower() != 'quit'): |
| 505 |
try: |
| 506 |
|
| 507 |
if task.default("USER_handle_SIGUSR1"): |
| 508 |
signal.signal(signal.SIGUSR1, signal_handler) |
| 509 |
|
| 510 |
if task.default("USER_interactive") and \ |
| 511 |
not task.default("USER_running"): |
| 512 |
if ns_info: |
| 513 |
display.vprint(VERB_QUIET, \ |
| 514 |
"Working with nodes: %s" % ns) |
| 515 |
ns_info = False |
| 516 |
prompt = "clush> " |
| 517 |
else: |
| 518 |
prompt = "" |
| 519 |
try: |
| 520 |
cmd = raw_input(prompt) |
| 521 |
assert cmd is not None, "Result of raw_input() is None!" |
| 522 |
finally: |
| 523 |
signal.signal(signal.SIGUSR1, signal.SIG_IGN) |
| 524 |
except EOFError: |
| 525 |
print() |
| 526 |
return |
| 527 |
except UpdatePromptException: |
| 528 |
if task.default("USER_interactive"): |
| 529 |
continue |
| 530 |
return |
| 531 |
except KeyboardInterrupt as kbe: |
| 532 |
|
| 533 |
|
| 534 |
if display.gather: |
| 535 |
|
| 536 |
task.suspend() |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
if not task.default("USER_running"): |
| 542 |
|
| 543 |
raise kbe |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
print_warn = False |
| 548 |
|
| 549 |
|
| 550 |
nodesetify = lambda v: (v[0], NodeSet._fromlist1(v[1])) |
| 551 |
for buf, nodeset in sorted(map(nodesetify, task.iter_buffers()), |
| 552 |
key=bufnodeset_cmpkey): |
| 553 |
if not print_warn: |
| 554 |
print_warn = True |
| 555 |
display.vprint_err(VERB_STD, \ |
| 556 |
"Warning: Caught keyboard interrupt!") |
| 557 |
display.print_gather(nodeset, buf) |
| 558 |
|
| 559 |
|
| 560 |
verbexit = VERB_QUIET |
| 561 |
if display.maxrc: |
| 562 |
verbexit = VERB_STD |
| 563 |
ns_ok = NodeSet() |
| 564 |
for rc, nodelist in task.iter_retcodes(): |
| 565 |
ns_ok.add(NodeSet._fromlist1(nodelist)) |
| 566 |
if rc != 0: |
| 567 |
|
| 568 |
nsdisp = ns = NodeSet._fromlist1(nodelist) |
| 569 |
if display.verbosity >= VERB_QUIET and len(ns) > 1: |
| 570 |
nsdisp = "%s (%d)" % (ns, len(ns)) |
| 571 |
msgrc = "clush: %s: exited with exit code %d" % (nsdisp, |
| 572 |
rc) |
| 573 |
display.vprint_err(verbexit, msgrc) |
| 574 |
|
| 575 |
|
| 576 |
kbe.uncompleted_nodes = ns - ns_ok |
| 577 |
|
| 578 |
|
| 579 |
if task.num_timeout() > 0: |
| 580 |
display.vprint_err(verbexit, \ |
| 581 |
"clush: %s: command timeout" % \ |
| 582 |
NodeSet._fromlist1(task.iter_keys_timeout())) |
| 583 |
raise kbe |
| 584 |
|
| 585 |
if task.default("USER_running"): |
| 586 |
ns_reg, ns_unreg = NodeSet(), NodeSet() |
| 587 |
for client in task._engine.clients(): |
| 588 |
if client.registered: |
| 589 |
ns_reg.add(client.key) |
| 590 |
else: |
| 591 |
ns_unreg.add(client.key) |
| 592 |
if ns_unreg: |
| 593 |
pending = "\nclush: pending(%d): %s" % (len(ns_unreg), ns_unreg) |
| 594 |
else: |
| 595 |
pending = "" |
| 596 |
display.vprint_err(VERB_QUIET, |
| 597 |
"clush: interrupt (^C to abort task)") |
| 598 |
gws = list(task.gateways) |
| 599 |
if not gws: |
| 600 |
display.vprint_err(VERB_QUIET, |
| 601 |
"clush: in progress(%d): %s%s" |
| 602 |
% (len(ns_reg), ns_reg, pending)) |
| 603 |
else: |
| 604 |
display.vprint_err(VERB_QUIET, |
| 605 |
"clush: in progress(%d): %s%s\n" |
| 606 |
"clush: [tree] open gateways(%d): %s" |
| 607 |
% (len(ns_reg), ns_reg, pending, |
| 608 |
len(gws), NodeSet._fromlist1(gws))) |
| 609 |
for gw, (chan, metaworkers) in task.gateways.items(): |
| 610 |
act_targets = set() |
| 611 |
for mw in metaworkers: |
| 612 |
act_targets.update(mw.gwtargets[gw]) |
| 613 |
if act_targets: |
| 614 |
act_tgt_ns = NodeSet.fromlist(act_targets) |
| 615 |
display.vprint_err(VERB_QUIET, |
| 616 |
"clush: [tree] in progress(%d) on %s: %s" |
| 617 |
% (len(act_targets), gw, act_tgt_ns)) |
| 618 |
else: |
| 619 |
cmdl = cmd.lower() |
| 620 |
try: |
| 621 |
ns_info = True |
| 622 |
if cmdl.startswith('+'): |
| 623 |
ns.update(cmdl[1:]) |
| 624 |
elif cmdl.startswith('-'): |
| 625 |
ns.difference_update(cmdl[1:]) |
| 626 |
elif cmdl.startswith('@'): |
| 627 |
ns = NodeSet(cmdl[1:]) |
| 628 |
elif cmdl == '=': |
| 629 |
display.gather = not display.gather |
| 630 |
if display.gather: |
| 631 |
display.vprint(VERB_STD, \ |
| 632 |
"Switching to gathered output format") |
| 633 |
else: |
| 634 |
display.vprint(VERB_STD, \ |
| 635 |
"Switching to standard output format") |
| 636 |
task.set_default("stdout_msgtree", \ |
| 637 |
display.gather or display.line_mode) |
| 638 |
ns_info = False |
| 639 |
continue |
| 640 |
elif not cmdl.startswith('?'): |
| 641 |
ns_info = False |
| 642 |
except NodeSetParseError: |
| 643 |
display.vprint_err(VERB_QUIET, \ |
| 644 |
"clush: nodeset parse error (ignoring)") |
| 645 |
|
| 646 |
if ns_info: |
| 647 |
continue |
| 648 |
|
| 649 |
if cmdl.startswith('!') and len(cmd.strip()) > 0: |
| 650 |
run_command(task, cmd[1:], None, timeout, display, remote, |
| 651 |
trytree) |
| 652 |
elif cmdl != "quit": |
| 653 |
if not cmd: |
| 654 |
continue |
| 655 |
if readline_avail: |
| 656 |
readline.write_history_file(get_history_file()) |
| 657 |
if task.default("USER_command_prefix"): |
| 658 |
prefix_cmdl = shlex.split(task.default("USER_command_prefix")) |
| 659 |
cmd = "%s %s" % (' '.join(prefix_cmdl), cmd) |
| 660 |
run_command(task, cmd, ns, timeout, display, remote, trytree) |
| 661 |
return rc |
| 662 |
|
| 663 |
def _stdin_thread_start(stdin_port, display): |
| 664 |
"""Standard input reader thread entry point.""" |
| 665 |
try: |
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
bufsize = 64 * 1024 |
| 671 |
|
| 672 |
|
| 673 |
while True: |
| 674 |
buf = os.read(sys_stdin().fileno(), bufsize) |
| 675 |
if not buf: |
| 676 |
break |
| 677 |
|
| 678 |
stdin_port.msg(buf) |
| 679 |
except IOError as ex: |
| 680 |
display.vprint(VERB_VERB, "stdin: %s" % ex) |
| 681 |
|
| 682 |
stdin_port.msg(None) |
| 683 |
|
| 684 |
def bind_stdin(worker, display): |
| 685 |
"""Create a stdin->port->worker binding: connect specified worker |
| 686 |
to stdin with the help of a reader thread and a ClusterShell Port |
| 687 |
object.""" |
| 688 |
assert sys.stdin is not None and not sys.stdin.isatty() |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
port = worker.task.port(handler=StdInputHandler(worker), autoclose=True) |
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
stdin_thread = threading.Thread(None, _stdin_thread_start, args=(port, display)) |
| 697 |
|
| 698 |
|
| 699 |
stdin_thread.daemon = True |
| 700 |
stdin_thread.start() |
| 701 |
|
| 702 |
def run_command(task, cmd, ns, timeout, display, remote, trytree): |
| 703 |
""" |
| 704 |
Create and run the specified command line, displaying |
| 705 |
results in a dshbak way when gathering is used. |
| 706 |
""" |
| 707 |
task.set_default("USER_running", True) |
| 708 |
|
| 709 |
if (display.gather or display.line_mode) and ns is not None: |
| 710 |
if display.gather and display.line_mode: |
| 711 |
handler = LiveGatherOutputHandler(display, ns) |
| 712 |
elif not display.gather and display.line_mode: |
| 713 |
handler = SortedOutputHandler(display) |
| 714 |
else: |
| 715 |
handler = GatherOutputHandler(display) |
| 716 |
|
| 717 |
if display.verbosity in (VERB_STD, VERB_VERB) or \ |
| 718 |
(display.progress and display.verbosity > VERB_QUIET): |
| 719 |
handler.runtimer_init(task, len(ns)) |
| 720 |
elif display.progress and display.verbosity > VERB_QUIET: |
| 721 |
handler = DirectProgressOutputHandler(display) |
| 722 |
handler.runtimer_init(task, len(ns)) |
| 723 |
elif (display.outdir or display.errdir) and ns is not None: |
| 724 |
if display.outdir and not exists(display.outdir): |
| 725 |
os.makedirs(display.outdir) |
| 726 |
if display.errdir and not exists(display.errdir): |
| 727 |
os.makedirs(display.errdir) |
| 728 |
handler = DirectOutputDirHandler(display, ns) |
| 729 |
else: |
| 730 |
|
| 731 |
handler = DirectOutputHandler(display) |
| 732 |
|
| 733 |
stdin = task.default("USER_stdin_worker") |
| 734 |
prompt_passwd = task.default("USER_password_prompt") |
| 735 |
worker = task.shell(cmd, nodes=ns, handler=handler, timeout=timeout, |
| 736 |
remote=remote, tree=trytree, |
| 737 |
stdin=stdin or prompt_passwd is not None) |
| 738 |
if ns is None: |
| 739 |
worker.set_key('LOCAL') |
| 740 |
if prompt_passwd: |
| 741 |
worker.write(prompt_passwd.encode() + b'\n') |
| 742 |
if stdin: |
| 743 |
bind_stdin(worker, display) |
| 744 |
if prompt_passwd and not stdin: |
| 745 |
worker.set_write_eof() |
| 746 |
task.resume() |
| 747 |
|
| 748 |
def run_copy(task, sources, dests, ns, timeout, preserve_flag, display): |
| 749 |
"""run copy command""" |
| 750 |
task.set_default("USER_running", True) |
| 751 |
task.set_default("USER_copies", len(sources)) |
| 752 |
|
| 753 |
copyhandler = CopyOutputHandler(display) |
| 754 |
if display.verbosity in (VERB_STD, VERB_VERB): |
| 755 |
copyhandler.runtimer_init(task, len(ns) * len(sources)) |
| 756 |
|
| 757 |
|
| 758 |
for source in sources: |
| 759 |
if not exists(source): |
| 760 |
display.vprint_err(VERB_QUIET, |
| 761 |
'ERROR: file "%s" not found' % source) |
| 762 |
clush_exit(1, task) |
| 763 |
task.copy(source, dests.pop(0), ns, handler=copyhandler, |
| 764 |
timeout=timeout, preserve=preserve_flag) |
| 765 |
task.resume() |
| 766 |
|
| 767 |
def run_rcopy(task, sources, dests, ns, timeout, preserve_flag, display): |
| 768 |
"""run reverse copy command""" |
| 769 |
task.set_default("USER_running", True) |
| 770 |
task.set_default("USER_copies", len(sources)) |
| 771 |
|
| 772 |
|
| 773 |
for dest in dests: |
| 774 |
if not exists(dest): |
| 775 |
display.vprint_err(VERB_QUIET, |
| 776 |
'ERROR: directory "%s" not found' % dest) |
| 777 |
clush_exit(1, task) |
| 778 |
if not isdir(dest): |
| 779 |
display.vprint_err(VERB_QUIET, |
| 780 |
'ERROR: destination "%s" is not a directory' % dest) |
| 781 |
clush_exit(1, task) |
| 782 |
|
| 783 |
copyhandler = CopyOutputHandler(display, True) |
| 784 |
if display.verbosity == VERB_STD or display.verbosity == VERB_VERB: |
| 785 |
copyhandler.runtimer_init(task, len(ns) * len(sources)) |
| 786 |
for source in sources: |
| 787 |
task.rcopy(source, dests.pop(0), ns, handler=copyhandler, |
| 788 |
timeout=timeout, stderr=True, preserve=preserve_flag) |
| 789 |
task.resume() |
| 790 |
|
| 791 |
def set_fdlimit(fd_max, display): |
| 792 |
"""Make open file descriptors soft limit the max.""" |
| 793 |
soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE) |
| 794 |
if hard < fd_max: |
| 795 |
msgfmt = 'Warning: fd_max set to %d but max open files hard limit is %d' |
| 796 |
display.vprint_err(VERB_VERB, msgfmt % (fd_max, hard)) |
| 797 |
rlim_max = min(hard, fd_max) |
| 798 |
if soft != rlim_max: |
| 799 |
msgfmt = 'Changing max open files soft limit from %d to %d' |
| 800 |
display.vprint(VERB_DEBUG, msgfmt % (soft, rlim_max)) |
| 801 |
try: |
| 802 |
resource.setrlimit(resource.RLIMIT_NOFILE, (rlim_max, hard)) |
| 803 |
except (ValueError, resource.error) as exc: |
| 804 |
|
| 805 |
msgfmt = 'Warning: Failed to set max open files limit to %d (%s)' |
| 806 |
display.vprint_err(VERB_VERB, msgfmt % (rlim_max, exc)) |
| 807 |
|
| 808 |
def ask_pass(): |
| 809 |
"""Prompt for password (--mode with password_prompt=True)""" |
| 810 |
return getpass.getpass() |
| 811 |
|
| 812 |
def clush_exit(status, task=None): |
| 813 |
"""Exit script, flushing stdio buffers and stopping ClusterShell task.""" |
| 814 |
if task: |
| 815 |
|
| 816 |
task.abort() |
| 817 |
task.join() |
| 818 |
sys.exit(status) |
| 819 |
else: |
| 820 |
|
| 821 |
for stream in [sys.stdout, sys.stderr]: |
| 822 |
try: |
| 823 |
stream.flush() |
| 824 |
except IOError: |
| 825 |
pass |
| 826 |
|
| 827 |
os._exit(status) |
| 828 |
|
| 829 |
def clush_excepthook(extype, exp, traceback): |
| 830 |
"""Exceptions hook for clush: this method centralizes exception |
| 831 |
handling from main thread and from (possible) separate task thread. |
| 832 |
This hook has to be previously installed on startup by overriding |
| 833 |
sys.excepthook and task.excepthook.""" |
| 834 |
try: |
| 835 |
raise exp |
| 836 |
except ClushConfigError as econf: |
| 837 |
print("ERROR: %s" % econf, file=sys.stderr) |
| 838 |
clush_exit(1) |
| 839 |
except KeyboardInterrupt as kbe: |
| 840 |
uncomp_nodes = getattr(kbe, 'uncompleted_nodes', None) |
| 841 |
if uncomp_nodes: |
| 842 |
print("Keyboard interrupt (%s did not complete)." % uncomp_nodes, |
| 843 |
file=sys.stderr) |
| 844 |
else: |
| 845 |
print("Keyboard interrupt.", file=sys.stderr) |
| 846 |
clush_exit(128 + signal.SIGINT) |
| 847 |
except GENERIC_ERRORS as exc: |
| 848 |
clush_exit(handle_generic_error(exc)) |
| 849 |
|
| 850 |
|
| 851 |
task_self().default_excepthook(extype, exp, traceback) |
| 852 |
|
| 853 |
def main(): |
| 854 |
"""clush script entry point""" |
| 855 |
sys.excepthook = clush_excepthook |
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
usage = "%prog [options] command" |
| 861 |
|
| 862 |
parser = OptionParser(usage) |
| 863 |
|
| 864 |
parser.add_option("-n", "--nostdin", action="store_true", dest="nostdin", |
| 865 |
help="don't watch for possible input from stdin") |
| 866 |
|
| 867 |
parser.install_groupsconf_option() |
| 868 |
parser.install_clush_config_options() |
| 869 |
parser.install_nodes_options() |
| 870 |
parser.install_display_options(verbose_options=True) |
| 871 |
parser.install_filecopy_options() |
| 872 |
parser.install_connector_options() |
| 873 |
|
| 874 |
(options, args) = parser.parse_args() |
| 875 |
|
| 876 |
set_std_group_resolver_config(options.groupsconf) |
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
config = ClushConfig(options, options.conf) |
| 882 |
|
| 883 |
|
| 884 |
if config.verbosity >= VERB_DEBUG: |
| 885 |
logging.basicConfig(level=logging.DEBUG) |
| 886 |
logging.debug("clush: STARTING DEBUG") |
| 887 |
else: |
| 888 |
logging.basicConfig(level=logging.CRITICAL) |
| 889 |
|
| 890 |
|
| 891 |
if config.color == "auto": |
| 892 |
color = sys.stdout.isatty() and (options.gatherall or \ |
| 893 |
sys.stderr.isatty()) |
| 894 |
else: |
| 895 |
color = config.color == "always" |
| 896 |
|
| 897 |
try: |
| 898 |
|
| 899 |
display = Display(options, config, color) |
| 900 |
except ValueError as exc: |
| 901 |
parser.error("option mismatch (%s)" % exc) |
| 902 |
|
| 903 |
if options.groupsource: |
| 904 |
|
| 905 |
std_group_resolver().default_source_name = options.groupsource |
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
wnodelist = [] |
| 910 |
xnodelist = [] |
| 911 |
if options.nodes: |
| 912 |
wnodelist = [NodeSet(nodes) for nodes in options.nodes] |
| 913 |
|
| 914 |
if options.exclude: |
| 915 |
xnodelist = [NodeSet(nodes) for nodes in options.exclude] |
| 916 |
|
| 917 |
for (opt, nodelist) in (('w', wnodelist), ('x', xnodelist)): |
| 918 |
for nodes in nodelist: |
| 919 |
if len(nodes) == 1 and exists(str(nodes)): |
| 920 |
display.vprint_err(VERB_STD, "Warning: using '-%s %s' and " |
| 921 |
"local path '%s' exists, was it expanded " |
| 922 |
"by the shell?" % (opt, nodes, nodes)) |
| 923 |
|
| 924 |
|
| 925 |
for opt_hostfile in options.hostfile: |
| 926 |
try: |
| 927 |
fnodeset = NodeSet() |
| 928 |
with open(opt_hostfile) as hostfile: |
| 929 |
for line in hostfile.read().splitlines(): |
| 930 |
fnodeset.updaten(nodes for nodes in line.split()) |
| 931 |
display.vprint_err(VERB_DEBUG, |
| 932 |
"Using nodeset %s from hostfile %s" |
| 933 |
% (fnodeset, opt_hostfile)) |
| 934 |
wnodelist.append(fnodeset) |
| 935 |
except IOError as exc: |
| 936 |
|
| 937 |
errno, strerror = exc.args |
| 938 |
raise OSError(errno, strerror, exc.filename) |
| 939 |
|
| 940 |
|
| 941 |
nodeset_base = NodeSet.fromlist(wnodelist) |
| 942 |
|
| 943 |
nodeset_exclude = NodeSet.fromlist(xnodelist) |
| 944 |
|
| 945 |
|
| 946 |
DEFAULTS.engine = options.engine |
| 947 |
|
| 948 |
|
| 949 |
if options.axis: |
| 950 |
DEFAULTS.fold_axis = parse_fold_axis(options.axis) |
| 951 |
|
| 952 |
|
| 953 |
task = task_self() |
| 954 |
task.set_info("debug", config.verbosity >= VERB_DEBUG) |
| 955 |
if options.nodes_all: |
| 956 |
all_nodeset = NodeSet.fromall() |
| 957 |
display.vprint(VERB_DEBUG, "Adding nodes from option -a: %s" % \ |
| 958 |
all_nodeset) |
| 959 |
nodeset_base.add(all_nodeset) |
| 960 |
|
| 961 |
if options.group: |
| 962 |
grp_nodeset = NodeSet.fromlist(options.group, |
| 963 |
resolver=RESOLVER_NOGROUP) |
| 964 |
for grp in grp_nodeset: |
| 965 |
addingrp = NodeSet("@" + grp) |
| 966 |
display.vprint(VERB_DEBUG, \ |
| 967 |
"Adding nodes from option -g %s: %s" % (grp, addingrp)) |
| 968 |
nodeset_base.update(addingrp) |
| 969 |
|
| 970 |
if options.exgroup: |
| 971 |
grp_nodeset = NodeSet.fromlist(options.exgroup, |
| 972 |
resolver=RESOLVER_NOGROUP) |
| 973 |
for grp in grp_nodeset: |
| 974 |
removingrp = NodeSet("@" + grp) |
| 975 |
display.vprint(VERB_DEBUG, \ |
| 976 |
"Excluding nodes from option -X %s: %s" % (grp, removingrp)) |
| 977 |
nodeset_exclude.update(removingrp) |
| 978 |
|
| 979 |
|
| 980 |
nodeset_base.difference_update(nodeset_exclude) |
| 981 |
if len(nodeset_base) < 1: |
| 982 |
parser.error('No node to run on.') |
| 983 |
|
| 984 |
if options.pick and options.pick < len(nodeset_base): |
| 985 |
|
| 986 |
|
| 987 |
keep = random.sample(list(nodeset_base), options.pick) |
| 988 |
nodeset_base.intersection_update(','.join(keep)) |
| 989 |
if config.verbosity >= VERB_VERB: |
| 990 |
msg = "Picked random nodes: %s" % nodeset_base |
| 991 |
print(Display.COLOR_RESULT_FMT % msg) |
| 992 |
|
| 993 |
|
| 994 |
set_fdlimit(config.fd_max, display) |
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
interactive = not len(args) and \ |
| 1001 |
not (options.copy or options.rcopy) |
| 1002 |
|
| 1003 |
stdin_isafgtty = sys.stdin is not None and sys.stdin.isatty() and \ |
| 1004 |
os.tcgetpgrp(sys.stdin.fileno()) == os.getpgrp() |
| 1005 |
|
| 1006 |
if interactive and not stdin_isafgtty: |
| 1007 |
|
| 1008 |
|
| 1009 |
interactive = False |
| 1010 |
|
| 1011 |
ssh_options = config.ssh_options or '' |
| 1012 |
ssh_options += ' -T' |
| 1013 |
config._set_main("ssh_options", ssh_options) |
| 1014 |
if options.nostdin and interactive: |
| 1015 |
parser.error("illegal option `--nostdin' in that case") |
| 1016 |
|
| 1017 |
|
| 1018 |
user_interaction = hasattr(sys.modules[__name__], '_f_user_interaction') |
| 1019 |
if not options.nostdin: |
| 1020 |
|
| 1021 |
stdout_isafgtty = sys.stdout.isatty() and \ |
| 1022 |
os.tcgetpgrp(sys.stdout.fileno()) == os.getpgrp() |
| 1023 |
user_interaction |= stdin_isafgtty and stdout_isafgtty |
| 1024 |
display.vprint(VERB_DEBUG, "User interaction: %s" % user_interaction) |
| 1025 |
if user_interaction: |
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
task = Task() |
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
task.set_default("USER_handle_SIGUSR1", user_interaction) |
| 1035 |
|
| 1036 |
task.excepthook = sys.excepthook |
| 1037 |
task.set_default("USER_stdin_worker", not (sys.stdin is None or \ |
| 1038 |
sys.stdin.isatty() or \ |
| 1039 |
options.nostdin or \ |
| 1040 |
user_interaction)) |
| 1041 |
display.vprint(VERB_DEBUG, "Create STDIN worker: %s" % \ |
| 1042 |
task.default("USER_stdin_worker")) |
| 1043 |
|
| 1044 |
task.set_info("debug", config.verbosity >= VERB_DEBUG) |
| 1045 |
task.set_info("fanout", config.fanout) |
| 1046 |
|
| 1047 |
if options.mode: |
| 1048 |
display.vprint(VERB_DEBUG, "ClushConfig parsed: %s" % config.parsed) |
| 1049 |
display.vprint(VERB_DEBUG, "Available run modes: %s" % ' '.join(config.modes())) |
| 1050 |
config.set_mode(options.mode) |
| 1051 |
display.vprint(VERB_VERB, "[%s] run mode activated" % options.mode) |
| 1052 |
|
| 1053 |
command_prefix = config.command_prefix |
| 1054 |
if command_prefix: |
| 1055 |
|
| 1056 |
task.set_default("USER_command_prefix", command_prefix) |
| 1057 |
prefix_cmdl = shlex.split(command_prefix) |
| 1058 |
display.vprint(VERB_VERB, "[%s] command prefix: %s" % \ |
| 1059 |
(options.mode, prefix_cmdl)) |
| 1060 |
args = prefix_cmdl + args |
| 1061 |
|
| 1062 |
if config.password_prompt: |
| 1063 |
display.vprint(VERB_VERB, "[%s] password prompt enabled" % options.mode) |
| 1064 |
|
| 1065 |
task.set_default("USER_password_prompt", ask_pass()) |
| 1066 |
|
| 1067 |
if options.worker: |
| 1068 |
try: |
| 1069 |
if options.remote == 'no': |
| 1070 |
task.set_default('local_worker', |
| 1071 |
_load_workerclass(options.worker)) |
| 1072 |
else: |
| 1073 |
task.set_default('distant_worker', |
| 1074 |
_load_workerclass(options.worker)) |
| 1075 |
except (ImportError, AttributeError): |
| 1076 |
msg = "ERROR: Could not load worker '%s'" % options.worker |
| 1077 |
display.vprint_err(VERB_QUIET, msg) |
| 1078 |
clush_exit(1, task) |
| 1079 |
elif options.topofile or task._default_tree_is_enabled(): |
| 1080 |
if options.topofile: |
| 1081 |
task.load_topology(options.topofile) |
| 1082 |
if config.verbosity >= VERB_VERB: |
| 1083 |
roots = len(task.topology.root.nodeset) |
| 1084 |
gws = task.topology.inner_node_count() - roots |
| 1085 |
msg = "enabling tree topology (%d gateways)" % gws |
| 1086 |
print("clush: %s" % msg, file=sys.stderr) |
| 1087 |
|
| 1088 |
if options.grooming_delay: |
| 1089 |
if config.verbosity >= VERB_VERB: |
| 1090 |
msg = Display.COLOR_RESULT_FMT % ("Grooming delay: %f" % |
| 1091 |
options.grooming_delay) |
| 1092 |
print(msg, file=sys.stderr) |
| 1093 |
task.set_info("grooming_delay", options.grooming_delay) |
| 1094 |
elif options.rcopy: |
| 1095 |
|
| 1096 |
task.set_info("grooming_delay", 0) |
| 1097 |
|
| 1098 |
if config.ssh_user: |
| 1099 |
task.set_info("ssh_user", config.ssh_user) |
| 1100 |
if config.ssh_path: |
| 1101 |
task.set_info("ssh_path", config.ssh_path) |
| 1102 |
if config.ssh_options: |
| 1103 |
task.set_info("ssh_options", config.ssh_options) |
| 1104 |
if config.scp_path: |
| 1105 |
task.set_info("scp_path", config.scp_path) |
| 1106 |
if config.scp_options: |
| 1107 |
task.set_info("scp_options", config.scp_options) |
| 1108 |
if config.rsh_path: |
| 1109 |
task.set_info("rsh_path", config.rsh_path) |
| 1110 |
if config.rcp_path: |
| 1111 |
task.set_info("rcp_path", config.rcp_path) |
| 1112 |
if config.rsh_options: |
| 1113 |
task.set_info("rsh_options", config.rsh_options) |
| 1114 |
|
| 1115 |
|
| 1116 |
task.set_info("connect_timeout", config.connect_timeout) |
| 1117 |
task.set_info("command_timeout", config.command_timeout) |
| 1118 |
|
| 1119 |
|
| 1120 |
task.set_default("stderr", not options.gatherall) |
| 1121 |
|
| 1122 |
|
| 1123 |
task.set_default("stdout_msgtree", display.gather or display.line_mode) |
| 1124 |
|
| 1125 |
|
| 1126 |
task.set_default("stderr_msgtree", False) |
| 1127 |
|
| 1128 |
|
| 1129 |
if config.command_timeout > 0: |
| 1130 |
timeout = config.command_timeout |
| 1131 |
else: |
| 1132 |
timeout = -1 |
| 1133 |
|
| 1134 |
|
| 1135 |
task.set_default("USER_interactive", interactive) |
| 1136 |
task.set_default("USER_running", False) |
| 1137 |
|
| 1138 |
if (options.copy or options.rcopy) and not args: |
| 1139 |
parser.error("--[r]copy option requires at least one argument") |
| 1140 |
dest_paths = [] |
| 1141 |
if options.copy: |
| 1142 |
if options.dest_path: |
| 1143 |
for arg in args: |
| 1144 |
dest_paths.append(options.dest_path) |
| 1145 |
else: |
| 1146 |
|
| 1147 |
for arg in args: |
| 1148 |
dest_paths.append(join(dirname(abspath(arg)), '')) |
| 1149 |
op = "copy sources=%s dest=%s" % (args, dest_paths) |
| 1150 |
elif options.rcopy: |
| 1151 |
if options.dest_path: |
| 1152 |
for arg in args: |
| 1153 |
dest_paths.append(options.dest_path) |
| 1154 |
else: |
| 1155 |
for arg in args: |
| 1156 |
dest_paths.append(dirname(abspath(arg))) |
| 1157 |
op = "rcopy sources=%s dest=%s" % (args, dest_paths) |
| 1158 |
else: |
| 1159 |
op = "command=\"%s\"" % ' '.join(args) |
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
display.vprint(VERB_DEBUG, "clush: nodeset=%s fanout=%d [timeout " \ |
| 1164 |
"conn=%.1f cmd=%.1f] %s" % (nodeset_base, config.fanout, |
| 1165 |
config.connect_timeout, |
| 1166 |
config.command_timeout, |
| 1167 |
op)) |
| 1168 |
if not task.default("USER_interactive"): |
| 1169 |
if display.verbosity >= VERB_DEBUG and task.topology: |
| 1170 |
print(Display.COLOR_RESULT_FMT % '-' * 15) |
| 1171 |
print(Display.COLOR_RESULT_FMT % task.topology, end='') |
| 1172 |
print(Display.COLOR_RESULT_FMT % '-' * 15) |
| 1173 |
if options.copy: |
| 1174 |
run_copy(task, args, dest_paths, nodeset_base, timeout, |
| 1175 |
options.preserve_flag, display) |
| 1176 |
elif options.rcopy: |
| 1177 |
run_rcopy(task, args, dest_paths, nodeset_base, timeout, |
| 1178 |
options.preserve_flag, display) |
| 1179 |
else: |
| 1180 |
run_command(task, ' '.join(args), nodeset_base, timeout, display, |
| 1181 |
options.remote != 'no', options.worker is None) |
| 1182 |
|
| 1183 |
if user_interaction: |
| 1184 |
ttyloop(task, nodeset_base, timeout, display, options.remote != 'no', |
| 1185 |
options.worker is None) |
| 1186 |
elif task.default("USER_interactive"): |
| 1187 |
display.vprint_err(VERB_QUIET, \ |
| 1188 |
"ERROR: interactive mode requires a tty") |
| 1189 |
clush_exit(1, task) |
| 1190 |
|
| 1191 |
rc = 0 |
| 1192 |
if config.maxrc: |
| 1193 |
|
| 1194 |
rc = task.max_retcode() |
| 1195 |
if task.num_timeout() > 0: |
| 1196 |
rc = 255 |
| 1197 |
clush_exit(rc, task) |
| 1198 |
|
| 1199 |
if __name__ == '__main__': |
| 1200 |
main() |