-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_service.py
executable file
·92 lines (70 loc) · 2.28 KB
/
run_service.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import os
import sys
##
# Env setup
##
if sys.version_info[:2] < (3, 0):
reload(sys)
sys.setdefaultencoding('utf-8')
nebula_root = os.path.abspath(os.path.split(sys.argv[0])[0])
##
# Vendor imports
##
vendor_dir = os.path.join(nebula_root, "vendor")
if os.path.exists(vendor_dir):
for pname in os.listdir(vendor_dir):
pname = os.path.join(vendor_dir, pname)
pname = os.path.abspath(pname)
if not pname in sys.path:
sys.path.insert(0, pname)
from nx import *
from nx.plugins import plugin_path
config["nebula_root"] = nebula_root
if plugin_path:
python_plugin_path = os.path.join(plugin_path, "python")
if os.path.exists(python_plugin_path):
sys.path.append(python_plugin_path)
if __name__ == "__main__":
try:
id_service = int(sys.argv[1])
except:
critical_error("You must provide service id as first parameter")
db = DB()
db.query("SELECT agent, title, host, loop_delay, settings FROM nx_services WHERE id_service=%d" % id_service)
try:
agent, title, host, loop_delay, settings = db.fetchall()[0]
except:
critical_error("Unable to start service %s. No such service" % id_service)
config["user"] = logging.user = title
if host != config["host"]:
critical_error("This service should not run here.")
if settings:
try:
settings = ET.XML(settings)
except:
db.query("UPDATE nx_services SET autostart=0 WHERE id_service=%d" % id_service)
db.commit()
critical_error("Malformed settings XML")
_module = __import__("services.%s" % agent, globals(), locals(), ["Service"], -1)
Service = _module.Service
service = Service(id_service, settings)
while True:
try:
service.on_main()
last_run = time.time()
while True:
time.sleep(min(loop_delay, 2))
service.heartbeat()
if time.time() - last_run >= loop_delay:
break
except (KeyboardInterrupt):
sys.exit(0)
except (SystemExit):
break
except:
log_traceback()
time.sleep(2)
sys.exit(1)