Select Git revision
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
make_db 3.48 KiB
#!/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 [ -z $LOGIN_PATH ] ; then
MYSQL_CONNECTION="--host=${MYSQL_HOST} --port=${MYSQL_PORT}
--user=${MYSQL_USER}
--password=${MYSQL_USER_PASSWORD}"
else
MYSQL_CONNECTION="--login-path=$LOGIN_PATH"
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