Skip to content
Snippets Groups Projects
Commit a6921cb4 authored by Daniel's avatar Daniel
Browse files

WIP: Making everything a bit fitter for dockerization

parent 8052a94d
No related branches found
No related tags found
No related merge requests found
...@@ -28,12 +28,22 @@ MYSQLDUMP_CMD=$(command -v mysqldump) # The mysqldump program which comes with t ...@@ -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_CONFIG_EDITOR_CMD=$(command -v mysql_config_editor) # The mysql_config_editor program which is used to store the credentials.
# MySQL Connection # MySQL Connection
MYSQL_HOST=localhost #The host of the MySQL server. 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_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
DATABASE_NAME=caosdb #The name of the database. DATABASE_NAME=caosdb # The name of the SQL database.
DATABASE_USER=caosdb_user #The user which is used by the CaosDB Server for the connection. DATABASE_USER=caosdb # The user which is used by the CaosDB Server for accessing
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. # 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,
make_db 0 → 100644
#!/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
...@@ -28,12 +28,13 @@ INSTALL_SQL_FILE=db_2_0.sql ...@@ -28,12 +28,13 @@ INSTALL_SQL_FILE=db_2_0.sql
ifdef LOGIN_PATH ifdef LOGIN_PATH
MYSQL_CONNECTION=--login-path=$(LOGIN_PATH) MYSQL_CONNECTION=--login-path=$(LOGIN_PATH)
else 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 endif
.PHONY: test-connection .PHONY: test-connection
test-connection: test-connection:
@$(MYSQL_CMD) $(MYSQL_CONNECTION) -e "select 0;" make_db test-connection
.PHONY: upgrade .PHONY: upgrade
upgrade: upgrade:
...@@ -44,36 +45,17 @@ install: _install _grant upgrade ...@@ -44,36 +45,17 @@ install: _install _grant upgrade
.PHONY: _install .PHONY: _install
_install: _install:
make_db install_db
@if $(MAKE) _exists > /dev/null 2>&1; then \ @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"; \ 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; \ exit 1; \
else sed 's/db_2_0/$(DATABASE_NAME)/g' $(INSTALL_SQL_FILE) | $(MYSQL_CMD) $(MYSQL_CONNECTION); fi else sed 's/db_2_0/$(DATABASE_NAME)/g' $(INSTALL_SQL_FILE) | $(MYSQL_CMD) $(MYSQL_CONNECTION); fi
comma:=,
.PHONY: _grant .PHONY: _grant
_grant: _grant:
@while true; do \ make_db grant
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
# Drop the user and a given database
.PHONY: drop-% .PHONY: drop-%
drop-%: drop-%:
@for host in $(subst $(comma), ,$(DATABASE_USER_HOST_LIST)); do \ @make_db drop $(patsubst drop-%,%,$@)
$(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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment