source: misc/scripts/gscf_continuous_integration.sh @ 800

Last change on this file since 800 was 800, checked in by duh, 12 years ago
  • changed script to tomcat before building new war to free resources
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Rev
File size: 6.3 KB
Line 
1#!/bin/sh
2# Continuous integration build script
3#
4# What it does:
5# - checkout the latest source code from svn
6# - check if the grails version matches (if not, it downloads and
7#   installs that particular grails release to /home/tomcat and
8#   updates the symbolic link ~/grails to the new release)
9# - build a production war, undeploy the old release and deploy the
10#   new release to tomcat's webapps
11# - patch apache's virtual host configuration files in and trigger
12#   restart of apache
13#
14# Prerequisites:
15# - tomcat webabbs should be writable by user tomcat
16# - in the end this script touches a file to restart apache
17#   webserver. This particular cronjob running as root should
18#   be available as well
19# - obviously, postgres databases should be available
20#
21# @Author       Jeroen Wesbeek <J****n.W******@gmail.com>
22# @Since        20100720
23#
24# Revision Information:
25# $Author: duh $
26# $Date: 2010-08-10 21:50:02 +0000 (di, 10 aug 2010) $
27# $Rev: 800 $
28
29# GRAILS CONFIG
30export JAVA_HOME=/etc/java-config-2/current-system-vm
31export JAVAC=/etc/java-config-2/current-system-vm/bin/javac
32export JDK_HOME=/etc/java-config-2/current-system-vm
33export GRAILS_HOME=/home/tomcat/grails
34export PATH=/home/tomcat/grails/bin:$PATH
35
36# CONFIGURATION
37STAMP=`date +'%Y%m%d%H%M'`
38PROJECT=gscf
39USER=`/usr/bin/whoami`
40PATH_TOMCAT_WEBAPPS=/var/lib/tomcat-6/webapps/
41PATH_GRAILS_SOURCE=/home/tomcat/workspace/gscf
42TEMP=/home/tomcat/tmp
43SVN=`which svn`
44APACHE_VHOST_FILES=/etc/apache2/vhosts.d/dbnp.org_ci.conf
45DEV_PLUGINS=( db-util nadd-neutralizer )
46
47# are we user tomcat?
48if [ $USER != 'tomcat' ]
49then
50        echo $STAMP "ERROR: you need to run this script as user tomcat..."
51        exit;
52fi
53
54# is the update locked?
55if [ -f $TEMP/$PROJECT.locked ]
56then
57        echo $STAMP project deployment is still happening
58        exit;
59fi
60
61# lock deployment
62touch $TEMP/$PROJECT.locked
63
64# get svn revision of HEAD and running application
65cd $PATH_GRAILS_SOURCE
66$SVN revert --recursive --quiet *
67$SVN update * --quiet *
68SVN_REVISION=`$SVN update . | sed -n 's/At revision \(.*\)\./\1/p'`
69RUNNING_REVISION=`cat $TEMP/$PROJECT.revision`
70
71# got an SVN revision?
72if [ "$SVN_REVISION" = "" ];
73then
74        echo $STAMP haven\'t got an svn revision?
75        rm $TEMP/$PROJECT.locked
76        exit;
77fi
78
79# is a new revision available?
80if [ "$RUNNING_REVISION" == "$SVN_REVISION" ]
81then
82        #echo $STAMP do nothing...
83        rm $TEMP/$PROJECT.locked
84        exit;
85fi
86
87# a new version is available, get application and
88# grails version of HEAD
89APP_VERSION=`sed -n 's/app.version=\(.*\)$/\1/p' <  application.properties`
90GRAILS_VERSION=`sed -n 's/app.grails.version=\(.*\)$/\1/p' <  application.properties`
91
92# feedback
93echo $STAMP newer version of $PROJECT is available
94echo $STAMP "deploying revision "$SVN_REVISION" (version "$APP_VERSION" / Grails "$GRAILS_VERSION") and undeploying revision "$RUNNING_REVISION"..."
95
96# create minified JS and CSS if required
97MINIFIED=`ls web-app/js/*.js web-app/css/*.css|grep -vi min.|xargs -i ~/scripts/minify_if_changed.sh {} 1|wc -l`
98if [ $MINIFIED -gt 0 ]; then
99        echo $STAMP "one or more JS / CSS files were minified, committing changes..."
100        $SVN commit -m "Automated continuous integration commit of minified JS/CSS" web-app/css/*.min.css web-app/js/*.min.js > /dev/null
101        SVN_REVISION=`$SVN update | sed -n 's/At revision \(.*\)\./\1/p'`
102        echo $STAMP "updated SVN revision to "$SVN_REVISION
103fi 
104
105# see if this grails version is available
106if [ ! -d "/home/tomcat/grails-"$GRAILS_VERSION ]
107then
108        echo $STAMP "grails version "$GRAILS_VERSION" is not installed."
109
110        cd ~
111        ls -1 ~|grep grails|grep zip|xargs -i rm ~/{}
112       
113        echo $STAMP "downloading grails release "$GRAILS_VERSION"... (this might take a while)"
114        wget -q "http://dist.codehaus.org/grails/grails-"$GRAILS_VERSION".zip"
115
116        # success?
117        if [ ! -f "grails-"$GRAILS_VERSION".zip" ]
118        then
119                echo $STAMP "could not download and install grails "$GRAILS_VERSION
120                echo $STAMP "do it manually instead and update the symbolic link..."
121                exit;
122        fi
123
124        # unzip archive
125        echo $STAMP "unzipping grails release "$GRAILS_VERSION
126        unzip -qq "grails-"$GRAILS_VERSION".zip"
127
128        # update symlink
129        echo $STAMP "updating symbolic link"
130        rm grails
131        ln -s "grails-"$GRAILS_VERSION grails
132
133        # switch to source dir
134        cd $PATH_GRAILS_SOURCE
135fi
136
137# uninstall development plugins
138index=0
139while [ "$index" -lt "${#DEV_PLUGINS[@]}" ]
140do
141        PLUGIN_INSTALLED=`find /home/tomcat/.grails/$GRAILS_VERSION/projects/gscf/plugins/ -maxdepth 1|grep -i ${DEV_PLUGINS[index]}|wc -l`
142        if [ "$PLUGIN_INSTALLED" -gt "0" ]; then
143                echo $STAMP uninstalling ${DEV_PLUGINS[index]} plugin
144                grails uninstall-plugin ${DEV_PLUGINS[index]} > /dev/null
145        fi
146        ((index++))
147done
148
149# patch index page to contain revision information
150echo $STAMP patching index page...
151sed -i 's/<\/b>/<\/b> \(<font color="red">svn revision: '$SVN_REVISION', deployed at: '$STAMP'<\/font>\)/g' grails-app/views/home/index.gsp
152sed -i 's/Welcome to GSCF version/Welcome to the <b>continuous integration build<\/b> of GSCF/g' grails-app/views/home/index.gsp
153
154# stop tomcat
155# (a couple of times, sometimes it does not work
156#  properly or is slow. make sure sudoers is
157#  configured properly. On NBX-es this does not
158#  work as it uses shared modules that -somehow-
159#  make the sudo commands fail)
160echo $STAMP stopping tomcat...
161index=0
162while [ "$index" -lt "3" ]; do
163        sudo /etc/init.d/tomcat-6 stop > /dev/null
164        sleep 10
165        ((index++))
166done
167
168# build new war
169echo $STAMP building new war...
170grails prod war > /dev/null
171
172# undeploying gscf releases
173rm -rf $PATH_TOMCAT_WEBAPPS/gscf*
174
175# deploying new build
176echo $STAMP deploying new production WAR
177cp target/gscf-$APP_VERSION.war $PATH_TOMCAT_WEBAPPS
178
179# start tomcat
180# (make sure sudoers is configured properly. On NBX-es
181#  this does not work as it uses shared modules that
182#  -somehow- make the sudo commands fail)
183echo $STAMP starting tomcat...
184sudo /etc/init.d/tomcat-6 start > /dev/null
185
186# fix virtual host files
187echo $STAMP updating apache virtual host configuration
188ls $APACHE_VHOST_FILES|xargs -i sed -i 's/gscf-[0-9]\.[0-9]\.[0-9]/gscf-'$APP_VERSION'/g' {}
189
190# restart apache
191# (make sure sudoers is configured properly. On NBX-es
192#  this does not work as it uses shared modules that
193#  -somehow- make the sudo commands fail)
194echo $STAMP reloading Apache
195sudo /etc/init.d/apache2 graceful > /dev/null
196
197# update revision file
198echo $SVN_REVISION > $TEMP/$PROJECT.revision
199
200# remove lock file
201if [ -f $TEMP/$PROJECT.locked ]
202then
203        rm $TEMP/$PROJECT.locked
204fi
205
206echo $STAMP" done deploying "$PROJECT" revision "$SVN_REVISION" (version "$APP_VERSION")"
Note: See TracBrowser for help on using the repository browser.