#!/bin/sh # Core Deploy Script Functionality # Written by Jason LaPorte (jason@agoragames.com) # # Copyright (C) 2009 Agora Games, Inc. # # This software is provided 'as-is', without any express or implied warranty. # In no event will the authors be held liable for any damages arising from the # use of this software. # # Permission is granted to anyone to use this software for any purpose, # including commercial applications, and to alter it and redistribute it # freely, subject to the following restrictions: # # 1. The origin of this software must not be misrepresented; you must not # claim that you wrote the original software. If you use this software # in a product, an acknowledgment in the product documentation would be # appreciated but is not required. # 2. Altered source versions must be plainly marked as such, and must not be # misrepresented as being the original software. # 3. This notice may not be removed or altered from any source distribution. if [ ! $LOGFILE ]; then echo "You must define LOGFILE in your script before sourcing the deploy core." exit 1 fi # define how to log messages and handle rollbacks. ROLLBACK= log () { echo "[`date '+%H:%M %D'`]" $@ >>$LOGFILE } run () { echo $1 log $1 test "$2" && ROLLBACK="$2 $ROLLBACK" $1 >>$LOGFILE 2>>$LOGFILE if [ $? != 0 ]; then echo "failed" log "failed" echo "rolling back" log "rolling back" for rollback in $ROLLBACK; do echo $rollback log $rollback $rollback >>$LOGFILE 2>>$LOGFILE if [ $? != 0 ]; then echo "also failed \(sorry today sucks. i love you, if it helps.\)" log "rollback failed" exit 2 fi done exit 1 fi } # below are core functions which are commonly used by rails apps. you can # override them by redeclaring them after sourcing this file. deploy () { svn -q export $REPOSITORY $NEW_RELEASE_DIR && \ chmod -R g+w $NEW_RELEASE_DIR } undeploy () { rm -rf $NEW_RELEASE_DIR; } symlink () { rm -f $CURRENT_DIR && \ ln -s $NEW_RELEASE_DIR $CURRENT_DIR && \ ln -s $SHARED_DIR/log $CURRENT_DIR/log && \ ln -s $SHARED_DIR/pids $CURRENT_DIR/tmp/pids } unsymlink () { rm -f $CURRENT_DIR && \ ln -s $OLD_RELEASE_DIR $CURRENT_DIR # we assume all the other symlinks are still in place from the prior deploy } migrate () { cd $CURRENT_DIR && rake db:migrate RAILS_ENV=production; } restart () { touch $SHARED_DIR/pids/restart.touch; } cleanup_old_releases () { ls -1cd $BASE/releases/* | tail -n +5 | xargs rm -rf }