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