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 |
---|
30 | if [ $1 ]; then |
---|
31 | PROJECT=$1 |
---|
32 | else |
---|
33 | echo "Usage: $0 <projectname> <type>" |
---|
34 | echo " where type is on of ci, test or www" |
---|
35 | exit; |
---|
36 | fi |
---|
37 | |
---|
38 | # set the type |
---|
39 | if [ $2 ]; then |
---|
40 | TYPE=$2 |
---|
41 | else |
---|
42 | echo "Usage: $0 <projectname> <type>" |
---|
43 | echo " where type is on of ci, test or www" |
---|
44 | exit; |
---|
45 | fi |
---|
46 | |
---|
47 | # check if we got a proper release type |
---|
48 | if [ ! -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; |
---|
54 | fi |
---|
55 | |
---|
56 | # Platform specific configuration |
---|
57 | if [ -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" |
---|
66 | elif [ -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" |
---|
75 | else |
---|
76 | echo Could not determine platform.... |
---|
77 | exit |
---|
78 | fi |
---|
79 | |
---|
80 | # GENERIC CONFIGURATION |
---|
81 | export CATALINA_OPTS="-Xms4096m -Xmx4096m -XX:MaxPermSize=2048m -XX:MaxHeapFreeRatio=70 -XX:MaxGCPauseMillis=10 -XX:+UseConcMarkSweepGC" |
---|
82 | #export GRAILS_HOME=$GRAILS_INSTALL_PATH/grails |
---|
83 | export JDK_HOME=$JAVA_HOME |
---|
84 | export JAVAC=$JAVA_HOME/bin/javac |
---|
85 | #export PATH=$GRAILS_HOME/bin:$PATH |
---|
86 | STAMP=`date +'%Y%m%d%H%M'` |
---|
87 | USER=`/usr/bin/whoami` |
---|
88 | PATH_GRAILS_SOURCE=$HOME/workspace/$PROJECT |
---|
89 | TEMP=$HOME/tmp |
---|
90 | SVN=`which svn` |
---|
91 | DEV_PLUGINS=( nadd-neutralizer ) |
---|
92 | MANAGER_URL=http://$TYPE.manager.nmcdsp.org/manager/html/undeploy?path= |
---|
93 | MANAGER_USER=ci |
---|
94 | MANAGER_PASSWORD=berterni3 |
---|
95 | # Keys registered by t.w.abma[at]umcutrecht address (gscf.nmcdsp.org) |
---|
96 | OAUTHKEY=Xhu1RtLtB43yIy29EYw |
---|
97 | OAUTHSECRET=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? |
---|
104 | if [ $USER != 'tomcat' ] |
---|
105 | then |
---|
106 | echo $STAMP "ERROR: you need to run this script as user tomcat..." |
---|
107 | exit; |
---|
108 | fi |
---|
109 | |
---|
110 | # is the update locked? |
---|
111 | if [ -f $TEMP/$PROJECT.locked ]; then |
---|
112 | echo $STAMP project deployment is still happening |
---|
113 | exit; |
---|
114 | fi |
---|
115 | |
---|
116 | # lock deployment |
---|
117 | touch $TEMP/$PROJECT.locked |
---|
118 | |
---|
119 | # get svn revision of HEAD and running application |
---|
120 | cd $PATH_GRAILS_SOURCE |
---|
121 | OLD_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'` |
---|
125 | SVN_REVISION=`$SVN update|sed -n 's/At revision \(.*\)\./\1/p'` |
---|
126 | RUNNING_REVISION=`cat $TEMP/$PROJECT-$TYPE.revision` |
---|
127 | |
---|
128 | # only use this for CI builds |
---|
129 | if [ "$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 |
---|
134 | fi |
---|
135 | |
---|
136 | # got an SVN revision? |
---|
137 | if [ "$SVN_REVISION" = "" ]; then |
---|
138 | echo $STAMP haven\'t got an svn revision? |
---|
139 | rm $TEMP/$PROJECT.locked |
---|
140 | exit; |
---|
141 | fi |
---|
142 | |
---|
143 | # is a new revision available? |
---|
144 | if [ "$RUNNING_REVISION" == "$SVN_REVISION" ]; then |
---|
145 | #echo $STAMP do nothing... |
---|
146 | rm $TEMP/$PROJECT.locked |
---|
147 | exit; |
---|
148 | fi |
---|
149 | |
---|
150 | # a new version is available, get application and |
---|
151 | # grails version of HEAD |
---|
152 | APP_VERSION=`sed -n 's/app.version=\(.*\)$/\1/p' < application.properties` |
---|
153 | GRAILS_VERSION=`sed -n 's/app.grails.version=\(.*\)$/\1/p' < application.properties` |
---|
154 | |
---|
155 | # feedback |
---|
156 | echo $STAMP newer version of $PROJECT is available |
---|
157 | echo $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 |
---|
160 | MINIFIED=`ls web-app/js/*.js web-app/css/*.css|grep -vi min.|xargs -i ~/scripts/minify_if_changed.sh {} 1|wc -l` |
---|
161 | if [ $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 |
---|
166 | fi |
---|
167 | |
---|
168 | # see if this grails version is available |
---|
169 | if [ ! -d $GRAILS_INSTALL_PATH/grails-$GRAILS_VERSION ] |
---|
170 | then |
---|
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 |
---|
206 | else |
---|
207 | GRAILS_UPGRADED=0 |
---|
208 | fi |
---|
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... |
---|
213 | export GRAILS_HOME=$GRAILS_INSTALL_PATH/grails-$GRAILS_VERSION |
---|
214 | export PATH=$GRAILS_HOME/bin:$PATH |
---|
215 | |
---|
216 | # upgrade project to new grails version? |
---|
217 | if [ $GRAILS_UPGRADED -eq 1 ]; then |
---|
218 | echo ".upgrade grails project" |
---|
219 | grails upgrade --non-interactive --quiet |
---|
220 | fi |
---|
221 | |
---|
222 | # uninstall development plugins |
---|
223 | index=0 |
---|
224 | while [ "$index" -lt "${#DEV_PLUGINS[@]}" ] |
---|
225 | do |
---|
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++)) |
---|
232 | done |
---|
233 | |
---|
234 | # patch index page to contain revision information |
---|
235 | echo $STAMP setting build information variables in application.properties |
---|
236 | TIMESTAMP=`date +'%s'` |
---|
237 | sed -i 's/app\.build\.type=\w\+/app.build.type=$TYPE/gi' application.properties |
---|
238 | sed -i 's/app\.build\.timestamp=\w\+/app.build.timestamp='$TIMESTAMP'/gi' application.properties |
---|
239 | sed -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 |
---|
242 | if [ "$TYPE" != "www" ]; then |
---|
243 | sed -i 's/app\.build\.display\.info=\w\+/app.build.display.info=1/gi' application.properties |
---|
244 | fi |
---|
245 | |
---|
246 | # patch datasource.groovy |
---|
247 | echo $STAMP patching datasource for $TYPE instance |
---|
248 | sed -i 's/5432\/'$PROJECT'/5432\/'$PROJECT'-$TYPE/g' grails-app/conf/DataSource.groovy |
---|
249 | |
---|
250 | # patch Searchable |
---|
251 | if [ -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 |
---|
254 | fi |
---|
255 | |
---|
256 | # adding patch for OAuth configuration file (consumer key and consumer secret generated by myExperiment). |
---|
257 | if [ "$PROJECT" = "gscf" ] |
---|
258 | then |
---|
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 |
---|
262 | fi |
---|
263 | |
---|
264 | # build new war |
---|
265 | PWD=$(pwd) |
---|
266 | echo $STAMP current workdirectory: $PWD |
---|
267 | echo $STAMP building new war... |
---|
268 | grails prod war --non-interactive > /dev/null |
---|
269 | |
---|
270 | # undeploying gscf releases |
---|
271 | echo $STAMP undeploying $PROJECT-$OLD_APP_VERSION-ci |
---|
272 | curl --silent --user $MANAGER_USER:$MANAGER_PASSWORD --url $MANAGER_URL/$PROJECT-$OLD_APP_VERSION-ci > /dev/null |
---|
273 | sleep 5 |
---|
274 | |
---|
275 | # deploying new build |
---|
276 | echo $STAMP deploying new production WAR |
---|
277 | cp target/$PROJECT-$APP_VERSION.war $PATH_TOMCAT_WEBAPPS/$PROJECT-$APP_VERSION-$TYPE.war |
---|
278 | |
---|
279 | # fix virtual host files |
---|
280 | echo $STAMP updating apache virtual host configuration |
---|
281 | ls $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) |
---|
287 | echo $STAMP reloading Apache |
---|
288 | $APACHE_RESTART > /dev/null 2>&1 |
---|
289 | |
---|
290 | # update revision file |
---|
291 | echo $SVN_REVISION > $TEMP/$PROJECT-$TYPE.revision |
---|
292 | |
---|
293 | # remove lock file |
---|
294 | if [ -f $TEMP/$PROJECT.locked ] |
---|
295 | then |
---|
296 | rm $TEMP/$PROJECT.locked |
---|
297 | fi |
---|
298 | |
---|
299 | echo $STAMP" done deploying "$PROJECT" revision "$SVN_REVISION" (version "$APP_VERSION")" |
---|