#!/bin/bash
#
# Copyright 2022 Cohesity Inc.
#
# Author: Mahesh Kumar
#
# Extract and apply all the service patches in a patch on all the nodes.
# This is executed by the cohesity user from the data folder.
#

PROGNAME=$(basename $0)
PROG_VERSION=1

# Standard places
readonly DATA_DIR="/home/cohesity/data"
readonly BIN_DIR="/home/cohesity/bin"
readonly SOFTWARE_DIR="/home/cohesity/software"
readonly PATCH_BIN="${SOFTWARE_DIR}/crux/patch/bin"
readonly HOTFIX_DIR="${DATA_DIR}/hotfix"
readonly PATCH_TMPDIR="${DATA_DIR}/iris"
readonly PATCHES_DIR="${DATA_DIR}/patches"
readonly STORAGE_PROXY="${BIN_DIR}/storage_proxy_exec"
readonly NEW_DIR="${PATCHES_DIR} ${HOTFIX_DIR}"
readonly ALL_DIR="${NEW_DIR} ${PATCH_TMPDIR} ${STORAGE_PROXY}"
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

# We do cluster-wide patch application by default.
# Local node patch application is supported only for cluster patch without
# security fixes(BaseOS patches).
LOCAL_PATCH_APPLICATION=""

scp_cmd="scp -o PreferredAuthentications=publickey,password \
    -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \
    -o ConnectTimeout=5 -o LogLevel=ERROR -p -q -r "
ssh_cmd="ssh -o PreferredAuthentications=publickey,password \
    -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \
    -o ConnectTimeout=5 -o LogLevel=ERROR -q -l cohesity "

if [ "${dodin_mode}" = "1" ]; then
  ssh_cmd=${PATCH_BIN}/pssh.sh
  scp_cmd=${PATCH_BIN}/pscp.sh
fi

function exit_with_status() {
  local exit_status=$1
  echo ${exit_status} > ${HOTFIX_DIR}/mystatus
  exit ${exit_status}
}

function usage() {
  echo
  echo "Usage: ${PROGNAME}"
  echo
  exit_with_status 1
}

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

function copy_scripts_to_remote_node() {
  local node=$1
  local node_ip=${node}
  if [[ ${node} =~ ":" ]]; then
    node_ip="[${node}]"
  fi
  if [ "${dodin_mode}" = "1" ]; then
    ${scp_cmd} ${node_ip} ${PATCHES_DIR}/patch_scripts.tgz ${DATA_DIR}
    code="$?"
  else
    ${scp_cmd} ${PATCHES_DIR}/patch_scripts.tgz ${node_ip}:${DATA_DIR}
    code="$?"
  fi
  if [ "${code}" != "0" ]; then
    echo "${node}: Copy patch scripts failed" >> ${err_file}
    return
  fi
  return
}

# Copies one service patch to all the nodes in parallel.
function copy_patch_scripts() {
  for node in ${HOSTIPS}
  do
    if [ "${node}" = "${MY_IP}" ]; then
      # It is already done.
      continue
    fi
    copy_scripts_to_remote_node ${node} &
  done
  wait

  if [ -s ${err_file} ]; then
    cat ${err_file}
    exit_with_status 1
  fi
  return
}

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

# Extract the patch scripts and set the folder permissions.
function extract_scripts_on_remote_node() {
  local remote_node=$1
  local code="0"
  local ssh_output=""
  local ntries=0
  while true
  do
    ssh_output=$(${ssh_cmd} ${remote_node} "(mkdir --parents ${NEW_DIR}; \
      sudo chown cohesity ${NEW_DIR}; \
      sudo chmod -f 775 ${ALL_DIR}; \
      tar -C ${SOFTWARE_DIR} -xf \
        ${DATA_DIR}/patch_scripts.tgz > /dev/null 2>&1 ; \
      mv ${DATA_DIR}/patch_scripts.tgz ${PATCHES_DIR}) ")
    code="$?"
    if [ "${code}" = "0" ]; then
      break
    fi
    if [ "${code}" != "255" ]; then
      echo "${remote_node}: ${ssh_output}" >> ${err_file}
      return
    fi
    let ntries++
    if [ ${ntries} -gt 10 ]; then
      echo "${remote_node}: ssh failed with error code 255" >> ${err_file}
      return
    fi
    sleep 6
  done
}

