From 84f3bc30b14d67482facfa05d2e60584148079cf Mon Sep 17 00:00:00 2001
From: felmer <felmer>
Date: Tue, 6 Sep 2011 08:22:07 +0000
Subject: [PATCH] LMS-2471 bugs fixed, making scripts more robust

SVN: 22806
---
 .../check-and-exit-if-new-sprint-server.sh    | 44 ++++++++++++++-----
 .../source/bash/create-config-snapshot.sh     |  3 ++
 .../source/bash/create-generic-snapshot.sh    |  2 +
 openbis_all/source/bash/create-snapshot.sh    | 15 +++----
 .../bash/create-sprint-screening-snapshot.sh  |  2 +
 openbis_all/source/bash/fetch-ci-artifacts.sh |  3 ++
 .../fetch-generic-sprint-server-artifacts.sh  |  2 +
 ...fetch-screening-sprint-server-artifacts.sh |  2 +
 openbis_all/source/bash/install-servers.sh    |  4 +-
 .../bash/nightly-upgrade-generic-openbis.sh   |  5 ++-
 .../bash/nightly-upgrade-screening-openbis.sh |  8 +++-
 openbis_all/source/bash/servers-shutdown.sh   |  2 +
 .../servers-startup-from-latest-snapshot.sh   |  2 +
 .../bash/servers-startup-from-snapshot.sh     |  2 +
 openbis_all/source/bash/servers-startup.sh    |  2 +
 15 files changed, 74 insertions(+), 24 deletions(-)

diff --git a/openbis_all/source/bash/check-and-exit-if-new-sprint-server.sh b/openbis_all/source/bash/check-and-exit-if-new-sprint-server.sh
index 6b9debc5846..400bfe6a589 100755
--- a/openbis_all/source/bash/check-and-exit-if-new-sprint-server.sh
+++ b/openbis_all/source/bash/check-and-exit-if-new-sprint-server.sh
@@ -1,37 +1,57 @@
 #! /bin/bash
 # 
 # This script checks whether openBIS AS server is a sprint server installed in the current week
-# or not. If yes exit (with exit code 0) is executed.
+# or not. If yes it exits with exit code 1.
 # 
-# usage: check-and-exit-if-new-sprint-server.sh <servers>
+# usage: check-and-exit-if-new-sprint-server.sh <servers> <version file>
 # 
 # where 
 # <servers> is the path to the directory containing the server folders 'openBIS-server' 
 #    and 'datastore_server',
+# <version file> is a file which will contain the time stamp of first installation of 
+#    a major version. If it doesn't exist it will be created.
 # 
