entrypoint.sh 1.78 KB
Newer Older
Evili del Rio's avatar
Evili del Rio committed
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
#!/usr/bin/env bash
#
# Exit if error occurs
#
set -e

if [[ $# -ge 1 ]]
then
    cd $(dirname $1)
else
    cd /app
fi

if [[ -n "${REQUIRED_PACKAGES}" ]]
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
REQUIREMENTS_FILES=${REQUIREMENTS_FILES:-requirements.txt}
for req in ${REQUIREMENTS_FILES}
do
    REQUIREMENTS="${REQUIREMENTS} -r ${req}"
done

pip install ${REQUIREMENTS}

## Remove gcc and friends but not the rest
if [[ -s "${REQUIRED_PACKAGES}" ]]
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)}

DJANGO_LOG_LEVEL=${DJANGO_LOG_LEVEL:-info}

# 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
    python3 manage.py diffsettings
fi

python3 manage.py migrate --no-input

python3 manage.py collectstatic --no-input

gunicorn --bind 0.0.0.0:5000 \
	          --capture-output \
	          --log-level=${DJANGO_LOG_LEVEL} \
		  --pid /var/run/gunicorn.pid \
		  ${DJANGO_WSGI} $*