def run_collapse(self, hosts, cmd):
progress = None
if self.progressbar:
from progressbar import ProgressBar, Percentage, Bar, ETA, FileTransferSpeed
progress = ProgressBar(
widgets=["Running: ", Percentage(), ' ', Bar(marker='.'), ' ', ETA(), ' ', FileTransferSpeed()],
maxval=len(hosts))
codes = {"total": 0, "error": 0, "success": 0}
outputs = defaultdict(list)
def worker(host, cmd):
p = Popen(self.get_parallel_ssh_options(host, cmd), stdout=PIPE, stderr=PIPE)
o = ""
while True:
outs, _, _ = select([p.stdout, p.stderr], [], [])
outline = errline = ""
if p.stdout in outs:
outline = p.stdout.readline()
if p.stderr in outs:
errline = p.stderr.readline()
o += outline + errline
if outline == "" and errline == "" and p.poll() is not None:
break
if o == "":
o = colored("[ No Output ]\n", "yellow")
outputs[o].append(host)
if p.poll() == 0:
codes["success"] += 1
else:
codes["error"] += 1
codes["total"] += 1
if self.progressbar:
progress.update(codes["total"])
pool = Pool(self.ssh_threads)
if self.progressbar:
progress.start()
for host in hosts:
pool.start(Greenlet(worker, host, cmd))
try:
pool.join()
except KeyboardInterrupt:
pass
if self.progressbar:
progress.finish()
self.print_exec_results(codes)
print()
for output, hosts in outputs.items():
msg = " %s " % ','.join(hosts)
table_width = min([len(msg) + 2, terminal_size()[0]])
cprint("=" * table_width, "blue", attrs=["bold"])
cprint(msg, "blue", attrs=["bold"])
cprint("=" * table_width, "blue", attrs=["bold"])
print(output)
评论列表
文章目录