From f93a3735fe49f331b383a7659b52b5168ac2c972 Mon Sep 17 00:00:00 2001
From: Daniel <daniel@harvey>
Date: Fri, 27 Mar 2020 13:13:14 +0100
Subject: [PATCH] FIX: Runs in Docker again.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Also:

- More strict `set -e`
- Global path variables.
- A few more comments
- Copyright notices

== Technically, this is a big squash merge: ===

Squashed commit of the following:

commit 5c26e223e78192ba3d788ab2c640e8c3a1c73866
Author: Daniel <daniel@harvey>
Date:   Fri Mar 27 13:11:43 2020 +0100

    MAINT: No warning if mysql_config_editor can not be found.

commit 06c3f1d25e3bd1a37942f7f0630daac82c8a7738
Author: Daniel <daniel@harvey>
Date:   Fri Mar 27 12:57:15 2020 +0100

    MAINT: Moved `set -e` more to the front.

commit 68ad8469e0dd8c5dc3c4d93f41b4902e622f579f
Author: Daniel <daniel@harvey>
Date:   Fri Mar 27 12:56:37 2020 +0100

    MAINT: Removed debugging output, added copyright lines.

commit 7ed57cdaf1d1299b5cb8d412988409410eee9b45
Author: Daniel <daniel@harvey>
Date:   Fri Mar 27 11:59:37 2020 +0100

    MAINT: moved MAINPATH definition into load_settings.sh

commit b34f82812a59e8913d40d71c3ae34f3f0106fdf2
Author: Daniel <daniel@harvey>
Date:   Fri Mar 27 10:59:57 2020 +0100

    MAINT: A bit of cleanup.

commit 4a88516991f39091edf628bcad066b6873b347c0
Author: Daniel <daniel@harvey>
Date:   Fri Mar 27 09:35:16 2020 +0100

    STY: Whitespace fixup.

commit 4724ee7014c65d26fe19cf1d530d45318056b5e3
Author: Henrik tom Wörden <henrik@trineo.org>
Date:   Fri Mar 27 08:22:53 2020 +0100

    FIX: added missing env setting in applypatches

commit 1d79730089c040cdd0751b59d347c290c01207f1
Author: Henrik tom Wörden <henrik@trineo.org>
Date:   Fri Mar 27 08:18:47 2020 +0100

    MAINT: make calling skripts more cwd independent

commit c908d69b6a945cb45e53bf9602dd6c684834ea84
Author: Daniel <daniel@harvey>
Date:   Thu Mar 26 19:48:58 2020 +0100

    MAINT: Failing fast if no SQL is found.

commit 2fa7d1bbfa127e23c16c1e06d3f5667e47e58058
Author: Daniel <daniel@harvey>
Date:   Thu Mar 26 19:26:57 2020 +0100

    Debug

commit a520f4a2f5d95341e762bb7491a3df69be983622
Author: Daniel <daniel@harvey>
Date:   Thu Mar 26 19:22:48 2020 +0100

    Debug

commit fb60953d3d1f3a3cd64a218cc3ae8c5a96843bb0
Author: Daniel <daniel@harvey>
Date:   Thu Mar 26 19:10:56 2020 +0100

    FIX: Loading load_settings.sh

commit d176828716313484df56f89887a20ff97767a55f
Author: Daniel <daniel@harvey>
Date:   Thu Mar 26 19:01:40 2020 +0100

    DBG .

commit b1c5d47ca98735d6f1780572dab4c338254aadb3
Author: Daniel <daniel@harvey>
Date:   Thu Mar 26 18:55:21 2020 +0100

    FIX: Load definitions first.

commit ed5c4d9c722690b5fe9492aa16bcc7528971a793
Author: Daniel <daniel@harvey>
Date:   Thu Mar 26 18:44:03 2020 +0100

    DBG

commit 0763ef3af2eebbbaf3f0c3caa6e8192a7bfe6a2f
Author: Daniel <daniel@harvey>
Date:   Thu Mar 26 18:19:40 2020 +0100

    FIX: Set UTILSPATH in applyPatches.sh

commit f351acebea56076f1a30bbbb46930ca19f7b5c9e
Author: Daniel <daniel@harvey>
Date:   Thu Mar 26 18:08:03 2020 +0100

    FIX: More strinct error handling, message if commands are not found.

commit 0338357e48702e1b0b7b228adbec1b3b77473045
Author: Daniel <daniel@harvey>
Date:   Thu Mar 26 17:26:04 2020 +0100

    DBG .

