reset_hms.sh #1

  • //
  • guest/
  • perforce_software/
  • hms/
  • dev/
  • p4/
  • common/
  • site/
  • hms/
  • setup/
  • reset_hms.sh
  • View
  • Commits
  • Open Download .zip Download (4 KB)
#!/bin/bash
#------------------------------------------------------------------------------
set -u

function msg () { echo -e "$*"; }
function warnmsg () { msg "\\nWARNING: ${1:-Unknown Warning}\\n"; WarningCount+=1; }
function errmsg () { msg "\\nERROR: ${1:-Unknown Error}\\n"; ErrorCount+=1; }
function bail () { errmsg "${1:-Unknown Error}"; exit "${2:-1}"; }

#------------------------------------------------------------------------------
# Functions run($cmd, $desc)
#
# This function is similar to functions defined in SDP core libraries, but we
# need to duplicate them here since this script runs before the SDP is
# available on the machine (and we require no external dependencies for this
# script).
function run {
   cmd="${1:-echo Testing run}"
   desc="${2:-}"
   [[ -n "$desc" ]] && msg "$desc"
   msg "Running: $cmd"
   if [[ "$NoOp" -eq 0 ]]; then
      $cmd
   else
      msg "NO_OP: Would execute: $cmd"
   fi
   return $?
}

#------------------------------------------------------------------------------
# Declarations and Environment

declare ThisScript="${0##*/}"
declare Version=1.0.5
declare -i ErrorCount=0
declare -i WarningCount=0
declare -i ResetNeeded=0
declare -i NoOp=1
declare CustomResetScript="${HMS_CUSTOM_RESET_SCRIPT:-}"
declare ThisUser=

#------------------------------------------------------------------------------
# Command Line Parsing

[[ "$*" == "-y" ]] && NoOp=0

#------------------------------------------------------------------------------
# Main Program

ThisUser=$(whoami)

msg "Started $ThisScript v$Version as $ThisUser@${HOSTNAME%%.*} at $(date)."

msg "\\nStopping running hms services (if running)."
for svc in /p4/hms/bin/p4d_hms_init /p4/hms/bin/p4broker_hms_init; do
   if [[ -x "$svc" ]]; then
      ResetNeeded=1
      run "$svc stop" "Stopping service ${svc##*/}." ||\
         warnmsg "Failed to stop service ${svc##*/}."
   fi
done

msg "\\nDisabling and removing systemd services (if any)."
for systemdService in p4d_hms p4broker_hms p4p_hms; do
   systemdServiceFile="/etc/systemd/system/${systemdService}.service"
   if [[ -e "$systemdServiceFile" ]]; then
      ResetNeeded=1
      run "sudo systemctl disable $systemdService" ||\
         errmsg "Failed to disable systemd service $systemdService."
      run "sudo rm -f $systemdServiceFile" ||\
         errmsg "Failed to remove systemd service file: $systemdServiceFile"
   fi
done

msg "\\nRemoving /hx*/p4/hms and /p4/hms folders if they exist."
for d in /hxdepots/p4/hms/ /hxmetadata/p4/hms/ /hxlogs/p4/hms/ /p4/hms/; do
   if [[ -d "$d" ]]; then
      ResetNeeded=1
      run "rm -rf $d" "Removing dir: $d" ||\
         errmsg "Failed to remove dir: $d"
   else
      msg "No dir to remove: $d" 
   fi
done

msg "\\nRemoving hms-specific files (if any)."
for f in /p4/common/config/p4_hms.* /p4/.p4config.SDP /p4/common/bin/p4*_hms_* /p4/sdp/Server/Unix/setup/mkdirs.hms.*; do
   if [[ -e "$f" ]]; then
      ResetNeeded=1
      run "rm -f $f" "Removing file $f" ||\
         errmsg "Failed to remove file: $f"
   else
      msg "No file to remove: $f" 
   fi
done

if [[ -x "$CustomResetScript" ]]; then
   run "$CustomResetScript" \
      "Executing custom reset script specified with \$HMS_CUSTOM_RESET_SCRIPT." ||\
      errmsg "Custom reset script returned non-zero exit code."
else
   msg "No custom reset script found."
fi

if [[ "$ErrorCount" -eq 0 ]]; then
   if [[ "$WarningCount" -eq 0 ]]; then
      if [[ "$ResetNeeded" -eq 1 ]]; then
         if [[ "$NoOp" -eq 0 ]]; then
            msg "\\nSUCCESS:  HMS has been reset."
         else
            msg "\\nSUCCESS:  HMS would have been reset (was not without -y)."
         fi
      else
         msg "\\nSUCCESS:  HMS reset was not needed."
      fi
   else
      msg "\\nSUCCESS:  HMS has been reset, but with $WarningCount warnings."
   fi
else
   msg "\\nHMS reset has completed, but with $ErrorCount errors and $WarningCount warnings."
fi

[[ "$NoOp" -eq 1 && "$ResetNeeded" -eq 1 ]] && warnmsg "This was preview mode; reset did not actually occur.  Specify '-y' to perform an actual reset.\\n"

exit "$ErrorCount"
# Change User Description Committed
#2 30123 C. Thomas Tyler Modernized service handling in reset_hms.sh.
#1 29182 C. Thomas Tyler Moved HMS files from /p4/common/bin -> /p4/common/site/bin.
Moved HMS files from /p4/common/lib -> /p4/common/site/lib.
Removed dependency on SDP libs so that HMS can be deployed
with a wider variety of SDP versions.
//guest/perforce_software/hms/dev/p4/common/hms/setup/reset_hms.sh
#6 27308 C. Thomas Tyler Fixed issue with assuming USER is always defined.
#5 26639 C. Thomas Tyler More thorough cleanup of prior HMS installs.
#4 26638 C. Thomas Tyler Cosmetic updates.
#3 25712 C. Thomas Tyler Made hms_ts_setup.sh shellcheck compliant.

Removed custom hacks for Battle School demo from hms_ts_setup.sh.
Added generic mechanism for calling custom scripts by defining
environment variables referencing them (undoc'd) to both
hms_ts_setup.sh and reset_hms.sh.
#2 25711 C. Thomas Tyler Enhanced output.
#1 25654 C. Thomas Tyler Added simple script to reset what hms_ts_setup.sh does, as a testing
utility.