Quick commit before delving into python stuff. See you later.
[aquarium] / make-dist.sh
index 3c86ae7..144384f 100755 (executable)
@@ -1,4 +1,33 @@
-#!/usr/bin/env bash
+#!/bin/bash
+
+# Copyright 2012 GRNET S.A. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+#   1. Redistributions of source code must retain the above copyright
+#      notice, this list of conditions and the following disclaimer.
+#
+#  2. Redistributions in binary form must reproduce the above copyright
+#     notice, this list of conditions and the following disclaimer in the
+#     documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+# SUCH DAMAGE.
+#
+# The views and conclusions contained in the software and documentation are
+# those of the authors and should not be interpreted as representing official
+# policies, either expressed or implied, of GRNET S.A.
 
 # Make an Aquarium binary distribution out of current working directory.
 # Use at your own risk (i.e. make sure it compiles etc).
@@ -11,79 +40,223 @@ NOW=`date $DATE_FORMAT`
 DIST=aquarium-$SHA
 SERVER_SCRIPTS_SRC=$WHERE/scripts
 CONF_SRC=$WHERE/src/main/resources
+ARG1="$1"
+
+ERROR=""
+P_VERBOSE="no"
+P_PROPS=""
+P_BUILD="normal"
+P_KEEPDIST="no"
+P_FAKEIT="no"
+P_ARCHIVE=""
+P_OFFLINE="no"
+MVN_OPTS=""
+
+verbose() {
+  if [ "$P_VERBOSE" = "yes" ]; then
+    echo "$@"
+  fi
+}
+
+verbose_p() {
+  verbose "Build type          :" $P_BUILD
+  verbose "Custom configuration:" $P_PROPS
+  verbose "Keep dist/ folder   :" $P_KEEPDIST
+  verbose "Custom achive name  :" $P_ARCHIVE
+  verbose "Fake it             :" $P_FAKEIT
+  verbose "maven offline mode  :" $P_OFFLINE
+}
 
 fail() {
     echo "failed while $1"
     exit 1
 }
 