commit 8f0545c26ec06f4ad271875335aaa243adbaaf85
Author: Daniel <daniel@harvey>
Date:   Thu Mar 26 16:46:15 2020 +0100

    MAINT: Earlier set -e, also documentation.
---
 config.defaults                |  8 ++--
 patches/applyPatches.sh        | 15 ++++++-
 utils/backup.sh                | 19 ++++-----
 utils/helpers.sh               | 33 ++++++++++-----
 utils/load_settings.sh         | 37 ++++++++++++++---
 utils/make_db                  | 74 +++++++++++++++++-----------------
 utils/patch_header.sh          | 15 +++++--
 utils/update_sql_procedures.sh | 18 ++++++---
 8 files changed, 141 insertions(+), 78 deletions(-)

diff --git a/config.defaults b/config.defaults
index bd669cb..bf6c941 100644
--- a/config.defaults
+++ b/config.defaults
@@ -23,13 +23,13 @@
 
 # # Commands
 # The MySQL client program.
-MYSQL_CMD=$(command -v mysql)
+MYSQL_CMD=$(command -v mysql || echo "mysql not found!" >&2)
 # The mysqladmin program which comes with the MySQL client.
-MYSQLADMIN_CMD=$(command -v mysqladmin)
+MYSQLADMIN_CMD=$(command -v mysqladmin || echo "mysqladmin not found!" >&2)
 # The mysqldump program which comes with the MySQL client.
-MYSQLDUMP_CMD=$(command -v mysqldump)
+MYSQLDUMP_CMD=$(command -v mysqldump || echo "mysqldump not found!" >&2)
 # The mysql_config_editor program which is used to store the credentials.
-MYSQL_CONFIG_EDITOR_CMD=$(command -v mysql_config_editor)
+MYSQL_CONFIG_EDITOR_CMD=$(command -v mysql_config_editor || true)
 
 # # MySQL Connection
 # The host of the MySQL server.
diff --git a/patches/applyPatches.sh b/patches/applyPatches.sh
index 56cbac7..735dd3e 100755
--- a/patches/applyPatches.sh
+++ b/patches/applyPatches.sh
@@ -6,6 +6,7 @@
 # Copyright (C) 2018 Research Group Biomedical Physics,
 # Max-Planck-Institute for Dynamics and Self-Organization Göttingen
 # Copyright (C) 2020 Timm Fitschen <t.fitschen@indiscale.com>
+# Copyright (C) 2020 Daniel Hornung <d.hornung@indiscale.com>
 # Copyright (C) 2020 IndiScale <info@indiscale.com>
 #
 # This program is free software: you can redistribute it and/or modify
@@ -24,8 +25,20 @@
 # ** end header
 #
 
-#apply all available patches.
+###############################################################################
+#                         Apply all available patches                         #
+###############################################################################
+
 set -e
+
+if [ -z "$UTILSPATH" ]; then
+    UTILSPATH=$(realpath "$(dirname $0)/../utils")
+    export UTILSPATH
+fi
+
+source $UTILSPATH/load_settings.sh
+source $UTILSPATH/helpers.sh
+
 PATCHES="./patch*/patch.sh"
 
 for p in $PATCHES
diff --git a/utils/backup.sh b/utils/backup.sh
index 752c4e4..56ca609 100755
--- a/utils/backup.sh
+++ b/utils/backup.sh
@@ -5,6 +5,8 @@
 #
 # Copyright (C) 2018 Research Group Biomedical Physics,
 # Max-Planck-Institute for Dynamics and Self-Organization Göttingen
+# Copyright (C) 2020 Daniel Hornung <d.hornung@indiscale.com>
+# Copyright (C) 2020 IndiScale <info@indiscale.com>
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Affero General Public License as
@@ -24,10 +26,9 @@
 
 # Dump a database with all procedures, permissions, structure and data
 
-
 if [ -z "$UTILSPATH" ]; then
-	UTILSPATH="$(realpath $(dirname $0))"
-	export UTILSPATH
+    UTILSPATH="$(realpath $(dirname $0))"
+    export UTILSPATH
 fi
 # The directory which the dump is to be stored to. Do not change it here. Use
 # the --backupdir=./my/dir/ option or an environment variable instead.
@@ -74,18 +75,18 @@ function backup() {
     NARG_FILE="$2"
 
     shift 2
-	  # parameters: connection, database, outfile
-	  if [ -e "$NARG_FILE" ]; then
-		    failure "dumpfile already exists."
-	  fi
+    # parameters: connection, database, outfile
+    if [ -e "$NARG_FILE" ]; then
+        failure "dumpfile already exists."
+    fi
     echo "Dumping database $NARG_NAME to $NARG_FILE ... "
-	  $MYSQLDUMP_CMD $(get_mysql_args_nodb) $* --opt --default-character-set=utf8 --routines \
+  $MYSQLDUMP_CMD $(get_mysql_args_nodb) $* --opt --default-character-set=utf8 --routines \
                     "$NARG_NAME" > "$NARG_FILE"
 
     if [[ "$YAML" == "true" ]] ; then
         yaml_success "$NARG_FILE" "$DATE"
     fi
-	  success
+    success
 }
 
 # Print a YAML failure message, if YAML is true.
diff --git a/utils/helpers.sh b/utils/helpers.sh
index b1f6d19..3791440 100644
--- a/utils/helpers.sh
+++ b/utils/helpers.sh
@@ -1,9 +1,10 @@
 # This file is a part of the CaosDB Project.
 #
 # Copyright (C) 2018 Research Group Biomedical Physics,
-# Max-Planck-Institute for Dynamics and Self-Organization Göttingen
-# Copyright 2019 Daniel Hornung
-# Copyright 2020 Henrik tom Wörden, IndiScale GmbH
+#   Max-Planck-Institute for Dynamics and Self-Organization Göttingen
+# Copyright (C) 2019, 2020 Daniel Hornung (d.hornung@indiscale.com)
+# Copyright (C) 2020 Henrik tom Wörden
+# Copyright (C) 2020 IndiScale GmbH <info@indiscale.com>
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Affero General Public License as
@@ -19,15 +20,25 @@
 # along with this program. If not, see <https://www.gnu.org/licenses/>.
 
 
