source: misc/scripts/continuous_integration.sh @ 1081

Last change on this file since 1081 was 1081, checked in by work@…, 10 years ago
  • updated CI script to support new build information
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Rev
File size: 8.6 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: work@osx.eu $
26# $Date: 2010-11-04 15:28:24 +0000 (do, 04 nov 2010) $
27# $Rev: 1081 $
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 )
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 setting build information variables in application.properties
209TIMESTAMP=`date +'%s'`
210sed -i 's/app\.build\.type=\w\+/app.build.type=ci/gi' application.properties
211sed -i 's/app\.build\.display\.info=\w\+/app.build.display.info=1/gi' application.properties
212sed -i 's/app\.build\.timestamp=\w\+/app.build.timestamp='$TIMESTAMP'/gi' application.properties
213sed -i 's/app\.build\.svn\.revision=\w\+/app.build.svn.revision='$SVN_REVISION'/gi' application.properties
214
215# patch datasource.groovy
216echo $STAMP patching datasource for ci instance
217sed -i 's/5432\/'$PROJECT'/5432\/'$PROJECT'-ci/g' grails-app/conf/DataSource.groovy
218
219# adding patch for OAuth configuration file (consumer key and consumer secret generated by myExperiment).
220if [ "$PROJECT" = "gscf" ]
221then
222        echo $STAMP GSCF project given as argument, patching Config.groovy with OAuth [key:$OAUTHKEY, SECRET:$OAUTHSECRET]...
223        sed -i 's/\$oauthkey\$/'$OAUTHKEY'/gi' grails-app/conf/Config.groovy
224        sed -i 's/\$oauthsecret\$/'$OAUTHSECRET'/gi' grails-app/conf/Config.groovy
225fi
226
227# build new war
228PWD=$(pwd)
229echo $STAMP current workdirectory: $PWD
230echo $STAMP building new war...
231grails prod war --non-interactive > /dev/null
232
233# undeploying gscf releases
234echo $STAMP undeploying $PROJECT-$OLD_APP_VERSION-ci
235curl --silent --user $MANAGER_USER:$MANAGER_PASSWORD --url $MANAGER_URL/$PROJECT-$OLD_APP_VERSION-ci > /dev/null
236sleep 5
237
238# deploying new build
239echo $STAMP deploying new production WAR
240cp target/$PROJECT-$APP_VERSION.war $PATH_TOMCAT_WEBAPPS/$PROJECT-$APP_VERSION-ci.war
241
242# fix virtual host files
243echo $STAMP updating apache virtual host configuration
244ls $APACHE_VHOST_FILES|xargs -i sed -i 's/'$PROJECT'-[0-9]\.[0-9]\.[0-9]/'$PROJECT'-'$APP_VERSION'/g' {}
245
246# restart apache
247# (make sure sudoers is configured properly. On NBX-es
248#  this does not work as it uses shared modules that
249#  -somehow- make the sudo commands fail)
250echo $STAMP reloading Apache
251$APACHE_RESTART > /dev/null 2>&1
252
253# update revision file
254echo $SVN_REVISION > $TEMP/$PROJECT.revision
255
256# remove lock file
257if [ -f $TEMP/$PROJECT.locked ]
258then
259        rm $TEMP/$PROJECT.locked
260fi
261
262echo $STAMP" done deploying "$PROJECT" revision "$SVN_REVISION" (version "$APP_VERSION")"
Note: See TracBrowser for help on using the repository browser.