From a6921cb478dc3a5b8665f0fc89feca18edb6ef8f Mon Sep 17 00:00:00 2001 From: Daniel <daniel@harvey> Date: Tue, 12 Feb 2019 16:53:40 +0100 Subject: [PATCH] WIP: Making everything a bit fitter for dockerization --- config.defaults | 20 ++++++-- make_db | 129 ++++++++++++++++++++++++++++++++++++++++++++++++ makefile | 32 +++--------- 3 files changed, 151 insertions(+), 30 deletions(-) create mode 100644 make_db diff --git a/config.defaults b/config.defaults index 088143f..625d47d 100644 --- a/config.defaults +++ b/config.defaults @@ -28,12 +28,22 @@ MYSQLDUMP_CMD=$(command -v mysqldump) # The mysqldump program which comes with t MYSQL_CONFIG_EDITOR_CMD=$(command -v mysql_config_editor) # The mysql_config_editor program which is used to store the credentials. # MySQL Connection -MYSQL_HOST=localhost #The host of the MySQL server. -MYSQL_USER=root # The user for the installation. Note: This is not the user which will then be used by the CaosDB Server. +MYSQL_HOST=localhost # The host of the MySQL server. +MYSQL_PORT=3306 # The port number of the MySQL server. +MYSQL_USER=root # The user for the installation. Note: This is not the user + # which will then be used by the CaosDB Server. # DATABASE -DATABASE_NAME=caosdb #The name of the database. -DATABASE_USER=caosdb_user #The user which is used by the CaosDB Server for the connection. -DATABASE_USER_HOST_LIST=localhost, # A comma-separated list of hosts from which MySQL will accept logins. This option follows the MySQL-style for host-names and ip addresses. E.g. `%` is a wildcard for all hosts, `192.168.0.%` permits logins from the local network etc. +DATABASE_NAME=caosdb # The name of the SQL database. +DATABASE_USER=caosdb # The user which is used by the CaosDB Server for accessing + # the database. +DATABASE_USER_PW=random1234 # The password for the database user. Leave empty + # if you want to be prompted at make time. + +# A comma-separated list of hosts from which MySQL will accept logins. This +# option follows the MySQL-style for host-names and ip addresses. E.g. `%` is a +# wildcard for all hosts, so `192.168.0.%` permits logins from the local +# network. +DATABASE_USER_HOST_LIST=localhost, diff --git a/make_db b/make_db new file mode 100644 index 0000000..34f1468 --- /dev/null +++ b/make_db @@ -0,0 +1,129 @@ +#!/bin/bash + +# ** header v3.0 +# This file is a part of the CaosDB Project. +# +# Copyright (C) 2019 Daniel Hornung, Göttingen +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# 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/>. +# +# ** end header + +# To fail fast, but beware https://mywiki.wooledge.org/BashFAQ/105 +set -e + +function fail() { + echo "Some error occured, exiting." + exit 1 +} + +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 +} + +function install_db() { + if _db_exists ; then + echo -e "\n +A database with with the name `$DATABASE_NAME` exists already. +Call `make drop-$DATABASE_NAME` to delete that database or reconfigure with +'./configure'." + exit 1 + fi + + sed "s/db_2_0/$DATABASE_NAME/g" "$INSTALL_SQL_FILE" | $SQL +} + +function test-connection() { + $SQL -e "select 0;" +} + +# Creates a user and grants it sufficient rights. +function grant() { + if [ -z "$DATABASE_USER_PW" ] ; then + while true; do + read -p "Please enter the password for ${DATABASE_USER}: " \ + -s password + echo + read -p "Please repeat the password: " -s password2 + echo + [[ "$password" == "$password2" ]] && { + DATABASE_USER_PW="$password" + break + } + echo -e "\nThe passwords didn't match. Try again." + done + fi + + for host in ${DATABASE_USER_HOST_LIST//,/ } ; do + $SQL <<EOF +CREATE USER '$DATABASE_USER'@'$host' identified by '$DATABASE_USER_PW'; +GRANT USAGE ON *.* TO '$DATABASE_USER'@'$host'; +GRANT ALL PRIVILEGES ON *.* TO '$DATABASE_USER'@'$host' WITH GRANT OPTION; +GRANT EXECUTE ON *.* TO '$DATABASE_USER'@'$host'; +EOF + done +} + +# Drops the caosdb user plus a given database from all listed database hosts. +# The first argument to this function is the database that shal be dropped. +function drop() { + DROPDB="$1" + for host in ${DATABASE_USER_HOST_LIST//,/ } ; do + $SQL -e "DROP USER '${DATABASE_USER}'@'${host}';" || true + done + "$MYSQLADMIN_CMD" $MYSQL_CONNECTION -f drop "$DROPDB" +} + +# Returns 0 or non-zero, depending on whether the database exists already. +function _db_exists() { + $SQL -D "$DATABASE_NAME" -e "show tables;" > /dev/null 2>&1 \ + && return 0 || return 1 +} + +. .config + +INSTALL_SQL_FILE="db_2_0.sql" +if [ -n $LOGIN_PATH ] ; then + MYSQL_CONNECTION="--login-path=$LOGIN_PATH" +else + MYSQL_CONNECTION="--host=${MYSQL_HOST} --port=${MYSQL_PORT} "\ + "--user=${MYSQL_USER} "\ + "--password=${MYSQL_USER_PASSWORD}" +fi + +SQL="$MYSQL_CMD" $MYSQL_CONNECTION + +case $1 in + "drop") drop $2 ;; + "grant") grant ;; + "test-connection") test-connection ;; + "install_db") install_db ;; + # "prep_sql") prepare_sql ;; + *) echo "Unknown action: $1" +esac diff --git a/makefile b/makefile index 4371608..d076a28 100644 --- a/makefile +++ b/makefile @@ -28,12 +28,13 @@ INSTALL_SQL_FILE=db_2_0.sql ifdef LOGIN_PATH MYSQL_CONNECTION=--login-path=$(LOGIN_PATH) else - MYSQL_CONNECTION=--user="$(MYSQL_USER)" --password="$(MYSQL_USER_PASSWORD)" + MYSQL_CONNECTION=--host="$(MYSQL_HOST)" --port="$(MYSQL_PORT)" \ + --user="$(MYSQL_USER)" --password="$(MYSQL_USER_PASSWORD)" endif .PHONY: test-connection test-connection: - @$(MYSQL_CMD) $(MYSQL_CONNECTION) -e "select 0;" + make_db test-connection .PHONY: upgrade upgrade: @@ -44,36 +45,17 @@ install: _install _grant upgrade .PHONY: _install _install: + make_db install_db @if $(MAKE) _exists > /dev/null 2>&1; then \ printf "\n\nA database with with the name \"$(DATABASE_NAME)\" does already exist.\nCall 'make drop-$(DATABASE_NAME)' to delete that database or reconfigure with './configure'.\n"; \ exit 1; \ else sed 's/db_2_0/$(DATABASE_NAME)/g' $(INSTALL_SQL_FILE) | $(MYSQL_CMD) $(MYSQL_CONNECTION); fi -comma:=, .PHONY: _grant _grant: - @while true; do \ - printf "\n Please enter the password for $(DATABASE_USER): "; read -s password; \ - printf "\nPlease repeat the password for $(DATABASE_USER): "; read -s password2; \ - if [ "$$password" != "$$password2" ]; then printf "\n\nThe passwords didn't match. Try again." ; else break ; fi; \ - done ; \ - echo "" ; \ - for host in $(subst $(comma), ,$(DATABASE_USER_HOST_LIST)); do \ - $(MYSQL_CMD) $(MYSQL_CONNECTION) -e "CREATE USER '$(DATABASE_USER)'@'$$host' identified by '$$password';"; \ - $(MYSQL_CMD) $(MYSQL_CONNECTION) -e "GRANT USAGE ON *.* TO '$(DATABASE_USER)'@'$$host';"; \ - $(MYSQL_CMD) $(MYSQL_CONNECTION) -e "GRANT ALL PRIVILEGES ON *.* TO '$(DATABASE_USER)'@'$$host' WITH GRANT OPTION;"; \ - $(MYSQL_CMD) $(MYSQL_CONNECTION) -e "GRANT EXECUTE ON *.* TO '$(DATABASE_USER)'@'$$host';"; \ - done + make_db grant +# Drop the user and a given database .PHONY: drop-% drop-%: - @for host in $(subst $(comma), ,$(DATABASE_USER_HOST_LIST)); do \ - $(MYSQL_CMD) $(MYSQL_CONNECTION) -e "DROP USER '$(DATABASE_USER)'@'$$host';" || true ; \ - done - @$(MYSQLADMIN_CMD) $(MYSQL_CONNECTION) -f drop $(patsubst drop-%,%,$@) - -.PHONY: _exists -_exists: - @if $(MYSQL_CMD) $(MYSQL_CONNECTION) -D "$(DATABASE_NAME)" -e "show tables;" > /dev/null 2>&1 ; then \ - echo "$(DATABASE_NAME) does exist." ; exit 0 ; \ - else echo "$(DATABASE_NAME) does not exist." ; exit 1; fi + @make_db drop $(patsubst drop-%,%,$@) -- GitLab