Skip to content

Instantly share code, notes, and snippets.

@SergioLuis
Created December 24, 2018 12:13
Show Gist options
  • Save SergioLuis/2c6b347e0c9a909482d6ec02f0e5ee6f to your computer and use it in GitHub Desktop.
Save SergioLuis/2c6b347e0c9a909482d6ec02f0e5ee6f to your computer and use it in GitHub Desktop.
A bash script to automatically autodetect and upgrade Plastic SCM's server and client to the latest available release.
#!/bin/bash
# A simple script to download and inflate Plastic SCM's server and client zips,
# notifying IFTTT in the process.
#
# Usage: ./updateplastic.sh [version]
#
# If the version is not specified, the script will try to autodetect it from
# the Plastic SCM web.
#
# Author: Sergio Luis Para
# Year: 2018
# CUSTOMIZE THESE FIELDS AS NEEDED.
IFTTTKEY="" # Your IFTTT key to connect to the maker API.
# You can get yours clicking on "Documentation" at:
# https://ifttt.com/maker_webhooks
SERVERPATH=/opt/plasticscm7/server # Server binaries path.
CLIENTPATH=/opt/plasticscm7/client # Client binaries path.
IFTTT_START="https://maker.ifttt.com/trigger/plastic_upgrade_start/with/key/$IFTTTKEY"
IFTTT_SUCCESS="https://maker.ifttt.com/trigger/plastic_upgrade_success/with/key/$IFTTTKEY"
IFTTT_FAIL="https://maker.ifttt.com/trigger/plastic_upgrade_fail/with/key/$IFTTTKEY"
RELEASE_NOTES="https://www.plasticscm.com/download/releasenotes/"
function notify {
BODY="{\"value1\" : \"$HOSTNAME\", \"value2\" : \"$1\"}"
curl -s --header "Content-Type: application/json" -d "$BODY" $2 >/dev/null
}
function checkError {
if [ $? -ne 0 ];
then
echo $1
notify "$1" "$2"
exit 1
fi
}
if [ "$#" -ne 1 ];
then
echo "No version specified. Trying to autodetect version..."
REDIRECTED_TO=$(curl -ILs -o /dev/null -w %{url_effective} -S $RELEASE_NOTES)
checkError "Could not autodetect version." $IFTTT_FAIL
VERSION=$(echo $REDIRECTED_TO | cut -d'/' -f 6)
echo "Autodetected version $VERSION."
else
VERSION=$1
echo "Specified version $VERSION."
fi
notify "$VERSION" "$IFTTT_START"
echo "Checking current Plastic SCM version installed..."
if [ "$(cm version)" == "$VERSION" ];
then
ERROR_MSG="Version $VERSION already installed!"
echo $ERROR_MSG
notify "$ERROR_MSG" "$IFTTT_FAIL"
exit 1
fi
SERVERZIPDIR="https://s3.eu-west-2.amazonaws.com/plastic-releases/releases/$VERSION/plasticscm/zipinstallers/linux/PlasticSCM-$VERSION-linux-server-binaries.zip"
CLIENTZIPDIR="https://s3.eu-west-2.amazonaws.com/plastic-releases/releases/$VERSION/plasticscm/zipinstallers/linux/PlasticSCM-$VERSION-linux-client-binaries.zip"
SERVERZIP="server-$VERSION.zip"
CLIENTZIP="client-$VERSION.zip"
echo "Downloading $SERVERZIPDIR to $SERVERZIP..."
wget --quiet "$SERVERZIPDIR" -O "$SERVERZIP"
if [ $? -ne 0 ];
then
notify "Downloading the serverzip failed!" "$IFTTT_FAIL"
exit 1
fi
echo "Downloading $CLIENTZIPDIR to $CLIENTZIP..."
wget --quiet "$CLIENTZIPDIR" -O "$CLIENTZIP"
if [ $? -ne 0 ];
then
notify "Downloading the clientzip failed!" $IFTTT_FAIL
exit 1
fi
echo "Cleaning out output directories..."
if [ -d client ];
then
rm -rf client
fi
if [ -d server ];
then
rm -rf server
fi
echo "Inflating $SERVERZIP"
unzip -qq $SERVERZIP
checkError "Error inflating serverzip!" $IFTTT_FAIL
echo "Inflating $CLIENTZIP"
unzip -qq $CLIENTZIP
checkError "Error inflating clientzip!" $IFTTT_FAIL
PLASTIC_PID=$(pidof plasticd)
if [ 0 -eq $? ];
then
echo "Found a Plastic SCM Server instance with PID $PLASTIC_PID. Sending SIGTERM..."
kill -15 $PLASTIC_PID
fi
while [ "$(pidof plasticd)" == "$PLASTIC_PID" ];
do
echo "Waiting for Plastic SCM Server to exit..."
sleep 0.1
done
echo "Copying server files to $SERVERPATH"
cp -a server/* $SERVERPATH
echo "Copying client files to $CLIENTPATH"
cp -a client/* $CLIENTPATH
echo "Starting the Plastic SCM server again!"
plasticd --daemon >/dev/null 2>&1 &
echo "Cleaning up files..."
rm $CLIENTZIP
rm $SERVERZIP
rm -rf client
rm -rf server
notify "$VERSION" "$IFTTT_SUCCESS"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment