#!/bin/bash
# Copyleft ITEG IT-Engineers GmbH 2024

echo "QUESTION: Are you sure you want HA (i.e. high availability feature) stopped resp. paused?"
echo "If you only want to set the desired service stati to 'ignored' call pu-ha-pause2ignore instead."
echo "Press Ctrl-C to abort or Enter to continue setting stati to ignored and stop all 3 HA services ..."
read ENTER

echo ""

if [ -f "/etc/pve/ha/pve-utils_ha-service-stati.txt" ] ; then
  echo "INFO: Already have /etc/pve/ha/pve-utils_ha-service-stati.txt, not re-creating it."
  echo "This is expected after calling pu-ha-stop on one PVE cluster node." 
  echo ""
else
  echo "INFO: Detecting states of HA guests ..."
  ha-manager status |egrep "^service" |awk '{print $2 " " $4}' |tr -d ')' |tee /etc/pve/ha/pve-utils_ha-service-stati.txt
  echo ""
  echo "INFO: Setting wanted HA service stati to 'ignored' ..."
  cat /etc/pve/ha/pve-utils_ha-service-stati.txt |while read HA_SERVICE_STATE
  do
    HA_SERVICE=$(echo "${HA_SERVICE_STATE}" |cut -f1 -d ' ')
    HA_STATE=$(echo "${HA_SERVICE_STATE}" |cut -f2 -d ' ')
    echo "INFO: Service ${HA_SERVICE_STATE} ..."
    ha-manager set ${HA_SERVICE} --state ignored || exit 11 
  done
  echo "INFO: Done setting wanted HA service stati to 'ignored', waiting for it to be applied ..."
  echo ""
  sleep 5
fi

echo "INFO: Waiting for all HA service stati to becom 'ignored' ..."
UNIGNORED_SERVICES=$(ha-manager status |egrep "^service" |grep -v ", ignored")
while [ -n "${UNIGNORED_SERVICES}" ]
do
  echo "WARNING: The following services are not in state 'ignored' yet:"
  echo "${UNIGNORED_SERVICES}"
  echo ""
  echo "Trying again in a bit ..."
  echo ""
  sleep 5
done
echo ""

echo "INFO: All HA services seem to be in state 'ignored', stopping services ..."
echo "INFO: Stopping pve-ha-lrm.service ..."
systemctl stop pve-ha-lrm.service
sleep 1
echo ""
echo "INFO: Stopping pve-ha-crm.service ..."
systemctl stop pve-ha-crm.service
sleep 1
echo ""
echo "INFO: Stopping watchdog-mux ..."
systemctl stop watchdog-mux.service
sleep 1
echo ""

echo "INFO: Checking wether any of these services is still active and aborting if so ..." 
echo -n "pve-ha-lrm.service: "
systemctl is-active pve-ha-lrm.service && exit 51
echo -n "pve-ha-crm.service: "
systemctl is-active pve-ha-crm.service && exit 52
echo -n "watchdog-mux:       "
systemctl is-active watchdog-mux && exit 53
echo ""

echo "INFO: Disabling pve-ha-lrm.service, pve-ha-crm.service and watchdog-mux.service (to prevent chaos in case of reboot for whatever reason) ..."
systemctl disable pve-ha-lrm.service pve-ha-crm.service watchdog-mux.service
echo ""

echo "GOOD: All HA service are in state ignored and succesfully stopped all HA services."
echo "When all cluster nodes agree network interruptions should not trigger hard resets of PVE nodes."
echo ""

exit 0
