Advance SVN post-commit hook script for System Administrators

Everyone knows how cool svn post-commit feature is. Instead of using the basic post-commit script provided by svn, we can do alot more with a bit of server side scripting. Here, I like to share a simple script that I wrote to automate the process of updating different server system files using svn post-commit. It works for me… it might work for you as well. There are of course alot of areas that needs improvement.

The first thing to do is to have the svn repo setup with the server name as the top dir and all sub directories mirror exactly the same way as the system dir. I have 2 servers here, ares.stag and zeus.dev for example:

server_config
|-- ares.stag
|   `-- home
|       `-- data
|               `-- vhost.ares.conf
|-- zeus.dev
|   |-- mutt
|   |   `-- muttrc
|   |-- vim
|   |   `-- vimrc
|   `-- xen
|   |-- var
|       `-- named
|           `-- chroot
|               |-- etc
|               |   `-- named.conf
|               `-- var
|                   `-- named
|                       |-- 115.2.10.in-addr.arpa
|                       |-- linux.dev.db
|                       |-- linux.live.db
|                       `-- linux.stag.db

Everytime I want to edit my stag dns for example, I dont need to manually do it in the server, I just need to edit linux.stag.db in my own desktop, then “svn commit”. The commit script is responsible to then put the files that I committed to the right place for me.

#!/bin/sh

# - This is a more advanced svn post-commit script.
# - This is currently used to rollover server config.
# - The script attempts to copy files from different server based on the
# current svn dir hierarchy.
# - ssh keys from user@{current_server} to root@{external_server} must exists.
# - The file in the server must exists else this script will fail.
# - It rolls over modified files, it doesn't delete file.
#
# author: Bernard Peh
# date: 16 April 2010
#
# version 1.0
# script created.

REPOS="$1"
REV="$2"

# define your admin for email alert
EMAIL="youremail@youremail.com"

# define log. Leave it as default if you want
TMP=/tmp/$$
LOG=/tmp/$$_log
USERLOG=/tmp/$$_userlog

# output svn details into log
svn log -v --xml -r$REV file://$REPOS > $LOG

# full path to command
WALL=/usr/bin/wall
SVN=/usr/bin/svn
SED=/bin/sed
AWK=/bin/awk
GREP=/bin/grep
CAT=/bin/cat
SSH=/usr/bin/ssh

# only copy committed files that are new or modified
$CAT $LOG | $GREP ' action=' | while read x; do SERVER=`echo $x | $AWK -F/ '{print $2}'`; RES=`echo $x | $SED 's/action=\"\(.*\)\">\/'$SERVER'\(.*\)<\/path>/\1 \2/'`; PATH=`echo $RES | $GREP '^\(A\|M\)'`; PATH=`if [ ! -z "$PATH" ]; then echo $PATH | $AWK '{print $2}'; fi;`; $SVN cat file://${REPOS}/${SERVER}${PATH} 2>/dev/null | $SSH root@$SERVER "cat - > $PATH" 2>> $USERLOG; if [ !$? ]; then echo "Successfully updated ${SERVER}:${PATH}" >> $USERLOG; fi; done

# mail to admin
$CAT "$USERLOG" | mail -s "$REPOS updated to rev $REV" $EMAIL

# clean up
rm -rf $TMP
rm -rf $LOG
rm -rf $USERLOG

# all good now exit
exit 0;
Like it.? Share it:
Tags:

One Response to Advance SVN post-commit hook script for System Administrators

  1. speeding

    Would you mind enabling rss feeds, because this page is difficult to read on my phone. Don’t mean to be a complainer, but I figure if it would help me it would probably help others as well. Thanks šŸ™‚