1 | #!/bin/sh |
---|
2 | # Automated deploy script for release a new production and a new |
---|
3 | # demo instance of gscf on nbx14. This script needs to be run as |
---|
4 | # user tomcat |
---|
5 | # |
---|
6 | # What it does: |
---|
7 | # - checkout the latest source code from svn |
---|
8 | # - check if the grails version matches (if not, it downloads and |
---|
9 | # installs that particular grails release to /home/tomcat and |
---|
10 | # updates the symbolic link ~/grails to the new release) |
---|
11 | # - build a production war, undeploy the old release and deploy the |
---|
12 | # new release to tomcat's webapps |
---|
13 | # - patch some configuration files for the demo version |
---|
14 | # - build a demo war, undeploy the old release and deploy the |
---|
15 | # new release to tomcat's webapps |
---|
16 | # - restart tomcat |
---|
17 | # - patch apache's virtual host configuration files in and trigger |
---|
18 | # restart of apache |
---|
19 | # |
---|
20 | # Prerequisites: |
---|
21 | # - tomcat webabbs should be writable by user tomcat |
---|
22 | # - tomcat bin files should be executable by user tomcat |
---|
23 | # - in the end this script touches a file to restart apache |
---|
24 | # webserver. This particular cronjob running as root should |
---|
25 | # be available as well |
---|
26 | # - obviously, postgres databases should be available (respectively |
---|
27 | # gscf and gscf-demo) |
---|
28 | # - a /home/tomcat/.bashrc containing the following lines: |
---|
29 | # export GRAILS_HOME=/home/tomcat/grails |
---|
30 | # export PATH=/home/tomcat/grails/bin:$PATH |
---|
31 | # export JAVA_HOME=/usr/lib/jvm/java-6-sun |
---|
32 | # export CATALINA_OPTS="-Xms1024m -Xmx1024m -XX:MaxPermSize=512m -XX:MaxHeapFreeRatio=70 -XX:MaxGCPauseMillis=10 -XX:+UseConcMarkSweepGC" |
---|
33 | # |
---|
34 | # @Author Jeroen Wesbeek <J****n.W******@gmail.com> |
---|
35 | # @Since 20100720 |
---|
36 | # |
---|
37 | # Revision Information: |
---|
38 | # $Author: duh $ |
---|
39 | # $Date: 2010-07-29 12:23:51 +0000 (do, 29 jul 2010) $ |
---|
40 | # $Rev: 753 $ |
---|
41 | |
---|
42 | # CONFIGURATION |
---|
43 | USER=`/usr/bin/whoami` |
---|
44 | PATH_TOMCAT_BIN=/home/tomcat/apache-tomcat-6.0.26/bin |
---|
45 | PATH_TOMCAT_WEBAPPS=/home/tomcat/apache-tomcat-6.0.26/webapps |
---|
46 | PATH_GRAILS_SOURCE=/home/tomcat/workspace/gscf |
---|
47 | SVN=`which svn` |
---|
48 | APACHE_VHOST_FILES=/etc/apache2/sites-available/dbnp* |
---|
49 | DEV_PLUGINS=( db-util nadd-neutralizer ) |
---|
50 | |
---|
51 | # are we user tomcat? |
---|
52 | if [ $USER != 'tomcat' ] |
---|
53 | then |
---|
54 | echo "ERROR: you need to run this script as user tomcat..." |
---|
55 | exit; |
---|
56 | fi |
---|
57 | |
---|
58 | # update project source to latest version |
---|
59 | echo .updating source |
---|
60 | cd $PATH_GRAILS_SOURCE |
---|
61 | $SVN revert --recursive --quiet . |
---|
62 | SVN_REVISION=`$SVN update | sed -n 's/At revision \(.*\)\./\1/p'` |
---|
63 | |
---|
64 | # get application version |
---|
65 | APP_VERSION=`sed -n 's/app.version=\(.*\)$/\1/p' < application.properties` |
---|
66 | GRAILS_VERSION=`sed -n 's/app.grails.version=\(.*\)$/\1/p' < application.properties` |
---|
67 | echo ".done updating application to revision "$SVN_REVISION" (version "$APP_VERSION")" |
---|
68 | |
---|
69 | # see if this grails version is available |
---|
70 | if [ ! -d "/home/tomcat/grails-"$GRAILS_VERSION ] |
---|
71 | then |
---|
72 | echo ".grails version "$GRAILS_VERSION" is not installed." |
---|
73 | |
---|
74 | cd ~ |
---|
75 | ls -1 ~|grep grails|grep zip|xargs -i rm ~/{} |
---|
76 | |
---|
77 | echo ".downloading grails release "$GRAILS_VERSION"... (this might take a while)" |
---|
78 | wget -q "http://dist.codehaus.org/grails/grails-"$GRAILS_VERSION".zip" |
---|
79 | |
---|
80 | # success? |
---|
81 | if [ ! -f "grails-"$GRAILS_VERSION".zip" ] |
---|
82 | then |
---|
83 | echo ".could not download and install grails "$GRAILS_VERSION |
---|
84 | echo " do it manually instead and update the symbolic link..." |
---|
85 | exit; |
---|
86 | fi |
---|
87 | |
---|
88 | # unzip archive |
---|
89 | echo ".unzipping grails release "$GRAILS_VERSION |
---|
90 | unzip -qq "grails-"$GRAILS_VERSION".zip" |
---|
91 | |
---|
92 | # update symlink |
---|
93 | echo ".updating symbolic link" |
---|
94 | rm grails |
---|
95 | ln -s "grails-"$GRAILS_VERSION grails |
---|
96 | |
---|
97 | # switch to source dir |
---|
98 | cd $PATH_GRAILS_SOURCE |
---|
99 | fi |
---|
100 | |
---|
101 | # test if grails version is the correct version |
---|
102 | GRAILS_RUNTIME_VERSION=`grails | sed -n 's/Welcome to Grails \(.*\) - http:\/\/grails.org\//\1/p'` |
---|
103 | if [ $GRAILS_RUNTIME_VERSION != $GRAILS_VERSION ] |
---|
104 | then |
---|
105 | echo "'"$GRAILS_VERSION"'" |
---|
106 | echo "'"$GRAILS_RUNTIME_VERSION"'" |
---|
107 | echo ".ERROR: application requires grails version "$GRAILS_VERSION" while version "$GRAILS_RUNTIME_VERSION" is installed... please fix!" |
---|
108 | exit; |
---|
109 | fi |
---|
110 | |
---|
111 | # clean source |
---|
112 | echo ".clean source" |
---|
113 | grails clean > /dev/null |
---|
114 | $SVN update > /dev/null |
---|
115 | |
---|
116 | # uninstall development plugins |
---|
117 | index=0 |
---|
118 | while [ "$index" -lt "${#DEV_PLUGINS[@]}" ] |
---|
119 | do |
---|
120 | PLUGIN_INSTALLED=`find /home/tomcat/.grails/$GRAILS_VERSION/projects/gscf/plugins/ -maxdepth 1|grep -i ${DEV_PLUGINS[index]}|wc -l` |
---|
121 | if [ "$PLUGIN_INSTALLED" -gt "0" ]; then |
---|
122 | echo $STAMP uninstalling ${DEV_PLUGINS[index]} plugin |
---|
123 | grails uninstall-plugin ${DEV_PLUGINS[index]} > /dev/null |
---|
124 | fi |
---|
125 | ((index++)) |
---|
126 | done |
---|
127 | |
---|
128 | # build production war |
---|
129 | echo ".build production war" |
---|
130 | grails prod war > /dev/null |
---|
131 | |
---|
132 | # stop tomcat |
---|
133 | echo ".stopping tomcat" |
---|
134 | $PATH_TOMCAT_BIN/shutdown.sh > /dev/null |
---|
135 | |
---|
136 | # wait a while |
---|
137 | echo ".waiting..." |
---|
138 | sleep 5 |
---|
139 | |
---|
140 | #backup database |
---|
141 | #NOW=`date +%F_%T` |
---|
142 | #sudo -u postgres pg_dump gscf > /home/tomcat/backups/gscf_$NOW.sql |
---|
143 | |
---|
144 | # undeploying gscf releases |
---|
145 | rm -rf $PATH_TOMCAT_WEBAPPS/gscf* |
---|
146 | |
---|
147 | # deploy production war |
---|
148 | echo ".deploying new production WAR" |
---|
149 | cp target/gscf-$APP_VERSION.war $PATH_TOMCAT_WEBAPPS |
---|
150 | |
---|
151 | # patch datasource.groovy |
---|
152 | echo ".patching datasource for demo instance" |
---|
153 | sed -i 's/5432\/gscf/5432\/gscf-demo/g' grails-app/conf/DataSource.groovy |
---|
154 | |
---|
155 | # patch Searchable.groovy |
---|
156 | echo ".patching searchable for demo instance" |
---|
157 | sed -i 's/searchable\/gscf\//searchable\/gscf-demo\//g' grails-app/conf/Searchable.groovy |
---|
158 | |
---|
159 | # check if the demo was indeed patched correctly |
---|
160 | CHECK=`cat grails-app/conf/DataSource.groovy | grep -i gscf-demo | wc -l` |
---|
161 | if [ $CHECK -eq 0 ] |
---|
162 | then |
---|
163 | echo ".ERROR: datasource was not patched correctly!" |
---|
164 | echo ".exiting..." |
---|
165 | exit; |
---|
166 | fi |
---|
167 | |
---|
168 | # build demo war |
---|
169 | echo ".building demo war" |
---|
170 | grails prod war > /dev/null |
---|
171 | |
---|
172 | # deploying new demo war |
---|
173 | echo ".deploying new demo war" |
---|
174 | cp target/gscf-$APP_VERSION.war $PATH_TOMCAT_WEBAPPS/gscf-$APP_VERSION-demo.war |
---|
175 | |
---|
176 | # start tomcat |
---|
177 | echo ".starting tomcat" |
---|
178 | $PATH_TOMCAT_BIN/startup.sh 2>&1 /dev/null & |
---|
179 | |
---|
180 | # fix apache virtual host configuration |
---|
181 | echo ".updating apache virtual host configuration" |
---|
182 | ls $APACHE_VHOST_FILES|xargs -i sed -i 's/gscf-[0-9]\.[0-9]\.[0-9]/gscf-'$APP_VERSION'/g' {} |
---|
183 | |
---|
184 | # reloading apache |
---|
185 | echo ".telling apache to reload the configuration files (this might take 1 minute to complete)..." |
---|
186 | touch ~/reload_apache |
---|
187 | |
---|
188 | # done... |
---|
189 | echo ".done..." |
---|
190 | |
---|