-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker_entrypoint.py
More file actions
82 lines (61 loc) · 2.3 KB
/
Copy pathdocker_entrypoint.py
File metadata and controls
82 lines (61 loc) · 2.3 KB
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
import os
import socket
import subprocess
import sys
import time
def wait_for_service(name, host, port, timeout=60):
if not host or not port:
return
print(f"Waiting for {name} at {host}:{port}...", flush=True)
deadline = time.time() + timeout
while time.time() < deadline:
try:
with socket.create_connection((host, int(port)), timeout=2):
return
except OSError:
time.sleep(1)
raise TimeoutError(f"Timed out waiting for {name} at {host}:{port}")
def run_manage_command(*args):
subprocess.check_call([sys.executable, "manage.py", *args])
def wait_for_django_migration(app_label, migration_name, timeout=120):
if not app_label or not migration_name:
return
print(f"Waiting for Django migration {app_label}.{migration_name}...", flush=True)
deadline = time.time() + timeout
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Car_Rental_System.settings")
while time.time() < deadline:
try:
import django
from django.db import connection
django.setup()
with connection.cursor() as cursor:
cursor.execute(
"""
SELECT 1
FROM django_migrations
WHERE app = %s AND name = %s
LIMIT 1
""",
[app_label, migration_name],
)
if cursor.fetchone():
return
except Exception:
time.sleep(2)
continue
time.sleep(2)
raise TimeoutError(f"Timed out waiting for Django migration {app_label}.{migration_name}")
def main():
wait_for_service("PostgreSQL", os.environ.get("DB_HOST"), os.environ.get("DB_PORT", "5432"))
wait_for_service("Redis", os.environ.get("REDIS_HOST"), os.environ.get("REDIS_PORT", "6379"))
if os.environ.get("RUN_MIGRATIONS") == "1":
run_manage_command("migrate", "--noinput")
wait_for_django_migration(
os.environ.get("WAIT_FOR_MIGRATION_APP"),
os.environ.get("WAIT_FOR_MIGRATION_NAME"),
)
if os.environ.get("RUN_COLLECTSTATIC") == "1":
run_manage_command("collectstatic", "--noinput")
os.execvp(sys.argv[1], sys.argv[1:])
if __name__ == "__main__":
main()