+
+###############################################################################
+#                         Helper function definitions                         #
+###############################################################################
+
+# When sourcing this file, the following variables must be set:
+#
+# MYSQL_CMD : source $UTILSPATH/load_settings.sh
+
+
+
 function mysql_execute {
-	#TODO is it meaningful that here always the database user is used???
-	set +e
-	$MYSQL_CMD $(get_db_args) -e "$1"
-	ret=${PIPESTATUS[0]}
-	if [ "$ret" -ne 0 ]; then
-		failure "MYSQL ERROR"
-	fi
-	set -e
+	  #TODO is it meaningful that here always the database user is used???
+	  set -e
+	  $MYSQL_CMD $(get_db_args) -e "$1"
+	  ret=${PIPESTATUS[0]}
+	  if [ "$ret" -ne 0 ]; then
+		    failure "MYSQL ERROR"
+	  fi
 }
 
 function get_mysql_args {
diff --git a/utils/load_settings.sh b/utils/load_settings.sh
index a001b98..5cacc0d 100644
--- a/utils/load_settings.sh
+++ b/utils/load_settings.sh
@@ -1,9 +1,10 @@
 # This file is a part of the CaosDB Project.
 #
 # Copyright (C) 2018 Research Group Biomedical Physics,
-# Max-Planck-Institute for Dynamics and Self-Organization Göttingen
-# Copyright 2019 Daniel Hornung
-# Copyright 2020 Henrik tom Wörden, IndiScale GmbH
+#   Max-Planck-Institute for Dynamics and Self-Organization Göttingen
+# Copyright (C) 2019, 2020 Daniel Hornung (d.hornung@indiscale.com)
+# Copyright (C) 2020 Henrik tom Wörden
+# Copyright (C) 2020 IndiScale GmbH <info@indiscale.com>
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Affero General Public License as
@@ -18,9 +19,33 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program. If not, see <https://www.gnu.org/licenses/>.
 
-[[ -n "config.defaults" ]] && source "config.defaults"
-[[ -n ".config" ]] && source ".config"
-[[ -n "$ENV_FILE" ]] && source "$ENV_FILE"
+# Load setting from different files, in this order:
+# - config.defaults (in the current directory)
+# - .config (in the current directory)
+# - The file given in ENV_FILE.
+
+if [ -z "$MAINPATH" ]; then
+    if [ -z "$UTILSPATH" ] ; then
+        echo "At least one of MAINPATH and UTILSPATH must be defined in load_settings.sh" >&2
+        exit 1
+    fi
+    MAINPATH="$(dirname $UTILSPATH)"
+    export MAINPATH
+fi
+
+source "$MAINPATH/config.defaults"
+
+if [[ -r "$MAINPATH/.config" ]] ; then
+    source "$MAINPATH/.config"
+fi
+if [[ -n "$ENV_FILE" ]] ; then
+    source "$ENV_FILE"
+fi
+
+if [[ -z "$MYSQL_CMD" ]] ; then
+    echo "'mysql' executable not defined!"
+    exit 1
+fi
 
 export MYSQL_CMD
 export MYSQLDUMP_CMD
diff --git a/utils/make_db b/utils/make_db
index 72a49da..4dbc80e 100755
--- a/utils/make_db
+++ b/utils/make_db
@@ -3,8 +3,9 @@
 # ** header v3.0
 # This file is a part of the CaosDB Project.
 #
-# Copyright (C) 2019 Daniel Hornung, Göttingen
+# Copyright (C) 2019, 2020 Daniel Hornung <d.hornung@indiscale.com>
 # Copyright (C) 2020 Timm Fitschen <t.fitschen@indiscale.com>
+# Copyright (C) 2020 Henrik tom Wörden <h.tomwoerden@indiscale.com>
 # Copyright (C) 2020 IndiScale <info@indiscale.com>
 #
 # This program is free software: you can redistribute it and/or modify
@@ -22,23 +23,18 @@
 #
 # ** end header
 
+set -e
+
 INSTALL_SQL_FILE="db_2_0.sql"
 
 if [ -z "$UTILSPATH" ]; then
-	UTILSPATH="$(realpath $(dirname $0))"
-	export UTILSPATH
+    UTILSPATH="$(realpath $(dirname $0))"
+    export UTILSPATH
 fi
 
 source $UTILSPATH/load_settings.sh
 source $UTILSPATH/helpers.sh
 
-set -e
-
-function fail() {
-  echo "Some error occured, exiting."
-  exit 1
-}
-
 UNITTEST_DATABASE=${UNITTEST_DATABASE-_caosdb_schema_unit_tests}
 
 # optional parameter: [--fresh] for installing a fresh data base. Otherwise an existing one would be reused.
@@ -48,7 +44,7 @@ function runtests() {
 
     _install_unit_test_database $@
 
-    _execute_tests || ( echo "[FAILURE]" && exit 1 )
+    _execute_tests || ( failure )
     echo "[PASS]"
 }
 
@@ -113,22 +109,24 @@ function _setup_mytap() {
 }
 
 function setup_os() {
-  # - mariadb-client :: For SQL server configuration.
-  PACKAGES="git
-  make
-  mariadb-client
-  maven
-  openjdk-8-jdk-headless
-  python3-pip
-  screen
-  unzip
-"
-  # - vim :: For debugging
-  PACKAGES+="vim
-"
-  apt-get update
-  apt-get dist-upgrade -y
-  apt-get install -y $PACKAGES
+    # - mariadb-client :: For SQL server configuration.
+    PACKAGES="
+        git
+        make
+        mariadb-client
+        maven
+        openjdk-8-jdk-headless
+        python3-pip
+        screen
+        unzip
+    "
+    # - vim :: For debugging
+    PACKAGES+="
+        vim
+    "
+    apt-get update
+    apt-get dist-upgrade -y
+    apt-get install -y $PACKAGES
 }
 
 function install_db() {
@@ -140,7 +138,7 @@ Call 'make drop-$DATABASE_NAME' to delete that database or reconfigure with
         exit 0
     fi
 
-	  sed "s/db_2_0/$DATABASE_NAME/g" "$INSTALL_SQL_FILE" | $MYSQL_CMD $(get_mysql_args_nodb)
+    sed "s/db_2_0/$DATABASE_NAME/g" "$INSTALL_SQL_FILE" | $MYSQL_CMD $(get_mysql_args_nodb)
 }
 
 # Inserts the dump (arg 1) into the database
@@ -160,7 +158,7 @@ function grant() {
         while true; do
             read -p "Please enter the password for ${DATABASE_USER}: " \
                  -s password
-            echo                
+            echo
             read -p "Please repeat the password: " -s password2
             echo
             [[ "$password" == "$password2" ]] && {
@@ -174,7 +172,7 @@ function grant() {
     if [[ "$1" == "--strict" ]] ; then
         for host in ${DATABASE_USER_HOST_LIST//,/ } ; do
             CMD="SELECT COUNT(*) FROM mysql.user WHERE user='${DATABASE_USER}' AND host='${host}';"
-		        [[ $($MYSQL_CMD $(get_mysql_args_nodb) -s -N -e "$CMD") == 0 ]] || {
+            [[ $($MYSQL_CMD $(get_mysql_args_nodb) -s -N -e "$CMD") == 0 ]] || {
                 echo "The user '${DATABASE_USER}@${host}' is already in the database."
                 echo "Please use another user or delete it, e.g. with"
                 echo "'mysql -u ${MYSQL_USER} -p -e \"DROP USER ${DATABASE_USER}@${host};\"'"
@@ -215,12 +213,12 @@ function _db_exists() {
 
 
 case $1 in
-  "drop") drop $2 ;;
-  "grant") grant $2 ;;
-  "test") shift ; runtests $@ ;;
-  "test-connection") test-connection ;;
-  "install_db") install_db ;;
-  "restore_db") restore_db $2 ;;
-  # "prep_sql") prepare_sql ;;
-  *) echo "Unknown action: $1"
+    "drop") drop $2 ;;
+    "grant") grant $2 ;;
+    "test") shift ; runtests $@ ;;
+    "test-connection") test-connection ;;
+    "install_db") install_db ;;
+    "restore_db") restore_db $2 ;;
+    # "prep_sql") prepare_sql ;;
+    *) echo "Unknown action: $1"
 esac
