Deployment script

dpeterka
dpeterka
A simple shell script to migrate code to servers based on environments and tags (although you can also set it to use the trunk).    Consists of a config file and a shell script.    Not the most tested as yet and I'm no shell expert per se, but found this handy, perhaps someone else will.    This script will get code from subversion to a temp dir, and move it into your target deployment directory, back up the old, remove the old, rename config files if needed and add a version stamp. It's handy to automate deployments and we've found using export from subversion directly to a production server can take longer than we like.    Further info can be found in the comments.        Shell Snip ----------------------------------------------    #!/bin/bash  # Script for migrating code from subversion  # David Peterka  # update.sh [-f config file] [-t tag] [-e env]    # This script will deploy code from Subversion to a directory on a server,  # it requires a correctly configured configuration file.  # Runtime options (-f, -t, -e) can be set in this configuration file, or  # alternatively can be overwritten or set each time you run it.  # This allows you to 'pre-package' the deployment so that one can  # simply do ./update.sh to deploy code, or if you don't want to edit a config  # file every bloody release you could do ./update.sh -t Build_1.0.  # If you do not set the switch -f it will look for a file called update.conf  # in the same directory where the script resides.  # You'll probably want to read through the configuration file, if you don't  # most likely you'll cause some damage that will make you want to cry.      # Get command line options  while [ $# -gt 0 ]  do   case "$1" in   -t) TAG=$2; shift;;   -f) UPDATE_CONFIG=$2; shift;;   -e) ENV=$2; shift;;   -*)   echo "$0: invalid option $1" >&2   echo "Usage: $0 [-f config file] [-t tag] [-e environment]" >&2   exit 1;;   *) break;; # terminate while loop   esac   shift  done    # Set default config file if non declared  if [ -z "$UPDATE_CONFIG" ]; then   UPDATE_CONFIG=`dirname $0`/update.conf  fi    # Load configuration options  . $UPDATE_CONFIG    # If tags are undeclared check config options and set that  # It's okay to leave this blank if you are deploying from trunk  if [ -z "$TAG" ]; then   TAG=$C_TAG  fi    # If tags are undeclared check config options and set that  if [ -z "$ENV" ]; then   ENV=$C_ENV  fi    # Set deploy environment  if [ "$ENV" = prod ]; then   DIR=$DIR_PROD  elif [ "$ENV" = qa ]; then   DIR=$DIR_QA  elif [ "$ENV" = dev ]; then   DIR=$DIR_DEV  else  echo "No valid Environment to deploy to, check config"  echo "or use -e to set the correct value"  exit 1  fi  echo "Files being deployed to: $DIR"    # Check that correct account is in use  whoami|grep $USER >/dev/null  if [ "$?" -ne 0 ]; then   echo "You should run this script as the user $USER"   exit 1  fi    # Backup  DATE=`date +%Y-%m-%d`  TAR=~/`echo $DIR|sed 's!/!!'|sed 's!/!.!g'`.$ENV.$DATE.tar  if [ -f "$TAR" ]; then   TAR=~/`echo $DIR|sed 's!/!!'|sed 's!/!.!g'`.$ENV.$DATE.$$.tar  else  tar -cf $TAR $DIR 2>&1 | grep -v "Removing leading"  fi  echo "Old install backed up to: $TAR"    #Checkout files to temp directory  $SVN $TYPE ${SVN_ARG}"$SVN_USER" ${SVN_BASE}${PROJ_BASE}${PROJ}${TAG} $TMP    # Clean up  # Print stuffage to a file  # cludge  find $DIR/* > /dev/null 2>&1  CLUDGE=$?  if [ "$CLUDGE" -eq 0 ]; then  find $DIR/* -prune |xargs -n 1 basename > $TMP/delete  find $TMP/* -prune |xargs -n 1 basename >> $TMP/delete    # Delete the old stuff  cd $DIR  echo "Beginning delete------------------" > ~/install.log  sort $TMP/delete|uniq -d|xargs rm -rv >> ~/install.log  else  echo "Target dir empty------------------" > ~/install.log  fi    # Deploy code  echo "Beginning copy-------------------" >> ~/install.log  cp -rv $TMP/* $DIR >> ~/install.log    # Add version stamp  if [ -z "$TAG" ]; then   echo "not a tagged release" > $DIR/version.txt  else   echo $TAG > $DIR/version.txt  fi    # Set config files to servers  while [ "$i" -gt -1 ]  do  echo "Updating configs-----------------" >> ~/install.log  cp -v $TMP/${BASE[$i]}${HOSTNAME%%.*}${EXT[$i]} $DIR/${BASE[$i]}${CONF[i]}${EXT[i]} >> ~/install.log  set $((i--))  done    rm -rf $TMP    echo "Install complete"  echo "Install log is in: ~/install.log"  exit 0      Config snip----------------------------------------------    # Variables for update.sh  # David Peterka    # Tag - tag name or number as created in the 'tags' branch of Subversion  # If you are deploying from trunk or want this declared at runtime,  # leave this blank  C_TAG=      # Environment - Case sensitive variable for your environment  # set this to any of these: prod,qa dev  # if you want this declared at runtime leave this blank    C_ENV=prod    # File paths - Where your code lives  # List your directories for each Environment (see above)  DIR_PROD=/opt/wiki  DIR_QA=  DIR_DEV=/opt/wikitest    # Temp directory - where files are exported to from subversion  TMP=/tmp/$$    # User account  # Enter the shell account this script should run as  USER=username    # Subversion info  # Type- export or checkout(co)  TYPE=export  # Subversion path  # This is chopped up so hopefully you can easily build your path as needed  # The variables are assembled like so:  # SVN_BASE PROJ_BASE PROJ    PROJ='projname/'  SVN_BASE='http://svnrepo.mydomain.com/'  PROJ_BASE='path/to/repoOrTrunk/'    # SVN user - valid active directory account  # I should probably set this so one can enter at runtime  SVN_USER="User Name"  SVN_ARG='-q --username '  SVN=/usr/bin/svn    # Configuration files  # Any file that has to be changed because of server specific settings  # should be listed here. For each config file make a copy with the server name  # as the file name plus an extension i.e. eu-apps-ux03.php.  # do a propset ignore on the actual file to avoid confusion.  # Then enter the path (relative to the File Paths above)  # separated by a space, the extension separated by a space  # and the file name separated by a space for each of the  # variables below.  # set i to indicate the number of config files to move  # i starts from 0, so i=1 would be to move two config files  # If you don't have any special files set i=-1 and leave the other variables as is.    i=1  BASE=(path/to/config1/ path/to/config2/)  EXT=(.php .js)  CONF=(somefilename someotherfilename)    End Config Snip-----------------------------------------------

1-2 of 2

Reply to this discussion

You cannot edit posts or make replies: You should be logged in before you can post.