#!/bin/bash
#-------------------------------------------------------------------------------
#
#-------------------------------------------------------------------------------
set -u
declare Version=1.0.3
export DemoID=${1:-1}
export P4BIN=${2:-p4}
export P4PORT=${3:-1666}
export P4USER=${4:-bruno}
export P4ENVIRO=/dev/null/.p4enviro
export P4CONFIG=.p4config.local
declare CIServerPort=2000
declare CIRepoNameBase=CIServer
declare CIBranch=Jam_MAIN
declare CIRemoteID="${CIRepoNameBase}-${CIBranch}"
declare UserRepoRemoteID="${CIRepoNameBase}-${CIBranch}-dvcs"
declare ExitCode=
echo -e "Running Demo $DemoID powered by $0 v$Version."
RootDir=/tmp/repos/$CIRepoNameBase
DepotPath="//depot/Jam/MAIN"
TmpFile=/tmp/tmpFile.demo$DemoID.$$.$RANDOM
/bin/rm -rf $RootDir
echo "Shuttding donw 'p4d' for CI Server."
# Try a 'p4 admin stop' and a 'kill -9' -- only OK because this is a throw-away demo server.
$P4BIN -u bruno -p $CIServerPort admin stop > /dev/null 2>&1 ||:
sleep 1
p4dCIPids=$(ps -ef | grep -v grep | grep "p4d_1 -r $RootDir/.p4root -p $CIServerPort" | awk '{print $2}')
if [[ -n "$p4dCIPids" ]]; then
kill -9 $p4dCIPids
fi
sleep 1
mkdir -p $RootDir || exit 2
echo "Operating in: $PWD"
cd $RootDir || exit 2
echo -e "P4PORT=localhost:$P4PORT\nP4USER=$P4USER\nP4TICKETS=/tmp/repos/.p4tickets\nP4IGNORE=.p4ignore\n" > /tmp/repos/.p4config
echo "Generated this P4CONFIG file [/tmp/repos/.p4config] for viewng the master server:\n$(cat $P4CONFIG)\n"
echo Enabling sensible DVCS workflows from the master server. Set server.allowfetch and server.allowpush to 2.
$P4BIN -p $P4PORT -u $P4USER configure set server.allowpush=2
$P4BIN -p $P4PORT -u $P4USER configure set server.allowfetch=2
echo "Creating Remote Spec $CIRemoteID"
echo -e "RemoteID: $CIRemoteID\n\nAddress: localhost:$P4PORT\n\nOwner: $P4USER\n\nOptions: unlocked nocompress\n\nDescription:\n\tCreated by $P4USER\n\nDepotMap:\n\t$DepotPath/... $DepotPath/...\n" > $TmpFile
$P4BIN -p $P4PORT -u $P4USER -s remote -i < $TmpFile
if [[ $? -ne 0 ]]; then
echo -e "Failed to create remote spec with these contents:\n$(cat $TmpFile)\n"
cat $TmpFile
exit 2
fi
unset P4ROOT P4JOURNAL P4AUDIT P4LOG P4DEBUG P4NAME
Cmd="$P4BIN -p $P4PORT -u $P4USER clone -p $P4PORT -r $CIRemoteID"
echo -e "Cloning CI Build Server from Master Server. Running:\n$Cmd"
$Cmd
if [[ $? -ne 0 ]]; then
echo -e "Failed to clone CI Build Server repo. Aborting."
exit 2
fi
echo "Writing basic server start/stop script for DVCS Repo."
export P4ROOT=$PWD/.p4root
echo -e "#!/bin/bash
export P4ROOT=$P4ROOT
export P4PORT=$CIServerPort
export P4AUDIT=audit
export P4JOURNAL=$P4ROOT/journal
export P4LOG=p4_log.txt
export P4DEBUG=server=3
unset P4NAME
case \${1:-help} in
(start)
StartLog=/tmp/p4d.$CIRepoNameBase.start.log
nohup /p4/1/bin/p4d_1 -r \$P4ROOT -p \$P4PORT -d < /dev/null > \$StartLog 2>&1 &
sleep 1
cat \$StartLog
;;
(stop) $P4BIN -u bruno -p $CIServerPort admin stop;;
(status|info) $P4BIN -p $CIServerPort info -s;;
(help) echo \$0 [start|stop|status|info|help];;
esac\n\n" > $P4ROOT/p4s
chmod +x $P4ROOT/p4s
echo "Starting CI Build Server."
$P4ROOT/p4s start
sleep 1
unset P4ROOT P4JOURNAL P4AUDIT P4LOG P4DEBUG P4NAME
echo "Creating Remote Spec $UserRepoRemoteID"
echo -e "RemoteID: $UserRepoRemoteID\n\nAddress: localhost:$CIServerPort\n\nOwner: $P4USER\n\nOptions: unlocked nocompress\n\nDescription:\n\tCreated by $P4USER\n\nDepotMap:\n\t$DepotPath/... $DepotPath/...\n" > $TmpFile
$P4BIN -p $CIServerPort -u $P4USER -s remote -i < $TmpFile
if [[ $? -ne 0 ]]; then
echo -e "Failed to create remote spec with these contents:\n$(cat $TmpFile)\n"
cat $TmpFile
exit 2
fi
BrunosRepo="bruno_jam_MAIN"
BrunosRepoRoot="/tmp/repos/$BrunosRepo"
echo Enabling the broadest range of DVCS workflows for the CI server. Set server.allowfetch and server.allowpush to 3, and server.allowrewrite=1.
echo Setting some of these is redundant as they are defaults for a server initiated with 'p4 clone'.
$P4BIN -p $CIServerPort -u $P4USER configure set server.allowpush=3
$P4BIN -p $CIServerPort -u $P4USER configure set server.allowfetch=3
$P4BIN -p $CIServerPort -u $P4USER configure set server.allowrewrite=1
/bin/rm -rf $BrunosRepoRoot || exit 2
/bin/mkdir -p "$BrunosRepoRoot" || exit 2
cd $BrunosRepoRoot || exit 2
Cmd="$P4BIN -p $CIServerPort -u $P4USER clone -p $CIServerPort -r $UserRepoRemoteID"
echo -e "Cloning a personal server from the CI Build Server. Running:\n$Cmd"
$Cmd
if [[ $? -ne 0 ]]; then
echo -e "Failed to clone personal repo. Aborting."
exit 2
fi
/bin/rm -f $TmpFile