reset_sdp.sh #61

  • //
  • guest/
  • perforce_software/
  • helix-installer/
  • main/
  • src/
  • reset_sdp.sh
  • View
  • Commits
  • Open Download .zip Download (46 KB)
#!/bin/bash
#==============================================================================
# Copyright and license info is available in the LICENSE file included with
# this package, and also available online:
# https://swarm.workshop.perforce.com/view/guest/perforce_software/helix-installer/main/LICENSE
#------------------------------------------------------------------------------

#==============================================================================
# Declarations
set -u
declare Version=3.2.1

# The tar file specified here is used if the '-t' flag is used to
# specify installing from a tar file, which is good for getting a known,
# fixed version of the SDP to work with.  The default install method
# uses Helix DVCS features to get the very latest code from a branch
# ('main' by default, but '-b dev' could be specified as well) by doing
# a 'p4 clone' command.
declare SDPTar="sdp.Unix.2019.1.25374.tgz"
declare SDPURL="https://swarm.workshop.perforce.com/projects/perforce-software-sdp/download/downloads/$SDPTar"
declare SDPInstallMethod=DVCS
declare SDPInstallBranch=Unset
declare WorkshopPort="public.perforce.com:1666"
declare WorkshopUser=ftp
declare WorkshopRemote=
declare HelixInstallerBaseURL="https://swarm.workshop.perforce.com/download/guest/perforce_software/helix-installer"
declare HelixInstallerBranch=main
declare HelixInstallerURL="$HelixInstallerBaseURL/$HelixInstallerBranch"
declare HxDepots=/hxdepots
declare ResetHome="$HxDepots/reset"
declare HxMetadata=/hxmetadata
declare HxLogs=/hxlogs
declare SDPHome="$HxDepots/sdp"
declare TmpFile=/tmp/tmp.reset_sdp.$$.$RANDOM
declare TmpDir=/tmp/tmp.dir.reset_sdp.$$.$RANDOM
declare ShelvedChange=Unset
declare LocalShelvedChange=Unset
declare CmdLine="${0##*/} $*"
declare -i WarningCount=0
declare -i ErrorCount=0
declare InitMechanism=Unset
declare PackageManager=Unset
declare -A PackageList
PackageList['yum']="curl gcc gcc-c++ make openssl openssl-devel rsync wget zlib zlib-devel"
PackageList['apt-get']="build-essential libssl-dev make zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev"
PackageList['zypper']="curl gcc gcc-c++ make openssl openssl-devel wget zlib zlib-devel"
declare -A Config ConfigDoc
ConfigDoc['DNS_name_of_master_server']="\\n# Specify the DNS alias to refer to he master server, e.g. by end\\n# users. This might be 'perforce' but probably not an actual host name\\n# like 'perforce01', which would be known only to admins."
Config['DNS_name_of_master_server']="helix"
ConfigDoc['P4AdminList']="\\n# Specify an email address to receive updates from admin scripts. This may be\\n# a distribution list or comma-separated list of addresses (with no spaces)."
Config['P4AdminList']="P4AdminList@p4demo.com"
ConfigDoc['SMTPServer']="\\n# Specify email server for the p4review script. Ignore if Helix Swarm is used."
Config['SMTPServer']="smtp.p4demo.com"
ConfigDoc['Password']="\\n# Set the admin password."
Config['Password']="F@stSCM!"
ConfigDoc['P4_PORT']="\\n# Specify the TCP port for p4d to listen on. Typically this is 1999 if \\n# p4broker is used, or 1666 if only p4d is used."
Config['P4_PORT']="1999"
ConfigDoc['P4BROKER_PORT']="\\n# Specify the TCP port for p4broker to listen on. Must be different\\n# from the P4_PORT."
Config['P4BROKER_PORT']="1666"
ConfigDoc['SiteTag']="\\n# Specify a geographic site tag for the master server location,\\n# e.g. 'bos' for Boston, MA, USA."
Config['SiteTag']="bos"
ConfigDoc['SimulateEmail']="\\n# Specify '1' to avoid sending email from admin scripts, or 0 to send\\n# email from admin scripts."
Config['SimulateEmail']="1"

# If using this Helix Installer to bootsrap a production installation,
# please do NOT be tempted to keep this password.  Replace it with your
# own.  And after bootsrapping, be sure to remove the Helix Installer,
# as this software has no business being on a production machine.

declare ExesDir=$HxDepots/exes
declare DownloadsDir=$HxDepots/downloads
declare -i BlastDownloadsAndExes=0
declare -i ExtremeCleanup=0
declare -i FastMode=0
declare -i LoadSampleDepot=1
declare -i PullFromWebAsNeeded=1
declare -i UseSSL=1
declare -i GenDefaultConfig=0
declare -i UseConfigFile=0
declare ConfigFile=Unset
declare PreserveDirList=Unset
declare P4ExeRel=r18.2
declare P4DExeRel=$P4ExeRel
declare P4PExeRel=$P4ExeRel
declare P4BrokerExeRel=$P4ExeRel
declare P4Rel=r18.2
declare RunUser=perforce
declare RunUserHomeDir=
declare RunGroup=
declare SudoersFile="/etc/sudoers.d/$RunUser"
declare SudoersEntry="$RunUser ALL=(ALL) NOPASSWD: ALL"
declare P4YumRepo="/etc/yum.repos.d/perforce.repo"
declare P4AptGetRepo="/etc/apt/sources.list.d/perforce.list":w
declare PerforcePackagePubkeyURL="https://package.perforce.com/perforce.pubkey"
declare SampleDepotTar=
declare ApiArch=
declare ThisArch=
declare ThisHost=
declare ThisOS=
declare ThisOSName=
declare ThisOSDistro=
declare ThisOSMajorVersion=
declare FirewallType=
declare FirewallDir=
declare ThisUser=
declare RunArch="x86_64"
declare CBIN="/p4/common/bin"
declare CCFG="/p4/common/config"
declare SDPSetupDir="$SDPHome/Server/Unix/setup"
declare SDPConfigDir="$SDPHome/Server/Unix/p4/common/config"
declare ThisScript="${0##*/}"
declare SDPInstances="1"

