entrypoint.sh 3.01 KB
Newer Older
Evili del Rio's avatar
Evili del Rio committed
1 2 3 4 5 6
#!/usr/bin/env bash
#
# Exit if error occurs
#
set -e

Evili del Rio's avatar
Evili del Rio committed
7 8 9 10 11 12 13 14 15 16 17
#
# Set FIRST_RUN
#
NOT_FIRST_RUN="/.NOT_FIRST_RUN"
if [[ -f ${NOT_FIRST_RUN} ]]
then
    export FIRST_RUN=
else
    export FIRST_RUN=1
fi

Evili del Rio's avatar
Evili del Rio committed
18 19 20 21 22 23 24
if [[ $# -ge 1 ]]
then
    cd $(dirname $1)
else
    cd /app
fi

Evili del Rio's avatar
Evili del Rio committed
25
if [[ -n "${FIRST_RUN}" && -n "${REQUIRED_PACKAGES}" ]]
Evili del Rio's avatar
Evili del Rio committed
26 27 28 29 30 31 32
then
    apk update
    ## we add gcc, g++, and libc-dev so that we can compile.
    apk add gcc g++ libc-dev ${REQUIRED_PACKAGES}
fi

# Install requirements
Evili del Rio's avatar
Evili del Rio committed
33 34 35 36 37 38 39 40 41
if  [[ -n "${FIRST_RUN}" ]]
then
    REQUIREMENTS_FILES=${REQUIREMENTS_FILES:-requirements.txt}
    for req in ${REQUIREMENTS_FILES}
    do
	REQUIREMENTS="${REQUIREMENTS} -r ${req}"
    done
    pip install ${REQUIREMENTS}
fi
Evili del Rio's avatar
Evili del Rio committed
42 43

## Remove gcc and friends but not the rest
Evili del Rio's avatar
Evili del Rio committed
44
if [[ -n "${FIRST_RUN}" -n "${REQUIRED_PACKAGES}" ]]
Evili del Rio's avatar
Evili del Rio committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58
then
    apk del gcc g++ libc-dev
fi

## Find settings in CWD
settings_found=$(find . -name settings  -o -name settings.py | \
		     head |tr / . |\
		     cut -d . -s -f 3- | head -1)

export DJANGO_SETTINGS_MODULE=${DJANGO_SETTINGS_MODULE:-${settings_found}}

## Find .wsgi
DJANGO_WSGI=${DJANGO_WSGI:-$(basename $(find . -maxdepth 2 -type f -name wsgi.py  | head -1|awk -F/ '{print $2"."$3}') .py)}

59 60 61 62 63
export DJANGO_LOG_LEVEL=${DJANGO_LOG_LEVEL:-info}

# Select POSTGRES_HOST (if any and no other database definition available):
if [[ -z "${DJANGO_DATABASES}" && -z "${DATABASE_URL}" && -z "${POSTGRES_HOST}" ]]
then
Evili del Rio's avatar
Evili del Rio committed
64
    p_host=$(/bin/printenv | /bin/egrep -e 'POSTGRES.*HOST' | /usr/bin/sort | /usr/bin/head -1)
65 66 67 68 69 70
    if [[ -n ${p_host} ]]
    then
	export POSTGRES_HOST=${p_host}
    fi
fi

Evili del Rio's avatar
Evili del Rio committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

# Populate settings with default, and override them with environment variables
cat > /settings/final_settings.py <<EOF

#
# Keep default settings apart
#
import ${DJANGO_SETTINGS_MODULE} as __default_settings
#
# Default project settings
#
from ${DJANGO_SETTINGS_MODULE} import *

EOF
#
# Module to override settings with environment variables
#
cat >> /settings/final_settings.py < /environment_settings.py

export PYTHONPATH=/settings
export DJANGO_SETTINGS_MODULE=final_settings

if [[ ${DJANGO_LOG_LEVEL} == "debug" ]]
then
95
    printenv
Evili del Rio's avatar
Evili del Rio committed
96 97 98 99 100 101 102
    python3 manage.py diffsettings
fi

python3 manage.py migrate --no-input

python3 manage.py collectstatic --no-input

Evili del Rio's avatar
Evili del Rio committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
#
# Create Super User
#
if [[ -n "${FIRST_RUN}"                 && \
      -n "${DJANGO_SUPERUSER_USERNAME}" && \
      -n "${DJANGO_SUPERUSER_EMAIL}"    && \
      -n "${DJANGO_SUPERUSER_PASSWORD}" ]]
then
    # Try Django>=3.0
    python manage.py createsuperuser --no-input
    if [[ $? -ne 0]]
    then
	# Try Django < 3.0
	python manage.py shell <<EOF
import os
from django.contrib.auth.models import User
User.objects.create_superuser(
  os.getenv('DJANGO_SUPERUSER_USERNAME'),
  email=os.getenv('DJANGO_SUPERUSER_EMAIL'),
  password=os.getenv('DJANGO_SUPERUSER_PASSWORD'),
)
EOF
    fi
fi

#
# Mark NOT_FIRST_RUN for later.
#
touch ${NOT_FIRST_RUN}


#
# Run app
#
Evili del Rio's avatar
Evili del Rio committed
137 138 139 140 141
gunicorn --bind 0.0.0.0:5000 \
	          --capture-output \
	          --log-level=${DJANGO_LOG_LEVEL} \
		  --pid /var/run/gunicorn.pid \
		  ${DJANGO_WSGI} $*