forked from l4rm4nd/MemeLord
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentrypoint.sh
More file actions
56 lines (47 loc) · 1.83 KB
/
entrypoint.sh
File metadata and controls
56 lines (47 loc) · 1.83 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
#!/bin/bash
# Function to generate a random string
generate_random_string() {
LC_ALL=C tr -dc 'a-zA-Z0-9' < /dev/urandom | head -c 20
}
# Wait for PostgreSQL database
if [ "$DB_ENGINE" = "postgres" ]; then
echo "[i] Waiting for PostgreSQL db..."
while ! nc -z $POSTGRES_HOST $POSTGRES_PORT; do
sleep 0.1
done
echo "[i] PostgreSQL started"
fi
# Function to perform database migrations and initialization
perform_migrations() {
echo "[~] Migrating changes to the database"
python manage.py makemigrations
python manage.py makemigrations myapp
python manage.py migrate
python manage.py migrate myapp
python manage.py collectstatic --no-input --verbosity=0
if [ -z "$DB_INITIALIZED" ]; then
echo "------------------------------------"
admin_password=$(generate_random_string)
echo "[!!] Creating admin superuser account"
echo "from django.contrib.auth.models import User; User.objects.create_superuser('admin', 'admin@example.com', '$admin_password')" | python manage.py shell
echo "[>] Randomly generated password: $admin_password"
echo "------------------------------------"
fi
}
# Check for prior database init
if [ -z "$DB_ENGINE" ] || [ "$DB_ENGINE" = "sqlite3" ]; then
if test -f "/opt/app/database/db.sqlite3"; then
echo "[i] SQLite3 database found. Skipping initialization."
DB_INITIALIZED=true
fi
elif [ "$DB_ENGINE" = "postgres" ]; then
if PGPASSWORD=$POSTGRES_PASSWORD psql -h $POSTGRES_HOST -U $POSTGRES_USER -d $POSTGRES_DB -c '\dt' | grep -q 'django_migrations'; then
echo "PostgreSQL database found. Skipping initialization."
DB_INITIALIZED=true
fi
fi
# Perform database migrations
perform_migrations
# Spawn the web server
echo "[~] Spawning the application server"
uwsgi --ini docker/docker_uwsgi.ini