-checkdist() {
-    [ -e $DIST ] && {
-        echo Folder $DIST exists.
-        echo Please remove it before proceeding
-        exit 1
-    }
-    echo Creating dist dirs
-
-    mkdir -pv $DIST
-    mkdir -pv $DIST/bin
-    mkdir -pv $DIST/lib
-    mkdir -pv $DIST/conf
-    mkdir -pv $DIST/logs
+makedistname() {
+  if [ -n "$P_ARCHIVE" ]; then
+    DIST="$P_ARCHIVE"
+  fi
+}
+
+removedist() {
+  local KEEP=$1
+  if [ "$KEEP" = "no" -a -e $DIST ]; then
+    echo Folder $DIST exists. Removing it.
+    rm -rf "$DIST"
+  fi
+}
+
+createdist() {
+  echo
+  echo Creating dist dirs
+
+  mkdir -pv $DIST
+  mkdir -pv $DIST/bin
+  mkdir -pv $DIST/lib
+  mkdir -pv $DIST/conf
+  mkdir -pv $DIST/logs
 }
 
 clean() {
-    local p="$1"
-    [ "$p"="dev" -o "$p"="fast" -o "$p"="noclean" ] || {
-        mvn clean || fail "cleaning compilation artifacts"
-    }
+  if [ "$P_BUILD" = "normal" ]; then
+    echo
+    echo "==============================="
+    echo "=== mvn clean ================="
+    echo "==============================="
+    echo
+    echo mvn ${MVN_OPTS} clean
+    mvn ${MVN_OPTS} clean || fail "cleaning compilation artifacts"
+    echo
+  elif [ "$P_BUILD"="fast" ]; then
+    echo
+    echo "==============================="
+    echo "=== NOT executing mvn clean ==="
+    echo "==============================="
+    echo
+  fi
 }
 
 collectdeps() {
-    mvn dependency:copy-dependencies && cp target/dependency/*.jar $DIST/lib || fail "collecting dependencies"
+  if [ "$P_BUILD" = "normal" ]; then
+    echo mvn ${MVN_OPTS} dependency:copy-dependencies
+    mvn ${MVN_OPTS} dependency:copy-dependencies
+  fi
+
+  cp target/dependency/*.jar $DIST/lib || fail "collecting dependencies"
 }
 
 build() {
-    echo "Building Aquarium"
-    mvn package -DskipTests && {
-        echo "Copying Aquarium classes"
-        aquariumjar=`find target -type f|egrep "aquarium-[0-9\.]+(-SNAPSHOT)?\.jar"`
-        cp $aquariumjar $DIST/lib || fail "copying $aquariumjar"
-    } || fail "building"
+  echo
+  echo "==============================="
+  echo "=== mvn package ==============="
+  echo "==============================="
+  echo
+  echo mvn ${MVN_OPTS} package -DskipTests
+  mvn ${MVN_OPTS} package -DskipTests && {
+    echo
+    echo "Copying Aquarium classes"
+    aquariumjar=`find target -type f|egrep "aquarium-[0-9\.]+(-SNAPSHOT)?\.jar"`
+    cp $aquariumjar $DIST/lib || fail "copying $aquariumjar"
+  } || fail "building"
 }
 
 collectconf() {
-    echo Copying config files from $CONF_SRC
-    cp $CONF_SRC/log4j.properties $DIST/conf|| fail "copying log4j.properties"
+  echo
+  echo Copying config files from $CONF_SRC
+  echo
+  cp $CONF_SRC/policy.json $DIST/conf          || fail "copying policy.json"
+#  cp $CONF_SRC/roles-agreements.map $DIST/conf || fail "copying roles-agreements.map"
+
+  if [ -n "$P_PROPS" ]; then
+    cp $P_PROPS $DIST/conf/aquarium.properties || fail "copying $P_PROPS"
+  else
     cp $CONF_SRC/aquarium.properties $DIST/conf || fail "copying aquarium.properties"
-    cp $CONF_SRC/policy.yaml $DIST/conf || fail "copying policy.yaml"
-    cp $CONF_SRC/roles-agreements.map $DIST/conf || fail "copying roles-agreements.map"
+  fi
 }
 
 collectscripts() {
-    echo Copying scripts from $SERVER_SCRIPTS_SRC
-    cp $SERVER_SCRIPTS_SRC/aquarium.sh $DIST/bin || fail "copying aquarium.sh"
-    cp $SERVER_SCRIPTS_SRC/test.sh $DIST/bin || fail "copying test.sh"
+  echo
+  echo Copying scripts from $SERVER_SCRIPTS_SRC
+  echo
+  cp $SERVER_SCRIPTS_SRC/aquarium.sh $DIST/bin || fail "copying aquarium.sh"
+  cp $SERVER_SCRIPTS_SRC/start.sh $DIST/bin || fail "copying start.sh"
+  cp $SERVER_SCRIPTS_SRC/stop.sh $DIST/bin || fail "copying stop.sh"
 }
 
 gitmark() {
-    echo $SHAFULL > $DIST/gitsha.txt
+  echo $SHAFULL > $DIST/gitsha.txt
 }
 
 archive() {
-    ARC=$DIST.tar.gz
-    echo "Creating archive"
-    tar zcvf $ARC $DIST/ || fail "creating archive"
-    echo "File $ARC created succesfully"
-    echo "Cleaning up"
-    ls -al $ARC
+  ARC="${DIST}.tar.gz"
+
+  if [ -e "$ARC" ]; then
+    echo
+    echo Removing previous $ARC
+    rm "$ARC"
+  fi
+  echo
+  echo "Creating archive"
+  tar zcvf "$ARC" $DIST/ || fail "creating archive"
+  echo "File $ARC created succesfully"
+  echo "Cleaning up"
+  ls -al "$ARC"
+}
+
+usage() {
+  echo "Usage: $0 [options]"
+  echo ""
+  echo "OPTIONS:"
+  echo "  -a NAME   Use archive NAME."
+  echo "            The full name will be NAME.tar.gz."
+  echo "  -b TYPE   Use build TYPE. One of 'normal', 'fast'."
+  echo "            'normal' is the default and can be omitted."
+  echo "            'fast' means that it will not run mvn clean."
+  echo "  -c FILE   Use FILE as aquarium.properties configuration."
+  echo "  -k        Keep generated dist folder."
+  echo "  -h        Show this message."
+  echo "  -n        As in make -n."
+  echo "  -o        Use mvn in offline mode (--offline, -o)"
+  echo "  -v        Be verbose."
+
+  exit 0
 }
 
-checkdist      && \
-clean          && \
-build          && \
-collectdeps    && \
-collectconf    && \
-collectscripts && \
-gitmark        && \
-archive
\ No newline at end of file
+while getopts ":a:b:hkc:nov" opt
+do
+  case $opt in
+    a) P_ARCHIVE=$OPTARG
+    ;;
+    b) P_BUILD=$OPTARG
+    ;;
+    c) P_PROPS=$OPTARG
+    ;;
+    h) usage
+    ;;
+    k) P_KEEPDIST="yes"
+    ;;
+    n) P_FAKEIT="yes"
+    ;;
+    o) P_OFFLINE="yes"; MVN_OPTS="-o"
+    ;;
+    v) P_VERBOSE="yes"
+    ;;
+    :) ERROR="Option -$OPTARG requires an argument. Aborting..."
+    ;;
+    \?) ERROR="Invalid option: -$OPTARG"
+    ;;
+  esac
+done
+
+if [ -n "$ERROR" ]; then
+  echo $ERROR >&2
+  exit 1
+fi
+
+if [ -n "$P_PROPS" -a ! -f "$P_PROPS" ]; then
+  echo $P_PROPS is not a file. Aborting... >&2
+  exit 1
+fi
+
+if [ ! "$P_BUILD" = "normal" -a ! "$P_BUILD" = "fast" ]; then
+  echo Build type must be one of normal, fast. $P_BUILD was given. Aborting... >&2
+  exit 1
+fi
+
+if [ "$P_FAKEIT" = "yes" ]; then
+  P_VERBOSE=yes verbose_p
+  exit 0
+fi
+
+verbose_p         && \
+makedistname      && \
+removedist     no && \
+createdist        && \
+clean             && \
+build             && \
+collectdeps       && \
+collectconf       && \
+collectscripts    && \
+gitmark           && \
+archive           && \
+removedist $P_KEEPDIST