-
Notifications
You must be signed in to change notification settings - Fork 2
/
manage.py
executable file
·121 lines (95 loc) · 3.3 KB
/
manage.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
#!/usr/bin/env python
import os
import sys
import time
def test(args, settings_module):
"""Launch tests via tox"""
import tox
sys.exit(tox.cmdline(args))
def bash(args, settings_module):
"""Launch bash session"""
from subprocess import call
sys.exit(call('/usr/bin/bash', *args))
def _wait_for_db(sleep_interval=2, max_wait=600):
"""Wait for DB container to start"""
from django.db import connections
from django.db.utils import OperationalError
t0 = time.time()
while True:
try:
connections['default'].cursor()
except OperationalError:
if time.time() - t0 > max_wait:
raise
else:
print "Waiting for DB initialization"
time.sleep(sleep_interval)
else:
break
def _create_superuser():
from django.contrib.auth import get_user_model
UserModel = get_user_model()
username = os.environ.get('DJANGO_ADMIN_USERNAME', '')
password = os.environ.get('DJANGO_ADMIN_PASSWORD', '')
if not username or not password:
print "Admin credentials were not provided"
return
username_kwargs = {UserModel.USERNAME_FIELD: username}
if UserModel.objects.filter(**username_kwargs).exists():
print 'Admin already exists'
return
UserModel.objects.create_superuser(password=password, email='',
**username_kwargs)
print 'Created default admin:', username
def launch(args, settings_module):
"""Launch the application"""
from django.conf import settings
from django.core.management import execute_from_command_line
_wait_for_db()
execute_from_command_line(['manage.py', 'migrate', '--noinput'])
execute_from_command_line(['manage.py', 'update_index'])
_create_superuser()
http_socket = (args[:1] + [':8000'])[0]
uwsgi_args = [
'uwsgi',
'--module', 'filmfest.wsgi',
'--env', 'DJANGO_SETTINGS_MODULE={}'.format(settings_module),
'--http-socket', http_socket,
'--master',
'--harakiri=120',
'--max-requests=1000',
'--vacuum',
'--static-map', '%s=%s' % (settings.MEDIA_URL, settings.MEDIA_ROOT),
]
if settings.DEBUG:
uwsgi_args.extend([
'--py-autoreload', '1',
])
else:
uwsgi_args.extend([
'--static-map', '%s=%s' % (settings.STATIC_URL,
settings.STATIC_ROOT)
])
if sys.real_prefix:
# we are in a virtual env - let's take it into account
uwsgi_args.extend(['--home', sys.prefix])
os.execv(os.path.join(sys.prefix, 'bin', 'uwsgi'), uwsgi_args)
else:
os.execvp('uwsgi', uwsgi_args)
def django(args, settings_module):
"""Call django manage.py handler"""
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
def main():
settings_module = os.environ.setdefault("DJANGO_SETTINGS_MODULE",
"filmfest.settings.dev")
cmd = (sys.argv[1:2] + [None])[0]
args = sys.argv[2:]
handler = {
'test': test,
'bash': bash,
'launch': launch,
}.get(cmd, django)
return handler(args, settings_module)
if __name__ == "__main__":
main()