#! /bin/sh
##
# This scripts runs at boot time for a VC appliance and determines
# whether the appliance is HA aware based on the 'guestinfo.ha.aware"
# config property.

# If HA aware is set, it has the following behavior -
# - If VCHA configuration is injected, it validates if VMON startup profile
#   is a HA profile.
# - If VCHA configuration is not injected, it sets VMON startup profile to
#   None. The VMON startup profile is changed to HA profile at configuration.
#   NOTE: Currently, we are running chkconfig to disable services' startup at
#   boot time. This is a temporary measure and once all services have finished
#   integration with VMON we can remove this as the services will not have
#   individual boot scripts.

# If HA aware is not set, it validates if VMON startup profile is a Default
# profile.
##

UPDATED_NW=0
VCHA_CFG_PATH="/etc/vmware-vcha/vcha.cfg"
VCHA_HOME="/usr/lib/vmware-vcha"
HA_AWARE_PATH="/etc/vmware-vcha/vcha.aware"
VCHA_LOG_DIR="/var/log/vmware/vcha"
HACHECK_LOG="$VCHA_LOG_DIR/vcha-hacheck.log"
DISABLE_LIST="$VCHA_HOME/data/service.list"
VCHA_MODIFY="$VCHA_HOME/scripts/modifyServices.py"
VCHA_IFUPDATE="$VCHA_HOME/scripts/ifupdate.py"
. /etc/profile.d/VMware-visl-integration.sh

set_none_profile()
{
   # First time Passive is configured Set Profile to None.
   # HACore will be set during config and subsequent reboots.
   echo NONE > $VMWARE_DATA_DIR/vmware-vmon/defaultStartProfile
}

set_hacore_profile()
{
   echo HACore > $VMWARE_DATA_DIR/vmware-vmon/defaultStartProfile
}

disable_services()
{
   echo "Disabling services..." >> $HACHECK_LOG
   $VMWARE_PYTHON_BIN $VCHA_MODIFY disabled >> $HACHECK_LOG
}

validate_services()
{
   echo "Validating if services are disabled..." >> $HACHECK_LOG
   while IFS='' read -r service_name || [[ -n "$service_name" ]]; do
      status=`systemctl is-enabled $service_name`
      if [ "$status" == "enabled" ]; then
         echo "Service $service_name not disabled"
         return 1
      fi
   done < $DISABLE_LIST
   return 0
}

fix_failover_interface()
{
   ETH0_MNL_PATH="/etc/systemd/network/10-eth0.network.manual"
   RECFG_ETH0_PATH="/etc/vmware-vcha/alt0.nw.m"
   echo "Fixing the failover interface..." >> $HACHECK_LOG
   echo "Bringing down and changing startmode to manual..." >> $HACHECK_LOG
   $VMWARE_PYTHON_BIN $VCHA_IFUPDATE >> $HACHECK_LOG
   if [ -f $RECFG_ETH0_PATH ]; then
      echo "Reconfiguring failover network..." >> $HACHECK_LOG
      mv -f $RECFG_ETH0_PATH $ETH0_MNL_PATH >> $HACHECK_LOG
   fi
}

fix_cluster_interface()
{
   ETH1_PATH="/etc/systemd/network/10-eth1.network"
   RECFG_ETH1_PATH="/etc/vmware-vcha/alt1.nw"
   if [ -f $RECFG_ETH1_PATH ]; then
      echo "Reconfiguring cluster network..." >> $HACHECK_LOG
      mv -f $RECFG_ETH1_PATH $ETH1_PATH >> $HACHECK_LOG
      UPDATED_NW=1
   fi
}

fix_hosts_file()
{
   HOSTS_PATH="/etc/hosts"
   RECFG_HOSTS_PATH="/etc/vmware-vcha/alt.h"
   if [ -f $RECFG_HOSTS_PATH ]; then
      echo "Reconfiguring hosts file..." >> $HACHECK_LOG
      mv -f $RECFG_HOSTS_PATH $HOSTS_PATH >> $HACHECK_LOG
      UPDATED_NW=1
   fi
}

restart_network()
{
   if [ $UPDATED_NW = 1 ]; then
      echo "Restarting network..." >> $HACHECK_LOG
      systemctl restart systemd-networkd >> $HACHECK_LOG
   fi
}


shopt -s nocasematch
if [ -f $HA_AWARE_PATH ]  && [ ! -f $VCHA_CFG_PATH ]; then
   # TODO: Add code for changing VMON startup profile and remove
   # disable_services and validate_services.
   set_none_profile
   fix_failover_interface
   fix_cluster_interface
   fix_hosts_file
   restart_network
   disable_services
   validate_services
   if [ $? -eq 1 ]; then
      echo "Some services are not disabled."
      exit 1
   fi
   echo "VCVA HA-aware check done. System updated." >> $HACHECK_LOG
elif  [ -f $VCHA_CFG_PATH ]; then
   set_hacore_profile
   # TODO: Add validation for VMON startup profile.
   # Do this to make sure failover interface is set
   # correctly for a newly deployed Passive node.
   fix_failover_interface
   fix_cluster_interface
   fix_hosts_file
   restart_network
   disable_services
   validate_services
   echo "VCHA configuration injected, nothing to update." >> $HACHECK_LOG
fi
