Quick commit before delving into python stuff. See you later.
[aquarium] / make-dist.sh
1 #!/bin/bash
2
3 # Copyright 2012 GRNET S.A. All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
8 #
9 #   1. Redistributions of source code must retain the above copyright
10 #      notice, this list of conditions and the following disclaimer.
11 #
12 #  2. Redistributions in binary form must reproduce the above copyright
13 #     notice, this list of conditions and the following disclaimer in the
14 #     documentation and/or other materials provided with the distribution.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 # SUCH DAMAGE.
27 #
28 # The views and conclusions contained in the software and documentation are
29 # those of the authors and should not be interpreted as representing official
30 # policies, either expressed or implied, of GRNET S.A.
31
32 # Make an Aquarium binary distribution out of current working directory.
33 # Use at your own risk (i.e. make sure it compiles etc).
34
35 WHERE=`dirname $0`
36 SHAFULL=`git rev-parse HEAD`
37 SHA=`echo $SHAFULL | cut -c 1-11`
38 DATE_FORMAT=+'%Y%m%d%H%M%S'
39 NOW=`date $DATE_FORMAT`
40 DIST=aquarium-$SHA
41 SERVER_SCRIPTS_SRC=$WHERE/scripts
42 CONF_SRC=$WHERE/src/main/resources
43 ARG1="$1"
44
45 ERROR=""
46 P_VERBOSE="no"
47 P_PROPS=""
48 P_BUILD="normal"
49 P_KEEPDIST="no"
50 P_FAKEIT="no"
51 P_ARCHIVE=""
52 P_OFFLINE="no"
53 MVN_OPTS=""
54
55 verbose() {
56   if [ "$P_VERBOSE" = "yes" ]; then
57     echo "$@"
58   fi
59 }
60
61 verbose_p() {
62   verbose "Build type          :" $P_BUILD
63   verbose "Custom configuration:" $P_PROPS
64   verbose "Keep dist/ folder   :" $P_KEEPDIST
65   verbose "Custom achive name  :" $P_ARCHIVE
66   verbose "Fake it             :" $P_FAKEIT
67   verbose "maven offline mode  :" $P_OFFLINE
68 }
69
70 fail() {
71     echo "failed while $1"
72     exit 1
73 }
74
75 makedistname() {
76   if [ -n "$P_ARCHIVE" ]; then
77     DIST="$P_ARCHIVE"
78   fi
79 }
80
81 removedist() {
82   local KEEP=$1
83   if [ "$KEEP" = "no" -a -e $DIST ]; then
84     echo Folder $DIST exists. Removing it.
85     rm -rf "$DIST"
86   fi
87 }
88
89 createdist() {
90   echo
91   echo Creating dist dirs
92
93   mkdir -pv $DIST
94   mkdir -pv $DIST/bin
95   mkdir -pv $DIST/lib
96   mkdir -pv $DIST/conf
97   mkdir -pv $DIST/logs
98 }
99
100 clean() {
101   if [ "$P_BUILD" = "normal" ]; then
102     echo
103     echo "==============================="
104     echo "=== mvn clean ================="
105     echo "==============================="
106     echo
107     echo mvn ${MVN_OPTS} clean
108     mvn ${MVN_OPTS} clean || fail "cleaning compilation artifacts"
109     echo
110   elif [ "$P_BUILD"="fast" ]; then
111     echo
112     echo "==============================="
113     echo "=== NOT executing mvn clean ==="
114     echo "==============================="
115     echo
116   fi
117 }
118
119 collectdeps() {
120   if [ "$P_BUILD" = "normal" ]; then
121     echo mvn ${MVN_OPTS} dependency:copy-dependencies
122     mvn ${MVN_OPTS} dependency:copy-dependencies
123   fi
124
125   cp target/dependency/*.jar $DIST/lib || fail "collecting dependencies"
126 }
127
128 build() {
129   echo
130   echo "==============================="
131   echo "=== mvn package ==============="
132   echo "==============================="
133   echo
134   echo mvn ${MVN_OPTS} package -DskipTests
135   mvn ${MVN_OPTS} package -DskipTests && {
136     echo
137     echo "Copying Aquarium classes"
138     aquariumjar=`find target -type f|egrep "aquarium-[0-9\.]+(-SNAPSHOT)?\.jar"`
139     cp $aquariumjar $DIST/lib || fail "copying $aquariumjar"
140   } || fail "building"
141 }
142
143 collectconf() {
144   echo
145   echo Copying config files from $CONF_SRC
146   echo
147   cp $CONF_SRC/policy.json $DIST/conf          || fail "copying policy.json"
148 #  cp $CONF_SRC/roles-agreements.map $DIST/conf || fail "copying roles-agreements.map"
149
150   if [ -n "$P_PROPS" ]; then
151     cp $P_PROPS $DIST/conf/aquarium.properties || fail "copying $P_PROPS"
152   else
153     cp $CONF_SRC/aquarium.properties $DIST/conf || fail "copying aquarium.properties"
154   fi
155 }
156
157 collectscripts() {
158   echo
159   echo Copying scripts from $SERVER_SCRIPTS_SRC
160   echo
161   cp $SERVER_SCRIPTS_SRC/aquarium.sh $DIST/bin || fail "copying aquarium.sh"
162   cp $SERVER_SCRIPTS_SRC/start.sh $DIST/bin || fail "copying start.sh"
163   cp $SERVER_SCRIPTS_SRC/stop.sh $DIST/bin || fail "copying stop.sh"
164 }
165
166 gitmark() {
167   echo $SHAFULL > $DIST/gitsha.txt
168 }
169
170 archive() {
171   ARC="${DIST}.tar.gz"
172
173   if [ -e "$ARC" ]; then
174     echo
175     echo Removing previous $ARC
176     rm "$ARC"
177   fi
178   echo
179   echo "Creating archive"
180   tar zcvf "$ARC" $DIST/ || fail "creating archive"
181   echo "File $ARC created succesfully"
182   echo "Cleaning up"
183   ls -al "$ARC"
184 }
185
186 usage() {
187   echo "Usage: $0 [options]"
188   echo ""
189   echo "OPTIONS:"
190   echo "  -a NAME   Use archive NAME."
191   echo "            The full name will be NAME.tar.gz."
192   echo "  -b TYPE   Use build TYPE. One of 'normal', 'fast'."
193   echo "            'normal' is the default and can be omitted."
194   echo "            'fast' means that it will not run mvn clean."
195   echo "  -c FILE   Use FILE as aquarium.properties configuration."
196   echo "  -k        Keep generated dist folder."
197   echo "  -h        Show this message."
198   echo "  -n        As in make -n."
199   echo "  -o        Use mvn in offline mode (--offline, -o)"
200   echo "  -v        Be verbose."
201
202   exit 0
203 }
204
205 while getopts ":a:b:hkc:nov" opt
206 do
207   case $opt in
208     a) P_ARCHIVE=$OPTARG
209     ;;
210     b) P_BUILD=$OPTARG
211     ;;
212     c) P_PROPS=$OPTARG
213     ;;
214     h) usage
215     ;;
216     k) P_KEEPDIST="yes"
217     ;;
218     n) P_FAKEIT="yes"
219     ;;
220     o) P_OFFLINE="yes"; MVN_OPTS="-o"
221     ;;
222     v) P_VERBOSE="yes"
223     ;;
224     :) ERROR="Option -$OPTARG requires an argument. Aborting..."
225     ;;
226     \?) ERROR="Invalid option: -$OPTARG"
227     ;;
228   esac
229 done
230
231 if [ -n "$ERROR" ]; then
232   echo $ERROR >&2
233   exit 1
234 fi
235
236 if [ -n "$P_PROPS" -a ! -f "$P_PROPS" ]; then
237   echo $P_PROPS is not a file. Aborting... >&2
238   exit 1
239 fi
240
241 if [ ! "$P_BUILD" = "normal" -a ! "$P_BUILD" = "fast" ]; then
242   echo Build type must be one of normal, fast. $P_BUILD was given. Aborting... >&2
243   exit 1
244 fi
245
246 if [ "$P_FAKEIT" = "yes" ]; then
247   P_VERBOSE=yes verbose_p
248   exit 0
249 fi
250
251 verbose_p         && \
252 makedistname      && \
253 removedist     no && \
254 createdist        && \
255 clean             && \
256 build             && \
257 collectdeps       && \
258 collectconf       && \
259 collectscripts    && \
260 gitmark           && \
261 archive           && \
262 removedist $P_KEEPDIST