#!/bin/bash
#
# Make the running system the active High Availability node
#

status=$(/usr/sbin/ngcp-check-active -v)

if [ "$status" = "active" ]; then
  echo "note: node is already active"
  exit 0
fi

HACRM=$(ngcp-ha-crm)
case "${HACRM}" in
  pacemaker)
    if [[ -r /etc/default/ngcp-roles ]]; then
      # shellcheck disable=SC1091
      . /etc/default/ngcp-roles
    fi

    if [[ -z "${NGCP_HOSTNAME}" ]]; then
      echo "error: NGCP_HOSTNAME is not set" >&2
      exit 1
    fi

    LIFETIME=PT2M

    /usr/libexec/ngcp-crm-helper <<-CRM
        cib new ngcp-node-state withstatus force
        maintenance off
        resource cleanup g_vips
        resource move g_vips ${NGCP_HOSTNAME} ${LIFETIME}
        cib commit
        cib use live
        cib delete ngcp-node-state
CRM
    ;;
esac
