Created
April 12, 2011 16:36
-
-
Save samsoir/915861 to your computer and use it in GitHub Desktop.
Rebuild script for the OU Annotate Staging Server
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 | |
# +--------------------------------------------------------------------------+ | |
# | OU Annotate Staging Rebuild Script | | |
# | Written by Sam de Freyssinet <[email protected]> | | |
# +--------------------------------------------------------------------------+ | |
# | Permission to use, copy, modify, and/or distribute this software for any | | |
# | purpose with or without fee is hereby granted, provided that the above | | |
# | copyright notice and this permission notice appear in all copies. | | |
# +--------------------------------------------------------------------------+ | |
# | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | | |
# | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | | |
# | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | | |
# | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | | |
# | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | | |
# | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | | |
# | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | | |
# +--------------------------------------------------------------------------+ | |
E_HELP_TEXT=1 | |
E_BAD_ARGS=65 | |
E_BAD_USER=10 | |
E_UNABLE_TO_CREATE_GIT_FOLDER=11 | |
E_DATABASE_DROP_FAILED=20 | |
E_DATABASE_CREATE_FAILED=21 | |
E_GIT_ORIGIN_PULL_FAIL=30 | |
E_GIT_ORIGIN_SUBMODULE_FAIL=31 | |
E_UNABLE_TO_CHECKOUT_GIT_BRANCH=32 | |
E_APPLY_ENV_SETTINGS=40 | |
# check for root user | |
if [ `whoami` != "root" ] | |
then | |
echo "This script must be run as root user!" | |
exit $E_BAD_USER; | |
fi | |
ANNOTATE_DATABASE_NAME='annotate' | |
ANNOTATE_MYSQL_ADMINISTRATOR_USER='root' | |
ANNOTATE_GITHUB_ORIGIN='[email protected]:ibuildings/OU-Annotate.git' | |
ANNOTATE_GIT_REMOTE='origin' | |
ANNOTATE_GIT_CODE='/var/www/OU-Annotate/current' | |
ANNOTATE_VIRTUAL_HOST='staging.openuniversity.ibuildings.com' | |
ANNOTATE_VHOST_USER='sdefreyssinet:PmhF7ro5' | |
SS_USE_BRANCH='2.4' | |
SS_MSSQL_BRANCH='master' | |
drop_annotate_db=0 | |
flush_annotate_app=0 | |
git_branch='!!none!!' | |
# help text | |
help_text() | |
{ | |
echo "usage: $0 <options> branch" | |
echo "" | |
echo "options:" | |
echo " -d drop annotate database" | |
echo " -f flush silverstripe to rebuild" | |
echo " -h this help text" | |
echo "" | |
echo "branch:" | |
echo " define the branch to pull from" | |
echo "" | |
echo "example: to drop database, flush silverstripe and pull from sprint/1" | |
echo " $0 -d -f sprint/1" | |
echo "" | |
} | |
# process options | |
process_options() | |
{ | |
for arg in "$@" | |
do | |
# test for options flag | |
test_argument=$(echo "$arg" | awk '/^\-.*+$/') | |
# if test argument is not empty | |
if [ -n "$test_argument" ] | |
then | |
case "$test_argument" in | |
-d) drop_annotate_db=1; shift;; | |
-f) flush_annotate_app=1; shift;; | |
-h) help_text; exit $E_HELP_TEXT;; | |
*) help_text; exit $E_BAD_ARGS;; | |
esac | |
else | |
# set branch | |
git_branch="$arg" | |
echo "Branch set to '$git_branch'" | |
fi | |
done | |
if [ "$git_branch" == "!!none!!" ] | |
then | |
help_text | |
exit $E_BAD_ARGS | |
fi | |
return 0 | |
} | |
# httpd control | |
httpd_ctl() | |
{ | |
service httpd $1 > /dev/null | |
} | |
# drop database | |
drop_database() | |
{ | |
default_char_set="--default-character-set=utf8" | |
echo "Dropping database: $ANNOTATE_DATABASE_NAME..." | |
mysqladmin drop -f $ANNOTATE_DATABASE_NAME | |
if [ "$?" -ne 0 ] | |
then | |
echo "Unable to drop database: $ANNOTATE_DATABASE_NAME" | |
exit $E_DATABASE_DROP_FAILED | |
fi | |
echo "[DONE]" | |
echo "Creating database: $ANNOTATE_DATABASE_NAME..." | |
mysqladmin create $default_char_set $ANNOTATE_DATABASE_NAME | |
if [ "$?" -ne 0 ] | |
then | |
echo "Unable to create database: $ANNOTATE_DATABASE_NAME" | |
exit $E_DATABASE_CREATE_FAILED | |
fi | |
echo "[DONE]" | |
return 0 | |
} | |
# rebuild the application | |
flush_application() | |
{ | |
echo "Flushing the Annotate application..." | |
uri="$ANNOTATE_VIRTUAL_HOST/dev/build?flush=true" | |
curl -v --digest -u $ANNOTATE_VHOST_USER $uri | |
echo "[DONE]" | |
return 0 | |
} | |
# get code from git repository | |
update_code() | |
{ | |
echo "Updating git code from branch: $git_branch..." | |
if [ -d $ANNOTATE_GIT_CODE ] | |
then | |
cd $ANNOTATE_GIT_CODE | |
else | |
mkdir -p $ANNOTATE_GIT_CODE | |
if [ "$?" -ne 0 ] | |
then | |
echo "Unable to create directory: $ANNOTATE_GIT_CODE" | |
exit $E_UNABLE_TO_CREATE_GIT_FOLDER | |
fi | |
cd $ANNOTATE_GIT_CODE | |
fi | |
#check for git repository | |
if [ -d "$ANNOTATE_GIT_CODE/.git" ] | |
then | |
echo "Detected current git repository in $ANNOTATE_GIT_CODE..." | |
else | |
echo "No git repository found, initialising git repository in $ANNOTATE_GIT_CODE..." | |
git init | |
if [ "$?" -ne 0 ] | |
then | |
echo "Unable to init git repository in: $ANNOTATE_GIT_CODE" | |
exit $E_UNABLE_TO_CREATE_GIT_FOLDER | |
fi | |
git remote add origin $ANNOTATE_GITHUB_ORIGIN | |
fi | |
echo "Current git branch..." | |
# Check for current branch | |
current_branch=`git name-rev --name-only HEAD` | |
# If we're not on the correct branch | |
if [ $current_branch == $git_branch ] | |
then | |
echo "Updating git branch: $git_branch" | |
# update the current branch | |
git pull origin $git_branch | |
if [ "$?" -ne 0 ] | |
then | |
echo "Unable to pull from origin branch: $git_branch" | |
exit $E_GIT_ORIGIN_PULL_FAIL | |
fi | |
else | |
echo "Switching to git branch: $git_branch" | |
git checkout -B $git_branch origin/$git_branch | |
if [ "$?" -ne 0 ] | |
then | |
echo "Unable to switch to git branch: $git_branch" | |
exit $E_UNABLE_TO_CHECKOUT_GIT_BRANCH | |
fi | |
fi | |
echo "[DONE]" | |
echo "Recreating local environment configuration files..." | |
# tidy up | |
cat app/_ss_environment.php.vm.dist > app/_ss_environment.php | |
cat app/www/annotate/_config.php.vm.dist > app/www/annotate/_config.php | |
if [ "$?" -ne 0 ] | |
then | |
echo "Unable apply local environment configuration files!" | |
exit $E_APPLY_ENV_SETTINGS | |
fi | |
echo "[DONE]" | |
# update submodules | |
echo "Updating (and initialising) submodules" | |
if [ -d "$ANNOTATE_GIT_CODE/app/www/sapphire/.git" ] | |
then | |
echo "SAPPHIRE submodule already initialised..." | |
else | |
git submodule update --init app/www/sapphire | |
if [ "$?" -ne 0 ] | |
then | |
echo "Unable initialise submodule: app/www/sapphire" | |
exit $E_GIT_ORIGIN_SUBMODULE_FAIL | |
fi | |
fi | |
# reset and update blackcandy to whichever branch we should be on | |
cd $ANNOTATE_GIT_CODE/app/www/sapphire | |
git fetch -q | |
git checkout -B $SS_USE_BRANCH origin/$SS_USE_BRANCH | |
if [ "$?" -ne 0 ] | |
then | |
echo "Unable initialise submodule: app/www/sapphire" | |
exit $E_GIT_ORIGIN_SUBMODULE_FAIL | |
fi | |
if [ -d "$ANNOTATE_GIT_CODE/app/www/cms/.git" ] | |
then | |
echo "CMS submodule already initialised..." | |
else | |
git submodule update --init app/www/cms | |
if [ "$?" -ne 0 ] | |
then | |
echo "Unable initialise submodule: app/www/cms" | |
exit $E_GIT_ORIGIN_SUBMODULE_FAIL | |
fi | |
fi | |
# reset and update blackcandy to whichever branch we should be on | |
cd $ANNOTATE_GIT_CODE/app/www/cms | |
git fetch -q | |
git checkout -B $SS_USE_BRANCH origin/$SS_USE_BRANCH | |
if [ "$?" -ne 0 ] | |
then | |
echo "Unable initialise submodule: app/www/cms" | |
exit $E_GIT_ORIGIN_SUBMODULE_FAIL | |
fi | |
if [ -d "$ANNOTATE_GIT_CODE/app/www/silverstripe-mssql/.git" ] | |
then | |
echo "SILVERSTRIPE-MSSQL submodule already initialised..." | |
else | |
git submodule update --init app/www/silverstripe-mssql | |
if [ "$?" -ne 0 ] | |
then | |
echo "Unable initialise submodule: app/www/silverstripe-mssql" | |
exit $E_GIT_ORIGIN_SUBMODULE_FAIL | |
fi | |
fi | |
# reset and update blackcandy to whichever branch we should be on | |
cd $ANNOTATE_GIT_CODE/app/www/silverstripe-mssql | |
git fetch -q | |
git checkout -B $SS_MSSQL_BRANCH origin/$SS_MSSQL_BRANCH | |
if [ "$?" -ne 0 ] | |
then | |
echo "Unable initialise submodule: app/www/silverstripe-mssql" | |
exit $E_GIT_ORIGIN_SUBMODULE_FAIL | |
fi | |
if [ -d "$ANNOTATE_GIT_CODE/app/www/themes/blackcandy/.git" ] | |
then | |
echo "BLACKCANDY submodule already initialised..." | |
else | |
git submodule update --init app/www/themes/blackcandy | |
if [ "$?" -ne 0 ] | |
then | |
echo "Unable initialise submodule: app/www/themes/blackcandy" | |
exit $E_GIT_ORIGIN_SUBMODULE_FAIL | |
fi | |
fi | |
# reset and update blackcandy to whichever branch we should be on | |
cd $ANNOTATE_GIT_CODE/app/www/themes/blackcandy | |
git fetch -q | |
git checkout -B $SS_USE_BRANCH origin/$SS_USE_BRANCH | |
if [ "$?" -ne 0 ] | |
then | |
echo "Unable initialise submodule: app/www/themes/blackcandy" | |
exit $E_GIT_ORIGIN_SUBMODULE_FAIL | |
fi | |
if [ -d "$ANNOTATE_GIT_CODE/app/bookmark/.git" ] | |
then | |
echo "BOOKMARK submodule already initialise..." | |
else | |
git submodule update --init app/bookmark | |
if [ "$?" -ne 0 ] | |
then | |
echo "Unable initialise submodule: app/bookmark" | |
exit $E_GIT_ORIGIN_SUBMODULE_FAIL | |
fi | |
fi | |
# reset and update bookmarklet to whichever branch we should be on | |
cd $ANNOTATE_GIT_CODE/app/bookmark | |
git fetch -q | |
git checkout -B $git_branch origin/$git_branch | |
if [ "$?" -ne 0 ] | |
then | |
echo "Unable initialise submodule: app/bookmark" | |
exit $E_GIT_ORIGIN_SUBMODULE_FAIL | |
fi | |
echo "[DONE]" | |
return 0 | |
} | |
# if no args supplied, show help text | |
if [ $# == 0 ] | |
then | |
help_text | |
exit $E_BAD_ARGS | |
fi | |
# process cmd line options | |
process_options $@ | |
# stop httpd | |
httpd_ctl 'stop' | |
# do work | |
if [ "$drop_annotate_db" -eq 1 ] | |
then | |
drop_database | |
fi | |
# pull | |
update_code | |
# start httpd | |
httpd_ctl 'start' | |
# stop for five seconds while HTTPD sorts itself out | |
sleep 5 | |
if [ "$flush_annotate_app" -eq 1 ] | |
then | |
flush_application | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment