diff --git a/config.defaults b/config.defaults index bd669cb2cc724a74e3dd81fc708a96dfdd6a4ebe..bf6c941b3251f24e0a0aaf1e7667d11e1ef88638 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 56cbac7e05a1528ee041540ed84cc7faf7b3a5d6..735dd3ea48a43d033b46dad1c7eb1700bda5c9b1 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 752c4e41e0ff1d254cd6f19c8f7a4738e8dac534..56ca60907bd7a4c4c73db7815efe168374bc37b6 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 b1f6d19c2270f8b3207a88f36151f6633318cfc5..37914402380f5b6ef9a6d192d5021e8a8774db43 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 a001b98b4e9188ac89f8200069dad3a34e2ea354..5cacc0d26d69c5e32cbd02062e9aa9982043a495 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 72a49dab721d337340f37aebe7fcc3231da9e40a..4dbc80e4b97c29816106184daaa5f4d894288331 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 7f85505234b2b5dd1232d2f7614b73908e70efc2..488b7a0f4df0c20bde1909872d923ea4c352cacc 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 73f6bacc86c80bc5b1be83e2c9c762777f99e878..8850d5c3311c4ee1a6c7cd635bb1b1bb98fe218c 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" - -