#!/bin/bash set -u #------------------------------------------------------------------------------ # Copyright (c) Perforce Software, Inc., 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 Version=1.0.6 declare ResetTarget=/depotdata declare DownloadsDir=$ResetTarget/downloads declare TmpFile=/tmp/tmp.csd4sdp.$$.$RANDOM declare RunUser=perforce declare Arch=x86_64 declare ApiArch= declare ThisArch= declare RunArch=x86_64 declare ThisOS=$(uname -s) declare CBIN=/p4/common/bin declare ThisScript=${0##*/} declare SDPInstance=Undefined #------------------------------------------------------------------------------ # 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 <sdp_instance> or $ThisScript [-h|-man] " if [[ $style == -man ]]; then echo -e " DESCRIPTION: This script transforms a stock Sample Depot instance into one that works with the SDP. REQUIREMENTS: A P4D process must be live and running with the stock Sample Depot data set, on a sport ARGUMENTS: <sdp_instance> Specify the SDP Instance in which the Sample Depot data set is running. This argument is required. -D Set extreme debugging verbosity. HELP OPTIONS: -h Display short help message -man Display man-style help message EXAMPLES: Usage to configure Instance 1: cd /where/this/script/is $ThisScript 1 2>&1 | tee log.${ThisScript%.sh}.1 Usage to configure Instance abc: cd /where/this/script/is $ThisScript abc 2>&1 | tee log.${ThisScript%.sh}.abc " 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 { declare cmd=${1:-echo Testing runCmd} declare desc=${2:-""} declare cmdToShow=$cmd [[ $cmdToShow == *"<"* ]] && cmdToShow=${cmdToShow%%<*} [[ $cmdToShow == *">"* ]] && cmdToShow=${cmdToShow%%>*} [[ -n "$desc" ]] && echo $desc echo Running: $cmdToShow if [[ $NoOp -eq 0 ]]; then $cmd else echo "NO-OP: Would run: $cmdToShow" fi return $? } #============================================================================== # Command Line Processing declare -i NoOp=0 declare -i shiftArgs=0 declare DataDir="$PWD" set +u while [[ $# -gt 0 ]]; do case $1 in (-i) SDPInstance=$2; shiftArgs=1;; (-d) DataDir="$2"; shiftArgs=1;; (-n) NoOp=1;; (-h) usage -h;; (-man) usage -man;; (-D) set -x;; # Debug; use 'set -x' mode. 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 [[ $SDPInstance == Undefined ]] && bail "Bad Usage: The '<sdp_instance>' argument is required." [[ ! -r $CBIN/p4_vars ]] && bail "Missing SDP Environment File [$CBIN/p4_vars]. Aborting." if [[ $USER != $RunUser ]]; then bail "Run as $RunUser, not $USER." else echo Verified: Running as user $RunUser. fi # Load SDP environment and variable definintions. source $CBIN/p4_vars $SDPInstance ||\ bail "Failed to load SDP environment. Aborting." unset P4CONFIG runCmd "$P4BIN -u bruno -s info -s" "Verifying server is offline." &&\ bail "Perforce server is unexpectedly online. Aborting." runCmd "/p4/${SDPInstance}/bin/p4d_${SDPInstance} -jr $DownloadsDir/PerforceSample/checkpoint" \ "Loading the Sample Depot metadata in instance ${SDPInstance}." ||\ bail "Failed to load Sample Depot checkpoint." runCmd "/p4/${SDPInstance}/bin/p4d_${SDPInstance} -Gc" \ "Generating OpenSSL Certificates for instance $SDPInstance." ||\ bail "Failed to generate OpenSSL Certs for Instance $SDPInstance." if [[ $NoOp -eq 0 ]]; then echo "Starting services p4broker_${SDPInstance}_init and p4d_${SDPInstance}_init." /p4/${SDPInstance}/bin/p4broker_${SDPInstance}_init start < /dev/null > /dev/null 2>&1 & /p4/${SDPInstance}/bin/p4d_${SDPInstance}_init start < /dev/null > /dev/null 2>&1 & sleep 1 else echo "NO-OP: Would start services p4broker_${SDPInstance}_init and p4d_${SDPInstance}_init." fi # Note: Automating a 'p4 trust -y' (especially with '-f') is TOTALLY # INAPPROPRIATE in any production environment, as it defeats the purpose of the # Open SSL trust mechanism. But for our purposes here scripts that spin up # throw-away data sets for testing or training purposes, it's just dandy. runCmd "/p4/${SDPInstance}/bin/p4_${SDPInstance} trust -f -y" \ "Trusting the OpenSSL Cert of the server." ||\ bail "Failed to trust the server." runCmd "$P4BIN -u bruno -s info -s" "Verifying connection to Perforce server." ||\ bail "Could not connect to Perforce server." [[ "$($P4BIN -u bruno protects -m)" == super ]] ||\ bail "Could not verify super user access for $P4USER on port $P4PORT. Is this the Sample depot? Aborting." echo "Super user access for bruno verified." if [[ $NoOp -eq 0 ]]; then echo "Creating user $P4USER." sed "s:__EDITME_ADMIN_P4USER__:$P4USER:g" $DataDir/admin.user.p4s > $TmpFile $P4BIN -u bruno user -f -i < $TmpFile echo "Adding user to NoTicketExpiration group." sed "s:__EDITME_ADMIN_P4USER__:$P4USER:g" $DataDir/NoTicketExpiration.group.p4s > $TmpFile $P4BIN -u bruno group -i < $TmpFile echo "Promoting user $P4USER to super user." $P4BIN -u bruno protect -o > $TmpFile echo -e "\tsuper user $P4USER * //...\n" >> $TmpFile $P4BIN -u bruno protect -i < $TmpFile else echo "NO-OP: Would create $P4USER as a super user." fi cat /p4/common/bin/adminpass > $TmpFile cat /p4/common/bin/adminpass >> $TmpFile $P4BIN -u bruno passwd $P4USER < $TmpFile runCmd "/p4/common/bin/p4login" "Logging in $P4USER super user." ||\ bail "Failed to login super user $P4USER. Aborting." runCmd "$ResetTarget/sdp/Server/setup/configure_new_server.sh $SDPInstance" \ "Applying SDP configurables." ||\ bail "Failed to set SDP configurables. Aborting." runCmd "$P4BIN admin updatespecdepot -a" \ "Updating spec depot." || bail "Failed to udpate spec depot. Aborting." for depot in $(/bin/ls -d $ResetTarget/downloads/PerforceSample/*); do [[ $depot == checkpoint* ]] && continue [[ $depot == README* ]] && continue [[ $depot == readme* ]] && continue runCmd "/usr/bin/rsync -av $depot/ /p4/$SDPInstance/depots/${depot##*/}" \ "Copying Sample Depot archive files for depot [${depot##*/}]." ||\ echo -e "\nWarning: Non-zero exit code $? from rsync for depot ${depot##*/}." done runCmd "/usr/bin/rsync /p4/$SDPInstance/root/spec/ /p4/$SDPInstance/depots/spec" \ "Copying a few spec depot files." ||\ echo -e "\nWarning: Non-zero exit code $? from rsync for spec depot." runCmd "/bin/rm -rf /p4/$SDPInstance/root/spec" \ "Cleanup redundant copy of spec depot files." ||: if [[ $ThisOS == Linux ]]; then runCmd "/p4/common/bin/live_checkpoint.sh $SDPInstance" \ "Taking Live Checkpoint." || bail "Live checkpoint failed." else runCmd "/p4/common/bin/live_checkpoint.sh $SDPInstance" \ "Taking Live Checkpoint." || echo -e "\nWarning: Ignoring checkpoint error on $ThisOS." fi runCmd "/usr/bin/tar -czf /p4/$SDPInstance/backup.$(date +t'%Y%m%d-%H%M').tgz /p4/${SDPInstance}/depots /p4/${SDPInstance}/checkpoints/p4_${SDPInstance}.ckp.*" \ "Creating backup of instance $SDPInstance." || bail "Failed to backup instance $SDPInstance."
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#17 | 15829 | C. Thomas Tyler |
Moved Helix Installer to new Workshop Standard for personal projects. |
||
#16 | 15060 | C. Thomas Tyler | Rollback of Copy Up done with a new utility that apparently needs some work. | ||
#15 | 15051 | C. Thomas Tyler | Copy Up using 'p4 copy -r -b tom_tyler-hi-dev' | ||
#14 | 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. |
||
#13 | 14985 | C. Thomas Tyler | Added exit 0 to keep Vagrant happy. | ||
#12 | 14982 | C. Thomas Tyler | Fixed embarrasing typo. | ||
#11 | 14974 | C. Thomas Tyler | Fixed issues handling non-SSL servers. | ||
#10 | 14972 | C. Thomas Tyler | Doc tweaks. | ||
#9 | 14961 | C. Thomas Tyler | Fixed optimization to avoid bogus rsync commands. | ||
#8 | 14958 | C. Thomas Tyler | Modified to do 'p4 trust' and 'p4 info' via-broker in addition to direct. | ||
#7 | 14957 | C. Thomas Tyler | Fixed 'tar' command, now cross-platform smart and PATH-dependent. | ||
#6 | 14955 | C. Thomas Tyler | Made 'sed' PATH-dependent, removing hard coding. | ||
#5 | 14120 | C. Thomas Tyler |
Moved sample depot checkpoint load to configurator script. Added '-fast' flag to speed up testing. Various minor tweaks. |
||
#4 | 14109 | C. Thomas Tyler |
Tweaked copy of spec depot files to get a clean verify. Added detection of rsync failure. |
||
#3 | 14106 | C. Thomas Tyler |
Implemented Sample Depot SDP configurator script. Tightened logic around process kill statement, and made it work for Linux/Mac. |
||
#2 | 14062 | C. Thomas Tyler | Added super user and set password. | ||
#1 | 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. |