source: misc/scripts/continuous_integration.sh @ 890

Last change on this file since 890 was 890, checked in by duh, 9 years ago

latest revision

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Rev
File size: 8.0 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: duh $
26# $Date: 2010-09-16 09:24:24 +0000 (do, 16 sep 2010) $
27# $Rev: 890 $
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 )
74
75# are we user tomcat?
76if [ $USER != 'tomcat' ]
77then
78        echo $STAMP "ERROR: you need to run this script as user tomcat..."
79        exit;
80fi
81
82# is the update locked?
83if [ -f $TEMP/$PROJECT.locked ]; then
84        echo $STAMP project deployment is still happening
85        exit;
86fi
87
88# lock deployment
89touch $TEMP/$PROJECT.locked
90
91# get svn revision of HEAD and running application
92cd $PATH_GRAILS_SOURCE
93OLD_APP_VERSION=`sed -n 's/app.version=\(.*\)$/\1/p' <  application.properties`
94$SVN revert --recursive --quiet *
95#$SVN update --quiet *
96#SVN_REVISION=`$SVN update .| sed -n 's/At revision \(.*\)\./\1/p'`
97SVN_REVISION=`$SVN update|sed -n 's/At revision \(.*\)\./\1/p'`
98RUNNING_REVISION=`cat $TEMP/$PROJECT.revision`
99
100# got an SVN revision?
101if [ "$SVN_REVISION" = "" ]; then
102        echo $STAMP haven\'t got an svn revision?
103        rm $TEMP/$PROJECT.locked
104        exit;
105fi
106
107# is a new revision available?
108if [ "$RUNNING_REVISION" == "$SVN_REVISION" ]; then
109        #echo $STAMP do nothing...
110        rm $TEMP/$PROJECT.locked
111        exit;
112fi
113
114# a new version is available, get application and
115# grails version of HEAD
116APP_VERSION=`sed -n 's/app.version=\(.*\)$/\1/p' <  application.properties`
117GRAILS_VERSION=`sed -n 's/app.grails.version=\(.*\)$/\1/p' <  application.properties`
118
119# feedback
120echo $STAMP newer version of $PROJECT is available
121echo $STAMP "deploying revision "$SVN_REVISION" (version "$APP_VERSION" / Grails "$GRAILS_VERSION") and undeploying revision "$RUNNING_REVISION"..."
122
123# create minified JS and CSS if required
124MINIFIED=`ls web-app/js/*.js web-app/css/*.css|grep -vi min.|xargs -i ~/scripts/minify_if_changed.sh {} 1|wc -l`
125if [ $MINIFIED -gt 0 ]; then
126        echo $STAMP "one or more JS / CSS files were minified, committing changes..."
127        $SVN commit -m "Automated continuous integration commit of minified JS/CSS" web-app/css/*.min.css web-app/js/*.min.js > /dev/null
128        SVN_REVISION=`$SVN update | sed -n 's/At revision \(.*\)\./\1/p'`
129        echo $STAMP "updated SVN revision to "$SVN_REVISION
130fi 
131
132# see if this grails version is available
133if [ ! -d $GRAILS_INSTALL_PATH/grails-$GRAILS_VERSION ]
134then
135        echo $STAMP "grails version "$GRAILS_VERSION" is not installed."
136
137        cd $GRAILS_INSTALL_PATH
138        ls -1 ~|grep grails|grep zip|xargs -i rm ~/{}
139       
140        echo $STAMP "downloading grails release "$GRAILS_VERSION"... (this might take a while)"
141        wget -q "http://dist.codehaus.org/grails/grails-"$GRAILS_VERSION".zip"
142
143        # success?
144        if [ ! -f "grails-"$GRAILS_VERSION".zip" ]
145        then
146                echo $STAMP "could not download and install grails "$GRAILS_VERSION
147                echo $STAMP "do it manually instead and update the symbolic link..."
148
149                # remove lock file
150                if [ -f $TEMP/$PROJECT.locked ]
151                then
152                        rm $TEMP/$PROJECT.locked
153                fi
154                exit;
155        fi
156
157        # unzip archive
158        echo $STAMP "unzipping grails release "$GRAILS_VERSION
159        unzip -qq "grails-"$GRAILS_VERSION".zip"
160
161        # update symlink
162        echo $STAMP "updating symbolic link"
163        rm grails
164        ln -s "grails-"$GRAILS_VERSION grails
165
166        # switch to source dir
167        cd $PATH_GRAILS_SOURCE
168
169        GRAILS_UPGRADED=1
170else
171        GRAILS_UPGRADED=0
172fi
173
174# stop tomcat
175# (a couple of times, sometimes it does not work
176#  properly or is slow. make sure sudoers is
177#  configured properly. On NBX-es this does not
178#  work as it uses shared modules that -somehow-
179#  make the sudo commands fail)
180echo $STAMP stopping tomcat...
181index=0
182while [ "$index" -lt "3" ]; do
183        #sudo /etc/init.d/tomcat-6 stop > /dev/null
184        $TOMCAT_STOP > /dev/null 2>&1
185        sleep 10
186        ((index++))
187done
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# clean project
213#echo $STAMP cleaning project cache...
214#grails clean --non-interactive > /dev/null
215
216# patch datasource.groovy
217echo $STAMP patching datasource for ci instance
218sed -i 's/5432\/'$PROJECT'/5432\/'$PROJECT'-ci/g' grails-app/conf/DataSource.groovy
219
220# build new war
221echo $STAMP building new war...
222grails prod war --non-interactive > /dev/null
223
224# undeploying gscf releases
225rm -rf $PATH_TOMCAT_WEBAPPS/$PROJECT-$OLD_APP_VERSION-ci*
226
227# deploying new build
228echo $STAMP deploying new production WAR
229cp target/$PROJECT-$APP_VERSION.war $PATH_TOMCAT_WEBAPPS/$PROJECT-$APP_VERSION-ci.war
230
231# start tomcat
232# (make sure sudoers is configured properly. On NBX-es
233#  this does not work as it uses shared modules that
234#  -somehow- make the sudo commands fail)
235echo $STAMP starting tomcat...
236#sudo /etc/init.d/tomcat-6 start > /dev/null
237$TOMCAT_START > /dev/null 2>&1
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.