#!/bin/bash
#------------------------------------------------------------------------------
set -u
declare ThisScript=${0##*/}
declare ThisScriptBase=${ThisScript%.sh}
declare CmdLine="$0 $*"
declare Version=1.0.2
declare -i Debug=0
declare -i NoOp=0
declare RunUser=
declare ThisUser=
declare ThisHost=
declare Log=
declare UserLog=
declare OldLogTimestamp=
declare OldLog=
declare SDPInstance=${SDP_INSTANCE:-Unset}
declare SDPEnvFile=/p4/common/bin/p4_vars
declare SDPInstanceFile=
declare -i ErrorCount=0
declare -i WarningCount=0
declare -i SilentMode=0
declare H1="\\n=============================================================================="
declare H2="\\n------------------------------------------------------------------------------"
declare CfgFile="${ThisScriptBase}.cfg"
declare DepotPath=
declare FileRevRange=
declare TmpFile=
#==============================================================================
# Local Functions
#------------------------------------------------------------------------------
# msg() Message to stdout.
# errmsg () Error message using msg, increment ErrorCount
# bail () Error message then exit
function msg () { echo -e "$*"; }
function dbg () { [[ "$Debug" -eq 0 ]] || msg "DEBUG: $*"; }
function errmsg () { msg "\\nError: ${1:-Unknown Error}\\n"; ErrorCount+=1; }
function warnmsg () { msg "\\nWarning: ${1:-Unknown Warning}\\n"; WarningCount+=1; }
function bail () { errmsg "${1:-Unknown Error}"; exit "${2:-1}"; }
#------------------------------------------------------------------------------
# Function: terminate()
#
# After the service is launched, the EXIT/SIGINT/SIGTERM signals are trapped,
# and receipt of those signals results in terminate() function being called.
#------------------------------------------------------------------------------
function terminate
{
# Disable signal trapping.
trap - EXIT SIGINT SIGTERM
msg "$ThisScript: EXIT_CODE: $ErrorCount"
# Stop logging.
[[ -n "${Log}" ]] || msg "\\nLog is: $Log"
# With the trap removed, exit.
exit "$ErrorCount"
}
#------------------------------------------------------------------------------
# Function: usage (required function)
#
# Input:
# $1 - style, either -h (for short form) or -man (for man-page like format).
# The default is -h.
#
# $2 - error message (optional). Specify this if usage() is called due to
# user error, in which case the given message displayed first, followed by the
# standard usage message (short or long depending on $1). If displaying an
# error, usually $1 should be -h so that the longer usage message doesn't
# obscure the error message.
#
# Sample Usage:
# usage
# usage -h
# usage -man
# usage -h "Incorrect command line usage."
#------------------------------------------------------------------------------
function usage {
declare style=${1:--h}
declare errorMessage=${2:-Unset}
if [[ "$errorMessage" != Unset ]]; then
msg "\\n\\nUsage Error:\\n\\n$errorMessage\\n\\n"
fi
echo "USAGE for $ThisScript v$Version:
$ThisScript [-i <SDPInstance>] [-L <log>] [-si] [-n] [-d|-D]
or
$ThisScript [-h|-man|-V]
"
if [[ $style == -man ]]; then
echo -e "
DESCRIPTION:
This script ensures that the head revisions of files are pulled for
depot paths. The depot paths are specified in a configuration
file:
${ThisScriptBase}.cfg.
The file specifies jsut a list of depot paths; each line should end
in: //...
OPTIONS:
-i <N>
Specify the SDP instance name. This is required unless the SDP_INSTANCE
environment variable is set, e.g. if the SDP environment has already been
loaded.
-L <log>
Specify the path to a log file, or the special value 'off' to disable
logging. By default, all output (stdout and stderr) goes to
${LOGS:-/tmp}/${ThisScriptBase}.log
NOTE: This script is self-logging. That is, output displayed on the screen
is simultaneously captured in the log file. Do not run this script with
redirection operators like '> log' or '2>&1', and do not use 'tee.'
-si Operate silently. All output (stdout and stderr) is redirected to the log
only; no output appears on the terminal. This cannot be used with '-L off'.
This is useful when running from cron, as it prevents automatic email from
being sent by cron directly, as cron does when a script called from cron
generates any output.
-n No-Op. Prints data-affecting commands instead of running them.
-d Enable debugging verbosity.
-D Enable extreme extreme debugging verbosity, using bash 'set -x' mode.
HELP OPTIONS:
-h Display short help message
-man Display man-style help message
"
fi
exit 1
}
#==============================================================================
# Command Line Processing
declare -i shiftArgs=0
set +u
while [[ $# -gt 0 ]]; do
case $1 in
(-h|-V) usage -h;;
(-man) usage -man;;
(-i) SDPInstance="${2:-}"; shiftArgs=1;;
(-L) UserLog="${2:-}"; shiftArgs=1;;
(-n) NoOp=1;;
(-d) Debug=1;; # Enable debug mode.
(-D) Debug=1; set -x;; # Extreme debug; use bash 'set -x' mode.
(*) usage -h "Unknown arg ($1)." ;;
esac
# Shift (modify $#) the appropriate number of times.
shift; while [[ $shiftArgs -gt 0 ]]; do
[[ $# -eq 0 ]] && usage -h "Incorrect number of arguments."
shiftArgs=$shiftArgs-1
shift
done
done
set -u
#==============================================================================
# Command Line Verification
[[ -n "$UserLog" ]] && Log="$UserLog"
[[ "$Log" == "off" && "$SilentMode" -eq 1 ]] && \
bail "The '-si' option cannot be used with '-L off'."
[[ "$SDPInstance" == Unset ]] && \
bail "The SDP environment is not defined. Add the '-i <SDPInstance>' option to specify."
SDPInstanceFile="/p4/common/config/p4_${SDPInstance}.vars"
[[ -e "$SDPInstanceFile" ]] || \
bail "Missing SDP instance file [$SDPInstanceFile].\\nIs the instance value [$SDPInstance] correct?"
#==============================================================================
# Main Program
if [[ -e "$SDPEnvFile" ]]; then
dbg "Loading SDP Environment with: source $SDPEnvFile $SDPInstance"
# shellcheck disable=SC1090
source "$SDPEnvFile" "$SDPInstance"
else
bail "No SDP Environment file [$SDPEnvFile] found. Aborting."
fi
ThisUser=$(whoami)
ThisHost=${HOSTNAME%%.*}
RunUser=${OSUSER:-UnsetOSUSER}
# Safety Preflight Checks
# Prestart Checks -- bail early if we're executed as the wrong user.
if [[ "$ThisUser" == "$RunUser" ]]; then
dbg "Verified: Running as $RunUser."
else
bail "Run this as $RunUser, not $ThisUser."
fi
# This script should only be run on edge servers. We make an assumption that the
# hostname of an edge server will contain the string 'edge'.
if [[ "$ThisHost" == *"edge"* ]]; then
dbg "Verified: Running on a host that appears to be an edge server."
else
bail "This host [$ThisHost] does not have 'edge' in the name. Run this only on an edge server."
fi
if [[ "$Log" != "off" ]]; then
# If Log was not set with '-L <log>' on the command line, use the default.
[[ -n "$Log" ]] || Log="${LOGS:-/tmp}/${ThisScriptBase}.log"
# If the log specified already exists, move it aside, injecting the modification
# timestmp to the existing log file name.
if [[ -e "$Log" ]]; then
# shellcheck disable=SC2012
OldLogTimestamp=$(ls -l --time-style=+'%Y%m%d-%H%M' "$Log" |cut -d ' ' -f 6)
OldLog="${LOGS:-/tmp}/${ThisScriptBase}.${OldLogTimestamp}.log"
mv "$Log" "$OldLog" ||\
bail "Failed to move old log [$Log] aside to: [$OldLog]."
fi
touch "$Log" || bail "Couldn't touch log file [$Log]."
# Redirect stdout and stderr to a log file.
if [[ "$SilentMode" -eq 0 ]]; then
exec > >(tee "$Log")
exec 2>&1
else
exec >"$Log"
exec 2>&1
fi
msg "${H1}\\nLog is: $Log\\n"
fi
trap terminate EXIT SIGINT SIGTERM
msg "${H2}\\nService Started with $ThisScript v$Version as $ThisUser@$ThisHost on $(date) as:\\n$CmdLine"
if [[ -r "$CfgFile" ]]; then
msg "Loading config file: $CfgFile"
else
bail "Missing config file: $CfgFile"
fi
TmpFile=$(mktemp "${P4TMP:-/tmp}/tmp.epl.XXXXXXXXX.txt")
while read -r DepotPath; do
if [[ "$DepotPath" != *"/..." ]]; then
warnmsg "Invalid DepotPath [$DepotPath]; must end in: /..."
continue
fi
msg "Processing DepoPath: $DepotPath"
msg "Getting latest file rev range with: p4 -ztag -F %depotFile%#%rev%,#%rev% files -e $DepotPath"
p4 -ztag -F %depotFile%#%rev%,#%rev% files -e "$DepotPath" > "$TmpFile"
if [[ -s "$TmpFile" ]]; then
msg "Processing $(wc -l "$TmpFile"|cut -d ' ' -f 1) files in $DepotPath"
else
warnmsg "No files found for $DepotPath\\nIgnoring this path."
continue
fi
while read -r FileRevRange; do
dbg "p4 -s verify -qt $FileRevRange"
if [[ "$NoOp" -eq 0 ]]; then
if ! p4 -s verify -qt "$FileRevRange"; then
errmsg "Error trying to schedule transfer for: $FileRevRange"
fi
else
msg "NO_OP: Would run: p4 -s verify -qt $FileRevRange"
fi
done < "$TmpFile"
done < "$CfgFile"
if [[ "$ErrorCount" -eq 0 && "$WarningCount" -eq 0 ]]; then
msg "Processing completed OK."
elif [[ "ErrorCount" -eq 0 ]]; then
warnmsg "Processing completed with no errors but with $WarningCount warnings."
else
msg "Processing completed, but with $ErrorCount errors and $WarningCount warnings."
fi
rm -f "$TmpFile"
msg "That took $((SECONDS/3600)) hours $((SECONDS%3600/60)) minutes $((SECONDS%60)) seconds.\\n"
exit "$ErrorCount"