#------------------------------------------------------------------------------
# Function: usage (required function)
#
# Input:
# $1 - style, either -h (for short form) or -man (for man-page like format).
#------------------------------------------------------------------------------
function usage
{
   declare style=${1:--h}

   msg "USAGE for $ThisScript v$Version:

$ThisScript [-B|-local] [-fast] [-no_ssl] [-no_sd] [-c <cfg>] {[-b <branch>[,@cl] | -t <tarfile>]} [-p <dir1>[,<dir2>,...]>] [-i <helix_installer_branch>] [-D] [-X]

or

$ThisScript -C > ${ThisScript/sh/cfg}

or

$ThisScript [-h|-man]
"
   if [[ $style == -man ]]; then
      msg "
SAFETY NOTICE:
	This script SHOULD NEVER EXIST on a Production Perforce server.

DESCRIPTION:
	This script simplifies the process of testing an SDP installation,
	repetitively blasting all process by the 'perforce' user and resetting
	the SDP from the ground up, blasting typical SDP folders each time.

	It installs the Perforce Helix Core server (P4D) with a P4Broker,
	and installs the Perforce Sample Depot data set used for training
	and PoC installations.

	It is helpful when bootstrapping a demo server with a sample data
	set, complete with broker, and optionally Perl/P4Perl and
	Python/P4Python.

	This script handles all aspects of installation. It does the
	following:
	* Creates the perforce OS user, if needed.
	* Creates the home directory for the perforce user, if needed.
	* Adds OS packages as needed for P4Perl/P4Python local builds.

	Following installation, it also does the following to be more
	convenient for demos, and also give a more production-like feel:
	* Grants the perforce user sudo access.
	* Creates default ~perforce/.bash_profile and .bashrc files.
	* Connects to the Perforce Package Repository (APT and YUM only).

PLATFORM SUPPORT:
	This works on Red Hat Enterprise Linux, CentOS, and Mac OSX
	10.10+ thru Mojave platforms.  It works on RHEL/CentOS
	6.4-7.6, SuSE Linux 12, and likely on Ubuntu 18 and other Linux
	distros with little or no modification.

	This script currently supports the bin.linux26x86_64 (Linux) and
	bin.maxosx1010x86_64 (Mac OSX/Darwin) architectures.

	This script recognizes SysV, Systemd, and Launchd init mechanisms,
	though does not currently support Launchd on OSX.

	For Mac OSX, note that this requires bash 4.x, and the default
	bash on Mac OSX remains 3.x as of OSX Mojave.  For operating on
	Mac, the /bin/bash shebang line needs to be adjusted to reference
	a bash 4 version, e.g. /usr/local/bin/bash if installed with
	Homebrew.

REQUIREMENTS:
	Development utilities such as 'make', the 'gcc' compiler,
	and 'curl' must be installed and available in the PATH (unless
	running with '-fast'), or accessible from the OS package manager.

	The following OS packages are installed (unless '-fast' is
	used):

	* Yum: ${PackageList[yum]}

	* AptGet: ${PackageList[apt-get]}

	* Zypper: ${PackageList[zypper]}

OPTIONS:
 -c <cfg>
	Specify a config file.  By default, values for various settings
	such as the email to send script logs to are configure with
	demo values, e.g. ${Config['P4AdminList']}.  Optionally, you can
	specify a config file to define your own values.

	For details on what settings you can define in this way, run:
	$ThisScript -C > ${ThisScript/sh/cfg}

	Then modify the generated config file as desired.  The generated
	config file contains documentation on settings and values.  If
	no changes are made to the generated file, running with
	'-c ${ThisScript/sh/cfg}' is the equivalent of running without
	using '-c' at all.

 -C	See '-c <cfg>' above.

 -B	Specify '-B' to blast base SDP dirs, for a clean start.

	Otherwise without '-B', downloaded components from earlier
	runs will be used (which should be fine if they were run
	recently).

	The '-B' flag also replaces files in the $ResetHome
	directory, where this script lives, with those downloaded
	from The Workshop (the versions of which are affected
	by the '-i <helix_installer_branch>' flag, described
	below).

	The '-B' flag also blasts the /tmp/downloads and /tmp/p4perl
	directories, used by install_sdp_python.sh and
	install_sdp_perl.sh, if they exist.

	The '-B' and '-local' arguments are mutually exclusive.

 -local
	By default, various files and executables are downloaded from
	the Perforce Workshop or the Perforce FTP server as needed.
	With '-local', any missing files are treated as an error
	condition.

	The '-B' and '-local' arguments are mutually exclusive.

 -fast	Specify '-fast' to skip installation SDP Perl and
	SDP Python, to include P4Perl and P4Python.

	The '-fast' argument changes a check for GCC/G++
	from a fatal error to a warning message.

	The '-fast' option typically takes just a few minutes,
	as compared to 20+ minutes without due to the time
	needed to compile and test the Perl and Python bits.

	The '-fast' flag should not be used if you plan to
	deploy or develop triggers that use P4Python or P4Perl,
	such as the SDP CheckCaseTrigger.py.  Alternately, you
	can run with '-fast', and then afterward run the
	following as the OS user $RunUser:

	cd $SDPSetupDir
	./install_sdp_python.sh
	./install_sdp_perl.sh

 -no_ssl
	By default, the Perforce server is setup SSL-enabled.  Specify
	'-no_ssl' to avoid using SSL feature.

 -no_sd
	By default, the Perforce Sample Depot data set is loaded.
	Specify '-no_sd' to skip loading the Sample Depot.

 -p <dir1>[,<dir2>,...]>]
	Specify a comma-delimited list of directories under /p4/common
	to preserve that would otherwise be removed.  Directories must
	be specified as paths relative to /p4/common, and cannot contain
	spaces.

	For example, the value '-p config,bin/triggers' would preserve the
	/p4/common/config and /p4/common/bin/triggers directories.

	Directories specified are moved aside to a temporary working area
	before the SDP folders are removed.  After installation, they are
	moved back via an 'rsync' command with no '--delete' option.  This
	means any files that overlap with the stock install are replaced
	by ones that originally existed, but non-overlapping files are not
	removed.

	This is intended to be useful for developing test suites that
	install server under /p4/common/bin, e.g. Component Based Development
	scripts which install under /p4/common/bin/cbd would use '-p bin/cbd'.

 -b <branch>[,@cl]
	The default SDP install method is to clone the SDP from the main
	branch in The Workshop ($WorkshopPort).  Specify '-b' to use
	a different branch, e.g. 'dev'.

	If '-b' is specified with the optional @cl syntax, where @cl
	specifies a changelist with files shelved on the given branch,
	a form of unshelving is done, enabling a workflow for testing
	shelved changes with the Helix Installer.  So for example,
	specify '-b dev,@23123' to clone from the dev branch, and then
	followed by a fetch of shelved changelist @23123, which is
	expected to have files shelved in the dev branch.

 -t <tarfile>
	Specify a tarfile to use, e.g. '-t sdp.Unix.2017.3.23041.tgz', or
	use the special value '-t default' to use the default SDP tarfile,
	$SDPTar

	The specified tar file must exist here:
	https://swarm.workshop.perforce.com/projects/perforce-software-sdp/files/downloads

	If '-t <tarfile>' is not specified, default install method is
	used instead, which is to clone the SDP main branch from The
	Workshop ($WorkshopPort).

	Use '-t' to get a known version of the SDP for testing.  Avoid '-t'
	to test with the very latest SDP from a given branch (see '-b' above
	for more info), even if the latest has not yet been packaged into a
	distribution tar file.

DEBUGGING OPTIONS:
 -i	<helix_installer_branch>

	Specify the branch of the Helix Installer to use.  This affects the
	URL from which Helix Installer files in $ResetHome are pulled from
	The Workshop.  The default is main; an alternative is '-i dev'.

 -D     Set extreme debugging verbosity.

 -X	Extreme reset. This removes the user accout for $RunUser and
	blast all SDP-related directories at the start of script operations,
	including the $RunUser home directory.

	Using '-X' does not blast the Helix Installer downloads or exes
	directories, and thus is compatible with either the'-B' or '-local'
	options.

HELP OPTIONS:
 -h	Display short help message
 -man	Display man-style help message

EXAMPLES:
	=== FAST INSTALLATION (skipping Perl, Python) ===

	su -
    	mkdir -p /hxdepots/reset
	cd /hxdepots/reset
	curl -k -s -O $HelixInstallerURL/src/$ThisScript
	curl -k -s -O $HelixInstallerURL/src/r
	chmod +x ${ThisScript} r
	./r

	Note that the 'r' wrapper script calls the $ThisScript script with
	a pre-defined of flags optimized for fast opreration.  The 'r' wapper
	also handles log capture, writing to the file '${ThisScript/sh/log}'.

	=== COMPREHENSIVE INSTALLATION ===

	su -
    	mkdir -p /hxdepots/reset
	cd /hxdepots/reset
	curl -k -s -O $HelixInstallerURL/src/$ThisScript

	chmod +x $ThisScript
	./$ThisScript 2>&1 | tee ${ThisScript/sh/log}

	=== CONFIGURED INSTALLATION ===

	su -
    	mkdir -p /hxdepots/reset
	cd /hxdepots/reset
	curl -k -s -O $HelixInstallerURL/src/$ThisScript
	chmod +x $ThisScript

 	### Generate a default config file:
	./$ThisScript -C > ${ThisScript/sh/cfg}

 	### Edit ${ThisScript/sh/cfg}, changing the values as desired:
	vi ${ThisScript/sh/cfg}

	./$ThisScript -c ${ThisScript/sh/cfg} 2>&1 | tee reset_sdp.log

	=== SDP DEV BRANCH TESTING ===

	The Helix Installer can be used to test SDP changes shelved to the SDP
	dev branch in The Workshop.  The following example illustrates testing
	a change in shelved changelist 23123:

	su -
    	mkdir -p /hxdepots/reset
	cd /hxdepots/reset
	curl -k -s -O $HelixInstallerURL/src/reset_sdp.sh

	./reset_sdp.sh -b dev,@23123 2>&1 | tee reset_sdp.CL23123.log

	After the first test, an iterative test cycle may follow on the same
	shelved changelist. For each test iteration, the shelved changelist
	is first updated in the workspace from which the change was originally
	shelved, e.g. with a command like 'p4 shelve -f -c 23123'.

	Then a new test can be done by calling reset_sdp.sh with the same
	arguments. The script will re-install the SDP cleanly, and then
	re-apply the updated shelved changelist.
"
   fi

   exit 1
}

#------------------------------------------------------------------------------
# Functions msg(), dbg(), and bail().
# Sample Usage:
#    bail "Missing something important. Aborting."
#    bail "Aborting with exit code 3." 3
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 dbg () { msg "DEBUG: $*" >&2; }
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 dependencies for this
# script).
function run {
   cmd="${1:-echo Testing run}"
   desc="${2:-}"
   [[ -n "$desc" ]] && msg "$desc"
   msg "Running: $cmd"
   $cmd
   return $?
}

#==============================================================================
# Command Line Processing

declare -i shiftArgs=0
set +u

