#!/bin/bash
set -eu

RUN_DIR=/var/vcap/sys/run/vault
LOG_DIR=/var/vcap/sys/log/vault
JOB_DIR=/var/vcap/jobs/vault
DAT_DIR=/var/vcap/store/vault
PIDFILE=$RUN_DIR/consul.pid
# Seconds to wait after SIGINT, then after SIGKILL.  monit's stop timeout
# for consul (jobs/vault/monit) must cover both.
STOP_TIMEOUT=30
KILL_TIMEOUT=10
mkdir -p $LOG_DIR
exec >>$LOG_DIR/consul.log 2>&1

case $1 in
  start)
    mkdir -p $RUN_DIR $DAT_DIR
    chown vcap:vcap $DAT_DIR $RUN_DIR

    if [[ -f $PIDFILE ]]; then
      PID=$(head -1 $PIDFILE)
      test -n "$PID" && test -e /proc/$PID && exit 1
      rm -f $PIDFILE
    fi

    ulimit -v unlimited
    setcap cap_net_bind_service=+ep $(readlink -nf /var/vcap/packages/consul/bin/consul)

    # set up a place to store consul serf/raft data
    mkdir -p ${DAT_DIR}/consul
    chown vcap:vcap ${DAT_DIR}/consul

    # run consul
    GOMAXPROCS=2 \
    setpriv --reuid=vcap --regid=vcap --groups=vcap -- \
      /var/vcap/packages/consul/bin/consul agent \
         -config-file ${JOB_DIR}/config/consul.json \
         -data-dir ${DAT_DIR}/consul \
         -pid-file $PIDFILE
    ;;

  stop)
    # SIGINT with skip_leave_on_interrupt shuts the server down without
    # leaving raft (see config/consul.json).  Wait for it to close its raft
    # and serf stores before monit reports it stopped, because BOSH may
    # unmount the persistent disk or delete the VM right after.
    if [[ ! -f $PIDFILE ]]; then
      echo "No pidfile at $PIDFILE; consul is not running"
      exit 0
    fi
    PID=$(head -1 $PIDFILE)
    if [[ -z "$PID" ]] || [[ ! -e /proc/$PID ]]; then
      echo "consul (pid ${PID:-unknown}) is not running"
      rm -f $PIDFILE
      exit 0
    fi

    echo "Stopping consul (pid $PID) with SIGINT, waiting up to ${STOP_TIMEOUT}s"
    kill -INT $PID || true
    waited=0
    while [ -e /proc/$PID ] && [ $waited -lt $(( STOP_TIMEOUT * 10 )) ]; do
      sleep 0.1
      waited=$(( waited + 1 ))
    done

    if [ -e /proc/$PID ]; then
      echo "consul (pid $PID) still running after ${STOP_TIMEOUT}s; sending SIGKILL"
      kill -KILL $PID || true
      waited=0
      while [ -e /proc/$PID ] && [ $waited -lt $(( KILL_TIMEOUT * 10 )) ]; do
        sleep 0.1
        waited=$(( waited + 1 ))
      done
      if [ -e /proc/$PID ]; then
        echo "consul (pid $PID) survived SIGKILL for ${KILL_TIMEOUT}s"
        exit 1
      fi
    fi
    echo "consul (pid $PID) stopped"
    rm -f $PIDFILE
    ;;

  reload)
    echo "Reloading consul configuration..."
    kill -SIGHUP $(cat $PIDFILE)
    ;;

  *)
    echo "Usage: consul {start|stop}"
    ;;
esac
exit 0
