#!/bin/sh

set -e

ulimit -H -l unlimited 2>/dev/null || {
    # https://bugs.launchpad.net/bugs/1828228
    echo "test disabled for unprivileged namespaces"
    exit 77
}

cleanup () {
  service pcsd stop
  service pacemaker stop
  service corosync stop
  passwd --delete --lock hacluster
}

trap "cleanup" 0 2 3 15

nodename="$(uname -n)"

# Explicitly set the node name on corosync.conf, because pcsd requires
# it.
sed -i "s@^\tnode {@\tnode {\n\t\tname: ${nodename}@" /etc/corosync/corosync.conf

# (Try to) Restart everything just in case.
service pcsd try-restart
service corosync try-restart
service pacemaker try-restart

sleep 60

echo hacluster:hacluster | chpasswd
pcs cluster auth -u hacluster -p hacluster
pcs cluster setup debian "${nodename}" --start --force

sleep 60

pcs pcsd status | grep -20 "${nodename}: Online"
