Doc for AQUARIUM_HOME and move HERE to resource locator
[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
52 verbose() {
53   if [ "$P_VERBOSE" = "yes" ]; then
54     echo "$@"
55   fi
56 }
57
58 verbose_p() {
59   verbose "Build type          :" $P_BUILD
60   verbose "Custom configuration:" $P_PROPS
61   verbose "Keep dist/ folder   :" $P_KEEPDIST
62   verbose "Fake it             :" $P_FAKEIT
63 }
64
65 fail() {
66     echo "failed while $1"
67     exit 1
68 }
69
70 removedist() {
71   local KEEP=$1
72   if [ "$KEEP" = "no" -a -e $DIST ]; then
73     echo Folder $DIST exists. Removing it.
74     rm -rf "$DIST"
75   fi
76 }
77
78 createdist() {
79   echo
80   echo Creating dist dirs
81
82   mkdir -pv $DIST
83   mkdir -pv $DIST/bin
84   mkdir -pv $DIST/lib
85   mkdir -pv $DIST/conf
86   mkdir -pv $DIST/logs
87 }
88
89 clean() {
90   if [ "$P_BUILD" = "normal" ]; then
91     echo
92     echo "==============================="
93     echo "=== mvn clean ================="
94     echo "==============================="
95     echo
96     mvn clean || fail "cleaning compilation artifacts"
97     echo
98   elif [ "$P_BUILD"="fast" ]; then
99     echo
100     echo "==============================="
101     echo "=== NOT executing mvn clean ==="
102     echo "==============================="
103     echo
104   fi
105 }
106
107 collectdeps() {
108   mvn dependency:copy-dependencies && cp target/dependency/*.jar $DIST/lib || fail "collecting dependencies"
109 }
110
111 build() {
112   echo
113   echo "==============================="
114   echo "=== mvn package ==============="
115   echo "==============================="
116   echo
117   mvn package -DskipTests && {
118     echo
119     echo "Copying Aquarium classes"
120     aquariumjar=`find target -type f|egrep "aquarium-[0-9\.]+(-SNAPSHOT)?\.jar"`
121     cp $aquariumjar $DIST/lib || fail "copying $aquariumjar"
122   } || fail "building"
123 }
124
125 collectconf() {
126   echo
127   echo Copying config files from $CONF_SRC
128   echo
129   cp $CONF_SRC/logback.xml $DIST/conf|| fail "copying logback.xml"
130   cp $CONF_SRC/policy.yaml $DIST/conf || fail "copying policy.yaml"
131   cp $CONF_SRC/roles-agreements.map $DIST/conf || fail "copying roles-agreements.map"
132
133   if [ -n "$P_PROPS" ]; then
134     cp $P_PROPS $DIST/conf/aquarium.properties || fail "copying $P_PROPS"
135   else
136     cp $CONF_SRC/aquarium.properties $DIST/conf || fail "copying aquarium.properties"
137   fi
138 }
139
140 collectscripts() {
141   echo
142   echo Copying scripts from $SERVER_SCRIPTS_SRC
143   echo
144   cp $SERVER_SCRIPTS_SRC/aquarium.sh $DIST/bin || fail "copying aquarium.sh"
145   cp $SERVER_SCRIPTS_SRC/start.sh $DIST/bin || fail "copying start.sh"
146   cp $SERVER_SCRIPTS_SRC/stop.sh $DIST/bin || fail "copying stop.sh"
147   cp $SERVER_SCRIPTS_SRC/test.sh $DIST/bin || fail "copying test.sh"
148 }
149
150 gitmark() {
151   echo $SHAFULL > $DIST/gitsha.txt
152 }
153
154 archive() {
155   ARC=$DIST.tar.gz
156   if [ -e "$ARC" ]; then
157     echo
158     echo Removing previous $ARC
159   fi
160   echo
161   echo "Creating archive"
162   tar zcvf $ARC $DIST/ || fail "creating archive"
163   echo "File $ARC created succesfully"
164   echo "Cleaning up"
165   ls -al $ARC
166 }
167
168 usage() {
169   echo "Usage: $0 [options]"
170   echo ""
171   echo "OPTIONS:"
172   echo "  -b TYPE   Use build TYPE. One of 'normal', 'fast'."
173   echo "            'normal' is the default and can be omitted."
174   echo "            'fast' means that it will not run mvn clean."
175   echo "  -c FILE   Use FILE as aquarium.properties configuration."
176   echo "  -k        Keep generated dist folder."
177   echo "  -h        Show this message."
178   echo "  -n        As in make -n."
179   echo "  -v        Be verbose."
180
181   exit 0
182 }
183
184 while getopts ":b:hkc:nv" opt
185 do
186   case $opt in
187     b) P_BUILD=$OPTARG
188     ;;
189     c) P_PROPS=$OPTARG
190     ;;
191     h) usage
192     ;;
193     k) P_KEEPDIST="yes"
194     ;;
195     n) P_FAKEIT="yes"
196     ;;
197     v) P_VERBOSE="yes"
198     ;;
199     :) ERROR="Option -$OPTARG requires an argument. Aborting..."
200     ;;
201     \?) ERROR="Invalid option: -$OPTARG"
202     ;;
203   esac
204 done
205
206 if [ -n "$ERROR" ]; then
207   echo $ERROR >&2
208   exit 1
209 fi
210
211 if [ -n "$P_PROPS" -a ! -f "$P_PROPS" ]; then
212   echo $P_PROPS is not a file. Aborting... >&2
213   exit 1
214 fi
215
216 if [ ! "$P_BUILD" = "normal" -a ! "$P_BUILD" = "fast" ]; then
217   echo Build type must be one of normal, fast. $P_BUILD was given. Aborting... >&2
218   exit 1
219 fi
220
221 if [ "$P_FAKEIT" = "yes" ]; then
222   P_VERBOSE=yes verbose_p
223   exit 0
224 fi
225
226 verbose_p         && \
227 removedist     no && \
228 createdist        && \
229 clean             && \
230 build             && \
231 collectdeps       && \
232 collectconf       && \
233 collectscripts    && \
234 gitmark           && \
235 archive           && \
236 removedist $P_KEEPDIST