vbv/prepare_server.sh

87 lines
2.6 KiB
Bash
Executable File

#!/bin/bash
# set location to script directory
cd "${0%/*}"
if [ -z "$CI" ];
then
# kill all subprocess on exit so that Bitbucket Pipelines process will not hang
trap "exit" INT TERM ERR
trap "kill 0" EXIT
else echo "CI is set to $CI";
fi
# script should fail when any process returns non zero code
set -e
# handle arguments
SKIP_SETUP=false
START_BACKGROUND=false
for i in "$@"
do
case $i in
--start-background)
START_BACKGROUND=true
shift # past argument
;;
--skip-setup)
SKIP_SETUP=true
shift # past argument with no value
;;
*)
# unknown option
;;
esac
done
echo "SKIP_SETUP = ${SKIP_SETUP}"
# migrate env vars to the built-in postgres variables
if [ -z ${PGDATABASE+x} ]; then # var is empty
PGDATABASE=${POSTGRES_DB:-vbv_lernwelt};
fi
if [ -z ${PGHOST+x} ]; then # var is empty
PGHOST=${POSTGRES_HOST:-localhost};
fi
if [ -z ${PGPORT+x} ]; then # var is empty
PGPORT=${POSTGRES_PORT:-5432}
fi
if [ -z ${PGUSER+x} ]; then # var is empty
PGUSER=${POSTGRES_USER:-postgres}
fi
DJANGO_PORT=${DJANGO_PORT:-8000}
if [ "$SKIP_SETUP" = false ]; then
# TODO: in heroku we must do a `pg:resets` to reset the db
echo "Check postgres connection"
psql -d postgres -c "SELECT 1" # use default db to connect to
echo "Drop all connections to $PGDATABASE"
psql -c "SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = '$PGDATABASE' AND pid <> pg_backend_pid();" > /dev/null 2>&1 || true
echo "Drop database: $PGDATABASE"
psql -d postgres -c "drop database if exists $PGDATABASE;" # use default db to drop another database, can't drop the currently connected database
echo "Create database: $PGDATABASE"
psql -d postgres -c "create database $PGDATABASE;" # use default database, as the set database does not exist currently
echo "initialize database for django"
python server/manage.py createcachetable
python server/manage.py migrate
python server/manage.py create_default_users
python server/manage.py create_default_courses
python server/manage.py create_default_notifications
# make django translations
(cd server && python manage.py compilemessages)
else
# TODO: can we reset important data without resetting the database?
echo "Skip database setup"
# python3 src/manage.py cypress_reset --settings="$DJANGO_SETTINGS_MODULE"
fi
if [ "$START_BACKGROUND" = true ]; then
cd server && python manage.py runserver "${DJANGO_PORT}" > /dev/null &
else
cd server && python manage.py runserver "${DJANGO_PORT}"
fi