forked from stevebrownlee/learn-ops-api
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathentrypoint.sh
More file actions
110 lines (96 loc) · 3.09 KB
/
entrypoint.sh
File metadata and controls
110 lines (96 loc) · 3.09 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
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
#!/bin/bash
set -e
export DJANGO_SETTINGS_MODULE="LearningPlatform.settings"
# Function to wait for PostgreSQL to be ready
wait_for_postgres() {
echo "Waiting for PostgreSQL at $LEARN_OPS_HOST:$LEARN_OPS_PORT..."
while ! pg_isready -h "$LEARN_OPS_HOST" -p "$LEARN_OPS_PORT" -U "$LEARN_OPS_USER"; do
sleep 1
done
echo "PostgreSQL is ready!"
}
wait_for_postgres
# Generate socialaccount fixture with environment variables
echo "Creating socialaccount fixture..."
cat > ./LearningAPI/fixtures/socialaccount.json << EOF
[
{
"model": "sites.site",
"pk": 1,
"fields": {
"domain": "learningplatform.com",
"name": "Learning Platform"
}
},
{
"model": "socialaccount.socialapp",
"pk": 1,
"fields": {
"provider": "github",
"name": "Github",
"client_id": "$LEARN_OPS_CLIENT_ID",
"secret": "$LEARN_OPS_SECRET_KEY",
"key": "",
"sites": [
1
]
}
}
]
EOF
# Generate superuser fixture with environment variables
echo "Creating superuser fixture..."
DJANGO_GENERATED_PASSWORD=$(python3 ./djangopass.py "$LEARN_OPS_SUPERUSER_PASSWORD")
cat > ./LearningAPI/fixtures/superuser.json << EOF
[
{
"model": "auth.user",
"pk": 3,
"fields": {
"password": "$DJANGO_GENERATED_PASSWORD",
"last_login": null,
"is_superuser": true,
"username": "$LEARN_OPS_SUPERUSER_NAME",
"first_name": "Admina",
"last_name": "Straytor",
"email": "me@me.com",
"is_staff": true,
"is_active": true,
"date_joined": "2023-03-17T03:03:13.265Z",
"groups": [
2
],
"user_permissions": []
}
}
]
EOF
# Run migrations (always — safe to re-run, applies any new migrations)
echo "Running database migrations..."
python3 manage.py migrate
# If WIPE_DB=true, flush everything so fixtures will reload below
if [ "${WIPE_DB:-false}" = "true" ]; then
echo "WIPE_DB=true — flushing database..."
python3 manage.py flush --no-input
fi
# Only load fixtures when the DB is empty (no users exist yet)
USER_COUNT=$(python3 manage.py shell -c "from django.contrib.auth.models import User; print(User.objects.count())" 2>/dev/null | tail -1 | tr -d '[:space:]')
if [ "$USER_COUNT" = "0" ]; then
echo "Database is empty, loading fixture data..."
python3 manage.py loaddata ./LearningAPI/fixtures/*.json
echo "Fixture data loaded."
else
echo "Database already has data ($USER_COUNT users) — skipping fixture load."
fi
# Clean up temporary fixture files
echo "Cleaning up temporary fixture files..."
rm -f ./LearningAPI/fixtures/socialaccount.json
rm -f ./LearningAPI/fixtures/superuser.json
echo "Database setup complete!"
# Hand off to whatever was given in CMD (or docker run args)
if [ "$DEBUG" = "True" ]; then
shift # drop "python3" so debugpy receives "manage.py runserver ..." as the target
exec python -m debugpy --listen 0.0.0.0:5678 "$@"
else
exec "$@"
fi