Skip to content

Commit

Permalink
Add files to allow deployment on OpenShift 3.
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamDumpleton committed Oct 3, 2017
1 parent 2c519ce commit b44ffab
Show file tree
Hide file tree
Showing 13 changed files with 500 additions and 0 deletions.
54 changes: 54 additions & 0 deletions .s2i/action_hooks/build
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/bash

# This is a 'build' action hook script. This script must be executable
# and will be run by the S2I process after the original S2I 'assemble'
# script has been run. This hook is to allow a user to run additional
# build steps which may need the source code or build artefacts in
# place, or to setup any data required for the application.

set -eo pipefail

# Use the directory /opt/app-root/data for data. This would usually be
# used as a mount point for a separate persistent volume, but could be
# use if creating an ephemral instance for testing with data being
# thrown away on restarts.

echo " -----> Creating mount point for data directory."

mkdir -p /opt/app-root/data

# Install mod_wsgi even though not mentioned in requirements.txt file.

echo " -----> Installing mod_wsgi-express."

pip install mod_wsgi

# Install any optional package requirements. Don't fail if a package
# cannot be installed. Skip 'tesserocr' as we know that will not be able
# to be installed and can cause memory resource limit to be reached with
# the build container being killed off.

echo " -----> Installing option package requirements."

while read line; do
case "$line" in
-r*|\#*) continue ;;
tesserocr*) continue ;;
*) pip install "$line" || true ;;
esac
done < requirements-optional.txt

# Collect together all static assets used by the application.

echo " -----> Running collection of Django static resources."

python manage.py collectstatic --noinput

#python manage.py setuplang
#python manage.py setupgroups
python manage.py compilemessages

# Fixup permissions on all directories/files so still writable by group
# 'root' and can be updated when image is run with assigned user ID.

fix-permissions /opt/app-root
14 changes: 14 additions & 0 deletions .s2i/action_hooks/build_env
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# This is a 'build_env' action hook. This script will be executed inline
# to the custom 'assemble' script and therefore doesn't need to start
# with a '#!' line nor be marked as an executable file. This script will
# be interpreted as if it is a 'bash' script. This script can be used to
# dynamically set additional environment variables required by the build
# process. These might for example be environment variables which
# specify where third party libraries or packages installed from the
# 'pre_build' hook are located and which may be needed when building
# application artefacts. When we source the 'build_env' script, any
# environment variables set by it will be automatically exported.
# Although principally intended for setting environment variables, it
# can include other script logic, but this should be avoided except
# to the extent it is required to work out what to set any environment
# variables to.
24 changes: 24 additions & 0 deletions .s2i/action_hooks/deploy
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

# This is a 'deploy' action hook script. This script must be executable
# and will be run by the S2I process just before any 'run' script is
# run. This script is to allow a user to run any final steps just before
# the application is to be started. This can include running background
# tasks.

set -eo pipefail

# Ensure the media directory exists before starting application.

echo " -----> Ensure media directory has been created."

mkdir -p /opt/app-root/data/media

# If setup has already been run, just run the data migrations, else run
# setup for the application.

if [ -f /opt/app-root/data/markers/setup ]; then
powershift image migrate
else
powershift image setup
fi
29 changes: 29 additions & 0 deletions .s2i/action_hooks/deploy_env
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# This is a 'deploy_env' action hook. This script will be executed
# inline to the custom 'run' script as well as for any interactive shell
# if so enabled. As it is executed inline, it doesn't need to start with
# a '#!' line nor be marked as an executable file. This script will be
# interpreted as if it is a 'bash' script. This script allows a user to
# dynamically set additional environment variables required by the
# deploy process. These might for example be environment variables which
# tell an application where files it requires are located. When we
# source the 'deploy_env' script, any environment variables set by it
# will be automatically exported. Although principally intended for
# setting environment variables, it can include other script logic, but
# this should be avoided except to the extent it is required to work
# out what to set any environment variables to.

# If a secret token is not being provided through an environment variable
# and one has not been generated previously, generate a secret and cache
# it in the data directory.

generate_secret_token() {
python -c 'import random; import string; import sys; sys.stdout.write("".join([random.SystemRandom().choice(string.ascii_letters+string.digits) for i in range(50)]))'
}

if [ x"$OPENSHIFT_SECRET_TOKEN" = x"" ]; then
if [ ! -f /opt/app-root/data/secret-token.txt ]; then
generate_secret_token > /opt/app-root/data/secret-token.txt
fi

OPENSHIFT_SECRET_TOKEN=`cat /opt/app-root/data/secret-token.txt`
fi
14 changes: 14 additions & 0 deletions .s2i/action_hooks/migrate
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

# This is a 'migrate' action hook script. This script must be executable
# and will only be run by explicit execution of 'powershift image migrate'.
# The script should include any actions to perform data migrations when
# deploying a new version of the application.

set -eo pipefail

