forked from heartshare/nginx-amplify-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nginx-amplify-agent.py
executable file
·109 lines (89 loc) · 2.74 KB
/
nginx-amplify-agent.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import traceback
import amplify
amplify_path = '/'.join(amplify.__file__.split('/')[:-1])
sys.path.insert(0, amplify_path)
from gevent import monkey
monkey.patch_all(socket=False, ssl=False, select=False)
from optparse import OptionParser, Option
from amplify.agent.common.util import configreader
__author__ = "Mike Belov"
__copyright__ = "Copyright (C) Nginx, Inc. All rights reserved."
__credits__ = ["Mike Belov", "Andrei Belov", "Ivan Poluyanov", "Oleg Mamontov", "Andrew Alexeev", "Grant Hulegaard"]
__license__ = ""
__maintainer__ = "Mike Belov"
__email__ = "dedm@nginx.com"
def test_config(config, pid):
return configreader.test(config, pid)
usage = "usage: %prog [start|stop|configtest] [options]"
option_list = (
Option(
'--config',
action='store',
dest='config',
type='string',
help='path to config file',
default=None,
),
Option(
'--pid',
action='store',
dest='pid',
type='string',
help='path to pid file',
default=None,
),
Option(
'--foreground',
action='store_true',
dest='foreground',
help='do not daemonize, run in foreground',
default=False,
),
)
parser = OptionParser(usage, option_list=option_list)
(options, args) = parser.parse_args()
if __name__ == '__main__':
try:
from setproctitle import setproctitle
setproctitle('amplify-agent')
except ImportError:
pass
try:
action = sys.argv[1]
if action not in ('start', 'stop', 'configtest'):
raise IndexError
except IndexError:
print("Invalid action or no action supplied\n")
parser.print_help()
sys.exit(1)
# check config before start
if action in ('configtest', 'start'):
rc = test_config(options.config, options.pid)
print("")
if action == 'configtest' or rc:
sys.exit(rc)
try:
from amplify.agent.common.context import context
context.setup(
app='agent',
config_file=options.config,
pid_file=options.pid
)
except:
import traceback
print(traceback.format_exc(sys.exc_traceback))
try:
from amplify.agent.supervisor import Supervisor
supervisor = Supervisor(foreground=options.foreground)
if not options.foreground:
from amplify.agent.common.runner import Runner
daemon_runner = Runner(supervisor)
daemon_runner.do_action()
else:
supervisor.run()
except:
context.default_log.error('uncaught exception during run time', exc_info=True)
print(traceback.format_exc(sys.exc_traceback))