-
Notifications
You must be signed in to change notification settings - Fork 4
/
runner
executable file
·171 lines (136 loc) · 4.56 KB
/
runner
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
#! /usr/bin/python3
import os
import re
import sys
import glob
import subprocess
import logging
from optparse import OptionParser
re_ignore = re.compile(r'(~$|^_|\.(dpkg-(old|dist|new|tmp)|example)$|\.pyc|\.comc$)')
def main():
parser = OptionParser("usage: %prog [options] start|stop|reload [configs]")
parser.add_option('--pid-dir', dest='pid_dir',
default='/var/run/gunicorn')
parser.add_option('--log-dir', dest='log_dir',
default='/var/log/gunicorn')
parser.add_option('--server-name', dest='server_name',
default='ksi-backend')
parser.add_option('-v', '--verbosity', type='int', dest='verbosity',
default=2, help='Verbosity level; 0=minimal output, 1=normal output' \
' 2=verbose output, 3=very verbose output')
options, args = parser.parse_args()
try:
logging.basicConfig(
format='%(levelname).1s: %(message)s',
level={
0: logging.ERROR,
1: logging.WARNING,
2: logging.INFO,
3: logging.DEBUG,
}[options.verbosity]
)
except KeyError:
parser.error("Invalid verbosity level")
log = logging.getLogger('gunicorn-debian')
configs = args[1:]
try:
action = args[0]
except IndexError:
parser.error("Missing action")
if action not in ('start', 'stop', 'restart', 'reload'):
parser.error("Invalid action: %s" % action)
if not os.path.exists(options.pid_dir):
log.info("Creating %s", options.pid_dir)
os.makedirs(options.pid_dir)
sys.dont_write_bytecode = True
CONFIG = {
'working_dir': '.',
'python': './ksi-py3-venv/bin/python3',
'user': 'ksi',
'group': 'ksi',
'environment': {
'PATH': os.environ[ "PATH" ],
},
'args': [
'-c',
'gunicorn_cfg.py',
'app:api',
]
}
config = Config(options.server_name, options, CONFIG, log)
log.debug("Calling .%s() on %s", action, config.basename())
getattr(config, action)()
return 0
class Config(dict):
def __init__(self, filename, options, data, log):
self.filename = filename
self.options = options
self.log = log
data['args'] = list(data.get('args', []))
data.setdefault('user', 'ksi')
data.setdefault('group', 'ksi')
data.setdefault('retry', '60')
data.setdefault('environment', {})
data.setdefault('working_dir', '.')
data.setdefault('python', '/usr/bin/python3')
self.update(data)
def print_name(self):
sys.stdout.write(" [%s]" % self.basename())
sys.stdout.flush()
def basename(self):
return os.path.basename(self.filename)
def pidfile(self):
return os.path.join(self.options.pid_dir, '%s.pid' % self.basename())
def logfile(self):
return os.path.join(self.options.log_dir, '%s.log' % self.basename())
def check_call(self, *args, **kwargs):
self.log.debug("Calling subprocess.check_call(*%r, **%r)", args, kwargs)
subprocess.check_call(*args, **kwargs)
def start(self):
daemon = './ksi-py3-venv/bin/gunicorn'
args = [
'start-stop-daemon',
'--start',
'--oknodo',
'--quiet',
'--chdir', self['working_dir'],
'--pidfile', self.pidfile(),
'--exec', self['python'], '--', daemon,
]
gunicorn_args = [
'--pid', self.pidfile(),
'--name', self.basename(),
'--user', self['user'],
'--group', self['group'],
'--daemon',
'--log-file', self.logfile(),
]
env = os.environ.copy()
env.update(self['environment'])
self.check_call(args + gunicorn_args + self['args'], env=env)
def stop(self):
self.check_call((
'start-stop-daemon',
'--stop',
'--oknodo',
'--quiet',
'--retry', self['retry'],
'--pidfile', self.pidfile(),
))
def restart(self):
self.stop()
self.start()
def reload(self):
try:
self.check_call((
'start-stop-daemon',
'--stop',
'--signal', 'HUP',
'--quiet',
'--pidfile', self.pidfile(),
))
except subprocess.CalledProcessError:
self.log.debug("Could not reload, so restarting instead")
self.restart()
if __name__ == '__main__':
sys.exit(main())