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 |
---|
30 | export JAVA_HOME=/etc/java-config-2/current-system-vm |
---|
31 | export JAVAC=/etc/java-config-2/current-system-vm/bin/javac |
---|
32 | export JDK_HOME=/etc/java-config-2/current-system-vm |
---|
33 | export GRAILS_HOME=/home/tomcat/grails |
---|
34 | export PATH=/home/tomcat/grails/bin:$PATH |
---|
35 | |
---|
36 | # CONFIGURATION |
---|
37 | STAMP=`date +'%Y%m%d%H%M'` |
---|
38 | PROJECT=gscf |
---|
39 | USER=`/usr/bin/whoami` |
---|
40 | PATH_TOMCAT_WEBAPPS=/var/lib/tomcat-6/webapps/ |
---|
41 | PATH_GRAILS_SOURCE=/home/tomcat/workspace/gscf |
---|
42 | TEMP=/home/tomcat/tmp |
---|
43 | SVN=`which svn` |
---|
44 | APACHE_VHOST_FILES=/etc/apache2/vhosts.d/osx.eu_gscf.conf |
---|
45 | DEV_PLUGINS=( db-util nadd-neutralizer ) |
---|
46 | |
---|
47 | # are we user tomcat? |
---|
48 | if [ $USER != 'tomcat' ] |
---|
49 | then |
---|
50 | echo $STAMP "ERROR: you need to run this script as user tomcat..." |
---|
51 | exit; |
---|
52 | fi |
---|
53 | |
---|
54 | # is the update locked? |
---|
55 | if [ -f $TEMP/$PROJECT.locked ] |
---|
56 | then |
---|
57 | echo $STAMP project deployment is still happening |
---|
58 | exit; |
---|
59 | fi |
---|
60 | |
---|
61 | # lock deployment |
---|
62 | touch $TEMP/$PROJECT.locked |
---|
63 | |
---|
64 | # get svn revision of HEAD and running application |
---|
65 | cd $PATH_GRAILS_SOURCE |
---|
66 | $SVN revert --recursive --quiet . |
---|
67 | SVN_REVISION=`$SVN update | sed -n 's/At revision \(.*\)\./\1/p'` |
---|
68 | RUNNING_REVISION=`cat $TEMP/$PROJECT.revision` |
---|
69 | |
---|
70 | # got an SVN revision? |
---|
71 | if [ "$SVN_REVISION" = "" ]; |
---|
72 | then |
---|
73 | echo $STAMP haven\'t got an svn revision? |
---|
74 | rm $TEMP/$PROJECT.locked |
---|
75 | exit; |
---|
76 | fi |
---|
77 | |
---|
78 | # is a new revision available? |
---|
79 | if [ "$RUNNING_REVISION" == "$SVN_REVISION" ] |
---|
80 | then |
---|
81 | #echo $STAMP do nothing... |
---|
82 | rm $TEMP/$PROJECT.locked |
---|
83 | exit; |
---|
84 | fi |
---|
85 | |
---|
86 | # a new version is available, get application and |
---|
87 | # grails version of HEAD |
---|
88 | APP_VERSION=`sed -n 's/app.version=\(.*\)$/\1/p' < application.properties` |
---|
89 | GRAILS_VERSION=`sed -n 's/app.grails.version=\(.*\)$/\1/p' < application.properties` |
---|
90 | |
---|
91 | # feedback |
---|
92 | echo $STAMP newer version of $PROJECT is available |
---|
93 | echo $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 |
---|
96 | MINIFIED=`ls web-app/js/*.js web-app/css/*.css|grep -vi min.|xargs -i ~/scripts/minify_if_changed.sh {} 1|wc -l` |
---|
97 | if [ $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 |
---|
102 | fi |
---|
103 | |
---|
104 | # see if this grails version is available |
---|
105 | if [ ! -d "/home/tomcat/grails-"$GRAILS_VERSION ] |
---|
106 | then |
---|
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 |
---|
134 | fi |
---|
135 | |
---|
136 | # uninstall development plugins |
---|
137 | index=0 |
---|
138 | while [ "$index" -lt "${#DEV_PLUGINS[@]}" ] |
---|
139 | do |
---|
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++)) |
---|
146 | done |
---|
147 | |
---|
148 | # patch index page to contain revision information |
---|
149 | echo $STAMP patching index page... |
---|
150 | sed -i 's/<\/b>/<\/b> \(<font color="red">svn revision: '$SVN_REVISION', deployed at: '$STAMP'<\/font>\)/g' grails-app/views/home/index.gsp |
---|
151 | sed -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 |
---|
154 | echo $STAMP building new war... |
---|
155 | grails 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) |
---|
163 | echo $STAMP stopping tomcat... |
---|
164 | index=0 |
---|
165 | while [ "$index" -lt "3" ]; do |
---|
166 | sudo /etc/init.d/tomcat-6 stop > /dev/null |
---|
167 | sleep 10 |
---|
168 | ((index++)) |
---|
169 | done |
---|
170 | |
---|
171 | # undeploying gscf releases |
---|
172 | rm -rf $PATH_TOMCAT_WEBAPPS/gscf* |
---|
173 | |
---|
174 | # deploying new build |
---|
175 | echo $STAMP deploying new production WAR |
---|
176 | cp 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) |
---|
182 | echo $STAMP starting tomcat... |
---|
183 | sudo /etc/init.d/tomcat-6 start > /dev/null |
---|
184 | |
---|
185 | # fix virtual host files |
---|
186 | echo $STAMP updating apache virtual host configuration |
---|
187 | ls $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) |
---|
193 | echo $STAMP reloading Apache |
---|
194 | sudo /etc/init.d/apache2 graceful > /dev/null |
---|
195 | |
---|
196 | # update revision file |
---|
197 | echo $SVN_REVISION > $TEMP/$PROJECT.revision |
---|
198 | |
---|
199 | # remove lock file |
---|
200 | if [ -f $TEMP/$PROJECT.locked ] |
---|
201 | then |
---|
202 | rm $TEMP/$PROJECT.locked |
---|
203 | fi |
---|
204 | |
---|
205 | echo $STAMP" done deploying "$PROJECT" revision "$SVN_REVISION" (version "$APP_VERSION")" |
---|