- Download source code as zip from gitlab instead of git+https because speed. - Check if the $DATABASE_URL is not defined, use a default sqlite database. - Use dj-database-url package to set database in django settings using DATABASE_URL environment variable. - Use UWSGI_LOG_URL as the URL for UWSGI logging and set it to a default value if it has not been defined.
95 lines
3.1 KiB
Bash
Executable File
95 lines
3.1 KiB
Bash
Executable File
#! /bin/bash
|
|
set -e
|
|
|
|
# Check if $DATABASE_URL is defined, if not, use a standard sqlite database.
|
|
#
|
|
# If the $DATABASE_URL is defined and is postgres, check if it is available
|
|
# yet. Do not start the container before the postgresql boots up.
|
|
#
|
|
# If the $DATABASE_URL is defined and is mysql, check if the database is
|
|
# available before the container boots up.
|
|
#
|
|
# TODO: Check the database type and detect if it is up based on that. For now,
|
|
# assume that postgres is being used if DATABASE_URL is defined.
|
|
if [[ -z "$DATABASES_URL" ]]; then
|
|
echo "$DATABASE_URL is not defined. Using sqlite database..."
|
|
DATABASE_URL="sqlite:///opt/mailman-web-data/database/mailmanweb.db"
|
|
DATABASE_TYPE='sqlite'
|
|
if [[ ! -e "/opt/mailman-web-data/database" ]]; then
|
|
mkdir -p /opt/mailman-web-data/database/
|
|
fi
|
|
else
|
|
DATABASE_TYPE='postgres'
|
|
wait_for_postgres()
|
|
fi
|
|
|
|
function wait_for_postgres {
|
|
# Check if the postgres database is up and accepting connections before
|
|
# moving forward.
|
|
# TODO: Use python's psycopg2 module to do this in python instead of
|
|
# installing postgres-client in the image.
|
|
until psql $DATABASE_URL -c '\l'; do
|
|
>&2 echo "Postgres is unavailable - sleeping"
|
|
sleep 1
|
|
done
|
|
>&2 echo "Postgres is up - continuing"
|
|
}
|
|
|
|
function check_or_create {
|
|
# Check if the path exists, if not, create the directory.
|
|
if [[ ! -e dir ]]; then
|
|
echo "$1 does not exist, creating ..."
|
|
mkdir "$1"
|
|
fi
|
|
}
|
|
|
|
# Check if we are in the correct directory before running commands.
|
|
if [[ ! $(pwd) == '/opt/mailman-web' ]]; then
|
|
echo "Running in the wrong directory...switching to /opt/mailman-web"
|
|
cd /opt/mailman-web
|
|
fi
|
|
|
|
# Check if the logs directory is setup.
|
|
|
|
if [[ ! -e /opt/mailman-web-data/logs/mailmanweb.log ]]; then
|
|
echo "Creating log file for mailman web"
|
|
mkdir -p /opt/mailman-web-data/logs/
|
|
touch /opt/mailman-web-data/logs/mailmanweb.log
|
|
fi
|
|
|
|
if [[ ! -e /opt/mailman-web-data/logs/mailmanweb.log ]]; then
|
|
echo "Creating log file for uwsgi.."
|
|
touch /opt/mailman-web-data/logs/uwsgi.log
|
|
fi
|
|
|
|
# Check if the settings_local.py file exists, if yes, copy it too.
|
|
if [[ -e /opt/mailman-web-data/settings_local.py ]]; then
|
|
echo "Copying settings_local.py ..."
|
|
cp /opt/mailman-web-data/settings_local.py /opt/mailman-web/settings_local.py
|
|
else
|
|
echo "settings_local.py not found, it is highly recommended that you provide one/"
|
|
echo "Using default configuration to run."
|
|
fi
|
|
|
|
# Collect static for the django installation.
|
|
python manage.py collectstatic --noinput
|
|
|
|
# Migrate all the data to the database if this is a new installation, otherwise
|
|
# this command will upgrade the database.
|
|
python manage.py migrate
|
|
|
|
# Check if there is a non-standard location for logging defined.
|
|
# It can be changed by $UWSGI_LOG_URL environment variable, which if not set points
|
|
# to /opt/mailman-web/logs/uwsgi.log
|
|
# It can also point to a logging daemon accessible at a URL.
|
|
if [[ -z "$UWSGI_LOG_URL" ]]; then
|
|
echo "No $UWSGI_LOG_URL defined, logging uwsgi to /opt/mailman-web-data/logs/uwsgi.log ..."
|
|
UWSGI_LOG_URL='/opt/mailman-web-data/logs/uwsgi.log'
|
|
if [[ ! -e "$UWSGI_LOG_URL" ]]; then
|
|
touch "$UWSGI_LOG_URL"
|
|
fi
|
|
fi
|
|
|
|
# Run the web server.
|
|
uwsgi --http-auto-chunked --http-keepalive --logto "$UWSGI_LOG_URL"
|