reset_sdp.sh #16

  • //
  • guest/
  • tom_tyler/
  • sw/
  • main/
  • hi/
  • src/
  • reset_sdp.sh
  • View
  • Commits
  • Open Download .zip Download (17 KB)
#!/bin/bash
declare Version=2.4.6

#------------------------------------------------------------------------------
# Copyright (c) Perforce Software, Inc., 2013-2015.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1  Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# 2.  Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PERFORCE
# SOFTWARE, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
# DAMAGE.
#------------------------------------------------------------------------------

#------------------------------------------------------------------------------
# Declarations
declare SDPTar=sdp.Unix.2015.1.14143.tgz
declare SDPURL=http://swarm.workshop.perforce.com/projects/perforce-software-sdp/download/downloads/$SDPTar
declare HelixInstallerBaseURL=http://swarm.workshop.perforce.com/download/guest/tom_tyler/sw/main/hi
declare Depotdata=/depotdata
declare ResetHome=$Depotdata/reset
declare Metadata=/metadata
declare Logs=/logs
declare SDPHome=$Depotdata/sdp
declare Password=G00dP@ssw0rd
declare ExesDir=$Depotdata/exes
declare DownloadsDir=$Depotdata/downloads
declare -i BlastDownloadsAndExes=0
declare P4ExeRel=r15.1
declare P4DExeRel=$P4ExeRel
declare P4PExeRel=$P4ExeRel
declare P4BrokerExeRel=$P4ExeRel
declare P4SwarmRel=r15.1
declare P4Rel=r15.1
declare RunUser=perforce
declare RunGroup=
declare Arch=x86_64
declare ApiArch=
declare ThisArch=
declare RunArch=x86_64
declare ThisOS=
declare CBIN=/p4/common/bin
declare CCFG=/p4/common/config
declare SDPSetupDir=$SDPHome/Server/Unix/setup
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}

   echo "USAGE for $ThisScript v$Version:

$ThisScript [-B] [-no_ssl] [-fast] [-D]

or

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

	It is designed simplify 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.
	This could be helpful bootstrapping a demo server with a sample data set.

PLATFORM SUPPORT:
	This works on Red Hat Linux, CentOS, and Mac OSX (Mavericks or
	Yosemitie) platforms.  It works on CentOS 6.4 and 6.5 and
	likely on other Linux derivatives with little or no modification.

	It currently supports only the bin.linux26x86_64 (Linux) and
	bin.darwin90x86_64 (Mac OSX/Darwin) architectures.

REQUIREMENTS:
	Development utilities such as 'make' and the 'gcc' compiler
	must be installed and available in the PATH.

	The 'wget' utility must be installed and available in the
	PATH.  Mac OSX users:  'wget' is not installed with Mac OSX
	by default, at least not os of OSX 10.10/Yosemite.  It can be
	acquired and compiled.  For example, see:
	http://osxdaily.com/2012/05/22/install-wget-mac-os-x/

OPTIONS:
 -B	Specify '-B' to blast base SDP dirs, for a clean start.
	Otherwise, downloaded components from earlier runs will
	be used (which should be fine if they were run recently).

 -fast	Specify '-fast' to skip installation of Perl auxiliary things
	like Perl, Python, P4Perl, P4Python.

 -no_ssl
	By default, the Perforce server is setup SSL-enabled.  Specify
	'-no_ssl' to avoid using SSL feature.
 
 -D     Set extreme debugging verbosity.

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

EXAMPLES:
	Typical usage:
	sudo bash # Because you need to run as root.
	cd /where/this/script/is
	./reset_sdp.sh -B 2>&1 | tee log.reset_sdp
"
   fi

   exit 1
}

#------------------------------------------------------------------------------
# Function bail().
# Sample Usage:
#    bail "Missing something important. Aborting."
#    bail "Aborting with exit code 3." 3
function bail () { echo -e "\nError: ${1:-Unknown Error}\n"; exit ${2:-1}; }

#------------------------------------------------------------------------------
# Functions.  The runCmd() 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 want no dependencies for this
# script.
function runCmd {
   cmd=${1:-echo Testing runCmd}
   desc=${2:-""}
   [[ -n "$desc" ]] && echo $desc
   echo Running: $cmd
   $cmd
   return $?
}

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

declare -i NoOp=0
declare -i shiftArgs=0
declare -i InstallAuxFiles=1
declare -i UseSSL=1
declare TmpFile=/tmp/tmp.reset_sdp.$$.$RANDOM

set +u

