Spotter-VM/opendatakit/setup.sh

46 lines
1.8 KiB
Bash
Executable File

#!/bin/sh
set -e
cd $(realpath $(dirname "${0}"))/setup
# Check prerequisites
[ ! -e /run/openrc/started/postgres ] && service postgres start && STOP_POSTGRES=1
# Create databases
export OPENDATAKIT_PWD=$(head -c 18 /dev/urandom | base64)
envsubst <createdb.sql | lxc-attach -u 5432 -g 5432 postgres -- psql
# Configure OpenDataKit
mkdir -p /srv/opendatakit/conf
export OPENDATAKIT_ADMIN_USER=admin
export OPENDATAKIT_ADMIN_REALM=spotter
envsubst <srv/opendatakit/conf/jdbc.properties >/srv/opendatakit/conf/jdbc.properties
envsubst <srv/opendatakit/conf/security.properties >/srv/opendatakit/conf/security.properties
cp srv/opendatakit/conf/server.xml /srv/opendatakit/conf/server.xml
chown -R 8015:8015 /srv/opendatakit/conf
# Install service
cp etc/init.d/opendatakit /etc/init.d/opendatakit
rc-update -u
# Populate database
service opendatakit start
until grep -q 'org.apache.catalina.startup.Catalina.start' /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)
export OPENDATAKIT_ADMIN_SALT=$(head -c 4 /dev/urandom | hexdump -e '"%x"') # Must be 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 postgres -- psql opendatakit
vmmgr update-login opendatakit "${OPENDATAKIT_ADMIN_USER}" "${OPENDATAKIT_ADMIN_PWD}"
# Install config update script
cp srv/opendatakit/update-conf.sh /srv/opendatakit/update-conf.sh
# Stop services required for build
[ ! -z ${STOP_POSTGRES} ] && service postgres stop