Spotter-VM/lxc-apps/cts/install.sh

49 lines
1.7 KiB
Bash
Executable File

#!/bin/sh
set -ev
cd $(realpath $(dirname "${0}"))/install
# Create Postgres instance
mkdir -p /srv/cts/postgres_data
chown -R 105432:105432 /srv/cts/postgres_data
chmod 700 /srv/cts/postgres_data
lxc-execute -n cts-postgres -- initdb -D /var/lib/postgresql
# Configure Postgres
cp postgres_data/postgresql.conf /srv/cts/postgres_data/postgresql.conf
cp postgres_data/pg_hba.conf /srv/cts/postgres_data/pg_hba.conf
# Create database
export CTS_PWD=$(head -c 18 /dev/urandom | base64 | tr -d '+/=')
service cts-postgres start
envsubst <createdb.sql | lxc-attach -u 5432 -g 5432 cts-postgres -- psql
# Copy existing config files into persistent storage
mkdir -p /srv/cts/cts_conf
chown 108080:108080 /srv/cts/cts_conf
lxc-execute cts -- tar -cC /srv/cts/cts/settings . | tar -xC /srv/cts/cts_conf
# Configure CTS
export CTS_SECRET=$(head -c 26 /dev/urandom | base64 | tr -d '+/=')
envsubst <cts_conf/spotter.py >/srv/cts/cts_conf/spotter.py
touch /srv/cts/cts_conf/__init__.py
# Populate database
lxc-execute cts -- manage.py migrate
# Create admin account
export CTS_ADMIN_EMAIL=admin@example.com
export CTS_ADMIN_PWD=$(head -c 12 /dev/urandom | base64 | tr -d '+/=')
export CTS_ADMIN_HASH=$(lxc-execute 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 <adminpwd.sql | lxc-attach -u 5432 -g 5432 cts-postgres -- psql cts
# Install config update script
cp update-conf.sh /srv/cts/update-conf.sh
# Stop services required for setup
service cts-postgres stop
# Register application
vmmgr register-app cts cts "${CTS_ADMIN_EMAIL}" "${CTS_ADMIN_PWD}"