-if [ $# -ne 1 ]; then
-    echo "Usage: check-and-exit-if-new-sprint-server.sh <servers>"
-    exit 1
-fi
+# 
+set -o nounset
+set -o errexit
+
+function getValue {
+    file="$1"
+    if [ -f "$file" ]; then
+        awk -F ' *= *' -v key=$2 '{map[$1] = $2} END {print map[key]}' "$file"
+    fi
+}
 
 SERVERS="$1"
+VERSION_FILE="$2"
 LOG_FILE=$SERVERS/openBIS-server/jetty/logs/jetty.out
 
 CURRENT_VERSION=UNKNOWN
 if [ -f $LOG_FILE ]; then
-    CURRENT_VERSION=`awk '/STATUS.CISDContextLoaderListener - Version/ {print $1" "$2" "$8" "$9}' $LOG_FILE | tail -1`
+    CURRENT_VERSION=`awk '/STATUS.CISDContextLoaderListener - Version/ {print $1" "$2" "$8" "$9}' $LOG_FILE | head -n 1`
+    CURRENT_VERSION_NAME=`echo $CURRENT_VERSION|awk '{print $3}'`
+    if [ $CURRENT_VERSION_NAME == "" ]; then
+        echo "Current version of openBIS Application Server not known"
+        exit
+    fi
     echo "Current openBIS Application Server: $CURRENT_VERSION"
-    if [ "SNAPSHOT" != "`echo $CURRENT_VERSION|awk '{print $3}'`" ]; then
-        TIME_STAMP="`echo $CURRENT_VERSION|awk '{print $1, $2}'`"
-        WEEK=`date --date="$TIME_STAMP" "+%W"`
-        if [ $? -ne 0 ]; then
+    if [ "SNAPSHOT" != $CURRENT_VERSION_NAME ]; then
+        CURRENT_MAJOR_VERSION=${CURRENT_VERSION_NAME%.*}
+        echo "Current major version: $CURRENT_MAJOR_VERSION"
+        TIME_STAMP=`getValue "$VERSION_FILE" $CURRENT_MAJOR_VERSION`
+        if [ -z "$TIME_STAMP" ]; then 
+            TIME_STAMP="`echo $CURRENT_VERSION|awk '{print $1, $2}'`"
+            echo "$CURRENT_MAJOR_VERSION = $TIME_STAMP" >> "$VERSION_FILE"
+        fi
+        echo "Time stamp of major version: $TIME_STAMP"
+        if ! WEEK=`date --date="$TIME_STAMP" "+%W"`; then
             # Mac OSX has a different flavor of date command
             WEEK=`date -j -f "%Y-%m-%d %H:%M:%S" "$TIME_STAMP" "+%W"`
         fi
         CURRENT_WEEK=`date "+%W"`
         if [ "$WEEK" = "$CURRENT_WEEK" ]; then
             echo "Sprint server not replaced until next week."
-            exit
+            exit 1
         fi
     fi 
 fi
diff --git a/openbis_all/source/bash/create-config-snapshot.sh b/openbis_all/source/bash/create-config-snapshot.sh
index 6b4327fc0da..3bcdfbd58a9 100755
--- a/openbis_all/source/bash/create-config-snapshot.sh
+++ b/openbis_all/source/bash/create-config-snapshot.sh
@@ -11,6 +11,9 @@
 #    Empty lines and lines starting with '#' are ignored
 # 
 # 
+set -o nounset
+set -o errexit
+
 if [ $# -lt 3 ]; then
     echo "Usage: create-config-snapshot.sh <servers> <snapshot repository> <config file list 1> ... <config file list n>"
     exit 1
diff --git a/openbis_all/source/bash/create-generic-snapshot.sh b/openbis_all/source/bash/create-generic-snapshot.sh
index a3456fb3981..c897767fe47 100644
--- a/openbis_all/source/bash/create-generic-snapshot.sh
+++ b/openbis_all/source/bash/create-generic-snapshot.sh
@@ -10,6 +10,8 @@
 # Dependencies:
 # - create-snapshot.sh
 #
+set -o nounset
+set -o errexit
 
 BIN_DIR=`dirname "$0"`
 REPOSITORY=~/snapshots
diff --git a/openbis_all/source/bash/create-snapshot.sh b/openbis_all/source/bash/create-snapshot.sh
index 5797e4cbb12..931429f0435 100755
--- a/openbis_all/source/bash/create-snapshot.sh
+++ b/openbis_all/source/bash/create-snapshot.sh
@@ -20,6 +20,9 @@
 # - Store dump doesn't contain archived data sets and data sets in a share which is a symbolic link. 
 # - The configuration file of the argument is stored in the snapshot. It is used for restoring.
 # 
+set -o nounset
+set -o errexit
+
 function getValue {
     file=$1
     key=$2
@@ -91,8 +94,7 @@ for path in "$STORE"/*; do
         else
             echo "Start dumping share $file_name."
             parent_folder=${path:0:$index_of_last_slash}
-            tar -rf "$SNAPSHOT/store.tar" -C "$parent_folder" $file_name 
-            if [ $? -ne 0 ]; then
+            if ! tar -rf "$SNAPSHOT/store.tar" -C "$parent_folder" $file_name; then
                 echo "Error while dumping share $file_name. Snapshot creation aborted."
                 exit 1
             fi
@@ -103,23 +105,20 @@ done
 echo "Dump of store $STORE has been successfully created."
 ############## dump databases ##############
 for db in $DATABASES; do
-    pg_dump -U postgres -O $db > "$SNAPSHOT/$db.sql"
-    if [ $? -ne 0 ]; then
+    if ! pg_dump -U postgres -O $db > "$SNAPSHOT/$db.sql"; then
         echo "Error dumping database '$db'. Snapshot creation aborted."
         exit 1
     fi
     echo "Database '$db' has been successfully dumped."
 done
 ############## dump index ##############
-tar -cf "$SNAPSHOT/index.tar" -C "$INDEX" .
-if [ $? -ne 0 ]; then
+if ! tar -cf "$SNAPSHOT/index.tar" -C "$INDEX" .; then
     echo "Error creating index dump. Snapshot creation aborted."
     exit 1
 fi
 echo "Dump of index $INDEX has been successfully created."
 ############## packaging ##############
-tar -zcf "$SNAPSHOT.tgz" -C "$REPOSITORY" "$SNAPSHOT_FOLDER_NAME" 
-if [ $? -ne 0 ]; then
+if ! tar -zcf "$SNAPSHOT.tgz" -C "$REPOSITORY" "$SNAPSHOT_FOLDER_NAME"; then
     echo "Error packaging snapshot $SNAPSHOT."
     exit 1
 fi
diff --git a/openbis_all/source/bash/create-sprint-screening-snapshot.sh b/openbis_all/source/bash/create-sprint-screening-snapshot.sh
index 6c31a432931..c4c7b09472f 100644
--- a/openbis_all/source/bash/create-sprint-screening-snapshot.sh
+++ b/openbis_all/source/bash/create-sprint-screening-snapshot.sh
@@ -10,6 +10,8 @@
 # Dependencies:
 # - create-snapshot.sh
 #
+set -o nounset
+set -o errexit
 
 BIN_DIR=`dirname "$0"`
 REPOSITORY=~/screening/snapshots
diff --git a/openbis_all/source/bash/fetch-ci-artifacts.sh b/openbis_all/source/bash/fetch-ci-artifacts.sh
index 83b75c25de8..aec356347e1 100755
--- a/openbis_all/source/bash/fetch-ci-artifacts.sh
+++ b/openbis_all/source/bash/fetch-ci-artifacts.sh
@@ -10,6 +10,9 @@
 # If the p option is specified only artifact file names matching the regular expression will 
 # be loaded from Hudson. 
 #
+set -o nounset
+set -o errexit
+
 CI_HOST=cisd-ci.ethz.ch:8090
 
 if [ $# -lt 1 ]; then
diff --git a/openbis_all/source/bash/fetch-generic-sprint-server-artifacts.sh b/openbis_all/source/bash/fetch-generic-sprint-server-artifacts.sh
index 49cd3a24b9a..60487b0d093 100755
--- a/openbis_all/source/bash/fetch-generic-sprint-server-artifacts.sh
+++ b/openbis_all/source/bash/fetch-generic-sprint-server-artifacts.sh
@@ -5,6 +5,8 @@
 # Dependencies:
 # - fetch-ci-artifacts.sh
 #
+set -o nounset
+set -o errexit
 
 BIN_DIR=`dirname "$0"`
 SCRIPT="$BIN_DIR/fetch-ci-artifacts.sh"
diff --git a/openbis_all/source/bash/fetch-screening-sprint-server-artifacts.sh b/openbis_all/source/bash/fetch-screening-sprint-server-artifacts.sh
index cf6d56042a2..fabb471127b 100755
--- a/openbis_all/source/bash/fetch-screening-sprint-server-artifacts.sh
+++ b/openbis_all/source/bash/fetch-screening-sprint-server-artifacts.sh
@@ -5,6 +5,8 @@
 # Dependencies:
 # - fetch-ci-artifacts.sh
 #
+set -o nounset
+set -o errexit
 
 BIN_DIR=`dirname "$0"`
 SCRIPT="$BIN_DIR/fetch-ci-artifacts.sh"
diff --git a/openbis_all/source/bash/install-servers.sh b/openbis_all/source/bash/install-servers.sh
index fe8294fee2d..38c019cc861 100755
--- a/openbis_all/source/bash/install-servers.sh
+++ b/openbis_all/source/bash/install-servers.sh
@@ -12,8 +12,10 @@
 # - restore-config-snapshot.sh
 # - install.sh of the openBIS AS distribution
 # 
+set -o nounset
+set -o errexit
 
-if [ $# -le 4 ]; then
+if [ $# -lt 4 ]; then
     echo "Usage: install-servers.sh <servers> <config snapshot repository> <builds fetching script> <config file list 1> ... <config file list n>"
     exit 1
 fi
diff --git a/openbis_all/source/bash/nightly-upgrade-generic-openbis.sh b/openbis_all/source/bash/nightly-upgrade-generic-openbis.sh
index 462789d6d1a..4641de8811c 100644
--- a/openbis_all/source/bash/nightly-upgrade-generic-openbis.sh
+++ b/openbis_all/source/bash/nightly-upgrade-generic-openbis.sh
@@ -17,13 +17,16 @@
 # - servers-startup-from-latest-snapshot.sh
 # - config-files.txt
 #
+set -o nounset
+set -o errexit
 
 BIN_DIR=`dirname "$0"`
 SERVERS=sprint
+VERSION_FILE=sprint-versions.txt
 
 echo ":::::::::::::::::::: Nightly Upgrade Generic openBIS Servers [`date`] :::::::::::::::::::::"
 
-"$BIN_DIR/check-and-exit-if-new-sprint-server.sh" "$SERVERS"
+if ! "$BIN_DIR/check-and-exit-if-new-sprint-server.sh" "$SERVERS" "$VERSION_FILE"; then exit; fi
 
 ##################################################
 #
diff --git a/openbis_all/source/bash/nightly-upgrade-screening-openbis.sh b/openbis_all/source/bash/nightly-upgrade-screening-openbis.sh
index 07719193991..86d015b22e3 100644
--- a/openbis_all/source/bash/nightly-upgrade-screening-openbis.sh
+++ b/openbis_all/source/bash/nightly-upgrade-screening-openbis.sh
@@ -18,14 +18,18 @@
 # - config-files.txt
 # - config-files-screening.txt
 #
+set -o nounset
+set -o errexit
 
 BIN_DIR=`dirname "$0"`
 SERVERS=screening/servers
+VERSION_FILE=screening/sprint-versions.txt
 
 echo ":::::::::::::::::::: Nightly Upgrade Screening openBIS Servers [`date`] :::::::::::::::::::::"
 
-"$BIN_DIR/check-and-exit-if-new-sprint-server.sh" "$SERVERS"
-
+if ! "$BIN_DIR/check-and-exit-if-new-sprint-server.sh" "$SERVERS" "$VERSION_FILE"; then exit; fi
+echo we exit
+exit
 ##################################################
 #
 # Upgrade servers and restart them
diff --git a/openbis_all/source/bash/servers-shutdown.sh b/openbis_all/source/bash/servers-shutdown.sh
index e1fec77600e..baba4fc49c6 100755
--- a/openbis_all/source/bash/servers-shutdown.sh
+++ b/openbis_all/source/bash/servers-shutdown.sh
@@ -10,6 +10,8 @@
 #   <servers folder>/datastore_server
 #   If one of these folders do not exists shutdown is aborted.
 #
+set -o nounset
+set -o errexit
 
 if [ $# -ne 1 ]; then
     echo "Usage: servers-shutdown.sh <servers folder>"
diff --git a/openbis_all/source/bash/servers-startup-from-latest-snapshot.sh b/openbis_all/source/bash/servers-startup-from-latest-snapshot.sh
index 0e3c3ecfcbf..7378fca6e00 100755
--- a/openbis_all/source/bash/servers-startup-from-latest-snapshot.sh
+++ b/openbis_all/source/bash/servers-startup-from-latest-snapshot.sh
@@ -14,6 +14,8 @@
 # Dependencies:
 # - servers-startup-from-snapshot.sh
 #
+set -o nounset
+set -o errexit
 
 if [ $# -ne 2 ]; then
     echo "Usage: servers-startup-from-latest-snapshot.sh <servers> <snapshot repository>"
diff --git a/openbis_all/source/bash/servers-startup-from-snapshot.sh b/openbis_all/source/bash/servers-startup-from-snapshot.sh
index 9a8c3b0ad34..8dd6a9abad8 100755
--- a/openbis_all/source/bash/servers-startup-from-snapshot.sh
+++ b/openbis_all/source/bash/servers-startup-from-snapshot.sh
@@ -17,6 +17,8 @@
 # - servers-shutdown.sh
 # - servers-startup.sh
 #
+set -o nounset
+set -o errexit
 
 function getValue {
     file=$1
diff --git a/openbis_all/source/bash/servers-startup.sh b/openbis_all/source/bash/servers-startup.sh
index 5333ef1c56b..1692f2b9389 100755
--- a/openbis_all/source/bash/servers-startup.sh
+++ b/openbis_all/source/bash/servers-startup.sh
@@ -10,6 +10,8 @@
 #   <servers folder>/datastore_server
 #   If one of these folders do not exists there will be no start up.
 #
+set -o nounset
+set -o errexit
 
 if [ $# -ne 1 ]; then
     echo "Usage: servers-startup.sh <servers folder>"
-- 
GitLab