forked from zentralopensource/zentral
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-entrypoint.py
executable file
·109 lines (96 loc) · 3.59 KB
/
docker-entrypoint.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/env python
# -*- coding:utf-8 -*-
import multiprocessing
import os
import random
import subprocess
import sys
import time
import warnings
try:
random = random.SystemRandom()
except NotImplementedError:
warnings.warn('No secure pseudo random number generator available.')
def wait_for_db_migration():
i = 0
while True:
try:
subprocess.check_call(['python', 'server/manage.py', 'migrate', '--noinput'])
except subprocess.CalledProcessError:
retry_delay = min(20, (i + 1)) * random.uniform(0.8, 1.2)
warnings.warn("Can't migrate DB! Sleep {:.1f}s…".format(retry_delay))
time.sleep(retry_delay)
i += 1
else:
break
print("DB migration OK")
def django_collectstatic():
subprocess.check_call(['python', 'server/manage.py', 'collectstatic', '-v0', '--noinput'])
def create_zentral_superuser():
username = os.environ.get("ZENTRAL_ADMIN_USERNAME")
email = os.environ.get("ZENTRAL_ADMIN_EMAIL")
if username and email:
print("Found admin username and email in environment. "
"Create superuser if missing.", flush=True)
args = ['python', 'server/manage.py', 'create_zentral_user', '--superuser']
force = os.environ.get("ZENTRAL_FORCE_ADMIN_PASSWORD_RESET")
if not force or force.upper() not in ("1", "TRUE", "YES", "Y"):
args.append("--skip-if-existing")
args.extend([username, email])
try:
subprocess.run(args, check=True)
except subprocess.CalledProcessError:
print("Could not create superuser!!!", flush=True)
else:
print("Admin username and email not found", flush=True)
KNOWN_COMMANDS = {
"runserver": ["python", 'server/manage.py', 'runserver', '0.0.0.0:8000'],
"gunicorn": ["gunicorn", "--chdir", "/zentral/server",
"-b", "0.0.0.0:8000",
"-w", str(2 * multiprocessing.cpu_count() + 1),
"--access-logfile", "-",
"--access-logformat", '"%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"',
"--error-logfile", "-",
"server.wsgi"],
"runworker": ["python", 'server/manage.py', 'runworker'],
"runworkers": ["python", 'server/manage.py', 'runworkers'],
"celery": ["celery", "-A", "server", "worker"],
# extras
"shell": ["python", 'server/manage.py', 'shell'],
"tests": ["python", 'server/manage.py', 'test', 'tests/'],
"createuser": ["python", 'server/manage.py', 'create_zentral_user'],
}
KNOWN_COMMANDS_EXTRA_ENV = {
"tests": {"ZENTRAL_PROBES_SYNC": "0"}
}
KNOWN_COMMANDS_CHDIR = {
"celery": "/zentral/server"
}
KNOWN_COMMANDS_TRIGGERING_COLLECTSTATIC = {
'gunicorn', # the staticfiles manifest is needed!
'runserver',
}
if __name__ == '__main__':
if len(sys.argv) < 2:
warnings.warn("Not enough arguments.")
sys.exit(2)
cmd = sys.argv[1]
env = os.environ.copy()
args = KNOWN_COMMANDS.get(cmd, None)
if args:
filename = args[0]
args.extend(sys.argv[2:])
wait_for_db_migration()
if cmd != "tests":
create_zentral_superuser()
if cmd in KNOWN_COMMANDS_TRIGGERING_COLLECTSTATIC:
django_collectstatic()
env.update(KNOWN_COMMANDS_EXTRA_ENV.get(cmd, {}))
wd = KNOWN_COMMANDS_CHDIR.get(cmd)
if wd:
os.chdir(wd)
print('Launch known command "{}"'.format(cmd), flush=True)
else:
filename = cmd
args = sys.argv[1:]
os.execvpe(filename, args, env)