#!/bin/bash
#
# Copyright 2020 Cohesity Inc.
#
# Author: Mahesh Kumar
#
# Revert a patch for a service and all its dependent patches on all the nodes.
# This is executed by the support user from the home folder.
#

# Standard places
readonly DATA_DIR="/home/cohesity/data"
readonly PATCHES_DIR="${DATA_DIR}/patches"
readonly SOFTWARE_DIR="/home/cohesity/software"
readonly PATCH_BIN="${SOFTWARE_DIR}/crux/patch/bin"
readonly DODIN_FLAG="${DATA_DIR}/.dodin_mode"
readonly FIPS_FILE="/proc/sys/crypto/fips_enabled"
dodin_mode="0"
if [ -f ${DODIN_FLAG} ]; then
  dodin_mode=$(cat ${DODIN_FLAG})
elif [ -f ${FIPS_FILE} ]; then
  dodin_mode=$(cat ${FIPS_FILE})
fi

ssh_cmd="sudo su -l cohesity -c"
if [ "${dodin_mode}" = "1" ]; then
  ssh_cmd="${PATCH_BIN}/pssh.sh localhost"
fi

readonly PROGNAME=$(basename $0)
readonly PROG_VERSION=1
lock_holder="NO"
trap unlock_script EXIT
trap "" SIGINT

function usage() {
  echo "Usage: ${PROGNAME} <service-name>"
  echo "  <service-name> is the service whose patch should be reverted"
  exit 1
}

#---------------------------------------------------------------------------

# Locking/unlocking ensuring running a single instance of the script:

readonly LOCKDIR=${DATA_DIR}/.revert_patch.lock

function unlock_script() {
  if [ "${lock_holder}" = "NO" ]; then
    # Losers do not have the right to delete the lock directory.
    return
  fi
  if [ -d ${LOCKDIR} ]; then
    rmdir ${LOCKDIR}
  fi
}

function lock_script() {
  # 1st instance: hold lock until it is done before allowing another instance.
  # 2nd instance: emit error message and exit.
  mkdir ${LOCKDIR} 2> /dev/null
  if [ "$?" != "0" ]; then
    pids=$(pidof ${PROGNAME})
    if [ "$?" = "0" ]; then
      lock_holder="NO"
      echo "Another instance of ${PROGNAME} is already running [${pids}]."
      exit 1
    fi
  fi
  lock_holder="YES"
  return
}

# Obtain the right to run this script. Exit if another instance is running.
lock_script

#---------------------------------------------------------------------------
# Start the work here.
#---------------------------------------------------------------------------

if [ "$#" -ne "1" ]; then
  usage
fi

readonly service=$1

if [ ! -d "${PATCHES_DIR}" ]; then
  echo "No patch was applied."
  exit 0
fi

if [ ! -f "${PATCHES_DIR}/revert_${service}_patch.sh" ]; then
  echo "There is nothing to revert for ${service}."
  exit 0
fi

OUTFILE=${PATCHES_DIR}/revert-${service}-patch.out
SCRIPT=revert_${service}_patch.sh
${ssh_cmd} "(cd ${PATCHES_DIR}; \
  rm -f ${PATCHES_DIR}/rev-pid;  \
  nohup ./revert_${service}_patch.sh > ${OUTFILE} 2>&1 & )"
if [ "$?" != "0" ]; then
  echo "Reverting ${service} patch failed"
  exit 1
fi

wait_cnt=0
while true
do
  sleep 2
  if [ -f ${PATCHES_DIR}/rev-pid ]; then
    break
  fi
  let wait_cnt+=1
  if [ ${wait_cnt} -gt 10 ]; then
    if [ -f ${OUTFILE} ]; then
      cat ${OUTFILE}
    fi
    exit 9
  fi
done

cpid=$(cat ${PATCHES_DIR}/rev-pid)
psstr=$(ps -h -p ${cpid})
if [ "$?" = "1" ]; then
  # process exited.
  cat ${OUTFILE}
  exit 10
fi

# Check if the pid is not recycled.
echo  ${psstr} | fgrep "${SCRIPT}" > /dev/null
if [ "$?" = "1" ]; then
  # process exited.
  cat ${OUTFILE}
  exit 11
fi

tail --pid=${cpid} -f ${OUTFILE}

exit 0