while [[ $# -gt 0 ]]; do
   case $1 in
      (-B) BlastDownloadsAndExes=1;;
      (-fast) InstallAuxFiles=0;;
      (-no_ssl) UseSSL=0;;
      (-h) usage -h;;
      (-man) usage -man;;
      (-D) set -x;; # Debug; use 'set -x' mode.
      (*) 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 numbers of args or flags to args."
      shiftArgs=$shiftArgs-1
      shift
   done
done
set -u

#------------------------------------------------------------------------------
# Usage Validation
if [[ $USER != root ]]; then
   bail "Run as root, not $USER."
else
   echo Verified: Running as user $USER.
fi

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

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

echo Verifying dependencies.

[[ -z "$(which gcc 2>/dev/null)" || -z "$(which g++ 2>/dev/null)" ]] && \
   bail "No gcc found in the path.  You may need to install it.  Please\n check that the gcc.x86_64 and gcc-c++.x86_64 packages are\n installed, e.g. with:\n\tyum install -y gcc.x86_64 gcc-c++.x86_64\n\n"

[[ -z "$(which wget 2>/dev/null)" ]] && \
   bail "No 'wget' found in the path.  You may need to install it.  Please check that the wget.x86_64 packages is installed, e.g. with:\n\tyum install -y wget.x86_64\n\n"

[[ -z "$(which su 2>/dev/null)" ]] && \
   bail "No 'su' found in the path.  You may need to install it or add it to the path for the root user.\n\n"

ThisArch=$(uname -m)
ThisOS=$(uname -s)

if [[ $ThisArch == $RunArch ]]; then
   echo Verified:  Running on a supported architecture [$ThisArch].
   ApiArch=UNDEFINED_API_ARCH
   case $ThisOS in
      (Darwin)
         ApiArch="darwin90x86_64"
         RunGroup=staff
         SampleDepotTar=sampledepot.mac.tar.gz
      ;;
      (Linux)
         ApiArch="linux26x86_64"
         RunGroup=perforce
         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

if [[ -d $ExesDir && $BlastDownloadsAndExes -eq 1 ]]; then
   runCmd "/bin/rm -r -f $ExesDir" \
      "Blasting exes dir [$ExesDir] due to '-B'."
fi

if [[ ! -d $ExesDir ]]; then
   runCmd "/bin/mkdir -p $ExesDir" ||\
      bail "Could not create dir [$ExesDir]."

   cd $ExesDir || exit 1
   echo Working in [$PWD].
   runCmd "wget -q ftp://ftp.perforce.com/perforce/$P4ExeRel/bin.$ApiArch/p4" ||\
      bail "Could not get 'p4' executable."
   runCmd "wget -q ftp://ftp.perforce.com/perforce/$P4DExeRel/bin.$ApiArch/p4d" ||\
      bail "Could not get 'p4d' executable."
   runCmd "wget -q ftp://ftp.perforce.com/perforce/$P4PExeRel/bin.$ApiArch/p4p" ||\
      bail "Could not get 'p4p' executable."
   runCmd "wget -q ftp://ftp.perforce.com/perforce/$P4BrokerExeRel/bin.$ApiArch/p4broker" ||\
      bail "Could not get 'p4broker' executable."

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

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

for i in $SDPInstances; do
   if [[ $ThisOS == Linux || $ThisOS == Darwin ]]; then
      echo -e "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).
         Pids=$(ps -u $RunUser -f | grep -v grep | grep "/$processCmd "| awk '{print $2}')

         if [[ -z "$Pids" && $svc == p4d ]]; then
            echo "$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"
            Pids=$(ps -u $RunUser -f | grep -v grep | grep "/$processCmd "| awk '{print $2}')
         fi

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

   if [[ $ThisOS == Linux ]]; then
      cd /etc/init.d

      echo -e "Removing Perforce-related servcies.\nWorking in [$PWD]."

      for svc in $(/bin/ls p4*_init); do
         runCmd "chkconfig --del $svc"
         runCmd "rm -f $svc"
      done
   fi
done

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

if [[ ! -d $DownloadsDir ]]; then
   runCmd "/bin/mkdir -p $DownloadsDir"

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

   echo Working in [$PWD].
   runCmd "wget -q ftp://ftp.perforce.com/perforce/$P4Rel/bin.$ApiArch/p4api.tgz" ||\
      bail "Could not get file 'p4api.tgz'"
   runCmd "wget -q ftp://ftp.perforce.com/perforce/$P4SwarmRel/bin.multiarch/swarm.tgz" ||\
      bail "Could not get file 'swarm.tgz'"
   runCmd "wget -q $SDPURL" ||\
      bail "Could not get SDP tar file from [$SDPURL]."
   runCmd "wget -q ftp://ftp.perforce.com/perforce/tools/$SampleDepotTar" ||\
      bail "Could not get file [$SampleDepotTar]. Aborting."
   if [[ ! -d PerforceSample ]]; then
      runCmd "tar -xzvpf $SampleDepotTar" "Unpacking $SampleDepotTar in $PWD."
   fi
   runCmd "chown -R $RunUser:$RunGroup $DownloadsDir" \
      "Fixing permissiongs on downloads dir." ||\
      bail "Failed to fix permissions on downloads dir [$DownloadsDir]. Aborting."
else
   echo "Using existing downloads dir [$DownloadsDir]."
fi

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

echo -e "Working in [$PWD]."

for d in $Depotdata $Metadata $Logs; do
   if [[ ! -d "$d" ]]; then
      runCmd "/bin/mkdir -p $d" "Initialized empty dir [$d]." ||\
         bail "Failed to create dir [$d]."
   else
      echo "Verified: Dir [$d] exists."
   fi
done

for i in $SDPInstances; do
   runCmd "/bin/rm -rf $Depotdata/p4/$i $Metadata/p4/$i $Logs/p4/$i" \
      "Blasting any existing SDP structures." ||\
      bail "Failed to blast existing SDP structures."
   runCmd "/bin/rm -f /p4/$i" "Blasting symlink." ||\
      bail "Failed to remove SDP symlink."
done

runCmd "/bin/rm -f /p4/common" "Removing /p4/common symink." ||\
   bail "Failed to remove /p4/common symlink."

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

#------------------------------------------------------------------------------
# SDP Setup
runCmd "tar -xzvpf $DownloadsDir/$SDPTar" "Unpacking $DownloadsDir/$SDPTar in $PWD." ||\
   bail "Failed to untar SDP tarfile."

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

echo "Localizing mkdirs.sh in [$PWD]."

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

echo "Generating custom mkdirs.sh."
sed "s:=p4admin:=perforce:g"  mkdirs.sh.orig |\
   sed "s:DNS_name_of_master_server:helix-01.p4demo.com:g" |\
   sed "s:DNS_name_of_replica_server:helix-02.p4demo.com:g" |\
   sed "s:DNS_name_of_proxy_server:p4proxy01:g" |\
   sed "s:P4_PORT=1666:P4_PORT=1999:g" |\
   sed "s:P4BROKER_PORT=1667:P4BROKER_PORT=1666:g" |\
   sed "s:=adminpass:=$Password:g" |\
   sed "s:=servicepass:=$Password:g" |\
   sed "s:OSGROUP=perforce:OSGROUP=$RunGroup:g" |\
   sed "s:REPLICANAME=replica:REPLICANAME=HA_Master:g" > mkdirs.sh

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

if [[ $ThisOS == Linux ]]; then
   echo "Making SDP case-sensitive on Linux."
   sed "s:CASEINSENSITIVE=1:CASEINSENSITIVE=0:g" mkdirs.sh > $TmpFile
   runCmd "mv -f $TmpFile mkdirs.sh"
fi

chmod +x mkdirs.sh

echo "SDP Localizations."
diff mkdirs.sh.orig mkdirs.sh

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

echo "Initializing SDP instances and configuring /etc/init.d services."

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

   if [[ $ThisOS == Linux ]]; then
      echo -e "\nConfiguring Linux init scripts.\n"
      cd /etc/init.d || bail "Could not cd to [/etc/init.d]."
      for svc in p4broker p4d p4p; do
         initScript=${svc}_${i}_init
         if [[ -x /p4/${i}/bin/$initScript ]]; then
            runCmd "ln -s /p4/${i}/bin/$initScript"
            runCmd "chkconfig --add $initScript"
            runCmd "chkconfig $initScript on"
         fi
      done
   fi

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

   runCmd "chown -R $RunUser:$RunGroup $Depotdata" \
      "Adjusting ownership of $Depotdata." || bail "Failed to adjust ownership of $Depotdata."
done

#------------------------------------------------------------------------------
# Install Aux Files
if [[ $InstallAuxFiles -eq 1 ]]; then
   echo -e "\nInstalling P4Python for SDP."
   su -l $RunUser -c '/depotdata/sdp/Server/Unix/setup/install_sdp_python.sh -L /p4/1/logs/install_sdp_python.log'

   echo -e "\nInstalling P4Perl for SDP."
   su -l $RunUser -c '/depotdata/sdp/Server/Unix/setup/install_sdp_perl.sh -L /p4/1/logs/install_sdp_perl.log'

   echo -e "\nPlaceholder for Installing Ruby and P4Ruby for SDP (Not yet implemented in the SDP).\n"
fi

echo "Preparing to run Sample Depot configuration script."
if [[ ! -d "$ResetHome" ]]; then
   runCmd "/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 $Depotdata/ResetHome. Aborting."

if [[ ! -x configure_sample_depot_for_sdp.sh ]]; then
   runCmd "wget -q $HelixInstallerBaseURL/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 log.reset_sdp protect.p4s r reset_sdp.sh; do
   if [[ ! -f $f ]]; then
      runCmd "wget -q $HelixInstallerBaseURL/src/$f" "Getting file $f."
   else
      echo "Using existing file $PWD/$f."
   fi

   if [[ ! -x $f ]]; then
      if [[ $f == *".sh" || $f == r ]]; then
         runCmd "chmod +x $f" "chmod +x"
      fi
   fi
done

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

echo "Configuring Sample Depot on Instance 1 for SDP."
su -l $RunUser -c "$ResetHome/configure_sample_depot_for_sdp.sh -i 1 -d $ResetHome"
# Change User Description Committed
#23 15829 C. Thomas Tyler Moved Helix Installer to new Workshop Standard
for personal projects.
#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.