-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·65 lines (55 loc) · 1.84 KB
/
main.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
#!/usr/bin/env python
import importlib
import json
import os
import time
# ----------------------------------------------------------------------
# MAIN FUNCTION
# ----------------------------------------------------------------------
def main():
"""
Based on environment variables calls functions to check different
services
"""
tries = int(os.getenv("TRIES", 0))
checks = json.load(open("checks.json"))
for exit_code, check in enumerate(checks, start=1):
args = dict()
for env, key in check['parameters'].items():
v = os.getenv(env)
if v:
args[key] = v
if args:
if "." in check['function']:
mod_name, func_name = check['function'].rsplit('.', 1)
mod = importlib.import_module(mod_name)
func = getattr(mod, func_name)
else:
func = globals()[check['function']]
check_func(tries, exit_code, func, **args)
def check_func(tries, exit_code, func, **kwargs):
"""
Simple wrapper to call function. Tries to call the given function until
the number of tries is done. If no more tries are left, it will exit the
program with the given exit code.
:param func: actual function to call
:param exit_code: exit with exit_code
:param tries: number of attempts before exiting
:param kwargs: arguments for function
:return:
"""
while tries >= 0:
try:
if func(**kwargs):
return
except Exception as error:
print('Error:', error.__class__.__name__, error)
time.sleep(1)
if tries == 1:
break
if tries > 0:
tries = tries - 1
exit(exit_code)
# ----------------------------------------------------------------------
if __name__ == '__main__':
main()