2024-07-08 11:44:39 -04:00
#!/bin/bash
# Function to check PostgreSQL availability
2025-05-26 17:36:22 -04:00
# Helper to get the first non-empty environment variable
get_env( ) {
for var in " $@ " ; do
value = " ${ !var } "
if [ -n " $value " ] ; then
echo " $value "
return
fi
done
}
2024-07-08 11:44:39 -04:00
check_postgres( ) {
2025-05-26 17:36:22 -04:00
local db_host
local db_user
local db_name
local db_pass
db_host = $( get_env PGHOST)
db_user = $( get_env PGUSER POSTGRES_USER)
db_name = $( get_env PGDATABASE POSTGRES_DB)
db_pass = $( get_env PGPASSWORD POSTGRES_PASSWORD)
PGPASSWORD = " $db_pass " psql -h " $db_host " -U " $db_user " -d " $db_name " -c '\q' >/dev/null 2>& 1
2024-07-08 11:44:39 -04:00
}
2025-05-26 17:36:22 -04:00
2024-07-08 11:44:39 -04:00
# Wait for PostgreSQL to become available
until check_postgres; do
>& 2 echo "PostgreSQL is unavailable - sleeping"
sleep 1
done
>& 2 echo "PostgreSQL is up - continuing..."
2024-08-23 23:49:05 -04:00
# run sql commands
# psql -h "$PGHOST" -U "$PGUSER" -d "$PGDATABASE" -f /app/backend/init-postgis.sql
2024-07-08 11:44:39 -04:00
# Apply Django migrations
python manage.py migrate
2024-08-17 08:00:01 -04:00
# Create superuser if environment variables are set and there are no users present at all.
2025-01-07 10:27:11 -05:00
if [ -n " $DJANGO_ADMIN_USERNAME " ] && [ -n " $DJANGO_ADMIN_PASSWORD " ] && [ -n " $DJANGO_ADMIN_EMAIL " ] ; then
2024-07-08 11:44:39 -04:00
echo "Creating superuser..."
python manage.py shell << EOF
from django.contrib.auth import get_user_model
2025-01-07 10:27:11 -05:00
from allauth.account.models import EmailAddress
2024-07-08 11:44:39 -04:00
User = get_user_model( )
2025-01-07 10:27:11 -05:00
# Check if the user already exists
if not User.objects.filter( username = '$DJANGO_ADMIN_USERNAME' ) .exists( ) :
# Create the superuser
superuser = User.objects.create_superuser(
username = '$DJANGO_ADMIN_USERNAME' ,
email = '$DJANGO_ADMIN_EMAIL' ,
password = '$DJANGO_ADMIN_PASSWORD'
)
2024-07-08 11:44:39 -04:00
print( "Superuser created successfully." )
2025-01-07 10:27:11 -05:00
# Create the EmailAddress object for AllAuth
EmailAddress.objects.create(
user = superuser,
email = '$DJANGO_ADMIN_EMAIL' ,
verified = True,
primary = True
)
print( "EmailAddress object created successfully for AllAuth." )
2024-07-08 11:44:39 -04:00
else :
print( "Superuser already exists." )
EOF
fi
2025-01-07 10:27:11 -05:00
2025-01-13 14:12:05 -05:00
# Sync the countries and world travel regions
2024-08-21 13:14:38 -04:00
# Sync the countries and world travel regions
2024-09-11 09:31:25 -04:00
python manage.py download-countries
2025-01-13 14:12:05 -05:00
if [ $? -eq 137 ] ; then
>& 2 echo "WARNING: The download-countries command was interrupted. This is likely due to lack of memory allocated to the container or the host. Please try again with more memory."
exit 1
fi
2024-08-21 13:14:38 -04:00
2024-11-11 23:56:16 -05:00
cat /code/adventurelog.txt
2025-03-21 12:02:23 -04:00
# Start Gunicorn in foreground
exec gunicorn main.wsgi:application \
2025-04-06 22:55:13 +02:00
--bind [ ::] :8000 \
2025-03-21 20:27:46 -04:00
--workers 2 \
2025-03-21 12:02:23 -04:00
--timeout 120