# Extract the patch scripts and set the folder permissions.
# Patch scripts were extracted already on the main node.
function extract_scripts_on_main_node() {
  local main_node=$1
  local code=0
  local err_output=""
  err_output=$(mkdir -p ${NEW_DIR}; \
    sudo chown cohesity ${NEW_DIR}; \
    sudo chmod -f 775 ${ALL_DIR})
  code="$?"
  if [ "${code}" != "0" ]; then
    echo "${err_output}" >> ${err_file}
    return
  fi
  return
}

function extract_patch_scripts() {
  local node=""
  for node in ${HOSTIPS}
  do
    if [ "${node}" = "${MY_IP}" ]; then
      extract_scripts_on_main_node ${node} &
    else
      extract_scripts_on_remote_node ${node} &
    fi
  done
  wait

  if [ -s ${err_file} ]; then
    cat ${err_file}
    exit_with_status 1
  fi
  return
}

#---------------------------------------------------------------------------
if [ $# -lt 1 ]; then
  usage
elif [ $# -eq 2 -a "${2}0" = "local0" ]; then
  LOCAL_PATCH_APPLICATION="$2"
  # If patch is being applied locally, the current script will be run by nexus
  # (cohesity user). Also patch service would not have been started on this
  # node by nexus at this point in case of patching after upgrade.
  ssh_cmd="sh -c"
elif [ $# -ge 2 ]; then
  usage
fi

readonly HOTFIX_TARFILE=$1
readonly HOTFIX_VERSION=$(echo $(basename ${HOTFIX_TARFILE}|sed 's/.tgz//'))

echo $$ > ${HOTFIX_DIR}/mypid

user_name=$(whoami)
if [ "${user_name}" != "cohesity" ]; then
  echo "Only cohesity user can apply a patch."
  exit_with_status 1
fi

HOSTIPS=$(hostips 2> /dev/null)
if [ "x${HOSTIPS}" = "x" ]; then
  echo "Node IPs list is empty."
  exit_with_status 5
fi

readonly MY_IP=$(echo ${HOSTIPS} | sed 's,.* \(.*\),\1,')
if [ "x${MY_IP}" = "x" ]; then
  echo "This node IP is unknown."
  exit_with_status 6
fi

if [ "${LOCAL_PATCH_APPLICATION}0" = "local0" ]; then
  HOSTIPS=${MY_IP}
fi

# Create the file to capture the errors.
err_file=apc-$$.txt
touch ${DATA_DIR}/${err_file}
if [ "$?" != "0" ]; then
  echo "Could not create status file before applying patch."
  exit_with_status 4
fi

echo
echo "Patch File: ${HOTFIX_TARFILE}"
echo
if [ "${LOCAL_PATCH_APPLICATION}0" = "local0" ]; then
  echo "It will take approximately 20 minutes to apply the patch on the" \
    "local node."
else
  echo "It will take approximately 20 minutes to apply on all the nodes."
fi
echo
echo "Please do not try to apply the patch from any other node."
echo
echo "Output will be kept in ${PATCHES_DIR}/${HOTFIX_VERSION}.out"
echo
if [ -f ${PATCHES_DIR}/patch_scripts.tgz ]; then
  copy_patch_scripts
  extract_patch_scripts
fi

if [ -s ${err_file} ]; then
  cat ${err_file}
  exit_with_status 7
fi

rm -f ${err_file}

cd ${HOTFIX_DIR}

./apply_hotfix.sh ${LOCAL_PATCH_APPLICATION}
exit_with_status $?
