source: misc/scripts/continuous_integration.sh @ 864

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

fixed the CI insertion

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Rev
File size: 7.7 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-08-30 16:07:03 +0000 (ma, 30 aug 2010) $
27# $Rev: 864 $
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
93$SVN revert --recursive --quiet *
94#$SVN update --quiet *
95#SVN_REVISION=`$SVN update .| sed -n 's/At revision \(.*\)\./\1/p'`
96SVN_REVISION=`$SVN update|sed -n 's/At revision \(.*\)\./\1/p'`
97RUNNING_REVISION=`cat $TEMP/$PROJECT.revision`
98
99# got an SVN revision?
100if [ "$SVN_REVISION" = "" ]; then
101        echo $STAMP haven\'t got an svn revision?
102        rm $TEMP/$PROJECT.locked
103        exit;
104fi
105
106# is a new revision available?
107if [ "$RUNNING_REVISION" == "$SVN_REVISION" ]; then
108        #echo $STAMP do nothing...
109        rm $TEMP/$PROJECT.locked
110        exit;
111fi
112
113# a new version is available, get application and
114# grails version of HEAD
115APP_VERSION=`sed -n 's/app.version=\(.*\)$/\1/p' <  application.properties`
116GRAILS_VERSION=`sed -n 's/app.grails.version=\(.*\)$/\1/p' <  application.properties`
117
118# feedback
119echo $STAMP newer version of $PROJECT is available
120echo $STAMP "deploying revision "$SVN_REVISION" (version "$APP_VERSION" / Grails "$GRAILS_VERSION") and undeploying revision "$RUNNING_REVISION"..."
121
122# create minified JS and CSS if required
123MINIFIED=`ls web-app/js/*.js web-app/css/*.css|grep -vi min.|xargs -i ~/scripts/minify_if_changed.sh {} 1|wc -l`
124if [ $MINIFIED -gt 0 ]; then
125        echo $STAMP "one or more JS / CSS files were minified, committing changes..."
126        $SVN commit -m "Automated continuous integration commit of minified JS/CSS" web-app/css/*.min.css web-app/js/*.min.js > /dev/null
127        SVN_REVISION=`$SVN update | sed -n 's/At revision \(.*\)\./\1/p'`
128        echo $STAMP "updated SVN revision to "$SVN_REVISION
129fi 
130
131# see if this grails version is available
132if [ ! -d $GRAILS_INSTALL_PATH/grails-$GRAILS_VERSION ]
133then
134        echo $STAMP "grails version "$GRAILS_VERSION" is not installed."
135
136        cd $GRAILS_INSTALL_PATH
137        ls -1 ~|grep grails|grep zip|xargs -i rm ~/{}
138       
139        echo $STAMP "downloading grails release "$GRAILS_VERSION"... (this might take a while)"
140        wget -q "http://dist.codehaus.org/grails/grails-"$GRAILS_VERSION".zip"
141
142        # success?
143        if [ ! -f "grails-"$GRAILS_VERSION".zip" ]
144        then
145                echo $STAMP "could not download and install grails "$GRAILS_VERSION
146                echo $STAMP "do it manually instead and update the symbolic link..."
147
148                # remove lock file
149                if [ -f $TEMP/$PROJECT.locked ]
150                then
151                        rm $TEMP/$PROJECT.locked
152                fi
153                exit;
154        fi
155
156        # unzip archive
157        echo $STAMP "unzipping grails release "$GRAILS_VERSION
158        unzip -qq "grails-"$GRAILS_VERSION".zip"
159
160        # update symlink
161        echo $STAMP "updating symbolic link"
162        rm grails
163        ln -s "grails-"$GRAILS_VERSION grails
164
165        # switch to source dir
166        cd $PATH_GRAILS_SOURCE
167
168        GRAILS_UPGRADED=1
169else
170        GRAILS_UPGRADED=0
171fi
172
173# stop tomcat
174# (a couple of times, sometimes it does not work
175#  properly or is slow. make sure sudoers is
176#  configured properly. On NBX-es this does not
177#  work as it uses shared modules that -somehow-
178#  make the sudo commands fail)
179echo $STAMP stopping tomcat...
180index=0
181while [ "$index" -lt "3" ]; do
182        #sudo /etc/init.d/tomcat-6 stop > /dev/null
183        $TOMCAT_STOP > /dev/null 2>&1
184        sleep 10
185        ((index++))
186done
187
188# upgrade project to new grails version?
189if [ $GRAILS_UPGRADED -eq 1 ]; then
190        echo ".upgrade grails project"
191        grails upgrade --non-interactive --quiet
192fi
193
194# uninstall development plugins
195index=0
196while [ "$index" -lt "${#DEV_PLUGINS[@]}" ]
197do
198        PLUGIN_INSTALLED=`find $HOME/.grails/$GRAILS_VERSION/projects/$PROJECT/plugins/ -maxdepth 1|grep -i ${DEV_PLUGINS[index]}|wc -l`
199        if [ "$PLUGIN_INSTALLED" -gt "0" ]; then
200                echo $STAMP uninstalling ${DEV_PLUGINS[index]} plugin
201                grails uninstall-plugin ${DEV_PLUGINS[index]} > /dev/null
202        fi
203        ((index++))
204done
205
206# patch index page to contain revision information
207echo $STAMP patching index page...
208sed -i 's/<\/b>/<\/b> \(<font color="red">svn revision: '$SVN_REVISION', deployed at: '$STAMP'<\/font>\)/gi' grails-app/views/home/index.gsp
209sed -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
210
211# build new war
212echo $STAMP building new war...
213grails prod war --non-interactive > /dev/null
214
215# undeploying gscf releases
216rm -rf $PATH_TOMCAT_WEBAPPS/$PROJECT*
217
218# deploying new build
219echo $STAMP deploying new production WAR
220cp target/$PROJECT-$APP_VERSION.war $PATH_TOMCAT_WEBAPPS
221
222# start tomcat
223# (make sure sudoers is configured properly. On NBX-es
224#  this does not work as it uses shared modules that
225#  -somehow- make the sudo commands fail)
226echo $STAMP starting tomcat...
227#sudo /etc/init.d/tomcat-6 start > /dev/null
228$TOMCAT_START > /dev/null 2>&1
229
230# fix virtual host files
231echo $STAMP updating apache virtual host configuration
232ls $APACHE_VHOST_FILES|xargs -i sed -i 's/'$PROJECT'-[0-9]\.[0-9]\.[0-9]/'$PROJECT'-'$APP_VERSION'/g' {}
233
234# restart apache
235# (make sure sudoers is configured properly. On NBX-es
236#  this does not work as it uses shared modules that
237#  -somehow- make the sudo commands fail)
238echo $STAMP reloading Apache
239$APACHE_RESTART > /dev/null 2>&1
240
241# update revision file
242echo $SVN_REVISION > $TEMP/$PROJECT.revision
243
244# remove lock file
245if [ -f $TEMP/$PROJECT.locked ]
246then
247        rm $TEMP/$PROJECT.locked
248fi
249
250echo $STAMP" done deploying "$PROJECT" revision "$SVN_REVISION" (version "$APP_VERSION")"
Note: See TracBrowser for help on using the repository browser.