-
Notifications
You must be signed in to change notification settings - Fork 2
/
bootstrap.py
executable file
·74 lines (50 loc) · 2.07 KB
/
bootstrap.py
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
#!/usr/bin/env python
# coding=utf-8
"""Usage:
/usr/bin/bootstrap --num=32 --cmd='/usr/bin/frontik --config=/etc/hh-xhh/frontik.cfg --app=xhh --port=15{num:0>2}'
"""
import argparse
import logging
import signal
import subprocess
import sys
import time
_PROCESSES = []
def start():
parser = argparse.ArgumentParser(description='Simple supervisor for docker')
parser.add_argument('--cmd', type=str, help='Command to be executed')
parser.add_argument('--num', type=int, help='Processes count')
parser.add_argument('--log', type=str, help='Logfile path (uses stderr if empty)')
args = parser.parse_args()
if args.log is None:
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG, format='%(asctime)s %(message)s')
else:
logging.basicConfig(filename=args.log, level=logging.DEBUG, format='%(asctime)s %(message)s')
signal.signal(signal.SIGTERM, sigterm_action)
for i in range(args.num):
start_process(i, args)
while True:
for i, proc in enumerate(_PROCESSES):
return_code = proc.poll()
if return_code is not None:
logging.info('child #%s was shut down, exit code: %s', i, return_code)
cmd_formatted = args.cmd.format(num=i)
logging.info('restarting process #%s, executing "%s"', i, cmd_formatted)
_PROCESSES[i] = subprocess.Popen(cmd_formatted.split())
time.sleep(1)
if not _PROCESSES:
logging.info('all children terminated, exiting')
sys.exit(0)
def sigterm_action(signum, stack):
logging.info('received SIGTERM')
term_processes = _PROCESSES[:]
_PROCESSES[:] = []
for i, proc in enumerate(term_processes):
logging.info('sending SIGTERM to process #%s', i)
proc.send_signal(signum)
def start_process(process_num, args):
cmd_formatted = args.cmd.format(num=process_num)
logging.info('starting process #%s, executing "%s"', process_num, cmd_formatted)
_PROCESSES.append(subprocess.Popen(cmd_formatted.split()))
if __name__ == '__main__':
start()