# Perform database migrations for the application.

echo " -----> Running Django database table migrations."

python manage.py migrate --noinput
14 changes: 14 additions & 0 deletions .s2i/action_hooks/pre_build
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

# This is a 'pre_build' action hook script. This script must be
# executable and will be run by the S2I process as the very first step.
# This script can be used to install additional third party libraries
# or packages that may be required by the build process. If the
# 'pre_build' hook needs any files from the application source code, it
# must grab them from the '/tmp/src' directory as they will only be
# copied into place by the original S2I 'assemble' script later on in
# the build process.

set -eo pipefail

mv /tmp/src/setup.py /tmp/src/setup-local-package.py
49 changes: 49 additions & 0 deletions .s2i/action_hooks/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash

# This is a 'run' action hook script. This script must be executable and
# will be run in place of the 'run' script provided by the S2I builder.
# The application to run in the container must be launched by this
# script and it must inherit the process ID of this script by using
# 'exec' to run the application.

set -eo pipefail

ARGS=""

# Ensure that all logging from Apache goes to the terminal. If you
# want exceptions to be logged from Django, you will need to setup
# LOGGING in Django settings appropriately.

ARGS="$ARGS --log-to-terminal"

# OpenShift S2I builders for Python all use port 8080 as exposed port.

ARGS="$ARGS --port 8080"

# Trust the proxy headers sent by the router for the remote client.

ARGS="$ARGS --trust-proxy-header X-Forwarded-For"
ARGS="$ARGS --trust-proxy-header X-Forwarded-Port"
ARGS="$ARGS --trust-proxy-header X-Forwarded-Proto"

# Map URLs to static files for browser icon and robots file.

ARGS="$ARGS --url-alias /robots.txt weblate/static/robots.txt"
ARGS="$ARGS --url-alias /favicon.ico weblate/static/favicon.ico"

# Map URL for static assets. Assumed here that URL_PREFIX is empty.

ARGS="$ARGS --url-alias /static /opt/app-root/src/wsgi/static"

# Map URL for media uploads. Assumed here that URL_PREFIX is empty.

ARGS="$ARGS --url-alias /media /opt/app-root/data/media"

# Import WSGI application via module import of weblate.wsgi.

ARGS="$ARGS --application-type module"
ARGS="$ARGS --entry-point weblate.wsgi"

# Run mod_wsgi-express.

exec mod_wsgi-express start-server $ARGS
51 changes: 51 additions & 0 deletions .s2i/action_hooks/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/bin/bash

# This is a 'setup' action hook script. This script must be executable
# and will only be run by explicit execution of 'powershift image setup'.
# The script should include any once off actions to setup an environment
# for the application.

set -eo pipefail

# Don't run setup of the application if it has already been done.

mkdir -p /opt/app-root/data/markers

if [ ! -f /opt/app-root/data/markers/setup ]; then
date > /opt/app-root/data/markers/setup
else
exit 0
fi

# Perform initial setup of the database for the application.

echo " -----> Running Django database table migrations."

python manage.py migrate --noinput

# Perform setup of Weblate definitions in database.

echo " -----> Setting up Weblate language definitions."

python manage.py setuplang

echo " -----> Setting up Weblate group definitions."

python manage.py setupgroups

# Create an initial super user account if supplied with credentials, or
# if script run in an interactive session, prompt for the credentials.

if [ x"$DJANGO_ADMIN_USERNAME" != x"" ]; then
echo " -----> Creating predefined Django super user"
(cat - | python manage.py shell) << !
from django.contrib.auth.models import User;
User.objects.create_superuser('$DJANGO_ADMIN_USERNAME',
'$DJANGO_ADMIN_EMAIL', '$DJANGO_ADMIN_PASSWORD')
!
else
if (tty > /dev/null 2>&1); then
echo " -----> Running Django super user creation"
python manage.py createsuperuser
fi
fi
11 changes: 11 additions & 0 deletions .s2i/bin/assemble
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

set -eo pipefail

# Install 'powershift-cli[image]' package to add support for action hooks.

pip install powershift-cli[image]

# Run the build phase with support for action hooks.

exec powershift image assemble
7 changes: 7 additions & 0 deletions .s2i/bin/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

set -eo pipefail

# Run the deployment phase with support for action hooks.

exec powershift image run
3 changes: 3 additions & 0 deletions .s2i/bin/save-artifacts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

true
7 changes: 7 additions & 0 deletions .s2i/environment
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
UPGRADE_PIP_TO_LATEST=1
DISABLE_COLLECTSTATIC=1
DISABLE_MIGRATE=1
OPENSHIFT_APP_DNS=*
OPENSHIFT_REPO_DIR=/opt/app-root/src
OPENSHIFT_DATA_DIR=/opt/app-root/data
DJANGO_SETTINGS_MODULE=weblate.settings_openshift
Loading

0 comments on commit b44ffab

Please sign in to comment.