source: misc/scripts/deploy.sh @ 1105

Last change on this file since 1105 was 1105, checked in by work@…, 10 years ago
  • refactored ci script to allow other (test, www) deployments
  • refactored ci script to allow different tomcat instances
  • renamed ci script to generic deploy script
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Rev
File size: 9.6 KB
Line 
1#!/bin/sh
2# deploy 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-09 11:55:32 +0000 (di, 09 nov 2010) $
27# $Rev: 1105 $
28
29# set the project
30if [ $1 ]; then
31        PROJECT=$1
32else
33        echo "Usage: $0 <projectname> <type>"
34        echo "       where type is on of ci, test or www"
35        exit;
36fi
37
38# set the type
39if [ $2 ]; then
40        TYPE=$2
41else
42        echo "Usage: $0 <projectname> <type>"
43        echo "       where type is on of ci, test or www"
44        exit;
45fi
46
47# check if we got a proper release type
48if [ ! -d /home/tomcat/apache-tomcat-$TYPE ]; then
49        echo "$TYPE is an invalid type"
50        echo ""
51        echo "Usage: $0 <projectname> <type>"
52        echo "       where type is on of ci, test or www"
53        exit;
54fi
55
56# Platform specific configuration
57if [ -f /etc/gentoo-release ]; then
58        # GENTOO
59        export JAVA_HOME=/etc/java-config-2/current-system-vm
60        GRAILS_INSTALL_PATH=$HOME
61        PATH_TOMCAT_WEBAPPS=/var/lib/tomcat-6/webapps/
62        APACHE_VHOST_FILES=/etc/apache2/vhosts.d/dbnp.org_ci.conf
63        TOMCAT_STOP="sudo /etc/init.d/tomcat-6 stop"
64        TOMCAT_START="sudo /etc/init.d/tomcat-6 start"
65        APACHE_RESTART="sudo /etc/init.d/apache2 graceful"
66elif [ -f /etc/debian_version ]; then
67        # DEBIAN
68        export JAVA_HOME=/usr/lib/jvm/java-6-sun
69        GRAILS_INSTALL_PATH=/app
70        PATH_TOMCAT_WEBAPPS=/home/tomcat/apache-tomcat-$TYPE/webapps
71        APACHE_VHOST_FILES=/etc/apache2/sites-available/nmcdsp.org_$PROJECT-$TYPE.conf
72        TOMCAT_STOP="/home/tomcat/apache-tomcat-$TYPE/bin/shutdown.sh"
73        TOMCAT_START="/home/tomcat/apache-tomcat-$TYPE/bin/startup.sh"
74        APACHE_RESTART="sudo /etc/init.d/apache2 restart"
75else
76        echo Could not determine platform....
77        exit
78fi
79
80# GENERIC CONFIGURATION
81export CATALINA_OPTS="-Xms4096m -Xmx4096m -XX:MaxPermSize=2048m -XX:MaxHeapFreeRatio=70 -XX:MaxGCPauseMillis=10 -XX:+UseConcMarkSweepGC"
82#export GRAILS_HOME=$GRAILS_INSTALL_PATH/grails
83export JDK_HOME=$JAVA_HOME
84export JAVAC=$JAVA_HOME/bin/javac
85#export PATH=$GRAILS_HOME/bin:$PATH
86STAMP=`date +'%Y%m%d%H%M'`
87USER=`/usr/bin/whoami`
88PATH_GRAILS_SOURCE=$HOME/workspace/$PROJECT
89TEMP=$HOME/tmp
90SVN=`which svn`
91DEV_PLUGINS=( nadd-neutralizer )
92MANAGER_URL=http://$TYPE.manager.nmcdsp.org/manager/html/undeploy?path=
93MANAGER_USER=ci
94MANAGER_PASSWORD=berterni3
95# Keys registered by t.w.abma[at]umcutrecht address (gscf.nmcdsp.org)
96OAUTHKEY=Xhu1RtLtB43yIy29EYw
97OAUTHSECRET=gYO5ps6JLDMEdJLOQkSBoTTbq11s8CrORqvrBkc6k
98
99# Keys registered by t.w.abma[at]gmail address (gscf.nmcdsp.org)
100#OAUTHKEY=M07bnXDthzcbWD7KKSlDYw
101#OAUTHSECRET=odfnT08AUBYhh2z0PVMS9JT11D2jASwGNzAj9YFk7s
102
103# are we user tomcat?
104if [ $USER != 'tomcat' ]
105then
106        echo $STAMP "ERROR: you need to run this script as user tomcat..."
107        exit;
108fi
109
110# is the update locked?
111if [ -f $TEMP/$PROJECT.locked ]; then
112        echo $STAMP project deployment is still happening
113        exit;
114fi
115
116# lock deployment
117touch $TEMP/$PROJECT.locked
118
119# get svn revision of HEAD and running application
120cd $PATH_GRAILS_SOURCE
121OLD_APP_VERSION=`sed -n 's/app.version=\(.*\)$/\1/p' <  application.properties`
122$SVN revert --recursive --quiet *
123#$SVN update --quiet *
124#SVN_REVISION=`$SVN update .| sed -n 's/At revision \(.*\)\./\1/p'`
125SVN_REVISION=`$SVN update|sed -n 's/At revision \(.*\)\./\1/p'`
126RUNNING_REVISION=`cat $TEMP/$PROJECT-$TYPE.revision`
127
128# only use this for CI builds
129if [ "$TYPE" != "ci" ]; then
130        # we don't want manual test and production
131        # builds to stop building because they have
132        # the same revision...
133        RUNNING_REVISION=0
134fi
135
136# got an SVN revision?
137if [ "$SVN_REVISION" = "" ]; then
138        echo $STAMP haven\'t got an svn revision?
139        rm $TEMP/$PROJECT.locked
140        exit;
141fi
142
143# is a new revision available?
144if [ "$RUNNING_REVISION" == "$SVN_REVISION" ]; then
145        #echo $STAMP do nothing...
146        rm $TEMP/$PROJECT.locked
147        exit;
148fi
149
150# a new version is available, get application and
151# grails version of HEAD
152APP_VERSION=`sed -n 's/app.version=\(.*\)$/\1/p' <  application.properties`
153GRAILS_VERSION=`sed -n 's/app.grails.version=\(.*\)$/\1/p' <  application.properties`
154
155# feedback
156echo $STAMP newer version of $PROJECT is available
157echo $STAMP "deploying revision "$SVN_REVISION" (version "$APP_VERSION" / Grails "$GRAILS_VERSION") and undeploying revision "$RUNNING_REVISION"..."
158
159# create minified JS and CSS if required
160MINIFIED=`ls web-app/js/*.js web-app/css/*.css|grep -vi min.|xargs -i ~/scripts/minify_if_changed.sh {} 1|wc -l`
161if [ $MINIFIED -gt 0 ]; then
162        echo $STAMP "one or more JS / CSS files were minified, committing changes..."
163        $SVN commit -m "Automated continuous integration commit of minified JS/CSS" web-app/css/*.min.css web-app/js/*.min.js > /dev/null
164        SVN_REVISION=`$SVN update | sed -n 's/At revision \(.*\)\./\1/p'`
165        echo $STAMP "updated SVN revision to "$SVN_REVISION
166fi 
167
168# see if this grails version is available
169if [ ! -d $GRAILS_INSTALL_PATH/grails-$GRAILS_VERSION ]
170then
171        echo $STAMP "grails version "$GRAILS_VERSION" is not installed."
172
173        cd $GRAILS_INSTALL_PATH
174        ls -1 ~|grep grails|grep zip|xargs -i rm ~/{}
175       
176        echo $STAMP "downloading grails release "$GRAILS_VERSION"... (this might take a while)"
177        wget -q "http://dist.codehaus.org/grails/grails-"$GRAILS_VERSION".zip"
178
179        # success?
180        if [ ! -f "grails-"$GRAILS_VERSION".zip" ]
181        then
182                echo $STAMP "could not download and install grails "$GRAILS_VERSION
183                echo $STAMP "do it manually instead and update the symbolic link..."
184
185                # remove lock file
186                if [ -f $TEMP/$PROJECT.locked ]
187                then
188                        rm $TEMP/$PROJECT.locked
189                fi
190                exit;
191        fi
192
193        # unzip archive
194        echo $STAMP "unzipping grails release "$GRAILS_VERSION
195        unzip -qq "grails-"$GRAILS_VERSION".zip"
196
197        # update symlink
198        echo $STAMP "updating symbolic link"
199        rm grails
200        ln -s "grails-"$GRAILS_VERSION grails
201
202        # switch to source dir
203        cd $PATH_GRAILS_SOURCE
204
205        GRAILS_UPGRADED=1
206else
207        GRAILS_UPGRADED=0
208fi
209
210# export environment variables so that the this CI script uses
211# the grails version for this specific project and hence is
212# grails version independent...
213export GRAILS_HOME=$GRAILS_INSTALL_PATH/grails-$GRAILS_VERSION
214export PATH=$GRAILS_HOME/bin:$PATH
215
216# upgrade project to new grails version?
217if [ $GRAILS_UPGRADED -eq 1 ]; then
218        echo ".upgrade grails project"
219        grails upgrade --non-interactive --quiet
220fi
221
222# uninstall development plugins
223index=0
224while [ "$index" -lt "${#DEV_PLUGINS[@]}" ]
225do
226        PLUGIN_INSTALLED=`find $HOME/.grails/$GRAILS_VERSION/projects/$PROJECT/plugins/ -maxdepth 1|grep -i ${DEV_PLUGINS[index]}|wc -l`
227        if [ "$PLUGIN_INSTALLED" -gt "0" ]; then
228                echo $STAMP uninstalling ${DEV_PLUGINS[index]} plugin
229                grails uninstall-plugin ${DEV_PLUGINS[index]} > /dev/null
230        fi
231        ((index++))
232done
233
234# patch index page to contain revision information
235echo $STAMP setting build information variables in application.properties
236TIMESTAMP=`date +'%s'`
237sed -i 's/app\.build\.type=\w\+/app.build.type=$TYPE/gi' application.properties
238sed -i 's/app\.build\.timestamp=\w\+/app.build.timestamp='$TIMESTAMP'/gi' application.properties
239sed -i 's/app\.build\.svn\.revision=\w\+/app.build.svn.revision='$SVN_REVISION'/gi' application.properties
240
241# only display build information on non-production instances
242if [ "$TYPE" != "www" ]; then
243        sed -i 's/app\.build\.display\.info=\w\+/app.build.display.info=1/gi' application.properties
244fi
245
246# patch datasource.groovy
247echo $STAMP patching datasource for $TYPE instance
248sed -i 's/5432\/'$PROJECT'/5432\/'$PROJECT'-$TYPE/g' grails-app/conf/DataSource.groovy
249
250# patch Searchable
251if [ -f grails-app/conf/Searchable.groovy ]; then
252        echo $STAMP patching searchable configuration for $TYPE instance
253        sed -i 's/$PROJECT/$PROJECT-$TYPE/gi' grails-app/conf/Searchable.groovy
254fi
255
256# adding patch for OAuth configuration file (consumer key and consumer secret generated by myExperiment).
257if [ "$PROJECT" = "gscf" ]
258then
259        echo $STAMP GSCF project given as argument, patching Config.groovy with OAuth [key:$OAUTHKEY, SECRET:$OAUTHSECRET]...
260        sed -i 's/\$oauthkey\$/'$OAUTHKEY'/gi' grails-app/conf/Config.groovy
261        sed -i 's/\$oauthsecret\$/'$OAUTHSECRET'/gi' grails-app/conf/Config.groovy
262fi
263
264# build new war
265PWD=$(pwd)
266echo $STAMP current workdirectory: $PWD
267echo $STAMP building new war...
268grails prod war --non-interactive > /dev/null
269
270# undeploying gscf releases
271echo $STAMP undeploying $PROJECT-$OLD_APP_VERSION-ci
272curl --silent --user $MANAGER_USER:$MANAGER_PASSWORD --url $MANAGER_URL/$PROJECT-$OLD_APP_VERSION-ci > /dev/null
273sleep 5
274
275# deploying new build
276echo $STAMP deploying new production WAR
277cp target/$PROJECT-$APP_VERSION.war $PATH_TOMCAT_WEBAPPS/$PROJECT-$APP_VERSION-$TYPE.war
278
279# fix virtual host files
280echo $STAMP updating apache virtual host configuration
281ls $APACHE_VHOST_FILES|xargs -i sed -i 's/'$PROJECT'-[0-9]\.[0-9]\.[0-9]/'$PROJECT'-'$APP_VERSION'/g' {}
282
283# restart apache
284# (make sure sudoers is configured properly. On NBX-es
285#  this does not work as it uses shared modules that
286#  -somehow- make the sudo commands fail)
287echo $STAMP reloading Apache
288$APACHE_RESTART > /dev/null 2>&1
289
290# update revision file
291echo $SVN_REVISION > $TEMP/$PROJECT-$TYPE.revision
292
293# remove lock file
294if [ -f $TEMP/$PROJECT.locked ]
295then
296        rm $TEMP/$PROJECT.locked
297fi
298
299echo $STAMP" done deploying "$PROJECT" revision "$SVN_REVISION" (version "$APP_VERSION")"
Note: See TracBrowser for help on using the repository browser.