forked from target/strelka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strelka.py
175 lines (151 loc) · 6.14 KB
/
strelka.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
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
#!/usr/bin/env python3
"""
strelka.py
Command line utility for running Strelka clusters. Hosts running Strelka
clients (see `strelka/user_client.py` as an example) send files to a
server running a Strelka broker which tasks out files to servers running
Strelka workers. This utility uses the default Broker and Worker classes.
"""
import argparse
import logging
import logging.config
import os
import signal
import sys
import time
import interruptingcow
from server import lib
from shared import conf
from shared import errors
from shared import utils
run = 1
DEFAULT_CONFIGS = {
"dev_strelka_cfg": "etc/strelka/strelka.yml",
"sys_strelka_cfg": "/etc/strelka/strelka.yml",
"dev_logging_ini": "etc/strelka/pylogging.ini",
"sys_logging_ini": "/etc/strelka/pylogging.ini"
}
def main():
def shutdown(signum, frame):
"""Signal handler for shutting down main."""
logging.debug("shutdown triggered")
global run
run = 0
signal.signal(signal.SIGTERM, shutdown)
signal.signal(signal.SIGINT, shutdown)
parser = argparse.ArgumentParser(prog="strelka.py",
description="runs Strelka as a"
" distributed cluster.",
usage="%(prog)s [options]")
parser.add_argument("-d", "--debug",
action="store_true",
default=False,
dest="debug",
help="enable debug messages to the console")
parser.add_argument("-c", "--strelka-config",
action="store",
dest="strelka_cfg",
help="path to strelka configuration file")
parser.add_argument("-l", "--logging-ini",
action="store",
dest="logging_ini",
help="path to python logging configuration file")
args = parser.parse_args()
logging_ini = None
if args.logging_ini:
if not os.path.exists(args.logging_ini):
sys.exit(f"logging configuration {args.logging_ini}"
" does not exist")
logging_ini = args.logging_ini
elif os.path.exists(DEFAULT_CONFIGS["sys_logging_ini"]):
logging_ini = DEFAULT_CONFIGS["sys_logging_ini"]
elif os.path.exists(DEFAULT_CONFIGS["dev_logging_ini"]):
logging_ini = DEFAULT_CONFIGS["dev_logging_ini"]
if logging_ini is None:
sys.exit("no logging configuration found")
logging.config.fileConfig(logging_ini)
strelka_cfg = None
if args.strelka_cfg:
if not os.path.exists(args.strelka_cfg):
sys.exit(f"strelka configuration {args.strelka_cfg}"
" does not exist")
strelka_cfg = args.strelka_cfg
elif os.path.exists(DEFAULT_CONFIGS["sys_strelka_cfg"]):
strelka_cfg = DEFAULT_CONFIGS["sys_strelka_cfg"]
elif os.path.exists(DEFAULT_CONFIGS["dev_strelka_cfg"]):
strelka_cfg = DEFAULT_CONFIGS["dev_strelka_cfg"]
if strelka_cfg is None:
sys.exit("no strelka configuration found")
logging.info(f"using strelka configuration {strelka_cfg}")
daemon_cfg = conf.parse_yaml(path=strelka_cfg, section="daemon")
processes_cfg = daemon_cfg.get("processes", {})
run_broker = processes_cfg.get("run_broker", True)
run_workers = processes_cfg.get("run_workers", True)
worker_count = processes_cfg.get("worker_count", 4)
run_logrotate = processes_cfg.get("run_logrotate", True)
shutdown_timeout = processes_cfg.get("shutdown_timeout", 45)
broker_process = None
logrotate_process = None
worker_processes = []
if run_broker:
broker_process = lib.Broker(daemon_cfg)
broker_process.start()
else:
logging.info("broker disabled")
if run_logrotate:
logrotate_process = lib.LogRotate(daemon_cfg)
logrotate_process.start()
else:
logging.info("log rotation disabled")
if run_workers:
for _ in range(worker_count):
worker_process = lib.Worker(strelka_cfg, daemon_cfg)
worker_process.start()
worker_processes.append(worker_process)
else:
logging.info("workers disabled")
while run:
if run_broker:
if not broker_process.is_alive():
broker_process.join()
broker_process = lib.Broker(daemon_cfg)
broker_process.start()
if run_logrotate:
if not logrotate_process.is_alive():
logrotate_process.join()
logrotate_process = lib.LogRotate(daemon_cfg)
logrotate_process.start()
if run_workers:
for process in list(worker_processes):
if not process.is_alive():
process.join()
worker_processes.remove(process)
worker_process = lib.Worker(strelka_cfg, daemon_cfg)
worker_process.start()
worker_processes.append(worker_process)
time.sleep(5)
logging.info("starting shutdown of running child processes"
f" (using timeout value {shutdown_timeout})")
try:
with interruptingcow.timeout(shutdown_timeout,
exception=errors.QuitStrelka):
if run_broker:
utils.signal_children([broker_process], signal.SIGUSR1)
if run_workers:
utils.signal_children(worker_processes, signal.SIGUSR1)
if run_logrotate:
utils.signal_children([logrotate_process], signal.SIGUSR1)
logging.debug("finished shutdown of running"
" child processes")
except errors.QuitStrelka:
logging.debug("starting forcible shutdown of running"
" child processes")
if run_broker:
utils.signal_children([broker_process], signal.SIGKILL)
if run_workers:
utils.signal_children(worker_processes, signal.SIGKILL)
if run_logrotate:
utils.signal_children([logrotate_process], signal.SIGKILL)
logging.info("finished")
if __name__ == "__main__":
main()