Spotter-VM/cts.sh

52 lines
2.0 KiB
Bash
Executable File

#!/bin/sh
SOURCE_DIR=$(realpath $(dirname "${0}"))/cts
# Check prerequisites
docker image ls | grep -q postgres || $(realpath $(dirname "${0}"))/postgres.sh
# Build Docker container
docker build -t cts ${SOURCE_DIR}
# Create database
export CTS_PWD=$(head -c 18 /dev/urandom | base64)
envsubst <${SOURCE_DIR}/createdb.sql | docker exec -i postgres psql
# Copy existing config files into persistent storage
mkdir -p /srv/cts/conf
chown 8006:8006 /srv/cts/conf
docker run --rm -v /srv/cts/conf:/mnt/conf cts cp -rp /srv/cts/cts/settings/. /mnt/conf
chown -R root:root /srv/cts/conf
# Configure CTS
export CTS_SECRET=$(head -c 26 /dev/urandom | base64)
envsubst <${SOURCE_DIR}/srv/cts/conf/spotter.py >/srv/cts/conf/spotter.py
# Set "production values" (increases performance) only if the DEBUG environment variable is not set
if [ ${DEBUG:-0} -eq 0 ]; then
# TODO: DEBUG = False needs static resources to be server by nginx, see https://docs.djangoproject.com/en/2.0/howto/static-files/#serving-static-files-during-development
sed -i 's/DEBUG = True/DEBUG = False/' /srv/cts/conf/spotter.py
fi
# Populate database
docker run --rm -h cts --link postgres -v /srv/cts/conf:/srv/cts/cts/settings cts manage.py migrate
# Create admin account
export CTS_ADMIN_EMAIL=admin@example.com
export CTS_ADMIN_PWD=$(head -c 12 /dev/urandom | base64)
export CTS_ADMIN_HASH=$(docker run --rm -h cts -v /srv/cts/conf:/srv/cts/cts/settings cts python -c "from django.contrib.auth.hashers import make_password; print make_password('${CTS_ADMIN_PWD}')")
export CTS_ADMIN_SECRET=$(head -c 12 /dev/urandom | sha256sum | cut -c1-13)
envsubst <${SOURCE_DIR}/adminpwd.sql | docker exec -i postgres psql cts
# Create CTS service
cp ${SOURCE_DIR}/etc/init.d/cts /etc/init.d/cts
rc-update add cts boot
service cts start
# Create nginx app definition
cp ${SOURCE_DIR}/etc/nginx/conf.d/cts.conf /etc/nginx/conf.d/cts.conf
service nginx reload
# Add portal application definition
portal-app-manager cts "https://{host}:8406/" "${CTS_ADMIN_EMAIL}" "${CTS_ADMIN_PWD}"