django-ex/.sti/bin/run

110 lines
3.2 KiB
Text
Raw Normal View History

2015-05-22 13:47:35 +02:00
#!/bin/bash
# This script uses these environment variables:
#
# APP_MODULE: Python dotted path to your WSGI application.
# If not provided, tries to find the Python path to a 'wsgi.py' file
# in the source tree.
# That file is present in Django projects by default.
# APP_CONFIG: Optional configuration file for gunicorn.
# APP_FILE: Optional path to Python script to run your application.
# Defaults to 'app.py' if it exists.
#
# DISABLE_MIGRATE: if not empty, inhibits execution of 'manage.py migrate'.
BIND_ADDR="0.0.0.0:8080"
function run() {
# For SCL enablement
source .bashrc
set -e
# Try to run application with the strategies below, in precedence order.
# The first successful strategy takes over this script's process via exec.
# If no strategy succeed, report an error message and terminate.
run_django
run_gunicorn "$APP_MODULE"
run_python_script "${APP_FILE:-app.py}"
echo "ERROR: don't know how to run your application."
echo "Please set either APP_MODULE or APP_FILE environment variables,"
echo "or create a file 'app.py' to launch your application."
return 1
2015-05-22 13:47:35 +02:00
}
function run_django() {
! pip show -q django && return
2015-05-22 13:47:35 +02:00
# Find shallowest manage.py script, either ./manage.py or <project>/manage.py
local MANAGE_FILE=$(find . -maxdepth 2 -type f -name 'manage.py' -printf '%d\t%P\n' | sort -nk1 | cut -f2 | head -1)
2015-05-22 13:47:35 +02:00
django_migrate "$MANAGE_FILE"
2015-05-22 14:00:57 +02:00
# try to use gunicorn
run_gunicorn "$APP_MODULE"
# or fallback to Django's development server
django_runserver "$MANAGE_FILE"
}
2015-05-22 14:00:57 +02:00
function django_migrate() {
[ -n "$DISABLE_MIGRATE" ] && return
echo "---> Migrating database ..."
django_manage_cmd "$1" migrate --noinput
}
2015-05-22 14:00:57 +02:00
function django_runserver() {
echo "---> Serving application with 'manage.py runserver' ..."
echo "---> WARNING: this is NOT a recommended way to run you application in production!"
django_manage_cmd "$1" runserver "$BIND_ADDR"
}
2015-05-22 14:00:57 +02:00
function django_manage_cmd() {
local MANAGE_FILE="$1"
local CMD="${@:2}"
2015-05-22 14:00:57 +02:00
if [ ! -f "$MANAGE_FILE" ]; then
echo "WARNING: seems that you're using Django, but we could not find a 'manage.py' file."
echo "'manage.py $CMD' ignored."
return
fi
if [[ "$CMD" =~ ^runserver ]]; then
exec python "$MANAGE_FILE" $CMD
else
python "$MANAGE_FILE" $CMD
fi
}
2015-05-22 13:47:35 +02:00
function run_gunicorn() {
! pip show -q gunicorn && return
local APP_MODULE="$1"
if [ -z "$APP_MODULE" ]; then
# Find shallowest wsgi.py file, one of ./wsgi.py, <project>/wsgi.py or <project>/<project>/wsgi.py,
# replace "/" with "." and remove ".py" suffix
APP_MODULE=$(find . -maxdepth 3 -type f -name 'wsgi.py' -printf '%d\t%P\n' | sort -nk1 | cut -f2 | head -1 | sed 's:/:.:;s:.py$::')
fi
if [ -z "$APP_MODULE" ]; then
echo "WARNING: seems that you're trying to use gunicorn, but no WSGI application module was specified."
return
fi
echo "---> Serving application with gunicorn ..."
exec gunicorn "$APP_MODULE" --bind="$BIND_ADDR" --access-logfile=- --config "$APP_CONFIG"
}
function run_python_script() {
local APP_FILE="$1"
[ ! -f "$APP_FILE" ] && return
echo "---> Running application from Python script ..."
exec python -u "$APP_FILE"
}
2015-05-22 13:47:35 +02:00
run