Updates
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import createDbFromSchema from "@/package-shared/shell/createDbFromSchema";
|
||||
import mysql, { ServerlessMysql } from "serverless-mysql";
|
||||
|
||||
console.log(`Initializing Database ...`);
|
||||
|
||||
/**
|
||||
* # Declare Global Variables
|
||||
*/
|
||||
declare global {
|
||||
var DB_CONN: ServerlessMysql;
|
||||
var DSQL_DB_CONN: ServerlessMysql;
|
||||
}
|
||||
|
||||
global.DSQL_DB_CONN = mysql({
|
||||
config: {
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_USERNAME,
|
||||
password: process.env.DSQL_DB_PASSWORD,
|
||||
database: process.env.DSQL_DB_NAME,
|
||||
charset: "utf8mb4",
|
||||
// ssl: grabDbSSL(),
|
||||
},
|
||||
});
|
||||
|
||||
global.DSQL_READ_ONLY_DB_CONN = mysql({
|
||||
config: {
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_READ_ONLY_USERNAME,
|
||||
password: process.env.DSQL_DB_READ_ONLY_PASSWORD,
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
// ssl: grabDbSSL(),
|
||||
},
|
||||
});
|
||||
|
||||
global.DSQL_FULL_ACCESS_DB_CONN = mysql({
|
||||
config: {
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_FULL_ACCESS_USERNAME,
|
||||
password: process.env.DSQL_DB_FULL_ACCESS_PASSWORD,
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
// ssl: grabDbSSL(),
|
||||
},
|
||||
});
|
||||
|
||||
/**
|
||||
* # Run Function
|
||||
*/
|
||||
await createDbFromSchema({});
|
||||
|
||||
process.exit(0);
|
||||
@@ -0,0 +1,93 @@
|
||||
import dsql from "@moduletrace/datasquirel";
|
||||
|
||||
type MysqlUser = {
|
||||
User: string;
|
||||
Host: string;
|
||||
};
|
||||
|
||||
export default async function initSQLCheckDsqlUsers() {
|
||||
const readOnlyUser = await dsql.utils.connDbHandler<MysqlUser[]>(
|
||||
global.INIT_SQL_ROOT_DB_CONN,
|
||||
`SELECT user,host FROM mysql.user WHERE user=? AND host=?`,
|
||||
[
|
||||
process.env.DSQL_DB_READ_ONLY_USERNAME,
|
||||
process.env.DSQL_DB_TARGET_IP_ADDRESS,
|
||||
]
|
||||
);
|
||||
|
||||
if (!readOnlyUser?.[0]?.User) {
|
||||
console.log(`Read Only User Does not Exit.`);
|
||||
|
||||
const createReadOnlyUser = await dsql.utils.connDbHandler(
|
||||
global.INIT_SQL_ROOT_DB_CONN,
|
||||
`CREATE USER IF NOT EXISTS \
|
||||
'${process.env.DSQL_DB_READ_ONLY_USERNAME}'@'${process.env.DSQL_DB_TARGET_IP_ADDRESS}' \
|
||||
IDENTIFIED BY '${process.env.DSQL_DB_READ_ONLY_PASSWORD}'`
|
||||
);
|
||||
} else {
|
||||
console.log("Read Only User Exists");
|
||||
|
||||
const grants = await dsql.utils.connDbHandler(
|
||||
global.INIT_SQL_ROOT_DB_CONN,
|
||||
`SHOW GRANTS FOR \
|
||||
'${process.env.DSQL_DB_READ_ONLY_USERNAME}'@'${process.env.DSQL_DB_TARGET_IP_ADDRESS}'`
|
||||
);
|
||||
|
||||
if (checkGrantsArrayForSSL(grants)) {
|
||||
const removeSSL = await dsql.utils.connDbHandler(
|
||||
global.INIT_SQL_ROOT_DB_CONN,
|
||||
`ALTER USER \
|
||||
'${process.env.DSQL_DB_READ_ONLY_USERNAME}'@'${process.env.DSQL_DB_TARGET_IP_ADDRESS}' \
|
||||
REQUIRE NONE`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const fullAccessUser = await dsql.utils.connDbHandler<MysqlUser[]>(
|
||||
global.INIT_SQL_ROOT_DB_CONN,
|
||||
`SELECT user,host FROM mysql.user WHERE user=? AND host=?`,
|
||||
[
|
||||
process.env.DSQL_DB_FULL_ACCESS_USERNAME,
|
||||
process.env.DSQL_DB_TARGET_IP_ADDRESS,
|
||||
]
|
||||
);
|
||||
|
||||
if (!fullAccessUser?.[0]?.User) {
|
||||
console.log(`Read Only User Does not Exit.`);
|
||||
|
||||
const createReadOnlyUser = await dsql.utils.connDbHandler(
|
||||
global.INIT_SQL_ROOT_DB_CONN,
|
||||
`CREATE USER IF NOT EXISTS \
|
||||
'${process.env.DSQL_DB_FULL_ACCESS_USERNAME}'@'${process.env.DSQL_DB_TARGET_IP_ADDRESS}' \
|
||||
IDENTIFIED BY '${process.env.DSQL_DB_FULL_ACCESS_PASSWORD}'`
|
||||
);
|
||||
} else {
|
||||
console.log("Full Access User Exists");
|
||||
|
||||
const grants = await dsql.utils.connDbHandler(
|
||||
global.INIT_SQL_ROOT_DB_CONN,
|
||||
`SHOW GRANTS FOR \
|
||||
'${process.env.DSQL_DB_FULL_ACCESS_USERNAME}'@'${process.env.DSQL_DB_TARGET_IP_ADDRESS}'`
|
||||
);
|
||||
|
||||
if (checkGrantsArrayForSSL(grants)) {
|
||||
const removeSSL = await dsql.utils.connDbHandler(
|
||||
global.INIT_SQL_ROOT_DB_CONN,
|
||||
`ALTER USER \
|
||||
'${process.env.DSQL_DB_FULL_ACCESS_USERNAME}'@'${process.env.DSQL_DB_TARGET_IP_ADDRESS}' \
|
||||
REQUIRE NONE`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function checkGrantsArrayForSSL(array: any[]) {
|
||||
for (let i = 0; i < array.length; i++) {
|
||||
const element = array[i];
|
||||
const firstKey = Object.keys(element)[0];
|
||||
|
||||
if (element[firstKey].match(/require ssl/i)) return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
export default async function initSQLCheckEnvVariables() {
|
||||
if (!process.env.DSQL_DB_TARGET_IP_ADDRESS) {
|
||||
console.log("DSQL_DB_TARGET_IP_ADDRESS env is not set");
|
||||
process.exit(1);
|
||||
}
|
||||
if (!process.env.DSQL_DB_PASSWORD) {
|
||||
console.log("DSQL_DB_PASSWORD env is not set");
|
||||
process.exit(1);
|
||||
}
|
||||
if (!process.env.DSQL_DB_READ_ONLY_PASSWORD) {
|
||||
console.log("DSQL_DB_READ_ONLY_PASSWORD env is not set");
|
||||
process.exit(1);
|
||||
}
|
||||
if (!process.env.DSQL_DB_FULL_ACCESS_PASSWORD) {
|
||||
console.log("DSQL_DB_FULL_ACCESS_PASSWORD env is not set");
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import dsql from "@moduletrace/datasquirel";
|
||||
|
||||
export default async function initSQLCreateDatabases() {
|
||||
const checkDatasquirelDatabase = await dsql.utils.connDbHandler<any[]>(
|
||||
global.INIT_SQL_MAIN_DB_CONN,
|
||||
"SHOW DATABASES LIKE 'datasquirel'"
|
||||
);
|
||||
|
||||
if (!checkDatasquirelDatabase?.[0]) {
|
||||
const createDsqlDatabase = await dsql.utils.connDbHandler(
|
||||
global.INIT_SQL_MAIN_DB_CONN,
|
||||
"CREATE DATABASE datasquirel"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import dsql from "@moduletrace/datasquirel";
|
||||
|
||||
type MysqlUser = {
|
||||
User: string;
|
||||
Host: string;
|
||||
};
|
||||
|
||||
export default async function initSQLHandleRootUsers() {
|
||||
if (process.env.NEXT_PUBLIC_DSQL_LOCAL) {
|
||||
console.log("Production Environment. Root user setup not required");
|
||||
return;
|
||||
}
|
||||
|
||||
const mainRootUser = await dsql.utils.connDbHandler<MysqlUser[]>(
|
||||
global.INIT_SQL_ROOT_DB_CONN,
|
||||
`SELECT user,host FROM mysql.user WHERE user='root' AND host=?`,
|
||||
[process.env.DSQL_DB_TARGET_IP_ADDRESS]
|
||||
);
|
||||
|
||||
if (!mainRootUser?.[0]?.User) {
|
||||
console.log(`Main Root User Does not Exit.`);
|
||||
|
||||
const createMainRootUser = await dsql.utils.connDbHandler<any[]>(
|
||||
global.INIT_SQL_ROOT_DB_CONN,
|
||||
[
|
||||
{
|
||||
query: `CREATE USER IF NOT EXISTS \
|
||||
'root'@'${process.env.DSQL_DB_TARGET_IP_ADDRESS}' \
|
||||
IDENTIFIED BY '${process.env.DSQL_DB_PASSWORD}'`,
|
||||
},
|
||||
{
|
||||
query: `GRANT ALL PRIVILEGES ON *.* TO 'root'@'${process.env.DSQL_DB_TARGET_IP_ADDRESS}' \
|
||||
WITH GRANT OPTION`,
|
||||
},
|
||||
{
|
||||
query: `FLUSH PRIVILEGES`,
|
||||
},
|
||||
]
|
||||
);
|
||||
|
||||
console.log("createMainRootUser", createMainRootUser);
|
||||
|
||||
const checkRootUser = await dsql.utils.connDbHandler<MysqlUser[]>(
|
||||
global.INIT_SQL_ROOT_DB_CONN,
|
||||
`SELECT user,host FROM mysql.user WHERE user='root' AND host='${process.env.DSQL_DB_TARGET_IP_ADDRESS}'`
|
||||
);
|
||||
|
||||
if (checkRootUser?.[0]?.User) {
|
||||
const dropWildcardRootUser = await dsql.utils.connDbHandler<
|
||||
MysqlUser[]
|
||||
>(global.INIT_SQL_ROOT_DB_CONN, "drop user 'root'@'%'");
|
||||
|
||||
console.log("dropWildcardRootUser", dropWildcardRootUser);
|
||||
}
|
||||
} else {
|
||||
console.log("Root User Exists");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
import execute from "../../docker/setup/(utils)/execute";
|
||||
import mysql, { ServerlessMysql } from "serverless-mysql";
|
||||
import initSQLHandleRootUsers from "./(functions)/handle-root-user";
|
||||
import initSQLCheckEnvVariables from "./(functions)/check-env-variables";
|
||||
import initSQLCreateDatabases from "./(functions)/create-databases";
|
||||
import initSQLCheckDsqlUsers from "./(functions)/check-dsql-users";
|
||||
|
||||
console.log(`Initializing Database ...`);
|
||||
console.log(`DB HOST => ${process.env.DSQL_DB_HOST}`);
|
||||
|
||||
/**
|
||||
* # Declare Global Variables
|
||||
*/
|
||||
declare global {
|
||||
var INIT_SQL_ROOT_DB_CONN: ServerlessMysql;
|
||||
var INIT_SQL_MAIN_DB_CONN: ServerlessMysql;
|
||||
}
|
||||
|
||||
/**
|
||||
* # Ping Database Function
|
||||
*/
|
||||
function pingDb() {
|
||||
return execute(
|
||||
`mysqladmin ping -h"${process.env.DSQL_DB_HOST}" -u"root" -p"${process.env.DSQL_MARIADB_ROOT_PASSWORD}" --silent`
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* # Check MariaDB Availability
|
||||
*/
|
||||
let tries = 0;
|
||||
|
||||
while (true) {
|
||||
if (tries > 10) {
|
||||
process.exit(2);
|
||||
}
|
||||
|
||||
const checkDb = pingDb();
|
||||
|
||||
if (checkDb?.match(/alive/)) break;
|
||||
|
||||
tries++;
|
||||
|
||||
await Bun.sleep(2000);
|
||||
}
|
||||
|
||||
console.log("MariaDB is ready!");
|
||||
|
||||
/**
|
||||
* # Check Environment Variables
|
||||
*/
|
||||
await initSQLCheckEnvVariables();
|
||||
|
||||
/**
|
||||
* # Handle Root Users
|
||||
*/
|
||||
global.INIT_SQL_ROOT_DB_CONN = mysql({
|
||||
config: {
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: "root",
|
||||
password: process.env.DSQL_MARIADB_ROOT_PASSWORD,
|
||||
charset: "utf8mb4",
|
||||
},
|
||||
});
|
||||
|
||||
await initSQLHandleRootUsers();
|
||||
|
||||
/**
|
||||
* # Handle Operations with New Root User
|
||||
*/
|
||||
global.INIT_SQL_MAIN_DB_CONN = mysql({
|
||||
config: {
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: "root",
|
||||
password: process.env.DSQL_DB_PASSWORD,
|
||||
charset: "utf8mb4",
|
||||
},
|
||||
});
|
||||
|
||||
await initSQLCreateDatabases();
|
||||
await initSQLCheckDsqlUsers();
|
||||
|
||||
/**
|
||||
* # All Done. Exit With Success.
|
||||
*/
|
||||
process.exit(0);
|
||||
Executable
+178
@@ -0,0 +1,178 @@
|
||||
#!/bin/bash
|
||||
|
||||
cd "$(dirname "${BASH_SOURCE[0]}")" || exit
|
||||
|
||||
echo "DB HOST => $DSQL_DB_HOST"
|
||||
|
||||
## ==========================
|
||||
# Wait for Mariadb Server to load
|
||||
## ==========================
|
||||
check_mariadb() {
|
||||
mysqladmin ping -h"$DSQL_DB_HOST" -u"root" -p"$DSQL_MARIADB_ROOT_PASSWORD" --silent
|
||||
}
|
||||
|
||||
echo "Waiting for MariaDB to be ready..."
|
||||
|
||||
until check_mariadb; do
|
||||
sleep 2
|
||||
done
|
||||
|
||||
echo "MariaDB is ready!"
|
||||
|
||||
## ==========================
|
||||
# Load Env File
|
||||
## ==========================
|
||||
env_file="${1:-.env}"
|
||||
|
||||
if [ -f $env_file ]; then
|
||||
source $env_file
|
||||
else
|
||||
echo ".env file not found! Please provide a .env file path or add said file to the working direcotry to continue."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
## ==========================
|
||||
# initialise variables
|
||||
## ==========================
|
||||
|
||||
## ==========================
|
||||
# Setup first root function
|
||||
## ==========================
|
||||
query_db_with_main_root() {
|
||||
local query="$1"
|
||||
local result=$(mariadb -u root -h $DSQL_DB_HOST -p$DSQL_MARIADB_ROOT_PASSWORD -e "$query")
|
||||
|
||||
if [[ $? -ne 0 ]]; then
|
||||
echo "Error executing the query."
|
||||
return 2
|
||||
fi
|
||||
|
||||
local row_count=$(echo "$result" | wc -l)
|
||||
if ((row_count > 1)); then
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
## ==========================
|
||||
# Mariadb Query Function
|
||||
## ==========================
|
||||
query_db() {
|
||||
local query="$1"
|
||||
local result=$(mariadb -u root -h $DSQL_DB_HOST -p$DSQL_DB_PASSWORD -e "$query")
|
||||
|
||||
if [[ $? -ne 0 ]]; then
|
||||
echo "Error executing the query."
|
||||
return 2
|
||||
fi
|
||||
|
||||
local row_count=$(echo "$result" | wc -l)
|
||||
if ((row_count > 1)); then
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# SQL=$(sed "s/\${\([^}]*\)}/\${\1}/g" "test.sql" | envsubst)
|
||||
|
||||
# query_db "$SQL"
|
||||
|
||||
## ==========================
|
||||
# Check for required ENV Variables
|
||||
## ==========================
|
||||
if [ -z "${DSQL_DB_TARGET_IP_ADDRESS}" ]; then
|
||||
echo "DSQL_DB_TARGET_IP_ADDRESS env is not set"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ -z "${DSQL_DB_PASSWORD}" ]; then
|
||||
echo "DSQL_DB_PASSWORD env is not set"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ -z "${DSQL_DB_READ_ONLY_PASSWORD}" ]; then
|
||||
echo "DSQL_DB_READ_ONLY_PASSWORD env is not set"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ -z "${DSQL_DB_FULL_ACCESS_PASSWORD}" ]; then
|
||||
echo "DSQL_DB_FULL_ACCESS_PASSWORD env is not set"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
## ==========================
|
||||
# Create Datasquirel Root User
|
||||
## ==========================
|
||||
|
||||
echo "HOST ENV: $DSQL_HOST_ENV"
|
||||
|
||||
if [ -n "${NEXT_PUBLIC_DSQL_LOCAL}" ]; then
|
||||
if [[ "$DSQL_HOST_ENV" == *"dev_dev"* ]]; then
|
||||
echo "Dev Environment. Root user setup not required"
|
||||
else
|
||||
query_db_with_main_root "select user,host from mysql.user where user='root' and host='$DSQL_DB_TARGET_IP_ADDRESS';"
|
||||
|
||||
if [[ $? == 0 ]]; then
|
||||
echo "Root User Exists"
|
||||
else
|
||||
echo "No Root User. Creating ..."
|
||||
query_db_with_main_root "CREATE USER IF NOT EXISTS 'root'@'$DSQL_DB_TARGET_IP_ADDRESS' IDENTIFIED BY '$DSQL_DB_PASSWORD';\
|
||||
GRANT ALL PRIVILEGES ON *.* TO 'root'@'$DSQL_DB_TARGET_IP_ADDRESS' WITH GRANT OPTION;\
|
||||
FLUSH PRIVILEGES;"
|
||||
|
||||
query_db "select user,host from mysql.user where user='root' and host='$DSQL_DB_TARGET_IP_ADDRESS';"
|
||||
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "Error: Couldn't create root user."
|
||||
exit 1
|
||||
else
|
||||
query_db "drop user 'root'@'%';"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
else
|
||||
echo "Production Environment. Root user setup not required"
|
||||
fi
|
||||
|
||||
## ==========================
|
||||
# Create Datasquirel Database
|
||||
## ==========================
|
||||
check_database_exists=$(query_db "show databases like 'datasquirel';")
|
||||
|
||||
if [[ $? == 0 ]]; then
|
||||
echo "Dsql Database exists"
|
||||
else
|
||||
echo "Dsql Database does not exist. Creating ..."
|
||||
query_db "create database datasquirel;"
|
||||
fi
|
||||
|
||||
## ==========================
|
||||
# Create Datasquirel Read Only User
|
||||
## ==========================
|
||||
query_db "select user,host from mysql.user where user = '$DSQL_DB_READ_ONLY_USERNAME' and host = '$DSQL_DB_TARGET_IP_ADDRESS';"
|
||||
|
||||
if [[ $? == 0 ]]; then
|
||||
echo "$DSQL_DB_READ_ONLY_USERNAME User Exists"
|
||||
else
|
||||
echo "No $DSQL_DB_READ_ONLY_USERNAME User. Creating ..."
|
||||
query_db "CREATE USER IF NOT EXISTS '$DSQL_DB_READ_ONLY_USERNAME'@'$DSQL_DB_TARGET_IP_ADDRESS' IDENTIFIED BY '$DSQL_DB_READ_ONLY_PASSWORD';"
|
||||
fi
|
||||
|
||||
## ==========================
|
||||
# Create Datasquirel Full Access User
|
||||
## ==========================
|
||||
DSQL_DB_FULL_ACCESS_USERNAME="datasquirel_full_access"
|
||||
query_db "select user,host from mysql.user where user = '$DSQL_DB_FULL_ACCESS_USERNAME' and host = '$DSQL_DB_TARGET_IP_ADDRESS';"
|
||||
|
||||
if [[ $? == 0 ]]; then
|
||||
echo "$DSQL_DB_FULL_ACCESS_USERNAME User Exists"
|
||||
else
|
||||
echo "No $DSQL_DB_FULL_ACCESS_USERNAME User. Creating ..."
|
||||
query_db "CREATE USER IF NOT EXISTS '$DSQL_DB_FULL_ACCESS_USERNAME'@'$DSQL_DB_TARGET_IP_ADDRESS' IDENTIFIED BY '$DSQL_DB_FULL_ACCESS_PASSWORD';"
|
||||
fi
|
||||
|
||||
query_db "FLUSH PRIVILEGES;"
|
||||
|
||||
exit 0
|
||||
Reference in New Issue
Block a user