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