while [[ $# -gt 0 ]]; do
   case $1 in
      (-B) BlastDownloadsAndExes=1;;
      (-local) PullFromWebAsNeeded=0;;
      (-fast) FastMode=1;;
      (-no_ssl) UseSSL=0;;
      (-no_sd) LoadSampleDepot=0;;
      (-C) GenDefaultConfig=1;;
      (-c) ConfigFile="$2"; UseConfigFile=1; shiftArgs=1;;
      (-t)
         if [[ "$2" != "default" ]]; then
            SDPTar=$2
         fi
         SDPInstallMethod=Tarfile
         shiftArgs=1
      ;;
      (-b)
         if [[ "$2" == *",@"* ]]; then
            SDPInstallBranch=${2%%,@*};
            ShelvedChange=${2##*,@}
         else
            SDPInstallBranch=$2;
         fi
         shiftArgs=1
      ;;
      (-p) PreserveDirList=$2; shiftArgs=1;;
      (-h) usage -h;;
      (-man) usage -man;;
      (-D) set -x;; # Debug; use 'set -x' mode.
      (-X) ExtremeCleanup=1;;
      (-i) HelixInstallerBranch="$2"; shiftArgs=1;;
      (*) bail "Usage Error: Unknown arg ($1).";;
   esac

   # Shift (modify $#) the appropriate number of times.
   shift; while [[ $shiftArgs -gt 0 ]]; do
      [[ $# -eq 0 ]] && bail "Usage Error: Wrong number of args or flags to args."
      shiftArgs=$shiftArgs-1
      shift
   done
done
set -u

#------------------------------------------------------------------------------
# Command Line Validation

[[ "$UseConfigFile" -eq 1 && "$GenDefaultConfig" -eq 1 ]] && \
   bail "The '-c <cfg>' and '-C' options are mutually exclusive."

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

ThisUser="$(whoami)"
ThisOS="$(uname -s)"
ThisArch="$(uname -m)"
ThisHost="$(hostname -s)"

#------------------------------------------------------------------------------
# Special Mode:  Generate Default Config File
# In this mode, generate a sample config file on stdout, and exit.

if [[ "$GenDefaultConfig" -eq 1 ]]; then
   echo -e "
# Config file for $ThisScript v$Version.
# This file is in bash shell script syntax.
# Avoid spaces before and after the '=' sign."

   for c in "${!Config[@]}"; do
      echo -e "${ConfigDoc[$c]}"
      echo "$c=${Config[$c]}"
   done

   exit 0
fi

#------------------------------------------------------------------------------
# Regular processing mode.

msg "Started $ThisScript v$Version on host $ThisHost at $(date), called as:\\n\\t$CmdLine"

if [[ "$UseConfigFile" -eq 1 ]]; then
   [[ -r "$ConfigFile" ]] || \
      bail "Config file specified with '-c $ConfigFile' is not readable."
   for c in "${!Config[@]}"; do
      value=$(grep ^$c= "$ConfigFile")
      value="${value#*=}"
      Config[$c]="$value"
   done
fi

# Get just enough detailed OS info in order to fill in
# details in the Perforce pacakge repository files.
if [[ "$ThisOS" == "Linux" ]]; then
   if [[ -r "/etc/redhat-release" ]]; then
      if grep ' 6\.' /etc/redhat-release > /dev/null; then
         ThisOSMajorVersion="6"
      elif grep ' 7\.' /etc/redhat-release > /dev/null; then
         ThisOSMajorVersion="7"
      fi
      [[ -n "$ThisOSMajorVersion" ]] || \
         warnmsg "Could not determine OS Major Version from contents of /etc/redhat-release."
   elif [[ -r "/etc/lsb-release" ]]; then
      ThisOSName=$(grep ^DISTRIB_ID= /etc/lsb-release)
      ThisOSName=${ThisOSName#*=}
      ThisOSName=${ThisOSName,,}
      ThisOSDistro=$(grep ^DISTRIB_CODENAME= /etc/lsb-release)
      ThisOSDistro=${ThisOSDistro#*=}

      [[ -n "$ThisOSName" && -n "$ThisOSDistro" ]] || \
         warnmsg "Could not determine OS Name and Distro from contents of /etc/lsb-release."
   fi

   if [[ -r "/etc/firewalld/services" ]]; then
      FirewallType="Firewalld"
      FirewallDir="/etc/firewalld/services"
   elif [[ -r "/etc/sysconfig/iptables" ]]; then
      FirewallType="IPTables"
      FirewallDir="/etc/sysconfig"
   fi
fi

if [[ $ThisUser != root ]]; then
   bail "Run as root, not $ThisUser."
else
   msg "Verified: Running as root user."
fi

[[ "$BlastDownloadsAndExes" -eq 1 && "$PullFromWebAsNeeded" -eq 0 ]] && \
   bail "The '-B' and '-local' arguments are mutually exclusive."

[[ "$SDPInstallBranch" != "Unset" && "$SDPInstallMethod" == "Tarfile" ]] && \
   bail "The '-b <branch>' and '-t <tarfile>' arguments are mutually exclusive."

[[ "$SDPInstallBranch" == Unset ]] && SDPInstallBranch=main

HelixInstallerURL="$HelixInstallerBaseURL/$HelixInstallerBranch"

#------------------------------------------------------------------------------
# Self-update

cd "$ResetHome" || bail "Could not cd to $HxDepots/ResetHome. Aborting."

if [[ ! -x configure_sample_depot_for_sdp.sh ]]; then
   run "curl -k -s -O $HelixInstallerURL/src/configure_sample_depot_for_sdp.sh" \
      "Getting script configure_sample_depot_for_sdp.sh."
fi

for f in NoTicketExpiration.group.p4s admin.user.p4s configure_sample_depot_for_sdp.sh protect.p4s perforce_bash_profile perforce_bashrc r; do
   if [[ ! -f $f ]]; then
      [[ "$PullFromWebAsNeeded" -eq 0 ]] && bail "Missing file [$f] and '-local' specified. Aborting."
      run "curl -k -s -O $HelixInstallerURL/src/$f" "Getting file $f." ||\
         bail "Failed to download file [$PWD/$f]. Aborting."
   else
      if [[ "$BlastDownloadsAndExes" -eq 1 ]]; then
         run "curl -k -s -O $HelixInstallerURL/src/$f" \
            "Replacing file $f due to '-B'." ||\
            bail "Failed to download file [$PWD/$f]. Aborting."
      else
         msg "Using existing file $PWD/$f."
      fi
   fi

   if [[ ! -x $f ]]; then
      if [[ $f == *".sh" || $f == r ]]; then
         run "chmod +x $f" "chmod +x" || bail "Failed to do: chmod +x $f. Aborting."
      fi
   fi
done

run "chown -R $RunUser:$RunGroup $ResetHome" "Fixing ownership of $ResetHome" ||:

#------------------------------------------------------------------------------
# Extreme Cleanup with -X
if [[ "$ExtremeCleanup" -eq 1 ]]; then
   msg "\\nStarted Extreme Cleanup due to -X."
   if id -u "$RunUser" > /dev/null 2>&1; then
      RunUserHomeDir="$(eval echo ~$RunUser)"
      run "pkill -9 -u $RunUser" "Blasting processes by OS user $RunUser." ||\
         warnmsg "Failed to blast all processes by OS user $RunUser."
      sleep 1
      run "userdel $RunUser" "Removing OS user $RunUser." ||\
         warnmsg "Failed to remove OS user $RunUser."

      run "/bin/rm -rf $RunUserHomeDir" "Removing home dir $RunUserHomeDir" ||\
         warnmsg "Failed to remove $RunUserHomeDir."
   else
      msg "Extreme Cleanup: OS User $RunUser does not exist."
   fi

   run "/bin/rm -rf /p4 $HxDepots/p4 $HxMetadata/p4 $HxLogs/p4" \
      "Extreme Cleanup: Blasting several SDP dirs." ||\
      warnmsg "Failed to blast some SDP dirs."

   msg "Extreme Cleanup complete.\\n"
fi

#------------------------------------------------------------------------------
# Digital asset acquisition and availability checks.
[[ ! -d "$HxDepots" ]] && run "/bin/mkdir -p $HxDepots"

cd "$HxDepots" || bail "Could not cd to [$HxDepots]."

if command -v yum > /dev/null; then
   PackageManager="yum"
elif command -v apt-get > /dev/null; then
   PackageManager="apt-get"
elif command -v zypper > /dev/null; then
   PackageManager="zypper"
fi

if [[ -d "/etc/systemd/system" ]]; then
   InitMechanism="Systemd"
elif [[ -x "/sbin/launchd" ]]; then
   InitMechanism="Launchd"
elif [[ -d "/etc/init.d" ]]; then
   InitMechanism="SysV"
fi

if [[ "$FastMode" -eq 0 ]]; then
   msg "Ensuring needed packages are installed."

   if [[ "$ThisOS" != "Darwin" ]]; then
      [[ "$PackageManager" == "Unset" ]] && \
         bail "Could not find one of these package managers: ${!PackageList[*]}"

      run "$PackageManager install -y ${PackageList[$PackageManager]}" \
         "Installing these pacakges with $PackageManager: ${PackageList[$PackageManager]}" ||\
         warnmsg "Not all packages installed successfully.  Proceeding."

      msg "Skipping package dependency checks in -fast mode."
      if ! command -v gcc > /dev/null || ! command -v g++ > /dev/null; then
         msg "Warning: No gcc found in the path.  You may need to install it.  Please\\n check that the gcc and gcc-c++ packages are\\n installed, e.g. with:\\n\\t$PackageManager install -y gcc gcc-c++\\nIgnoring missing gcc/g++ due to '-fast'.\\n"
      else
         msg "Verified: gcc and g++ are available and in the PATH."
      fi
   else
      warnmsg "Skipping package handling on Mac OSX/$ThisOS"
   fi
fi

if [[ "$PullFromWebAsNeeded" -eq 1 ]]; then
   if ! command -v curl > /dev/null; then
      bail "No 'curl' found in the path.  You may need to install it or adjust the PATH for the root user to find it.\\n\\n"
   fi
fi

if ! command -v su > /dev/null; then
   bail "No 'su' found in the path.  You may need to install it or adjust the PATH for the root user to find it.\\n\\n"
fi

if [[ "$ThisArch" == "$RunArch" ]]; then
   msg "Verified:  Running on a supported architecture [$ThisArch]."
   ApiArch=UNDEFINED_API_ARCH
   case $ThisOS in
      (Darwin)
         ApiArch="macosx1010x86_64"
         RunGroup=staff
         SampleDepotTar=sampledepot.mac.tar.gz
      ;;
      (Linux)
         ApiArch="linux26x86_64"
         if [[ -r "/etc/SuSE-release" ]]; then
            RunGroup=users
         else
            # CentOS, RHEL, and Ubuntu default group is same as user name.
            RunGroup=perforce
         fi
         SampleDepotTar=sampledepot.tar.gz
      ;;
      (*) bail "Unsupported value returned by 'uname -m': $ThisOS. Aborting.";;
   esac
else
   bail "Running on architecture $ThisArch.  Run this only on hosts with '$RunArch' architecture. Aborting."
fi

# In this block, we just check that directories specified to be preserved
# with the '-p' flag actually exist, in which case we abort before further
# processing.
if [[ "$PreserveDirList" != Unset ]]; then
   for d in $(echo "$PreserveDirList"| tr ',' ' '); do
      preserveDir="$HxDepots/p4/common/$d"
      if [[ -d "$preserveDir" ]]; then
         parentDir=$(dirname "$TmpDir/$d")
         if [[ ! -d "$parentDir" ]]; then
            run "/bin/mkdir -p $parentDir" "Creating parent temp dir [$parentDir]." ||\
               bail "Failed to create parent temp dir [$parentDir]."
         fi
      fi
   done
fi

if [[ "$BlastDownloadsAndExes" -eq 1 ]]; then
   if [[ -d "$ExesDir" ]]; then
      run "/bin/rm -rf $ExesDir" \
         "Blasting exes dir [$ExesDir] due to '-B'." ||\
          warnmsg "Failed to blast exes dir."

      for d in /tmp/downloads /tmp/p4perl; do
         if [[ -d "$d" ]]; then
            run "/bin/rm -rf $d" \
               "Blasting $d dir due to '-B'." ||\
                warnmsg "Failed to blast dir $d."
         fi
      done
   fi
fi

if [[ ! -d "$ExesDir" ]]; then
   [[ "$PullFromWebAsNeeded" -eq 0 ]] && bail "ExesDir [$ExesDir] is missing and '-local' specified. Aborting."
   run "/bin/mkdir -p $ExesDir" ||\
      bail "Could not create dir [$ExesDir]."

   cd "$ExesDir" || bail "Could not cd to $ExesDir."
   msg "Working in [$PWD]."
   run "curl -s -O http://ftp.perforce.com/perforce/$P4ExeRel/bin.$ApiArch/p4" ||\
      bail "Could not get 'p4' executable."
   run "curl -s -O http://ftp.perforce.com/perforce/$P4DExeRel/bin.$ApiArch/p4d" ||\
      bail "Could not get 'p4d' executable."
   run "curl -s -O http://ftp.perforce.com/perforce/$P4PExeRel/bin.$ApiArch/p4p" ||\
      bail "Could not get 'p4p' executable."
   run "curl -s -O http://ftp.perforce.com/perforce/$P4BrokerExeRel/bin.$ApiArch/p4broker" ||\
      bail "Could not get 'p4broker' executable."

   run "chmod +x p4 p4d p4p p4broker" \
      "Doing chmod +x for downloaded executables."
else
   msg "Using existing exes dir [$ExesDir]."
fi

#------------------------------------------------------------------------------
# Services Shutdown and Cleanup.

if id -u "$RunUser" > /dev/null 2>&1; then
   msg "Verified: User $RunUser exists."
else
   if command -v useradd > /dev/null; then
      run "useradd $RunUser" "Creating user $RunUser." ||\
         bail "Failed to create user $RunUser."

      RunUserHomeDir="$(eval echo ~$RunUser)"
      if [[ -d "$RunUserHomeDir" ]]; then
         msg "Verified: Home directory for user $RunUser exists."
      else
         run "mkdir -p $RunUserHomeDir" \
            "Creating home dir for $RunUser" ||\
            bail "Failed to create home directory $RunUserHomeDir for OS user $RunUser."
         run "chmod -R $RunUser:$RunGroup $RunUserHomeDir" \
            "Ensuring $RunUser owns home dir $RunUserHomeDir." ||\
            warnmsg "Failed to change ownership of home directory $RunUserHomeDir for OS user $RunUser."
      fi

      run "cp $ResetHome/perforce_bash_profile $RunUserHomeDir/.bash_profile" \
         "Creating $RunUserHomeDir/.bash_profile." ||\
         warnmsg "Failed to copy to $RunUserHomeDir/.bash_profile."

      run "cp $ResetHome/perforce_bashrc $RunUserHomeDir/.bashrc" \
         "Creating $RunUserHomeDir/.bashrc." ||\
         warnmsg "Failed to copy to $RunUserHomeDir/.bashrc."

      run "chown $RunUser:$RunGroup $(eval echo ~$RunUser)/.bash_profile $(eval echo ~$RunUser)/.bashrc" "Adjusting perms of .bash_profile and .bashrc." ||\
         warnmsg "Adjusting ownership failed."
   else
      bail "User $RunUser does not exist, and the 'useradd' utility was not found."
   fi
fi

for i in $SDPInstances; do
   if [[ "$ThisOS" == "Linux" || "$ThisOS" == "Darwin" ]]; then
      msg "Stopping Perforce-related servcies for Instance $i."
      for svc in p4d p4broker p4p p4web p4dtg; do
         processCmd="${svc}_${i}"

         # This 'ps' command should work on Linux and Mac (Yosemite+ at least).
         # shellcheck disable=SC2009
         Pids=$(ps -u $RunUser -f | grep -v grep | grep "/$processCmd "| awk '{print $2}')

         if [[ -z "$Pids" && $svc == p4d ]]; then
            msg "$processCmd not found for p4d service; looking for _bin variant instead."
            # For the p4d service, the process command may look like 'p4d_1_bin' or just 'p4d_1', so
            # we check for both.
            processCmd="${svc}_${i}_bin"
            # shellcheck disable=SC2009
            Pids=$(ps -u $RunUser -f | grep -v grep | grep "/$processCmd "| awk '{print $2}')
         fi

         if [[ -n "$Pids" ]]; then
            run "kill -9 $Pids" \
               "Killing user $RunUser processes for command $processCmd."
            sleep 1
         else
            msg "Verified: No processes by user $RunUser for command [$processCmd] are running."
         fi
      done
   fi

   if [[ "$InitMechanism" == "SysV" ]]; then
      cd /etc/init.d || bail "Could not cd to /etc/init.d."

      msg "Removing Perforce-related SysV services in $PWD."

      for svc in p4*_init; do
         run "chkconfig --del $svc"
         run "rm -f $svc"
      done
   elif [[ "$InitMechanism" == "Systemd" ]]; then
      cd /etc/systemd/system || bail "Could not cd to /etc/systemd/system."

      msg "Disabling and removing Perforce-related Systemd services in $PWD."
      for svcFile in p4*.service; do
         run "systemctl disable ${svcFile%.service}"
         run "rm -f $svcFile"
      done
   fi
done

#------------------------------------------------------------------------------
if [[ -d $DownloadsDir && $BlastDownloadsAndExes -eq 1 ]]; then
   run "/bin/rm -r -f $DownloadsDir" \
      "Blasting downloads dir [$DownloadsDir] due to '-B'."
fi

if [[ ! -d $DownloadsDir ]]; then
   [[ $PullFromWebAsNeeded -eq 0 ]] && bail "DownloadsDir [$DownloadsDir] is missing and '-local' specified. Aborting."
   run "/bin/mkdir -p $DownloadsDir"

   cd $DownloadsDir || bail "Could not cd to [$DownloadsDir]."

   msg "Working in [$PWD]."
   run "curl -s -O http://ftp.perforce.com/perforce/$P4Rel/bin.$ApiArch/p4api.tgz" ||\
      bail "Could not get file 'p4api.tgz'"

   if [[ $SDPInstallMethod == Tarfile ]]; then
      run "curl -k -s -O $SDPURL" ||\
         bail "Could not get SDP tar file from [$SDPURL]."
   fi

   run "curl -s -O http://ftp.perforce.com/perforce/tools/$SampleDepotTar" ||\
      bail "Could not get file [$SampleDepotTar]. Aborting."
   if [[ ! -d PerforceSample ]]; then
      run "tar -xzpf $SampleDepotTar" "Unpacking $SampleDepotTar in $PWD."
   fi
   run "chown -R $RunUser:$RunGroup $DownloadsDir" \
      "Setting ownership on downloads dir." ||\
      bail "Failed to set ownership on downloads dir [$DownloadsDir]. Aborting."
else
   msg "Using existing downloads dir [$DownloadsDir]."

   cd "$DownloadsDir" || bail "Could not cd to downloads dir: $DownloadsDir"

   if [[ $SDPInstallMethod == Tarfile ]]; then
      if [[ -r "$SDPTar" ]]; then
         msg "Using existing SDP tarfile [$SDPTar]."
      else
         run "curl -k -s -O $SDPURL" ||\
            bail "Could not get SDP tar file from [$SDPURL]."
      fi
   fi

fi

#------------------------------------------------------------------------------
# Cleanup
cd "$HxDepots" || bail "Could not cd to [$HxDepots]. Aborting."

msg "Working in [$PWD]."

for d in $HxMetadata $HxLogs; do
   if [[ ! -d "$d" ]]; then
      run "/bin/mkdir -p $d" "Initialized empty dir [$d]." ||\
         bail "Failed to create dir [$d]."
   else
      msg "Verified: Dir [$d] exists."
   fi
done

if [[ $PreserveDirList != Unset ]]; then
   run "/bin/mkdir -p $TmpDir" "Creating temp dir [$TmpDir]." ||\
      bail "Failed to create temp dir [$TmpDir]."

   for d in $(echo "$PreserveDirList" | tr ',' ' '); do
      preserveDir="$HxDepots/p4/common/$d"
      if [[ -d "$preserveDir" ]]; then
         parentDir=$(dirname "$TmpDir/$d")
         if [[ ! -d "$parentDir" ]]; then
            run "/bin/mkdir -p $parentDir" "Creating parent temp dir [$parentDir]." ||\
               bail "Failed to create parent temp dir [$parentDir]."
         fi

         run "/bin/mv $preserveDir $TmpDir/$d" \
            "Moving preserved dir $preserveDir aside for safe keeping."
      else
         bail "Missing expected preserve dir [$preserveDir]. Check that paths specified with '-p' are relative to $HxDepots/p4/common."
      fi
   done
fi

for i in $SDPInstances; do
   run "/bin/rm -rf $HxDepots/p4/$i $HxMetadata/p4/$i $HxLogs/p4/$i" \
      "Blasting any existing SDP structures." ||\
      bail "Failed to blast existing SDP structures."
   if [[ -L "/p4/$i" ]]; then
      run "/bin/rm -f /p4/$i" "Blasting symlink /p4/$i." ||\
         bail "Failed to remove SDP symlink."
   elif [[ -d "/p4/$i" ]]; then
      run "/bin/rm -rf /p4/$i" "Blasting directory /p4/$i." ||\
         bail "Failed to remove SDP instance directory /p4/$i."
   fi
done

run "/bin/rm -rf $HxDepots/p4/common" \
   "Blasting SDP common folder." ||\
   bail "Failed to remove SDP common folder."

if [[ -L /p4/common ]]; then
   run "/bin/rm -f /p4/common" "Removing /p4/common symlink." ||\
      bail "Failed to remove /p4/common symlink."
elif [[ -d /p4/common ]]; then
   run "/bin/rm -rf /p4/common" "Removing /p4/common directory." ||\
      bail "Failed to remove /p4/common local directory."
else
   msg "Warning: /p4/common did not exist as a directory or symlink."
fi

run "/bin/rm -rf $SDPHome /p4/ssl" \
   "Removing old SDP Home and /p4/ssl." ||\
   bail "Failed to remove old $SDPHome and /p4/ssl."

#------------------------------------------------------------------------------
# SDP Setup
if [[ $SDPInstallMethod == Tarfile ]]; then
   run "tar -xzpf $DownloadsDir/$SDPTar" "Unpacking $DownloadsDir/$SDPTar in $PWD." ||\
      bail "Failed to untar SDP tarfile."
else
   export PATH="$ExesDir:$PATH"
   export P4ENVIRO=/dev/null/.p4enviro
   export P4CONFIG=.p4config.local
   run "/bin/mkdir -p $SDPHome" "Creating dir $SDPHome" ||\
      bail "Failed to create dir $SDPHome. Aborting."
   cd "$SDPHome" || bail "Failed to cd to [$SDPHome]."
   WorkshopRemote=perforce_software-sdp_${SDPInstallBranch}

   run "$ExesDir/p4 -s -u $WorkshopUser clone -p $WorkshopPort -r $WorkshopRemote" \
      "Cloning SDP $SDPInstallBranch branch from The Workshop." ||\
      bail "Failed to clone SDP from The Workshop."

   run "$ExesDir/p4 -s sync -f .p4ignore" \
      "Force-sync .p4ignore file."

   if [[ "$ShelvedChange" != Unset ]]; then
      run "$ExesDir/p4 -s fetch -s $ShelvedChange" \
         "Fetching shelved change @$ShelvedChange from The Workshop." ||\
      bail "Failed to fetch shelved change @$ShelvedChange from The Workshop."

      LocalShelvedChange=$($ExesDir/p4 -ztag -F %change% changes -s shelved -m 1)

      [[ -n "$LocalShelvedChange" ]] || \
         bail "Could not determine local shelved change fetched for shelved change @$ShelvedChange."

      run "$ExesDir/p4 -s unshelve -s $LocalShelvedChange" \
         "Unshelving local shelved change @$LocalShelvedChange." ||\
         bail "Failed to unshelve local shelved change @$LocalShelvedChange."
   fi

   unset P4ENVIRO
   unset P4CONFIG
fi

if [[ "${Config['SimulateEmail']}" == "1" ]]; then
   cd "$SDPConfigDir" ||\
   bail "Could not cd to [$SDPConfigDir]."

   msg "Generating custom p4_vars.template to simulate email in [$PWD]."

   run "/bin/mv -f p4_vars.template p4_vars.template.orig"
   run "chmod 444 p4_vars.template.orig"

   sed -e "s:SDPMAIL=mailx:SDPMAIL='/bin/echo Simulated mail':g" \
      -e "s:SDPMAIL=mail:SDPMAIL='/bin/echo Simulated mail':g" \
      p4_vars.template.orig > p4_vars.template

   msg "Changes in p4_vars.template:"
   diff p4_vars.template.orig p4_vars.template
else
   msg "Using standard p4_vars.template file."
fi

cd "$SDPSetupDir" ||\
   bail "Could not cd to [$SDPSetupDir]."

if [[ "${Config['SimulateEmail']}" == "1" ]]; then
   msg "Generating custom mkdirs.sh in $PWD."

   run "/bin/mv -f mkdirs.sh mkdirs.sh.orig"
   run "chmod 444 mkdirs.sh.orig"

   sed -e "s:export MAIL=mailx:export MAIL='/bin/echo Simulated mail':g" \
      -e "s:export MAIL=mail:export MAIL='/bin/echo Simulated mail':g" \
      mkdirs.sh.orig > mkdirs.sh
else
   msg "Using standard mkdirs.sh script."
fi

run "/bin/mv -f mkdirs.cfg mkdirs.cfg.orig" \
   "Generating custom mkdirs.cfg in $PWD."

sed -e "s:=DNS_name_of_master_server:=${Config['DNS_name_of_master_server']}:g" \
   -e "s:admin@example.com:P4AdminList@p4demo.com:g" \
   -e "s:mail.example.com:${Config['SMTPServer']}:g" \
   -e "s:^DB1=.*:DB1=$HxMetadata:g" \
   -e "s:^DB2=.*:DB2=$HxMetadata:g" \
   -e "s:^P4_PORT=.*:P4_PORT=SeeBelow:g" \
   -e "s:^P4BROKER_PORT=.*:P4BROKER_PORT=SeeBelow:g" \
   -e "s:# P4_PORT=1666:P4_PORT=${Config['P4_PORT']}:g" \
   -e "s:# P4BROKER_PORT=1667:P4BROKER_PORT=${Config['P4BROKER_PORT']}:g" \
   -e "s:=adminpass:=${Config['Password']}:g" \
   -e "s:=servicepass:=${Config['Password']}:g" \
   -e "s:OSGROUP=perforce:OSGROUP=$RunGroup:g" \
   -e "s:REPLICA_ID=replica:REPLICA_ID=p4d_ha_bos:g" \
   -e "s:SVCUSER=service:SVCUSER=svc_p4d_ha_bos:g" \
   mkdirs.cfg.orig > mkdirs.cfg

if [[ $UseSSL -eq 0 ]]; then
   msg "Not using SSL feature due to '-no_ssl'."
   sed "s/SSL_PREFIX=ssl:/SSL_PREFIX=/g" mkdirs.cfg > $TmpFile
   run "mv -f $TmpFile mkdirs.cfg"
fi

chmod +x mkdirs.sh

msg "SDP Localizations in mkdirs.sh:"
diff mkdirs.sh.orig mkdirs.sh

msg "\nSDP Localizations in mkdirs.cfg:"
diff mkdirs.cfg.orig mkdirs.cfg

run "cp -p $ExesDir/p4* $SDPHome/Server/Unix/p4/common/bin/." \
   "Copying perforce executables."

msg "Initializing SDP instances and configuring $InitMechanism services."

for i in $SDPInstances; do
   cd "$SDPSetupDir" || bail "Could not cd to [$SDPSetupDir]."
   log="$PWD/mkdirs.${i}.log"
   msg "Initializing SDP instance [$i], writing log [$log]."
   "$PWD/mkdirs.sh" "$i" > "$log" 2>&1
   cat "$log"

   if [[ "$InitMechanism" == "SysV" ]]; then
      msg "\\nConfiguring $InitMechanism services.\\n"
      cd /etc/init.d || bail "Could not cd to [/etc/init.d]."
      for svc in p4broker p4d; do
         initScript=${svc}_${i}_init
         if [[ -x /p4/${i}/bin/$initScript ]]; then
            run "ln -s /p4/${i}/bin/$initScript"
            run "chkconfig --add $initScript"
            run "chkconfig $initScript on"
         fi
      done
   elif [[ "$InitMechanism" == "Systemd" ]]; then
      msg "\\nConfiguring $InitMechanism services.\\n"
      cd /etc/systemd/system || bail "Could not cd to /etc/systemd/system."
      for exe in p4broker p4d; do
         svcName=${exe}_${i}
         svcFile=${svcName}.service
         sed "s:1:${i}:g" "$SDPSetupDir/systemd/$svcFile" > $svcFile ||\
            bail "Failed to generated $PWD/svcFile."
         run "systemctl enable $svcName" "Enabling $svcName to start on boot." ||\
            warnmsg "Failed to enable $svcName with $InitMechanism."
      done
   fi

   run "chown -R $RunUser:$RunGroup $HxDepots" \
      "Adjusting ownership of $HxDepots to $RunUser:$RunGroup." || bail "Failed to adjust ownership of $HxDepots."

   msg "\nGenerating broker config for instance $i.\n"
   su -l $RunUser -c "$CBIN/gen_default_broker_cfg.sh ${i} > $CCFG/p4_${i}.broker.cfg"

done

if [[ $UseSSL -eq 1 ]]; then
   msg "Generating /p4/ssl/config.txt SSL config file for autogen cert."
   sed -e "s/REPL_DNSNAME/helix/g" /p4/ssl/config.txt > $TmpFile ||\
      bail "Failed to substitute content in /p4/ssl/config.txt."
   run "mv -f $TmpFile /p4/ssl/config.txt"
   msg "Contents of /p4/ssl/config.txt:\n$(cat /p4/ssl/config.txt)\n"
fi

if [[ $PreserveDirList != Unset ]]; then
   for d in $(echo "$PreserveDirList" | tr ',' ' '); do
      preserveDir=$HxDepots/p4/common/$d
      tempCopyDir=$TmpDir/$d
      run "rsync -av --exclude=.p4root --exclude=.p4ignore --exclude=.p4config $tempCopyDir/ $preserveDir" \
         "Restoring $preserveDir" ||\
         bail "Failed to restore $preserveDir."
   done

   run "/bin/rm -rf $TmpDir" "Cleanup: Removing temp dir [$TmpDir]." ||\
      bail "Failed to remove temp dir [$TmpDir]."
fi

#------------------------------------------------------------------------------
# Install P4Perl and P4Python.
if [[ "$FastMode" -eq 0 ]]; then
   msg "\\nInstalling P4Python for SDP."
   su -l $RunUser -c '/hxdepots/sdp/Server/Unix/setup/install_sdp_python.sh' ||\
      warnmsg "Failed to install P4Python"

   msg "\\nInstalling P4Perl for SDP."
   su -l $RunUser -c '/hxdepots/sdp/Server/Unix/setup/install_sdp_perl.sh' ||\
      warnmsg "Failed to install P4Perl."
fi

msg "Preparing to run Sample Depot configuration script."
if [[ ! -d "$ResetHome" ]]; then
   run "/bin/mkdir -p $ResetHome" "Creating reset home dir [$ResetHome]." ||\
      bail "Could not create reset home dir [$ResetHome]. Aborting."
fi

cd "$ResetHome" || bail "Could not cd to $HxDepots/ResetHome. Aborting."

if [[ $LoadSampleDepot -eq 1 ]]; then
   msg "Configuring Sample Depot for SDP on Instance 1 only."
   if su -l $RunUser -c "$ResetHome/configure_sample_depot_for_sdp.sh -i 1 -d $ResetHome"; then
      msg "\\nSample Depot configured successfully.\\n"
   else
      bail "Failed to load the Sample Depot."
   fi
else
   msg "\\nSkipping configuration of Sample Depot due to '-no_sd'.\\n"
fi

#------------------------------------------------------------------------------
# Add sudoers to /etc/sudoers.d if the directory exists and the user file doesn't.
if [[ -d "${SudoersFile%/*}" && ! -e "$SudoersFile" ]]; then
   msg "Adding $RunUser to sudoers."
   if echo -e "$SudoersEntry" > "$SudoersFile"; then
      run "chmod 0400 $SudoersFile" "Setting perms on sudoers file, $SudoersFile." ||\
         warnmsg "Failed to set perms on $SudoersFile."
   else
      warnmsg "Failed to create $SudoersFile."
   fi
fi

#------------------------------------------------------------------------------
# Apply Package Repository
if [[ -d "${P4YumRepo%/*}" ]]; then
   if [[ -n "$ThisOSMajorVersion" ]]; then
      run "rpm --import $PerforcePackagePubkeyURL" \
         "Adding Perforce's packaging key to RPM keyring." ||\
         warnmsg "Failed to add Perforce packaging key to RPM keyring."

      msg "Generating $P4YumRepo."
      if ! echo -e "[perforce]\\nname=Perforce\\nbaseurl=http://package.perforce.com/yum/rhel/$ThisOSMajorVersion/x86_64\\nenabled=1\\ngpgcheck=1\\n" > "$P4YumRepo"; then
         warnmsg "Unable to generate $P4YumRepo."
      fi
   else
      warnmsg "Skipping generation of $P4YumRepo due to lack of OS Major Version info."
   fi
elif [[ -d "${P4AptGetRepo%/*}" ]]; then # /etc/apt/sources.list.d
   if [[ -n "$ThisOSName" && -n "$ThisOSDistro" ]]; then
      run "wget -qO - https://package.perforce.com/perforce.pubkey | sudo apt-key add -" \
         "Adding Perforce's packaging key to APT keyring." ||\
         warnmsg "Failed to add Perforce packaging key to APT keyring."

      msg "Generating $P4AptGetRepo."
      if ! echo "deb http://package.perforce.com/apt/$ThisOSName $ThisOSDistro release" > "$P4AptGetRepo"; then
         warnmsg "Unable to generate $P4AptGetRepo."
      fi
   else
      warnmsg "Skipping generation of $P4AptGetRepo due to lack of OS Name and Distro info."
   fi
else
   warnmsg "No Perforce supported package repository, RPM or APT, found to add. Skipping."
fi

if [[ "$FirewallType" == "Firewalld" ]]; then
   msg "\\nConfiguring $FirewallType services.\\n"
   cd "$FirewallDir" || bail "Could not cd to $FirewallDir."
   run "firewall-cmd --reload" "Baseline firewall load." ||\
      warnmsg "Baseline firewall load failed."
   for exe in p4broker p4d; do
      svcName=${exe}_${i}
      svcFile=${svcName}.xml
      sed "s:1:${i}:g" "$SDPSetupDir/firewalld/$svcFile" > $svcFile ||\
         bail "Failed to generated $PWD/svcFile."
      run "firewall-cmd --permanent --zone=public --add-service $svcName" \
         "Adding firewall entry for $svcName" ||\
         warnmsg "Adding firewall entry for $svcName failed."
   done
   run "iptables-save" "Showing firewall rules." ||\
      warnmsg "Showing firewall failed."
   run "firewall-cmd --reload" "Firewall reload." ||\
      warnmsg "Firewall reload failed."
elif [[ "$FirewallType" == "IPTables" ]]; then
   warnmsg "IPtables firewall detected, but not handled."
fi

if [[ "$ErrorCount" -eq 0 ]]; then
   if [[ "$WarningCount" -eq 0 ]]; then
      msg "\\nSUCCESS:  SDP Configuration complete."
   else
      msg "\\nSUCCESS:  SDP Configuration complete with $WarningCount warnings."
   fi

else
   msg "\\nSDP Configuration completed, but with $ErrorCount errors and $WarningCount warnings."
fi

exit 0
# Change User Description Committed
#93 29707 C. Thomas Tyler Released SDP 2023.1.29698 (2023/07/11).
Copy Up using 'p4 copy -r -b perforce_software-helix-installer-dev'.
#92 29618 C. Thomas Tyler Released SDP 2023.1.29616 (2023/05/25).
Copy Up using 'p4 copy -r -b perforce_software-helix-installer-dev'.
#91 29450 C. Thomas Tyler Released SDP 2022.2.29448 (2023/02/27).
Copy Up using 'p4 copy -r -b perforce_software-helix-installer-dev'.
#90 29405 C. Thomas Tyler Released SDP 2022.2.29403 (2023/02/06).
Copy Up using 'p4 copy -r -b perforce_software-helix-installer-dev'.
#89 28931 C. Thomas Tyler Released SDP 2022.1.28929 (2022/07/05).
Copy Up using 'p4 copy -r -b perforce_software-helix-installer-dev'.
#88 28863 C. Thomas Tyler Released SDP 2022.1.28834 (2022/05/27).
Copy Up using 'p4 copy -r -b perforce_software-helix-installer-dev'.
#87 28665 C. Thomas Tyler Released SDP 2022.1.28663 (2022/03/08).
Copy Up using 'p4 copy -r -b perforce_software-helix-installer-dev'.
#86 28415 C. Thomas Tyler Released SDP 2021.4.28409 (2021/11/24).
Copy Up using 'p4 copy -r -b perforce_software-helix-installer-dev'.
#85 28129 C. Thomas Tyler Released SDP 2021.3.28126 (2021/10/24).
Copy Up using 'p4 copy -r -b perforce_software-helix-installer-dev'.
#84 27427 C. Thomas Tyler Released SDP 2021.2.27425 (2021/02/09).
Copy Up using 'p4 copy -r -b perforce_software-helix-installer-dev'.
#83 27229 C. Thomas Tyler Released Helix Installer 2021.1.27227 (2021/01/20).
#82 27165 C. Thomas Tyler Released HelixInstaller 2021.1.27163 (2021/01/11).
#81 27139 C. Thomas Tyler Released HelixInstaller 2021.1.27137 (2021/01/03).
#80 27036 C. Thomas Tyler Released HelixInstaller 2020.2.27031 (2020/12/11).
#79 26975 C. Thomas Tyler Released HelixInstaller 2020.2.26973 (2020/12/01).
#78 26831 C. Thomas Tyler Released HelixInstaller/MultiArch/2020.1/26829 (2020/10/07).
#77 26793 C. Thomas Tyler Released HelixInstaller/MultiArch/2020.1/26784 (2020/09/25).
#76 26609 C. Thomas Tyler Release HelixInstaller 2020.1.26607 (2020/07/10).
#75 26578 C. Thomas Tyler Released Helix Installer 2020.1.26576 (2020/06/17).
#74 26268 C. Thomas Tyler Released HelixInstaller/MultiArch/2019.4/26181 (2020/01/22).
#73 26031 C. Thomas Tyler Released Helix Installer 2019.4.26026 (2019/08/22).
#72 26013 C. Thomas Tyler Released HelixInstaller 2019.4.26010 (2019/08/21).
#71 26008 C. Thomas Tyler Released HelixInstaller 2019.4.26006 (2019/08/20).
#70 25999 C. Thomas Tyler Released HelixInstaller 2019.4.25995 (2019/08/20).
#69 25900 C. Thomas Tyler Released HelixInstaller 2019.3.25890 (2019/07/24).
#68 25871 C. Thomas Tyler Released Helix Installer 2019.3.25869 (2019/07/23)
#67 25822 C. Thomas Tyler Released HelixInstaller/MultiArch/2019.3/25820 (2019/07/11).
#66 25725 C. Thomas Tyler Released HelixInstaller/MultiArch/2019.2/25723 (2019/06/19).
#65 25600 C. Thomas Tyler Released HelixInstaller 2019.2.25598.
#64 25499 C. Thomas Tyler Released HelixInstaller/2019.2/25497 (2019/04/12).
#63 25495 C. Thomas Tyler Released.
#62 25394 C. Thomas Tyler Released HelixInstaller/MultiArch/2019.1/25392 (2019/03/21).
#61 25384 C. Thomas Tyler Released HelixInstaller/MultiArch/2019.1/25382 (2019/03/21).
#60 25328 C. Thomas Tyler Released Rev.
HelixInstaller/MultiArch/2019.1/25324 (2019/03/08).
#59 25315 C. Thomas Tyler Released Rev.
HelixInstaller/MultiArch/2019.1/25313 (2019/03/07).
#58 25287 C. Thomas Tyler Released Rev.
HelixInstaller/MultiArch/2019.1/25285 (2019/03/06).
#57 25284 C. Thomas Tyler Released Rev.
HelixInstaller/MultiArch/2019.1/25282 (2019/03/06).
#56 25260 C. Thomas Tyler Released Rev.
HelixInstaller/MultiArch/2019.1/25250 (2019/03/02).
#55 25166 C. Thomas Tyler Released Helix Installer 2019.1.25164 (2019/02/22).
#54 24914 C. Thomas Tyler Released HelixInstaller 2018.2.24912.
#53 24865 C. Thomas Tyler Released HelixInstaller v2018.2.24862.
#52 23828 C. Thomas Tyler Released Helix Installer 2018.1.23826.
#51 23599 C. Thomas Tyler Released Helix Installer patch.
#50 23515 C. Thomas Tyler Released.
#49 23365 C. Thomas Tyler Released 2017.4.23363.
#48 23361 C. Thomas Tyler Released 2017.4.23359.
#47 23335 C. Thomas Tyler Released Helix Installer 2017.4.23333.
#46 23283 C. Thomas Tyler Released Helix Installer 2017.4.23281
#45 23245 C. Thomas Tyler Released reset_sdp.sh v2.9.5 adding support for a new workflow
supporting testing a shelved changelist from the SPD dev branch.

Use like this example:
reset_sdp.sh -fast -b dev,@23123 2>&1 | tee reset.log

where change @23123 would be a shelved changelist containing
files shelved on the SDP dev branch.

After cloning the SDP from the dev branch, it fetches the
shelved changes, and then unshelves it in the local repo.
#44 23060 C. Thomas Tyler reset_sdp.sh v2.8.32:
* Corrected 'sed' replacements of email addresses in mkdirs.cfg.
* Removed obsolete logic related to case sensitivity setting.
#43 23056 C. Thomas Tyler No functional change; cosmetic and convention-compliance/
maintainability enhancements only.
#42 23054 C. Thomas Tyler Updated to latest SDP version.
#41 23008 C. Thomas Tyler Updated SDP Version.
#40 22299 C. Thomas Tyler Tweaked to use preferred ports: 1666 for p4broker, 1999 for p4d.
#39 22292 C. Thomas Tyler Tweaked to use P4CONFIG=.p4config.local for 'p4 clone' operation only,
and then it is unset to defer to the SDP standard.  This fixes an issue
where the local repo cloned from The Workshop with 'p4 clone' (using DVCS
features) was getting confused with the 'p4d' instance we were trying to
initialize.

Also updated to download 2017.1 p4/p4d/p4broker executables plus Swarm 2017.1
package.
#38 22209 C. Thomas Tyler Updated SDP version.
#37 22203 C. Thomas Tyler Adapted to SDP structural changes including refactoring to add
mkdirs.cfg.
#36 21844 C. Thomas Tyler Adjusted base URL for The Workshop to use 'https:' only; the
'http:' base URL no longer works due to a redirect on the Workshop's
Swarm server.
#35 21839 C. Thomas Tyler reset_sdp.sh v2.8.23:
* Added exlusions to 'preserve dirs' feature to avoid copying
  DVCS repo files.
* Reduced excessive noise losing the '-v' flag for tarfile extraction.
* Enhanced to use 'curl' rather than 'wget', as it is functionally
  equivalent for our purposes, and is more ubiquituous.

configure_sample_depot_for_sdp.sh v1.4.7:
* Enhanced environment isolation.
#34 21812 C. Thomas Tyler Updated to latest SDP relelase.
Added insulation from P4ENVIRO hijacking.
#33 21539 C. Thomas Tyler Cosmetic fix.
#32 21536 C. Thomas Tyler Updated SDP Version.
Disabled shallow cloning to work around issue shallow cloning SDP from
The Workshop (due to a rename in prior history).
#31 21505 C. Thomas Tyler Updated SDP version.
#30 21503 C. Thomas Tyler Fixed cosmetic issue.
#29 21501 C. Thomas Tyler Updated to latest SDP release.
#28 21357 C. Thomas Tyler Fixed transposed digit.
#27 21354 C. Thomas Tyler Fixed typo in version id.
#26 21349 C. Thomas Tyler Updated SDP version.
Updated default release for P4/P4D to r16.2.
Updated default Swarm to 2016.3.
Adapated to new volume layout structure.
Added support for generating SSL autogen cert file.
#25 21246 C. Thomas Tyler Updated SDP version.
#24 21238 C. Thomas Tyler Changed all 'ftp://' URLs to 'http://' protocol to have a better
chance at dodging network and port filtering.
#23 21037 C. Thomas Tyler Updated to latest SDP.
#22 20999 C. Thomas Tyler Updated SDP version.
#21 20978 C. Thomas Tyler Updated default SDP version.
#20 20864 C. Thomas Tyler Updated default SDP tar file.
#19 20863 C. Thomas Tyler Optimized for shallow cloning of SDP from Workshop.
#18 20656 C. Thomas Tyler Removed obsolete HostIP logic.
#17 20654 C. Thomas Tyler A few minor tweaks to make it more in-line with the Battle School
Lab Engine, a primary customer of the Helix Installer.
#16 20568 C. Thomas Tyler Updated SDP to 2016.1.20559.
#15 20522 C. Thomas Tyler Updated SDP to 2016.1.20491.
#14 20483 C. Thomas Tyler Updated SDP to 2016.1.20460.
#13 20411 C. Thomas Tyler Optimzied sed.
#12 20400 C. Thomas Tyler Updated SDP to 2016.1.20395.
#11 20392 C. Thomas Tyler Updated to SDP 2016.1.20387.
#10 20375 C. Thomas Tyler Tweaked sample DNS name of master server to use 'helix' name,
emphasizing that it should be a host alias, not hard-coded to
a specific machine (e.g. helix-01 or helix-02).  Also uses the
host short name, which should work if networking best practices
are followed.
#9 20359 C. Thomas Tyler Added comment encouring people not to be lazy and use the
built-in password for production environments.
#8 20358 C. Thomas Tyler reset_sdp.sh v2.7.6:
* Updated to latest SDP, including change in password file location.
* Updated to acquire Swarm 2016.1.
#7 20052 C. Thomas Tyler Updated latest packaged SDP default to 2016.1.20028.
#6 19900 C. Thomas Tyler Updated default SDP version.
#5 19843 C. Thomas Tyler Updated default to latest SDP.
#4 19416 C. Thomas Tyler Updated SDP version.
#3 19263 C. Thomas Tyler Updated to test with 2016.1 executables.
Enhanced code and internal docs.
Tweaked URLS for pulling files from The Workshop.
#2 19231 C. Thomas Tyler Adjusted Swarm URL to reflect the Helix Installer move from
//guest/tom_tyler to //guest/perforce_software.
#1 19230 C. Thomas Tyler Populate -o //guest/tom_tyler/helix-installer/...
//guest/perforce_software/helix-installer/....
//guest/tom_tyler/helix-installer/main/src/reset_sdp.sh
#21 18963 C. Thomas Tyler Updated to latest SDP 2016.1.18958.
#20 16837 C. Thomas Tyler Updated to latest SDP tarfile.
#19 16697 C. Thomas Tyler Tweaked to account for cases where 'hostname -i' call returns
more than one IP address.
#18 16671 C. Thomas Tyler reset_sdp.sh v2.6.9:
* Minor optimization to error message when gcc and/or
gcc-c++ packages are missing.
* The '-fast' flag now changes the error relating to
gcc/g++ not being installed to a warning.
* Added confirmation message if gcc/g++ are found.
#17 16670 C. Thomas Tyler reset_sdp.sh v2.6.8:
* Now clones SDP main branch from The Workshop by default, rather
than using a tar file.
* Added '-b' to override default branch (main), e.g. '-b dev'.
* Added '-t <tarfile>' (or '-t default') to use the old tarfile
install method of using a hard-coded tarball, good for when stability
is preferred over using the very latest.
#16 16660 C. Thomas Tyler Added '-p' flag to enable preserving existing directories
under /p4/common.
#15 16642 C. Thomas Tyler Updated default SDP version.
#14 16624 C. Thomas Tyler reset_sdp.sh v2.6.5.
For Linux hosts, optimized to use 'hostname -i' rather than
'hostname -I'.  Added inline comments on IP selection on
Linux and OSX.
#13 16623 C. Thomas Tyler Changed order of commands to fix issue with permissions
when generating SDP broker config file.
#12 16607 C. Thomas Tyler Audit enhancement.
#11 16606 C. Thomas Tyler Improved handling of simulated mail from SDP scripts, so that
live_checkpoint succeeds on all platforms; it had failed
previosuly on Mac/Darwin due to failure sending email.

Improved backup handling.
#10 16602 C. Thomas Tyler Added '-local' option; avoids pulling from Workshop/FTP server.
Updated Swarm to 2015.3.
#9 16567 C. Thomas Tyler Updated SDP Version.
#8 16396 C. Thomas Tyler Rolled back Swarm version.
#7 16395 C. Thomas Tyler Updated to 2015.2 GA for P4D and 2015.3 GA for Swarm.
#6 16393 C. Thomas Tyler Fixed issue with 'sed' call.
Updated to work with new HOSTIP setting in SDP.
#5 16377 C. Thomas Tyler Updated to pick up latest SDP, and made corresponding
adjustments to processing of subsitutions in mkdirs.sh.
#4 16359 C. Thomas Tyler Minor code cleanlup.
#3 16358 C. Thomas Tyler Adjusted base path for Helix Installer.
Updated coypright to reference LICENSE file.
#2 16349 C. Thomas Tyler Updated to latest SDP version.
#1 15829 C. Thomas Tyler Moved Helix Installer to new Workshop Standard
for personal projects.
//guest/tom_tyler/sw/main/hi/src/reset_sdp.sh
#22 15629 C. Thomas Tyler reset_sdp.sh v2.5.0:
* Updated SDP to 2015.1.15607.
* Added '-C' (clean) flag to blast /depotdata/p4/common.
#21 15611 C. Thomas Tyler reset_sdp.sh v2.4.9:
* Updated default Swarm to 2015.2.
* Fixed mostly harmless bug attempting to pull a non-existing file from the web.
#20 15060 C. Thomas Tyler Rollback of Copy Up done with a new utility that apparently needs some work.
#19 15051 C. Thomas Tyler Copy Up using 'p4 copy -r -b tom_tyler-hi-dev'
#18 15007 C. Thomas Tyler Fixed expression.
#17 14987 C. Thomas Tyler Reduced excessive noise from from rsync and tar commands.

Added '--delete' to rsync commands handling the Sample Depot
to force a clean reset.

Added clear completion messages.
#16 14973 C. Thomas Tyler Added -no_ssl flag.
#15 14956 C. Thomas Tyler Fixed 'ps' check.
#14 14954 C. Thomas Tyler Fixed typo in case sensitivity mod.
#13 14953 C. Thomas Tyler Updated various settings to match changes in SDP.
Removed generation of a duplicate broker config file.
#12 14952 C. Thomas Tyler Adjusted case sensitivitiy to be case-sensitive on Linux,
the opposite of the SDP default.
#11 14949 C. Thomas Tyler Enhanced to ensure that 'reset_sdp.sh' is the only script
required.  It now downloads all other scripts and data files as
needed.
#10 14910 C. Thomas Tyler Added initialtion of empty /depotdata /metadata and /logs
dirs.
#9 14903 C. Thomas Tyler Fixed issue running under Vagrant.
#8 14902 C. Thomas Tyler Changed default list of SDP instances to initialize from
"1 12 abc" (illustrating ability to use named rather than
number instances) to just "1", which is mostly all that's
wanted for testing and pilot installations.
#7 14901 C. Thomas Tyler Fixed a typo in 'chmod' call.
#6 14188 C. Thomas Tyler Updated SDP version.
#5 14120 C. Thomas Tyler Moved sample depot checkpoint load to configurator script.
Added '-fast' flag to speed up testing.
Various minor tweaks.
#4 14106 C. Thomas Tyler Implemented Sample Depot SDP configurator script.

Tightened logic around process kill statement, and made it
work for Linux/Mac.
#3 14063 C. Thomas Tyler Added generation of initial live checkpoint for each instance.
Adjusted permissions on downloads folder.
Removed bad code fragements.
Cosmetic enhancement to the password.
#2 14033 C. Thomas Tyler Added script to transform stock Sample Depot data set to be
SDP compliant (mostly a stub at this point).  Various improvements
to reset_sdp.sh and the wrapper r.sh.
#1 13940 C. Thomas Tyler Added my personal reset_sdp test script.
 DO NOT DEPLOY on a live server.