Created
November 29, 2012 06:59
-
-
Save bastman/4167278 to your computer and use it in GitHub Desktop.
deploy.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#============================================================================== | |
# | |
# Script for automated deployments. | |
# | |
# This system expects at least the following directories to exists: | |
# | |
# * ../../temp | |
# * ../../transfer | |
# * ../../testing | |
# * ../../staging | |
# * ../../production | |
# | |
# Default deployment target is "testing". You can specify a different | |
# target (one of "testing", "staging" and "production") as first parameter | |
# to this script | |
# | |
#============================================================================== | |
# === Configuration === | |
# which directories to synchronize / deploy: | |
DIR_LIST="bin build contrib doc htdocs src" | |
# temporary space, attention, gets cleaned each run! (do NOT use /tmp!) | |
# this directory holds ONE backup copy of the changed directies of the | |
# LAST deployment run. | |
TEMP_DIR=temp | |
# marker, if a deployment is still running and/or failed .. | |
LOCK_FILE=/tmp/deployment.lck | |
# default deployment: transfer to testing | |
SOURCE_SYSTEM=transfer | |
TARGET_SYSTEM=testing | |
#============================================================================== | |
# remember current directory .. | |
CWD=`pwd` | |
# if this script is symlinked, fetch the "real" thing .. | |
SCRIPT=`readlink $0` | |
if test -z "$SCRIPT" | |
then | |
SCRIPT=$0 | |
fi | |
# where is this script located? | |
CMD_DIR=`dirname $SCRIPT` | |
# change to root of "systems" directory | |
# (aka the dir containing the production, staging, transfer etc directories) | |
cd $CMD_DIR/../../.. | |
# check, what kind of deployment (testing, staging, production) is requested: | |
# | |
# transfer => testing | |
# testing => staging | |
# staging => production | |
# | |
if test -n "$1" | |
then | |
case $1 in | |
testing) | |
SOURCE_SYSTEM=transfer | |
TARGET_SYSTEM=testing | |
;; | |
staging) | |
SOURCE_SYSTEM=testing | |
TARGET_SYSTEM=staging | |
;; | |
production) | |
SOURCE_SYSTEM=staging | |
TARGET_SYSTEM=production | |
;; | |
*) | |
echo "ERROR: unsupported deployment to [$1]!" | |
exit 1 | |
;; | |
esac | |
fi | |
# check if the needed directories exist: | |
for DIR in "$TEMP_DIR" "$SOURCE_SYSTEM" "$TARGET_SYSTEM" | |
do | |
if test \! -d "$DIR" | |
then | |
echo "ERROR: directory $DIR does not exist!" | |
exit 1 | |
fi | |
done | |
# tell the user what we are about to do .. | |
echo "ATTENTION: About to deploy from \"$SOURCE_SYSTEM\" to => \"$TARGET_SYSTEM\" <=" | |
echo " Directories: $DIR_LIST" | |
# ask for confirmation (except for testing deployments) | |
if test \! "$TARGET_SYSTEM" = "testing" | |
then | |
echo " Enter \"yes\" in order to commence the deployment." | |
echo -n "(yes/no)? " | |
read YESNO | |
if test \! "yes" = "$YESNO" | |
then | |
echo "USER ABORTED PROCESS." | |
exit 0 | |
fi | |
fi | |
if test -f "$LOCK_FILE" | |
then | |
echo "ERROR: Lock file $LOCK_FILE exists, a deployment still running?" | |
echo "FATAL ERROR - ABORTING!" | |
exit 1 | |
else | |
touch $LOCK_FILE | |
fi | |
echo -n "Cleaning $TEMP_DIR directory ... " | |
rm -rf $TEMP_DIR/* || | |
(echo "FATAL ERROR - ABORTING!" && exit 1) | |
echo "OK" | |
echo "Creating $TARGET_SYSTEM backups ..." | |
for DIR in $DIR_LIST | |
do | |
echo -n " copying: $DIR to $TEMP_DIR ... " | |
cp -a $TARGET_SYSTEM/$DIR $TEMP_DIR/${DIR} || | |
(echo "FATAL ERROR - ABORTING!" && exit 1) | |
echo "OK" | |
done | |
echo " DONE"; | |
echo "Preparing new directories from $SOURCE_SYSTEM ..." | |
for DIR in $DIR_LIST | |
do | |
echo -n " copying: $DIR to $TEMP_DIR/$DIR.new ... " | |
cp -a $SOURCE_SYSTEM/$DIR $TEMP_DIR/${DIR}.new || | |
(echo "FATAL ERROR - ABORTING!" && exit 1) | |
echo "OK" | |
done | |
echo " DONE"; | |
echo "Switching to new version..." | |
for DIR in $DIR_LIST | |
do | |
echo -n " moving: $TEMP_DIR/$DIR.new to $DIR ... " | |
mv --no-target-directory $TARGET_SYSTEM/$DIR $TARGET_SYSTEM/${DIR}.old && | |
mv --no-target-directory $TEMP_DIR/$DIR.new $TARGET_SYSTEM/${DIR} || | |
(mv --no-target-directory $TARGET_SYSTEM/${DIR}.old $TARGET_SYSTEM/$DIR && | |
echo "FATAL ERROR - ABORTING!" && exit 1) | |
echo "OK" | |
done | |
echo " DONE"; | |
echo "Cleaning up backup directories..." | |
for DIR in $DIR_LIST | |
do | |
echo -n " deleting: $DIR.old ... " | |
rm -rf $TARGET_SYSTEM/${DIR}.old || | |
( echo "FATAL ERROR - ABORTING!" && exit 1 ) | |
echo "OK" | |
done | |
cd $CWD | |
rm $LOCK_FILE | |
echo " DONE"; | |
echo "SUCCESS: PROCESS FINISHED OK!"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment