dsql-admin/dsql-app/docker-root-user-init.js
Benjamin Toby 86f931fb82 Updates
2024-12-08 09:57:48 +01:00

58 lines
1.6 KiB
JavaScript
Executable File

// @ts-check
const path = require("path");
require("dotenv").config({ path: path.resolve(__dirname, ".env") });
const fs = require("fs");
const { execSync } = require("child_process");
/**
* # Create Extra users
* @description Create `READ_ONLY` and `FULL_ACCESS` users
*/
async function initRootUser() {
/**
* Grab Schema
*
* @description Grab Schema
*/
const first_name = process.env.DSQL_LOCAL_SU_FIRST_NAME;
const last_name = process.env.DSQL_LOCAL_SU_LAST_NAME;
const email = process.env.DSQL_LOCAL_SU_EMAIL;
const username = process.env.DSQL_LOCAL_SU_USERNAME;
const password = process.env.DSQL_LOCAL_SU_PASSWORD;
if (!password) {
throw new Error("Please enter a root admin user password");
}
/** @type {any} */
const userObject = {
id: 1,
first_name,
last_name,
username,
email,
password,
verification_status: 1,
user_priviledge: 7,
};
try {
const tmpDir = "/app/.tmp";
if (!fs.existsSync(tmpDir)) {
fs.mkdirSync(tmpDir, { recursive: true });
}
const tmpFilePath = `${tmpDir}/new-user.json`;
fs.writeFileSync(tmpFilePath, JSON.stringify(userObject));
const execStr = `node /app/package-shared/shell/mariadb-users/users/create-user.js ${tmpFilePath}`;
execSync(execStr, { stdio: "inherit" });
} catch (error) {
console.log(`docker-root-user-init.js ERROR: ${error.message}`);
}
process.exit();
}
initRootUser();