#!/bin/bash
#
# Copyright 2020 Cohesity Inc.
#
# Author: Mahesh Kumar
#
# Apply 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 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


PROGNAME=$(basename $0)
PROG_VERSION=1

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} > ${PATCHES_DIR}/mystatus
  exit ${exit_status}
}

function usage() {
  echo "Usage 1: ${PROGNAME} <service-patch-file>"
  echo "    <service-patch-file> is a single service patch like"
  echo "         apollo-6.5.1e_cpd-patch-2021Sep02-ddb022e8.tgz"
  echo
  echo "Usage 2: ${PROGNAME} <dependent-service-patch-files> ..."
  echo "    <dependent-service-patch-files> two or more service patch files"
  echo "    that are dependent, i.e. essential parts of the same patch/fix."
  echo "    All the dependent patch files should be listed as parameters."
  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
}

#----------------------------------------
# Start the work here.
#----------------------------------------
if [ "$#" -lt "1" ]; then
  usage
fi

readonly patch_files=$@
sudo chmod -f 664 ${patch_files}
sudo chown -f cohesity ${patch_files}

echo $$ > ${PATCHES_DIR}/mypid

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

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

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

trap "rm -f ${PATCHES_DIR}/${err_file}" EXIT

if [ -f ${PATCHES_DIR}/patch_scripts.tgz ]; then
  copy_patch_scripts
  extract_patch_scripts
fi

cd ${DATA_DIR}

service=""
for patch in ${patch_files}
do
  if [ ! -f ${patch} ]; then
    echo "${patch} is not found"
    exit_with_status 1
  fi
  patch_file=$(basename -- ${patch})
  sname=$(echo ${patch_file} | sed 's/\(.*\)-.*-patch-.*.tgz/\1/')
  if [ ${sname} = ${patch} ]; then
    echo "${patch} is not a patch file"
    exit_with_status 1
  elif [ -z ${service} ]; then
    service=${sname}
  fi
done

for patch in ${patch_files}
do
  sudo tar xf ${patch} 2> /dev/null
  if [ "$?" != "0" ]; then
    echo "Could not extract ${patch}"
    exit_with_status 1
  fi
done

cd ${PATCHES_DIR}
./apply_${service}_patch.sh
exit_with_status $?
