New assemble and run scripts with Django support
This commit is contained in:
parent
5488484238
commit
97a7a56175
2 changed files with 156 additions and 74 deletions
|
@ -1,5 +1,10 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
|
# This script uses these environment variables:
|
||||||
|
#
|
||||||
|
# DISABLE_COLLECTSTATIC: if not empty, inhibits execution of 'manage.py collectstatic'.
|
||||||
|
|
||||||
|
function assemble() {
|
||||||
# For SCL enablement
|
# For SCL enablement
|
||||||
source .bashrc
|
source .bashrc
|
||||||
|
|
||||||
|
@ -19,34 +24,40 @@ fi
|
||||||
# set permissions for any installed artifacts
|
# set permissions for any installed artifacts
|
||||||
chmod -R og+rwx /opt/openshift
|
chmod -R og+rwx /opt/openshift
|
||||||
|
|
||||||
|
|
||||||
# Support for Django
|
|
||||||
|
|
||||||
# Take shallowest manage.py script
|
|
||||||
MANAGE_FILE=$(find . -maxdepth 3 -type f -name 'manage.py' -printf '%d\t%P\n' | sort -nk1 | cut -f2 | head -1)
|
|
||||||
|
|
||||||
if pip show -q django && [ -f "$MANAGE_FILE" ]; then
|
|
||||||
set +e
|
set +e
|
||||||
|
django_collectstatic
|
||||||
# Check if collectstatic can be run.
|
set -e
|
||||||
python $MANAGE_FILE collectstatic --dry-run --noinput &> /dev/null && RUN_COLLECTSTATIC=true
|
|
||||||
|
|
||||||
# Collect assets if settings seems okay.
|
|
||||||
if [ "$RUN_COLLECTSTATIC" ]; then
|
|
||||||
echo "---> Collecting static assets ..."
|
|
||||||
python $MANAGE_FILE collectstatic --noinput 2>&1 | sed '/^Copying/d;/^$/d;/^ /d'
|
|
||||||
|
|
||||||
[ $? -ne 0 ] && {
|
|
||||||
echo "ERROR: 'manage.py collectstatic' failed. See the build logs for more info."
|
|
||||||
exit 1
|
|
||||||
} || true
|
|
||||||
else
|
|
||||||
echo "WARNING: 'manage.py collectstatic' ignored. To debug, run:"
|
|
||||||
echo " $ python $MANAGE_FILE collectstatic --noinput"
|
|
||||||
echo "Ignore this warning if you're not serving static files with Django."
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
|
|
||||||
# remove pip temporary directory
|
# remove pip temporary directory
|
||||||
rm -rf /tmp/pip_build_default
|
rm -rf /tmp/pip_build_default
|
||||||
|
}
|
||||||
|
|
||||||
|
function django_collectstatic() {
|
||||||
|
[ -n "$DISABLE_COLLECTSTATIC" ] && return
|
||||||
|
! pip show -q django && return
|
||||||
|
|
||||||
|
# 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)
|
||||||
|
|
||||||
|
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 collectstatic' ignored."
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "---> Collecting Django static files ..."
|
||||||
|
if ! python $MANAGE_FILE collectstatic --dry-run --noinput &> /dev/null; then
|
||||||
|
echo "WARNING: could not run 'manage.py collectstatic'. To debug, run:"
|
||||||
|
echo " $ python $MANAGE_FILE collectstatic --noinput"
|
||||||
|
echo "Ignore this warning if you're not serving static files with Django."
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
if ! python $MANAGE_FILE collectstatic --noinput 2>&1 | sed '/^Copying/d;/^$/d;/^ /d'; then
|
||||||
|
local status=$?
|
||||||
|
echo "ERROR: 'manage.py collectstatic' failed."
|
||||||
|
return $status
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
assemble
|
||||||
|
|
109
.sti/bin/run
109
.sti/bin/run
|
@ -1,38 +1,109 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
function is_gunicorn_installed() {
|
# This script uses these environment variables:
|
||||||
pip show gunicorn
|
#
|
||||||
}
|
# 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
|
# For SCL enablement
|
||||||
source .bashrc
|
source .bashrc
|
||||||
|
|
||||||
set -e
|
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}"
|
||||||
|
|
||||||
# Support for Django
|
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
|
||||||
|
}
|
||||||
|
|
||||||
# Take shallowest manage.py script
|
function run_django() {
|
||||||
MANAGE_FILE=$(find . -maxdepth 3 -type f -name 'manage.py' -printf '%d\t%P\n' | sort -nk1 | cut -f2 | head -1)
|
! pip show -q django && return
|
||||||
|
|
||||||
if pip show -q django && [ -f "$MANAGE_FILE" ]; then
|
# Find shallowest manage.py script, either ./manage.py or <project>/manage.py
|
||||||
set -x
|
local MANAGE_FILE=$(find . -maxdepth 2 -type f -name 'manage.py' -printf '%d\t%P\n' | sort -nk1 | cut -f2 | head -1)
|
||||||
python $MANAGE_FILE migrate --noinput
|
|
||||||
set +x
|
django_migrate "$MANAGE_FILE"
|
||||||
|
|
||||||
|
# try to use gunicorn
|
||||||
|
run_gunicorn "$APP_MODULE"
|
||||||
|
# or fallback to Django's development server
|
||||||
|
django_runserver "$MANAGE_FILE"
|
||||||
|
}
|
||||||
|
|
||||||
|
function django_migrate() {
|
||||||
|
[ -n "$DISABLE_MIGRATE" ] && return
|
||||||
|
echo "---> Migrating database ..."
|
||||||
|
django_manage_cmd "$1" migrate --noinput
|
||||||
|
}
|
||||||
|
|
||||||
|
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"
|
||||||
|
}
|
||||||
|
|
||||||
|
function django_manage_cmd() {
|
||||||
|
local MANAGE_FILE="$1"
|
||||||
|
local CMD="${@:2}"
|
||||||
|
|
||||||
|
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
|
fi
|
||||||
|
|
||||||
|
if [[ "$CMD" =~ ^runserver ]]; then
|
||||||
|
exec python "$MANAGE_FILE" $CMD
|
||||||
|
else
|
||||||
|
python "$MANAGE_FILE" $CMD
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
export APP_FILE=${APP_FILE:-"app.py"}
|
function run_gunicorn() {
|
||||||
|
! pip show -q gunicorn && return
|
||||||
|
|
||||||
if [[ ! -v APP_MODULE && -f setup.py ]]; then
|
local APP_MODULE="$1"
|
||||||
APP_MODULE=`python setup.py --name`":application"
|
|
||||||
|
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
|
fi
|
||||||
|
|
||||||
if is_gunicorn_installed && [[ -v APP_MODULE ]]; then
|
if [ -z "$APP_MODULE" ]; then
|
||||||
if [[ -v APP_CONFIG ]]; then
|
echo "WARNING: seems that you're trying to use gunicorn, but no WSGI application module was specified."
|
||||||
export CONFIG="--config ${APP_CONFIG}"
|
return
|
||||||
fi
|
|
||||||
exec gunicorn ${APP_MODULE} --bind=:8080 ${CONFIG}
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
exec python -u ${APP_FILE}
|
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"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
run
|
||||||
|
|
Loading…
Reference in a new issue