source: misc/scripts/continuous_integration.sh @ 1026

Last change on this file since 1026 was 1026, checked in by j.a.m.wesbeek@…, 10 years ago
  • changed the CI script so that it works grails independently. This means that it will adapt the grails runtime environment to the version the application is using. Hence, when an application is using grails 1.3.4 the grails runtime will point to /app/grails-1.3.4 and when 1.3.5 is used the runtime at /app/grails-1.3.5 will be used. This will allow continious integration to work properly for applications using different versions of grails. However, the latest will always be simlinked to /app/grails
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Rev
File size: 8.5 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 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: j.a.m.wesbeek@umail.leidenuniv.nl $
26# $Date: 2010-11-01 10:52:32 +0000 (ma, 01 nov 2010) $
27# $Rev: 1026 $
28
29if [ $1 ]; then
30# set project
31        PROJECT=$1
32else
33        echo "Usage: $0 <projectname>"
34        exit;
35fi
36
37# Platform specific configuration
38if [ -f /etc/gentoo-release ]; then
39        # GENTOO
40        export JAVA_HOME=/etc/java-config-2/current-system-vm
41        GRAILS_INSTALL_PATH=$HOME
42        PATH_TOMCAT_WEBAPPS=/var/lib/tomcat-6/webapps/
43        APACHE_VHOST_FILES=/etc/apache2/vhosts.d/dbnp.org_ci.conf
44        TOMCAT_STOP="sudo /etc/init.d/tomcat-6 stop"
45        TOMCAT_START="sudo /etc/init.d/tomcat-6 start"
46        APACHE_RESTART="sudo /etc/init.d/apache2 graceful"
47elif [ -f /etc/debian_version ]; then
48        # DEBIAN
49        export JAVA_HOME=/usr/lib/jvm/java-6-sun
50        GRAILS_INSTALL_PATH=/app
51        PATH_TOMCAT_WEBAPPS=/home/tomcat/apache-tomcat/webapps
52        APACHE_VHOST_FILES=/etc/apache2/sites-available/nmcdsp.org_$PROJECT-ci.conf
53        TOMCAT_STOP="/home/tomcat/apache-tomcat/bin/shutdown.sh"
54        TOMCAT_START="/home/tomcat/apache-tomcat/bin/startup.sh"
55        APACHE_RESTART="sudo /etc/init.d/apache2 restart"
56else
57        echo Could not determine platform....
58        exit
59fi
60
61# GENERIC CONFIGURATION
62export CATALINA_OPTS="-Xms4096m -Xmx4096m -XX:MaxPermSize=2048m -XX:MaxHeapFreeRatio=70 -XX:MaxGCPauseMillis=10 -XX:+UseConcMarkSweepGC"
63#export GRAILS_HOME=$GRAILS_INSTALL_PATH/grails
64export JDK_HOME=$JAVA_HOME
65export JAVAC=$JAVA_HOME/bin/javac
66#export PATH=$GRAILS_HOME/bin:$PATH
67STAMP=`date +'%Y%m%d%H%M'`
68USER=`/usr/bin/whoami`
69PATH_GRAILS_SOURCE=$HOME/workspace/$PROJECT
70TEMP=$HOME/tmp
71SVN=`which svn`
72DEV_PLUGINS=( db-util nadd-neutralizer grom )
73MANAGER_URL=http://manager.nmcdsp.org/manager/html/undeploy?path=
74MANAGER_USER=ci
75MANAGER_PASSWORD=berterni3
76# Keys registered by t.w.abma[at]umcutrecht address (gscf.nmcdsp.org)
77OAUTHKEY=Xhu1RtLtB43yIy29EYw
78OAUTHSECRET=gYO5ps6JLDMEdJLOQkSBoTTbq11s8CrORqvrBkc6k
79
80# Keys registered by t.w.abma[at]gmail address (gscf.nmcdsp.org)
81#OAUTHKEY=M07bnXDthzcbWD7KKSlDYw
82#OAUTHSECRET=odfnT08AUBYhh2z0PVMS9JT11D2jASwGNzAj9YFk7s
83
84# are we user tomcat?
85if [ $USER != 'tomcat' ]
86then
87        echo $STAMP "ERROR: you need to run this script as user tomcat..."
88        exit;
89fi
90
91# is the update locked?
92if [ -f $TEMP/$PROJECT.locked ]; then
93        echo $STAMP project deployment is still happening
94        exit;
95fi
96
97# lock deployment
98touch $TEMP/$PROJECT.locked
99
100# get svn revision of HEAD and running application
101cd $PATH_GRAILS_SOURCE
102OLD_APP_VERSION=`sed -n 's/app.version=\(.*\)$/\1/p' <  application.properties`
103$SVN revert --recursive --quiet *
104#$SVN update --quiet *
105#SVN_REVISION=`$SVN update .| sed -n 's/At revision \(.*\)\./\1/p'`
106SVN_REVISION=`$SVN update|sed -n 's/At revision \(.*\)\./\1/p'`
107RUNNING_REVISION=`cat $TEMP/$PROJECT.revision`
108
109# got an SVN revision?
110if [ "$SVN_REVISION" = "" ]; then
111        echo $STAMP haven\'t got an svn revision?
112        rm $TEMP/$PROJECT.locked
113        exit;
114fi
115
116# is a new revision available?
117if [ "$RUNNING_REVISION" == "$SVN_REVISION" ]; then
118        #echo $STAMP do nothing...
119        rm $TEMP/$PROJECT.locked
120        exit;
121fi
122
123# a new version is available, get application and
124# grails version of HEAD
125APP_VERSION=`sed -n 's/app.version=\(.*\)$/\1/p' <  application.properties`
126GRAILS_VERSION=`sed -n 's/app.grails.version=\(.*\)$/\1/p' <  application.properties`
127
128# feedback
129echo $STAMP newer version of $PROJECT is available
130echo $STAMP "deploying revision "$SVN_REVISION" (version "$APP_VERSION" / Grails "$GRAILS_VERSION") and undeploying revision "$RUNNING_REVISION"..."
131
132# create minified JS and CSS if required
133MINIFIED=`ls web-app/js/*.js web-app/css/*.css|grep -vi min.|xargs -i ~/scripts/minify_if_changed.sh {} 1|wc -l`
134if [ $MINIFIED -gt 0 ]; then
135        echo $STAMP "one or more JS / CSS files were minified, committing changes..."
136        $SVN commit -m "Automated continuous integration commit of minified JS/CSS" web-app/css/*.min.css web-app/js/*.min.js > /dev/null
137        SVN_REVISION=`$SVN update | sed -n 's/At revision \(.*\)\./\1/p'`
138        echo $STAMP "updated SVN revision to "$SVN_REVISION
139fi 
140
141# see if this grails version is available
142if [ ! -d $GRAILS_INSTALL_PATH/grails-$GRAILS_VERSION ]
143then
144        echo $STAMP "grails version "$GRAILS_VERSION" is not installed."
145
146        cd $GRAILS_INSTALL_PATH
147        ls -1 ~|grep grails|grep zip|xargs -i rm ~/{}
148       
149        echo $STAMP "downloading grails release "$GRAILS_VERSION"... (this might take a while)"
150        wget -q "http://dist.codehaus.org/grails/grails-"$GRAILS_VERSION".zip"
151
152        # success?
153        if [ ! -f "grails-"$GRAILS_VERSION".zip" ]
154        then
155                echo $STAMP "could not download and install grails "$GRAILS_VERSION
156                echo $STAMP "do it manually instead and update the symbolic link..."
157
158                # remove lock file
159                if [ -f $TEMP/$PROJECT.locked ]
160                then
161                        rm $TEMP/$PROJECT.locked
162                fi
163                exit;
164        fi
165
166        # unzip archive
167        echo $STAMP "unzipping grails release "$GRAILS_VERSION
168        unzip -qq "grails-"$GRAILS_VERSION".zip"
169
170        # update symlink
171        echo $STAMP "updating symbolic link"
172        rm grails
173        ln -s "grails-"$GRAILS_VERSION grails
174
175        # switch to source dir
176        cd $PATH_GRAILS_SOURCE
177
178        GRAILS_UPGRADED=1
179else
180        GRAILS_UPGRADED=0
181fi
182
183# export environment variables so that the this CI script uses
184# the grails version for this specific project and hence is
185# grails version independent...
186export GRAILS_HOME=$GRAILS_INSTALL_PATH/grails-$GRAILS_VERSION
187export PATH=$GRAILS_HOME/bin:$PATH
188
189# upgrade project to new grails version?
190if [ $GRAILS_UPGRADED -eq 1 ]; then
191        echo ".upgrade grails project"
192        grails upgrade --non-interactive --quiet
193fi
194
195# uninstall development plugins
196index=0
197while [ "$index" -lt "${#DEV_PLUGINS[@]}" ]
198do
199        PLUGIN_INSTALLED=`find $HOME/.grails/$GRAILS_VERSION/projects/$PROJECT/plugins/ -maxdepth 1|grep -i ${DEV_PLUGINS[index]}|wc -l`
200        if [ "$PLUGIN_INSTALLED" -gt "0" ]; then
201                echo $STAMP uninstalling ${DEV_PLUGINS[index]} plugin
202                grails uninstall-plugin ${DEV_PLUGINS[index]} > /dev/null
203        fi
204        ((index++))
205done
206
207# patch index page to contain revision information
208echo $STAMP patching index page...
209sed -i 's/<\/b>/<\/b> \(<font color="red">svn revision: '$SVN_REVISION', deployed at: '$STAMP'<\/font>\)/gi' grails-app/views/home/index.gsp
210sed -i 's/Welcome to '$PROJECT' version/Welcome to the <b>continuous integration build<\/b> of <b>'$PROJECT'<\/b>/gi' grails-app/views/home/index.gsp
211
212# patch datasource.groovy
213echo $STAMP patching datasource for ci instance
214sed -i 's/5432\/'$PROJECT'/5432\/'$PROJECT'-ci/g' grails-app/conf/DataSource.groovy
215
216# adding patch for OAuth configuration file (consumer key and consumer secret generated by myExperiment).
217if [ "$PROJECT" = "gscf" ]
218then
219        echo $STAMP GSCF project given as argument, patching Config.groovy with OAuth [key:$OAUTHKEY, SECRET:$OAUTHSECRET]...
220        sed -i 's/\$oauthkey\$/'$OAUTHKEY'/gi' grails-app/conf/Config.groovy
221        sed -i 's/\$oauthsecret\$/'$OAUTHSECRET'/gi' grails-app/conf/Config.groovy
222fi
223
224# build new war
225PWD=$(pwd)
226echo $STAMP current workdirectory: $PWD
227echo $STAMP building new war...
228grails prod war --non-interactive > /dev/null
229
230# undeploying gscf releases
231echo $STAMP undeploying $PROJECT-$OLD_APP_VERSION-ci
232curl --silent --user $MANAGER_USER:$MANAGER_PASSWORD --url $MANAGER_URL/$PROJECT-$OLD_APP_VERSION-ci > /dev/null
233sleep 5
234
235# deploying new build
236echo $STAMP deploying new production WAR
237cp target/$PROJECT-$APP_VERSION.war $PATH_TOMCAT_WEBAPPS/$PROJECT-$APP_VERSION-ci.war
238
239# fix virtual host files
240echo $STAMP updating apache virtual host configuration
241ls $APACHE_VHOST_FILES|xargs -i sed -i 's/'$PROJECT'-[0-9]\.[0-9]\.[0-9]/'$PROJECT'-'$APP_VERSION'/g' {}
242
243# restart apache
244# (make sure sudoers is configured properly. On NBX-es
245#  this does not work as it uses shared modules that
246#  -somehow- make the sudo commands fail)
247echo $STAMP reloading Apache
248$APACHE_RESTART > /dev/null 2>&1
249
250# update revision file
251echo $SVN_REVISION > $TEMP/$PROJECT.revision
252
253# remove lock file
254if [ -f $TEMP/$PROJECT.locked ]
255then
256        rm $TEMP/$PROJECT.locked
257fi
258
259echo $STAMP" done deploying "$PROJECT" revision "$SVN_REVISION" (version "$APP_VERSION")"
Note: See TracBrowser for help on using the repository browser.