#!/usr/bin/env bash
#
# This script is designed to be run inside the container
#

# fail hard and fast even on pipelines
set -eo pipefail

# set debug based on envvar
[[ $DEBUG ]] && set -x

# configure etcd
export ETCD_PORT=${DEIS_ETCD_1_SERVICE_PORT_CLIENT:-4001}
export ETCD_HOST=${DEIS_ETCD_1_SERVICE_HOST:-$HOST}
export ETCD="$ETCD_HOST:$ETCD_PORT"
export ETCD_PATH=${ETCD_PATH:-/deis/controller}
export ETCD_TTL=${ETCD_TTL:-20}

# wait for etcd to be available
until etcdctl --no-sync -C "$ETCD" ls >/dev/null 2>&1; do
	echo "waiting for etcd at $ETCD..."
	sleep $((ETCD_TTL/2))  # sleep for half the TTL
done

# wait until etcd has discarded potentially stale values
sleep $((ETCD_TTL+1))

function etcd_set_default {
	set +e
	ERROR="$(etcdctl --no-sync -C "$ETCD" mk "$ETCD_PATH/$1" "$2" 2>&1)"
	if [[ $? -ne 0 ]] && echo "$ERROR" | grep -iqve "key already exists"; then
		echo "etcd_set_default: an etcd error occurred ($ERROR)"
		echo "aborting..."
		exit 1
	fi
	set -e
}

function etcd_safe_mkdir {
	set +e
	ERROR="$(etcdctl --no-sync -C "$ETCD" mkdir "$1" 2>&1)"

	if [[ $? -ne 0 ]] && echo "$ERROR" | grep -iqve "key already exists"; then
		echo "etcd_safe_mkdir: an etcd error occurred ($ERROR)"
		echo "aborting..."
		exit 1
	fi
	set -e
}

etcd_set_default protocol "${DEIS_PROTOCOL:-http}"
etcd_set_default secretKey "${DEIS_SECRET_KEY:-$(openssl rand -base64 64 | tr -d '\n')}"
etcd_set_default builderKey "${DEIS_BUILDER_KEY:-$(openssl rand -base64 64 | tr -d '\n')}"
etcd_set_default registrationMode "enabled"
etcd_set_default webEnabled 0

# safely create required keyspaces
etcd_safe_mkdir /deis/domains
etcd_safe_mkdir /deis/platform
etcd_safe_mkdir /deis/scheduler
etcd_safe_mkdir /deis/services

# HACK: set up keys for platform
etcd_safe_mkdir /deis/platform
ETCD_PATH=/deis/platform etcd_set_default domain localhost

# wait for confd to run once and install initial templates
until confd -onetime -node "$ETCD" --confdir /app --log-level error; do
	echo "controller: waiting for confd to write initial templates..."
	sleep $((ETCD_TTL/2))  # sleep for half the TTL
done

cd /app

mkdir -p /data/logs
chmod 777 /data/logs

# allow deis user group permission to Docker socket
if addgroup -g "$(stat -c "%g" /var/run/docker.sock)" docker; then
	addgroup deis docker
else
	addgroup deis "$(stat -c "%G" /var/run/docker.sock)"
fi

# run an idempotent database migration
sudo -E -u deis ./manage.py syncdb --migrate --noinput

# spawn a gunicorn server in the background
sudo -E -u deis gunicorn -c deis/gconf.py deis.wsgi &

./manage.py load_db_state_to_etcd

# smart shutdown on SIGTERM (SIGINT is handled by gunicorn)
function on_exit() {
	GUNICORN_PID=$(cat /tmp/gunicorn.pid)
	kill -TERM "$GUNICORN_PID" 2>/dev/null
	wait "$GUNICORN_PID" 2>/dev/null
	exit 0
}
trap on_exit TERM

# spawn confd in the background to update services based on etcd changes
confd -node "$ETCD" --confdir /app --log-level error --interval 5 &

echo deis-controller running...

wait
