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

61 lines
2.6 KiB
Bash
Executable File

#!/bin/sh
set -ev
cd $(realpath $(dirname "${0}"))/install
# Create Postgres instance
mkdir -p /srv/opendatakit/postgres_data
chown -R 105432:105432 /srv/opendatakit/postgres_data
chmod 700 /srv/opendatakit/postgres_data
lxc-execute -n opendatakit-postgres -- initdb -D /var/lib/postgresql
# Configure Postgres
cp postgres_data/postgresql.conf /srv/opendatakit/postgres_data/postgresql.conf
cp postgres_data/pg_hba.conf /srv/opendatakit/postgres_data/pg_hba.conf
# Create databases
export OPENDATAKIT_PWD=$(head -c 18 /dev/urandom | base64 | tr -d '+/=')
export OPENDATAKITBUILD_PWD=$(head -c 18 /dev/urandom | base64 | tr -d '+/=')
service opendatakit-postgres start
envsubst <createdb.sql | lxc-attach -u 5432 -g 5432 opendatakit-postgres -- psql
# Configure OpenDataKit
mkdir -p /srv/opendatakit/odk_conf
export OPENDATAKIT_ADMIN_USER=admin
export OPENDATAKIT_ADMIN_REALM=spotter
envsubst <odk_conf/jdbc.properties >/srv/opendatakit/odk_conf/jdbc.properties
envsubst <odk_conf/security.properties >/srv/opendatakit/odk_conf/security.properties
cp odk_conf/server.xml /srv/opendatakit/odk_conf/server.xml
chown -R 108080:108080 /srv/opendatakit/odk_conf
# Configure OpenDataKit Build
export OPENDATAKITBUILD_COOKIE_SECRET=$(head -c 8 /dev/urandom | hexdump -e '"%x"')
mkdir -p /srv/opendatakit/odkbuild_conf
envsubst <odkbuild_conf/config.yml >/srv/opendatakit/odkbuild_conf/config.yml
lxc-execute opendatakit-build -- sh -c 'cd /srv/opendatakit-build; rake db:migrate'
chown -R 100000:100000 /srv/opendatakit/odkbuild_conf
# Populate database
service opendatakit start
until grep -q 'org.apache.catalina.startup.Catalina.start Server startup' /var/log/lxc/opendatakit.log; do
sleep 1
done
service opendatakit stop
# Update admin account
export OPENDATAKIT_ADMIN_PWD=$(head -c 12 /dev/urandom | base64 | tr -d '+/=')
export OPENDATAKIT_ADMIN_SALT=$(head -c 4 /dev/urandom | hexdump -e '"%x"') # Must be exactly 8 characters
export OPENDATAKIT_ADMIN_BASIC_HASH=$(echo -n "${OPENDATAKIT_ADMIN_PWD}{${OPENDATAKIT_ADMIN_SALT}}" | sha1sum | tr -d " -")
export OPENDATAKIT_ADMIN_DIGEST_HASH=$(echo -n "${OPENDATAKIT_ADMIN_USER}:${OPENDATAKIT_ADMIN_REALM}:${OPENDATAKIT_ADMIN_PWD}" | md5sum | tr -d " -")
envsubst <adminpwd.sql | lxc-attach -u 5432 -g 5432 opendatakit-postgres -- psql opendatakit
# Install config update script
cp update-conf.sh /srv/opendatakit/update-conf.sh
# Stop services required for setup
service opendatakit-postgres stop
# Register application
vmmgr register-app opendatakit odk "${OPENDATAKIT_ADMIN_USER}" "${OPENDATAKIT_ADMIN_PWD}"
vmmgr register-app opendatakit-build odkbuild