source: misc/scripts/gscf_continuous_integration.sh @ 753

Last change on this file since 753 was 753, checked in by duh, 13 years ago

bugfix

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Rev
File size: 6.3 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/tomcat 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-07-29 12:23:51 +0000 (do, 29 jul 2010) $
27# $Rev: 753 $
28
29# GRAILS CONFIG
30export JAVA_HOME=/etc/java-config-2/current-system-vm
31export JAVAC=/etc/java-config-2/current-system-vm/bin/javac
32export JDK_HOME=/etc/java-config-2/current-system-vm
33export GRAILS_HOME=/home/tomcat/grails
34export PATH=/home/tomcat/grails/bin:$PATH
35
36# CONFIGURATION
37STAMP=`date +'%Y%m%d%H%M'`
38PROJECT=gscf
39USER=`/usr/bin/whoami`
40PATH_TOMCAT_WEBAPPS=/var/lib/tomcat-6/webapps/
41PATH_GRAILS_SOURCE=/home/tomcat/workspace/gscf
42TEMP=/home/tomcat/tmp
43SVN=`which svn`
44APACHE_VHOST_FILES=/etc/apache2/vhosts.d/osx.eu_gscf.conf
45DEV_PLUGINS=( db-util nadd-neutralizer )
46
47# are we user tomcat?
48if [ $USER != 'tomcat' ]
49then
50        echo $STAMP "ERROR: you need to run this script as user tomcat..."
51        exit;
52fi
53
54# is the update locked?
55if [ -f $TEMP/$PROJECT.locked ]
56then
57        echo $STAMP project deployment is still happening
58        exit;
59fi
60
61# lock deployment
62touch $TEMP/$PROJECT.locked
63
64# get svn revision of HEAD and running application
65cd $PATH_GRAILS_SOURCE
66$SVN revert --recursive --quiet .
67SVN_REVISION=`$SVN update | sed -n 's/At revision \(.*\)\./\1/p'`
68RUNNING_REVISION=`cat $TEMP/$PROJECT.revision`
69
70# got an SVN revision?
71if [ "$SVN_REVISION" = "" ];
72then
73        echo $STAMP haven\'t got an svn revision?
74        rm $TEMP/$PROJECT.locked
75        exit;
76fi
77
78# is a new revision available?
79if [ "$RUNNING_REVISION" == "$SVN_REVISION" ]
80then
81        #echo $STAMP do nothing...
82        rm $TEMP/$PROJECT.locked
83        exit;
84fi
85
86# a new version is available, get application and
87# grails version of HEAD
88APP_VERSION=`sed -n 's/app.version=\(.*\)$/\1/p' <  application.properties`
89GRAILS_VERSION=`sed -n 's/app.grails.version=\(.*\)$/\1/p' <  application.properties`
90
91# feedback
92echo $STAMP newer version of $PROJECT is available
93echo $STAMP "deploying revision "$SVN_REVISION" (version "$APP_VERSION" / Grails "$GRAILS_VERSION") and undeploying revision "$RUNNING_REVISION"..."
94
95# create minified JS and CSS if required
96MINIFIED=`ls web-app/js/*.js web-app/css/*.css|grep -vi min.|xargs -i ~/scripts/minify_if_changed.sh {} 1|wc -l`
97if [ $MINIFIED -gt 0 ]; then
98        echo $STAMP "one or more JS / CSS files were minified, committing changes..."
99        $SVN commit -m "Automated continuous integration commit of minified JS/CSS" web-app/css/*.min.css web-app/js/*.min.js > /dev/null
100        SVN_REVISION=`$SVN update | sed -n 's/At revision \(.*\)\./\1/p'`
101        echo $STAMP "updated SVN revision to "$SVN_REVISION
102fi 
103
104# see if this grails version is available
105if [ ! -d "/home/tomcat/grails-"$GRAILS_VERSION ]
106then
107        echo $STAMP "grails version "$GRAILS_VERSION" is not installed."
108
109        cd ~
110        ls -1 ~|grep grails|grep zip|xargs -i rm ~/{}
111       
112        echo $STAMP "downloading grails release "$GRAILS_VERSION"... (this might take a while)"
113        wget -q "http://dist.codehaus.org/grails/grails-"$GRAILS_VERSION".zip"
114
115        # success?
116        if [ ! -f "grails-"$GRAILS_VERSION".zip" ]
117        then
118                echo $STAMP "could not download and install grails "$GRAILS_VERSION
119                echo $STAMP "do it manually instead and update the symbolic link..."
120                exit;
121        fi
122
123        # unzip archive
124        echo $STAMP "unzipping grails release "$GRAILS_VERSION
125        unzip -qq "grails-"$GRAILS_VERSION".zip"
126
127        # update symlink
128        echo $STAMP "updating symbolic link"
129        rm grails
130        ln -s "grails-"$GRAILS_VERSION grails
131
132        # switch to source dir
133        cd $PATH_GRAILS_SOURCE
134fi
135
136# uninstall development plugins
137index=0
138while [ "$index" -lt "${#DEV_PLUGINS[@]}" ]
139do
140        PLUGIN_INSTALLED=`find /home/tomcat/.grails/$GRAILS_VERSION/projects/gscf/plugins/ -maxdepth 1|grep -i ${DEV_PLUGINS[index]}|wc -l`
141        if [ "$PLUGIN_INSTALLED" -gt "0" ]; then
142                echo $STAMP uninstalling ${DEV_PLUGINS[index]} plugin
143                grails uninstall-plugin ${DEV_PLUGINS[index]} > /dev/null
144        fi
145        ((index++))
146done
147
148# patch index page to contain revision information
149echo $STAMP patching index page...
150sed -i 's/<\/b>/<\/b> \(<font color="red">svn revision: '$SVN_REVISION', deployed at: '$STAMP'<\/font>\)/g' grails-app/views/home/index.gsp
151sed -i 's/Welcome to GSCF version/Welcome to the <b>continuous integration build<\/b> of GSCF/g' grails-app/views/home/index.gsp
152
153# build new war
154echo $STAMP building new war...
155grails prod war > /dev/null
156
157# stop tomcat
158# (a couple of times, sometimes it does not work
159#  properly or is slow. make sure sudoers is
160#  configured properly. On NBX-es this does not
161#  work as it uses shared modules that -somehow-
162#  make the sudo commands fail)
163echo $STAMP stopping tomcat...
164index=0
165while [ "$index" -lt "3" ]; do
166        sudo /etc/init.d/tomcat-6 stop > /dev/null
167        sleep 10
168        ((index++))
169done
170
171# undeploying gscf releases
172rm -rf $PATH_TOMCAT_WEBAPPS/gscf*
173
174# deploying new build
175echo $STAMP deploying new production WAR
176cp target/gscf-$APP_VERSION.war $PATH_TOMCAT_WEBAPPS
177
178# start tomcat
179# (make sure sudoers is configured properly. On NBX-es
180#  this does not work as it uses shared modules that
181#  -somehow- make the sudo commands fail)
182echo $STAMP starting tomcat...
183sudo /etc/init.d/tomcat-6 start > /dev/null
184
185# fix virtual host files
186echo $STAMP updating apache virtual host configuration
187ls $APACHE_VHOST_FILES|xargs -i sed -i 's/gscf-[0-9]\.[0-9]\.[0-9]/gscf-'$APP_VERSION'/g' {}
188
189# restart apache
190# (make sure sudoers is configured properly. On NBX-es
191#  this does not work as it uses shared modules that
192#  -somehow- make the sudo commands fail)
193echo $STAMP reloading Apache
194sudo /etc/init.d/apache2 graceful > /dev/null
195
196# update revision file
197echo $SVN_REVISION > $TEMP/$PROJECT.revision
198
199# remove lock file
200if [ -f $TEMP/$PROJECT.locked ]
201then
202        rm $TEMP/$PROJECT.locked
203fi
204
205echo $STAMP" done deploying "$PROJECT" revision "$SVN_REVISION" (version "$APP_VERSION")"
Note: See TracBrowser for help on using the repository browser.