#!/bin/bash source /opt/app-root/etc/generate_container_user set -e function is_gunicorn_installed() { hash gunicorn &>/dev/null } function is_django_installed() { python -c "import django" &>/dev/null } function should_migrate() { is_django_installed && [[ -z "$DISABLE_MIGRATE" ]] } # Guess the number of workers according to the number of cores function get_default_web_concurrency() { limit_vars=$(cgroup-limits) local $limit_vars if [ -z "${NUMBER_OF_CORES:-}" ]; then echo 1 return fi local max=$((NUMBER_OF_CORES*2)) # Require at least 43 MiB and additional 40 MiB for every worker local default=$(((${MEMORY_LIMIT_IN_BYTES:-MAX_MEMORY_LIMIT_IN_BYTES}/1024/1024 - 43) / 40)) default=$((default > max ? max : default)) default=$((default < 1 ? 1 : default)) # According to http://docs.gunicorn.org/en/stable/design.html#how-many-workers, # 12 workers should be enough to handle hundreds or thousands requests per second default=$((default > 12 ? 12 : default)) echo $default } function maybe_run_in_init_wrapper() { if [[ -z "$ENABLE_INIT_WRAPPER" ]]; then exec "$@" else exec $STI_SCRIPTS_PATH/init-wrapper "$@" fi } APP_HOME=$(readlink -f "${APP_HOME:-.}") # Change the working directory to APP_HOME PYTHONPATH="$(pwd)${PYTHONPATH:+:$PYTHONPATH}" cd "$APP_HOME" app_script_check="${APP_SCRIPT-}" APP_SCRIPT="${APP_SCRIPT-app.sh}" if [[ -f "$APP_SCRIPT" ]]; then echo "---> Running application from script ($APP_SCRIPT) ..." if [[ "$APP_SCRIPT" != /* ]]; then APP_SCRIPT="./$APP_SCRIPT" fi maybe_run_in_init_wrapper "$APP_SCRIPT" else test -n "$app_script_check" && (>&2 echo "ERROR: file '$app_script_check' not found.") && exit 1 fi app_file_check="${APP_FILE-}" APP_FILE="${APP_FILE-app.py}" if [[ -f "$APP_FILE" ]]; then echo "---> Running application from Python script ($APP_FILE) ..." maybe_run_in_init_wrapper python "$APP_FILE" else test -n "$app_file_check" && (>&2 echo "ERROR: file '$app_file_check' not found.") && exit 1 fi # Look for 'manage.py' in the current directory manage_file=./manage.py if should_migrate; then if [[ -f "$manage_file" ]]; then echo "---> Migrating database ..." python "$manage_file" migrate --noinput else echo "WARNING: seems that you're using Django, but we could not find a 'manage.py' file." echo "Skipped 'python manage.py migrate'." fi fi if is_gunicorn_installed; then setup_py=$(find "$HOME" -maxdepth 2 -type f -name 'setup.py' -print -quit) # Look for wsgi module in the current directory if [[ -z "$APP_MODULE" && -f "./wsgi.py" ]]; then APP_MODULE=wsgi elif [[ -z "$APP_MODULE" && -f "$setup_py" ]]; then APP_MODULE="$(python "$setup_py" --name)" fi if [[ "$APP_MODULE" ]]; then export WEB_CONCURRENCY=${WEB_CONCURRENCY:-$(get_default_web_concurrency)} echo "---> Serving application with gunicorn ($APP_MODULE) ..." exec gunicorn "$APP_MODULE" --bind=0.0.0.0:8080 --access-logfile=- --config "$APP_CONFIG" fi fi if is_django_installed; then if [[ -f "$manage_file" ]]; then echo "---> Serving application with 'manage.py runserver' ..." echo "WARNING: this is NOT a recommended way to run you application in production!" echo "Consider using gunicorn or some other production web server." maybe_run_in_init_wrapper python "$manage_file" runserver 0.0.0.0:8080 else echo "WARNING: seems that you're using Django, but we could not find a 'manage.py' file." echo "Skipped 'python manage.py runserver'." fi fi >&2 echo "ERROR: don't know how to run your application." >&2 echo "Please set either APP_MODULE, APP_FILE or APP_SCRIPT environment variables, or create a file 'app.py' to launch your application." exit 1