-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_supervisord_config.py
executable file
·161 lines (141 loc) · 5.94 KB
/
create_supervisord_config.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
#!/usr/bin/env python
import sys, os
from LogConfigParser import Config
from ConfigParser import ConfigParser
OPTIONAL_SUPERVISOR_PROCESS_OPTIONS = ['user']
if __name__ == "__main__":
if not os.path.exists("workers_enabled"):
os.mkdir("workers_enabled")
else:
# clear out the enabled directory
for conf in os.listdir("workers_enabled"):
if os.path.isfile(os.path.join("workers_enabled", conf)):
os.remove(os.path.join("workers_enabled", conf))
if not os.path.exists("workers_available"):
os.mkdir("workers_available")
c = Config()
base_superv_conf = "supervisord.conf.base"
if len(sys.argv) == 2:
# use a different base supervisor file
base_superv_conf = sys.argv[1]
supervisord_config = Config(base_superv_conf)
if 'supervisor' in c.sections():
supervisord_config.add_section('inet_http_server')
params = {'username':'guest',
'password':'mypassword',
'port':'127.0.0.1:9001'}
for key in params:
if c.has_option('supervisor', key):
supervisord_config.set('inet_http_server', key, c.get('supervisor', key))
else:
supervisord_config.set('inet_http_server', key, params['key'])
with open("supervisord.conf", "w") as cfgfile:
supervisord_config.write(cfgfile)
# process_* for simple, single use processes that don't require additional configuration
# aside from the 'command' instruction
# eg command = ../redis/redis-server ../redis/redis.conf
for worker in [x for x in c.sections() if x.startswith("process_")]:
# Worker defaults:
params = {'autorestart':'true',
'numprocs':'1',
'process_name':'%s_%%(process_num)s' % worker,
'autostart':'true',
'redirect_stderr':'True',
'stopwaitsecs':'10',
'startsecs':'10',
'priority':'999',
'startretries':'3',
'stdout_logfile':'workerlogs/%s.log' % worker}
worker_config = ConfigParser()
section_name = "program:%s" % worker
worker_config.add_section(section_name)
for key in params:
if c.has_option(worker, key):
worker_config.set(section_name, key, c.get(worker, key))
else:
worker_config.set(section_name, key, params[key])
for key in OPTIONAL_SUPERVISOR_PROCESS_OPTIONS:
if c.has_option(worker, key):
worker_config.set(section_name, key, c.get(worker, key))
# set command
command = c.get(worker, 'command')
worker_config.set(section_name, 'command', command )
worker_conf_fname = "workers_available/%s.conf" % worker
with open(worker_conf_fname, "w") as cfgfile:
worker_config.write(cfgfile)
try:
os.symlink(os.path.join("..", worker_conf_fname), "workers_enabled/%s.conf" % worker)
except Exception, e:
# make a copy, for those systems that cannot symlink
import shutil
shutil.copy(worker_conf_fname, "workers_enabled/%s.conf" % worker)
for worker in [x for x in c.sections() if x.startswith("worker_")]:
# Worker defaults:
params = {'autorestart':'true',
'numprocs':'3',
'process_name':'%s_%%(process_num)s' % worker,
'autostart':'true',
'redirect_stderr':'True',
'stopwaitsecs':'10',
'startsecs':'10',
'priority':'999',
'startretries':'3',
'stdout_logfile':'procname.log'}
worker_config = ConfigParser()
section_name = "program:%s" % worker
worker_config.add_section(section_name)
for key in params:
if c.has_option(worker, key):
worker_config.set(section_name, key, c.get(worker, key))
else:
worker_config.set(section_name, key, params[key])
for key in OPTIONAL_SUPERVISOR_PROCESS_OPTIONS:
if c.has_option(worker, key):
worker_config.set(section_name, key, c.get(worker, key))
# set command
command = c.get(worker, 'command')
worker_config.set(section_name, 'command', "%s %s" % (command, "%(process_num)s") )
worker_conf_fname = "workers_available/%s.conf" % worker
with open(worker_conf_fname, "w") as cfgfile:
worker_config.write(cfgfile)
try:
os.symlink(os.path.join("..", worker_conf_fname), "workers_enabled/%s.conf" % worker)
except Exception, e:
# make a copy, for those systems that cannot symlink
import shutil
shutil.copy(worker_conf_fname, "workers_enabled/%s.conf" % worker)
for logger in [x for x in c.sections() if x.startswith("logger_")]:
# Worker defaults:
params = {'autorestart':'true',
'numprocs':'1',
'process_name':'%s_%%(process_num)s' % logger,
'autostart':'true',
'redirect_stderr':'True',
'stopwaitsecs':'10',
'startsecs':'10',
'priority':'999',
'startretries':'3',
'stdout_logfile':'procname.log'}
worker_config = ConfigParser()
section_name = "program:%s" % logger
worker_config.add_section(section_name)
for key in params:
if c.has_option(logger, key):
worker_config.set(section_name, key, c.get(logger, key))
else:
worker_config.set(section_name, key, params[key])
for key in OPTIONAL_SUPERVISOR_PROCESS_OPTIONS:
if c.has_option(logger, key):
worker_config.set(section_name, key, c.get(logger, key))
# set command
command = c.get(logger, 'command')
worker_config.set(section_name, 'command', "%s %s %s" % (command, "%(process_num)s", logger) )
worker_conf_fname = "workers_available/%s.conf" % logger
with open("workers_available/%s.conf" % logger, "w") as cfgfile:
worker_config.write(cfgfile)
try:
os.symlink(os.path.join("..", worker_conf_fname), "workers_enabled/%s.conf" % logger)
except Exception, e:
# make a copy, for those systems that cannot symlink
import shutil
shutil.copy(worker_conf_fname, "workers_enabled/%s.conf" % logger)