wwwgmo/createDbOnKube.sh
2025-02-07 20:52:27 +01:00

62 lines
2.4 KiB
Bash
Executable File

#!/bin/bash
# check yq is installed
if [ ! -f "/usr/local/bin/yq" ]; then
echo "yq does not exist"
echo "see https://github.com/mikefarah/yq/releases/ to install yq"
exit
fi
# check env var NS (namespace is define)
if [ -z "${NS}" ]; then
echo "Environmet var NS is not defined"
exit
fi
echo "Namespace is $NS"
# set rootPass
root_pass=$(yq .mariadb.rootPass < helm/values-secrets-k3s.yaml)
# set user db
db_user=$(yq .site.phpfpmSite.db.user < helm/values-configs-k3s.yaml)
# set pass db
db_user_pass=$(yq .mariadb.dbPass < helm/values-secrets-k3s.yaml)
# set db name
db_name=$(yq .site.phpfpmSite.db.name < helm/values-configs-k3s.yaml)
echo "Root pass: $root_pass"
echo "DB user: $db_user"
echo "DB user pass: $db_user_pass"
echo "DB name: $db_name"
echo -e "\n"
echo "Create user if not exists"
kubectl -n $NS exec statefulset-mariadb-0 --container mariadb -- /bin/bash -c \
"/usr/bin/mysql -u root -p$root_pass -e \"CREATE USER IF NOT EXISTS '$db_user'@'%' IDENTIFIED BY '$db_user_pass';\""
echo "Check user ------------"
kubectl -n $NS exec statefulset-mariadb-0 --container mariadb -- /bin/bash -c \
"/usr/bin/mysql -u root -p$root_pass -e \"SELECT user,host FROM mysql.user WHERE user LIKE '$db_user%';\""
echo -e "-----------------------\n"
echo "Create database if not exists"
kubectl -n $NS exec statefulset-mariadb-0 --container mariadb -- /bin/bash -c \
"/usr/bin/mysql -u root -p$root_pass -e \"CREATE DATABASE IF NOT EXISTS $db_name;\""
echo "Check database---------"
kubectl -n $NS exec statefulset-mariadb-0 --container mariadb -- /bin/bash -c \
"/usr/bin/mysql -u root -p$root_pass -e \"SHOW DATABASES;\""
echo -e "-----------------------\n"
echo "Grants access"
kubectl -n $NS exec statefulset-mariadb-0 --container mariadb -- /bin/bash -c \
"/usr/bin/mysql -u root -p$root_pass -e \"GRANT ALL PRIVILEGES ON $db_name.* TO '$db_user'@'%';FLUSH PRIVILEGES;\""
echo "Check access-----------"
kubectl -n $NS exec statefulset-mariadb-0 --container mariadb -- /bin/bash -c \
"/usr/bin/mysql -u root -p$root_pass -e \"SHOW GRANTS FOR '$db_user'@'%';\""
echo -e "-----------------------\n"
#kubectl -n $NS exec statefulset-mariadb-0 --container mariadb -- /bin/bash -c \
#"/usr/bin/mysql -u root -p$root_pass -e \"DROP USER IF EXISTS '$db_user'@'%';\""
#kubectl -n $NS exec statefulset-mariadb-0 --container mariadb -- /bin/bash -c \
#"/usr/bin/mysql -u root -p$root_pass -e \"DROP DATABASE IF EXISTS $db_name;\""