diff --git a/utils/patch_header.sh b/utils/patch_header.sh
index 7f85505..488b7a0 100644
--- a/utils/patch_header.sh
+++ b/utils/patch_header.sh
@@ -4,7 +4,9 @@
 #
 # Copyright (C) 2018 Research Group Biomedical Physics,
 # Max-Planck-Institute for Dynamics and Self-Organization Göttingen
-# Copyright 2019 Daniel Hornung
+# Copyright (C) 2019, 2020 Daniel Hornung <d.hornung@indiscale.com>
+# Copyright (C) 2020 Henrik tom Wörden <h.tomwoerden@indiscale.com>
+# Copyright (C) 2020 IndiScale <info@indiscale.com>
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Affero General Public License as
@@ -21,10 +23,16 @@
 #
 # ** end header
 #
-#header for patch scripts
+
+###############################################################################
+#                          header for patch scripts                           #
+###############################################################################
+
+set -e
 
 # load useful functions #######################################################
-. $UTILSPATH/helpers.sh
+source $UTILSPATH/load_settings.sh
+source $UTILSPATH/helpers.sh
 
 USAGE="$1 [ --env=ENV_FILE ] [ --patch=PATCH ] [ --backupdir=BACKUPDIR ]\n\n"
 CMD_OPTIONS=$(cat <<EOF
@@ -56,7 +64,6 @@ configuration make file.  Notable examples are:
 
 EOF
               )
-set -e
 
 function _print_help() {
   echo -e "$USAGE"
diff --git a/utils/update_sql_procedures.sh b/utils/update_sql_procedures.sh
index 73f6bac..8850d5c 100755
--- a/utils/update_sql_procedures.sh
+++ b/utils/update_sql_procedures.sh
@@ -4,7 +4,10 @@
 # This file is a part of the CaosDB Project.
 #
 # Copyright (C) 2018 Research Group Biomedical Physics,
-# Max-Planck-Institute for Dynamics and Self-Organization Göttingen
+#   Max-Planck-Institute for Dynamics and Self-Organization Göttingen
+# Copyright (C) 2020 Daniel Hornung <d.hornung@indiscale.com>
+# Copyright (C) 2020 Henrik tom Wörden <h.tomwoerden@indiscale.com>
+# Copyright (C) 2020 IndiScale <info@indiscale.com>
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Affero General Public License as
@@ -24,16 +27,21 @@
 
 # Updates all SQL procedures
 
+set -e
+
+if [ -z "$UTILSPATH" ]; then
+    UTILSPATH=$(realpath "$(dirname $0)")
+    export UTILSPATH
+fi
+
+source $UTILSPATH/load_settings.sh
 source $UTILSPATH/helpers.sh
 
 echo -n "updating rules ... "
-set -e
 
 mysql_execute "$(sed s/db_2_0/$DATABASE_NAME/g rules.sql)"
 echo "OK"
 
 echo -n "updating procedures ... "
-mysql_execute "$(sed s/db_2_0/$DATABASE_NAME/g procedures/*.sql procedures/query/*.sql)" 
+mysql_execute "$(sed s/db_2_0/$DATABASE_NAME/g procedures/*.sql procedures/query/*.sql)"
 echo "OK"
-
-
-- 
GitLab