-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpara-simple
executable file
·110 lines (90 loc) · 2.92 KB
/
para-simple
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
#!/usr/bin/env python2
import sys
import logging
import argparse
import threading
import subprocess
import re
def init_logging():
logging.basicConfig(level = logging.INFO, format = '%(asctime)s \033[1;32m[%(tag)s]\033[0m %(message)s')
logger = logging.getLogger('MAIN')
logger.setLevel(logging.INFO)
return logger
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('cmd', nargs='+', help='Single command. Wrap long commands with quotes(\"\").')
parser.add_argument('-t', '--hosts', help='Target hosts (1-19) splitted with comma(\",\")')
return parser.parse_args()
def log(logger, tag, message, is_err=False):
logging_message_format = is_err and '\033[1;35m%s\033[0m' or '%s'
if message.endswith('\n'): message = message[:-1]
lines = message.split('\n')
for line in lines:
logger.info(logging_message_format % line, extra={'tag': tag})
def trans_cmds(args, logger):
# Hosts specified?
if args.hosts:
cmds = []
hosts = []
illegal_hosts = []
hosts_str = args.hosts.split(',')
for host_str in hosts_str:
hyphen_pos = host_str.find('-')
if hyphen_pos == -1:
try:
hostid = int(host_str)
hosts.append(hostid)
except ValueError:
illegal_hosts.append(host_str)
continue
else:
hstart = host_str[:hyphen_pos]
hend = host_str[hyphen_pos + 1:]
try:
hstartid = int(hstart)
hendid = int(hend)
except ValueError:
illegal_hosts.append(host_str)
continue
hosts += range(hstartid, hendid + 1)
hosts = list(set(hosts)) # Unique
logger.info('\033[1;33mIgnoring hosts: %s\033[0m' % ','.join(illegal_hosts), extra = {'tag': 'MAIN'})
for hostid in hosts:
for cmd in args.cmd:
cmds.append('ssh n%d \"%s\"' % (hostid, cmd))
else:
cmds = args.cmd
return cmds
def run_single_cmd(cmd, logger, thrd_index):
if not cmd: return
logging_tag = 'Thread %2d' % thrd_index
logger.info('Starting command: %s' % cmd, extra = {'tag': logging_tag})
subp = subprocess.Popen(cmd, shell = True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while True:
line = subp.stdout.readline()
if not line: break
line = line[:-1]
log(logger, logging_tag, line)
err = subp.stderr.read()
if err:
log(logger, logging_tag, err, is_err=True)
def main():
logger = init_logging()
args = parse_args()
cmds = trans_cmds(args, logger)
logger.info('\033[1;33mStarting tasks ...\033[0m', extra = {'tag': 'MAIN'})
tasks_acc = 1
tasks_total = len(cmds)
thrd_pool = []
for cmd in cmds:
logger.info('\033[1;33mStarting tasks (%d/%d) ...\033[0m' % (tasks_acc, tasks_total), extra = {'tag': 'MAIN'})
t = threading.Thread(target = run_single_cmd, args = (cmd, logger, tasks_acc))
t.start()
thrd_pool.append(t)
tasks_acc += 1
logger.info('\033[1;33mWaiting for all tasks done...\033[0m', extra = {'tag': 'MAIN'})
for t in thrd_pool:
t.join()
logger.info('\033[1;33mAll tasks done.\033[0m', extra = {'tag': 'MAIN'})
if __name__ == '__main__':
sys.exit(main())