diff --git a/scripts/mariadb-cluster-create-database-and-grant b/scripts/mariadb-cluster-create-database-and-grant new file mode 100755 index 0000000..57753a2 --- /dev/null +++ b/scripts/mariadb-cluster-create-database-and-grant @@ -0,0 +1,51 @@ +#!/bin/bash + +set -e + +HOST="${1}" +DATABASE="${2}" +USERNAME="${3}" + +function usage() { + echo "Usage: ${0} " + echo "Example: ${0} db-1.example.com" + exit 1 +} +if [ -z "${HOST}" ]; then + usage +fi + +if [ -z "${DATABASE}" ]; then + usage +fi + +if [ -z "${USERNAME}" ]; then + usage +fi + +PASSWORD=$(openssl rand 32 | base64) + + +SQL="CREATE DATABASE ${DATABASE}; CREATE USER '${USERNAME}'@'%' IDENTIFIED BY '${PASSWORD}'; GRANT ALL PRIVILEGES ON ${DATABASE}.* TO '${USERNAME}'@'%';" + +echo "Execute the following command on ${HOST} [Y/n]?" +echo "$SQL" + +read -r x +case $x in + y|Y|"") + true + ;; + *) + echo "script aborted" + exit 1 + ;; +esac + +ssh -t "${HOST}" "docker exec -ti mariadb-db-1 mariadb -u root -p\$(puppet lookup --render-as s mariadb_root_password 2> /dev/null) -e \"${SQL}\"" + + +echo "Command successful" +echo "Database: ${DATABASE}" +echo "Username: ${USERNAME}" +echo "Password: ${PASSWORD}"