source: misc/scripts/continuous_integration.sh @ 932

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