source: misc/scripts/continuous_integration.sh @ 891

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