#!/bin/bash #------------------------------------------------------------------------------ # See License.txt for Copyright and License. #============================================================================== # Declarations and Environment declare Version=1.0.8 if [[ -z "$MT_HOME" ]]; then if [[ -r ../env.sh ]]; then cd .. source env.sh cd - > /dev/null else echo "Error: Missing env.sh file, and MT_HOME variable not set. Aborting." exit 1 fi fi # Allow override of P4U_HOME, which is set only when testing P4U scripts. export P4U_HOME=${P4U_HOME:-/p4/common/bin} export P4U_LIB=${P4U_LIB:-/p4/common/lib} export P4U_ENV=$P4U_LIB/p4u_env.sh export P4U_LOG="/tmp/load_sample_depot.$(date +'%Y%m%d-%H%M%S').log" export VERBOSITY=${VERBOSITY:-3} export P4CONFIG=BogusValueNonExistentFile [[ -r "$P4U_ENV" ]] || { echo -e "\nError: Cannot load environment from: $P4U_ENV\n\n" exit 1 } source $P4U_ENV source $P4U_LIB/libcore.sh source $P4U_LIB/libp4u.sh if [[ $(uname) == Linux ]]; then declare SampleDepotURL=ftp://ftp.perforce.com/perforce/tools/sampledepot.tar.gz else declare SampleDepotURL=ftp://ftp.perforce.com/perforce/tools/sampledepot.zip fi declare SampleDepotHome=$MT_HOME/sd declare SampleDepotZipFile=${SampleDepotURL##*\/} declare SampleDepotSuperUser=bruno declare TestTag=test-p4-101 declare TestWS=bruno_jam.$TestTag declare TestWSRoot=$SampleDepotHome/ws/$TestWS declare Platform=linux26x86_64 [[ "$(uname)" == Darwin ]] && Platform=darwin90x86_64 declare p4Version=r15.1 declare p4URL=ftp://ftp.perforce.com/perforce/$p4Version/bin.$Platform/p4 declare p4dURL=ftp://ftp.perforce.com/perforce/$p4Version/bin.$Platform/p4d declare p4brokerURL=ftp://ftp.perforce.com/perforce/$p4Version/bin.$Platform/p4broker declare p4Exe=$SampleDepotHome/p4 declare p4dExe=$SampleDepotHome/p4d declare p4brokerExe=$SampleDepotHome/p4broker declare -i SilentMode=0 #============================================================================== # Local Functions #------------------------------------------------------------------------------ # Function: terminate function terminate { # Disable signal trapping. trap - EXIT SIGINT SIGTERM # Don't litter. cleanTrash vvmsg "$THISSCRIPT: EXITCODE: $OverallReturnStatus" # Stop logging. [[ "${P4U_LOG}" == off ]] || stoplog # With the trap removed, exit. exit $OverallReturnStatus } #------------------------------------------------------------------------------ # Function: get_sample_depot() #------------------------------------------------------------------------------ function get_sample_depot() { vvmsg "CALL get_sample_depot($*)" msg "Getting Sample Depot." declare wgetExe=$(which wget 2>/dev/null) [[ -z "$wgetExe" ]] && bail "Could not find required 'wget' utility in the PATH. Aborting." if [[ -d $SampleDepotHome ]]; then runCmd "/bin/rm -rf $SampleDepotHome" "Blasting existing Sample Depot home dir [$SampleDepotHome]." ||\ bail "Failed to blast existing Sample Depot home dir [$SampleDepotHome]." fi runCmd "/bin/mkdir -p $SampleDepotHome" "Creating empty new Sample Depot home dir [$SampleDepotHome]" ||\ bail "Failed to create new Sample Depot root [$SampleDepotHome]." cd $SampleDepotHome || bail "Failed to cd to Sample Depot home dir [$SampleDepotHome]." msg "Operating in [$PWD]." runCmd "$wgetExe -q $SampleDepotURL" \ "Pulling Sample Depot from [$SampleDepotURL]." ||\ bail "Failed to pull Sample Depot." msg "Sample Depot pulled successfully." msg "Pulling Perforce executables for $p4Version on $Platform." for exeUrl in $p4URL $p4dURL $p4brokerURL; do exe=${exeUrl##*/} runCmd "$wgetExe -q $exeUrl" \ "Pulling exe from [$exeUrl]." ||\ bail "Failed to pull exe from [$exeUrl]." runCmd "chmod +x $exe" runCmd "./$exe -V" "Obtained [$exe] version:" 0 1 done msg "Perforce executables pulled." } #------------------------------------------------------------------------------ # Function: init_sample_depot() # # This picks up where get_sample_depot leaves off. # The $SampleDepotHome folder will exist and contain the sample_depot.tar.gz # file, as downloaded from the Perforce FTP site. #------------------------------------------------------------------------------ function init_sample_depot() { vvmsg "CALL init_sample_depot($*)" declare tarExe=$(which tar 2>/dev/null) declare unzipExe=$(which unzip 2>/dev/null) [[ -z "$tarExe" ]] && bail "Could not find required 'tar' utility in the PATH. Aborting." [[ -z "$unzipExe" ]] && bail "Could not find required 'unzip' utility in the PATH. Aborting." if [[ ! -d $SampleDepotHome ]]; then warnmsg "Sample depot home dir does not exist. Doing full reset as if -G was specified." get_sample_depot fi cd $SampleDepotHome || bail "Failed to cd to Sample Depot Home dir [$SampleDepotHome]." msg "Operating in [$PWD]." [[ -r "$SampleDepotZipFile" ]] || bail "Cannot access Sample Depot file [$SampleDepotZipFile]." if [[ "$SampleDepotZipFile" == *"gz" ]]; then runCmd "$tarExe -xpf $SampleDepotZipFile" || bail "Failed to expand tar file [$SampleDepotZipFile]." elif [[ "$SampleDepotZipFile" == *".zip" ]]; then runCmd "$unzipExe -o $SampleDepotZipFile" || bail "Failed to unzip file [$SampleDepotZipFile]." else bail "Dunno how to extract from $SampleDepotZipFile." fi cd PerforceSample ||\ bail "Exploded tarfile is missing expected 'PerforceSample' dir." find . -type f -name "*,v" -print -exec perl -p -i -e s/\r\n/\n/ {} \; msg "Operating in [$PWD]." cd $SampleDepotHome || bail "Failed to cd to Sample Depot Home dir [$SampleDepotHome]." msg "Operating in [$PWD]." export P4ROOT=$SampleDepotHome/.p4root echo -e "P4IGNORE=.p4ignore\nP4USER=bruno\nP4CHARSET=none\nP4INITROOT=$P4ROOT\nP4PORT=rsh:/bin/bash -c \"exec $p4dExe -i -J off -r '$P4ROOT'\"\n" > .p4config export P4CONFIG=$PWD/.p4config msg "Generated this .p4config file:\n$(cat .p4config)\n" runCmd "/bin/rm -rf $P4ROOT" ||\ bail "Failed to remove P4ROOT dir!" runCmd "/bin/mkdir -p $P4ROOT" ||\ bail "Failed to create P4ROOT dir!" runCmd "$p4dExe -r $P4ROOT -jr $SampleDepotHome/PerforceSample/checkpoint" \ "Loading Sample Depot checkpoint." ||\ bail "Failed to load Sample Depot checkpoint." runCmd "$p4dExe -r $P4ROOT -xu" \ "Upgrading Sample Depot databases." ||\ bail "Failed to upgrade Sample Depot checkpoint." export P4USER=$(whoami) msg "Adding user account for $P4USER." tmpProtectFile=/tmp/protect.$$.txt tmpPasswdFile=/tmp/protect.$$.txt echo -e "User: $P4USER\n\nEmail: perforce@p4bsw.com\n\nFullName: Perforce Admin\n\n" | $p4Exe -u $SampleDepotSuperUser user -f -i msg "Promoting $P4USER to super user status." $p4Exe -u $SampleDepotSuperUser protect -o |\ perl -e "while(<>){next if /^\s*$/; print;}" > $tmpProtectFile echo -e "\tsuper user $P4USER * //..." >> $tmpProtectFile $p4Exe -u $SampleDepotSuperUser protect -i < $tmpProtectFile /bin/rm -f $tmpProtectFile echo G00dPassword > $tmpPasswdFile echo G00dPassword >> $tmpPasswdFile msg "Setting password for $P4USER." $p4Exe -u $SampleDepotSuperUser passwd $P4USER < $tmpPasswdFile /bin/rm -f $tmpProtectFile $tmpPasswdFile } #------------------------------------------------------------------------------ # 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 [-G|-I] [-L <log>] [-si] [-v<n>] [-D] or $THISSCRIPT [-h|-man] " if [[ $style == -man ]]; then echo -e " DESCRIPTION: Sample Depot Loader OPTIONS: -G Start from scratch, by getting a pristine copy of the Sample Depot from the Perforce web site, then installing the SDP structure. Using -G implies -I. The existing Sample Depot and related Perforce server instance is blasted. -I Start almost from scratch, skipping only the download of the Sample Depot. -v<n> Set verbosity 1-5 (-v1 = quiet, -v5 = highest). -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: $(dirname ${P4U_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'. -D Set extreme debugging verbosity. HELP OPTIONS: -h Display short help message -man Display man-style help message FILES: EXAMPLES: SEE ALSO: " fi exit 1 } #============================================================================== # Command Line Processing declare -i GetSampleDepot=0 declare -i InitSampleDepot=0 declare -i shiftArgs=0 set +u while [[ $# -gt 0 ]]; do case $1 in (-h) usage -h;; (-man) usage -man;; (-G) GetSampleDepot=1; InitSampleDepot=1;; (-I) InitSampleDepot=1;; (-v1) export VERBOSITY=1;; (-v2) export VERBOSITY=2;; (-v3) export VERBOSITY=3;; (-v4) export VERBOSITY=4;; (-v5) export VERBOSITY=5;; (-L) export P4U_LOG=$2; shiftArgs=1;; (-si) SilentMode=1;; (-D) set -x;; # Debug; use 'set -x' mode. (*) usageError "Unknown arg ($1).";; esac # Shift (modify $#) the appropriate number of times. shift; while [[ $shiftArgs -gt 0 ]]; do [[ $# -eq 0 ]] && usageError "Bad usage." shiftArgs=$shiftArgs-1 shift done done set -u #============================================================================== # Command Line Verification [[ $SilentMode -eq 1 && $P4U_LOG == off ]] && \ usageError "Cannot use '-si' with '-L off'." #============================================================================== # Main Program trap terminate EXIT SIGINT SIGTERM declare -i OverallReturnStatus=0 if [[ "${P4U_LOG}" != off ]]; then logDir=$(dirname $P4U_LOG) if [[ ! -d $logDir ]]; then /bin/mkdir $logDir || bail "Couldn't create log dir [$logDir]." fi touch ${P4U_LOG} || bail "Couldn't touch log file [${P4U_LOG}]." # Redirect stdout and stderr to a log file. if [[ $SilentMode -eq 0 ]]; then exec > >(tee ${P4U_LOG}) exec 2>&1 else exec >${P4U_LOG} exec 2>&1 fi initlog fi [[ $GetSampleDepot -eq 1 ]] && get_sample_depot [[ $InitSampleDepot -eq 1 ]] && init_sample_depot if [[ $OverallReturnStatus -eq 0 ]]; then msg "${H}\nAll processing completed successfully.\n" else msg "${H}\nProcessing completed, but with errors. Scan above output carefully.\n" fi # Illustrate using $SECONDS to display runtime of a script. msg "That took about $(($SECONDS/3600)) hours $(($SECONDS%3600/60)) minutes $(($SECONDS%60)) seconds.\n" # See the terminate() function, which is really where this script exits. exit $OverallReturnStatus
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#4 | 12903 | C. Thomas Tyler | Set P4USER=bruno in generated .p4config file. | ||
#3 | 12902 | C. Thomas Tyler | Removed excess code. | ||
#2 | 12868 | C. Thomas Tyler | Fixed issue when re-running with junk from a prior run in place. | ||
#1 | 12865 | C. Thomas Tyler |
Starting to add 'mktask' sample script to The Workshop. This is a work in progress. |