Merge pull request #2141 from bluewave-labs/fix/prod-docker
single init script
This commit is contained in:
4 files changed
+60
-34
No files matched your search
@@ -52,7 +52,6 @@ services:
|
|||||||
- "27017:27017"
|
- "27017:27017"
|
||||||
volumes:
|
volumes:
|
||||||
- ./mongo/data:/data/db
|
- ./mongo/data:/data/db
|
||||||
- ./mongo/init/01_init_replica_set.js:/docker-entrypoint-initdb.d/01_init_replica_set.js
|
- ./mongo/init/init.js:/docker-entrypoint-initdb.d/init.js
|
||||||
- ./mongo/init/02_create_users.js:/docker-entrypoint-initdb.d/02_create_users.js
|
|
||||||
env_file:
|
env_file:
|
||||||
- mongo.env
|
- mongo.env
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
function waitForPrimary() {
|
|
||||||
while (true) {
|
|
||||||
try {
|
|
||||||
rs.initiate({
|
|
||||||
_id: "rs0",
|
|
||||||
members: [{ _id: 0, host: "localhost:27017" }],
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
} catch (e) {
|
|
||||||
print("Replica set initiation failed, retrying in 2s...");
|
|
||||||
sleep(2000);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
waitForPrimary();
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
var username = process.env.USERNAME_ENV_VAR;
|
|
||||||
var password = process.env.PASSWORD_ENV_VAR;
|
|
||||||
|
|
||||||
db = db.getSiblingDB("uptime_db");
|
|
||||||
|
|
||||||
db.createUser({
|
|
||||||
user: username,
|
|
||||||
pwd: password,
|
|
||||||
roles: [
|
|
||||||
{
|
|
||||||
role: "readWrite",
|
|
||||||
db: "uptime_db",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
print("User uptime_user created successfully");
|
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
function sleep(ms) {
|
||||||
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||||
|
}
|
||||||
|
|
||||||
|
async function initReplicaSet() {
|
||||||
|
while (true) {
|
||||||
|
try {
|
||||||
|
rs.initiate({
|
||||||
|
_id: "rs0",
|
||||||
|
members: [{ _id: 0, host: "localhost:27017" }],
|
||||||
|
});
|
||||||
|
print("Replica set initiated.");
|
||||||
|
break;
|
||||||
|
} catch (e) {
|
||||||
|
print("Replica set initiation failed, retrying in 2s...");
|
||||||
|
await sleep(2000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait until the node becomes primary
|
||||||
|
while (true) {
|
||||||
|
try {
|
||||||
|
const status = rs.status();
|
||||||
|
if (status.myState === 1) {
|
||||||
|
print("Node is now PRIMARY.");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
print("⏳ Waiting for PRIMARY state...");
|
||||||
|
}
|
||||||
|
await sleep(1000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function createAppUser() {
|
||||||
|
const username = process.env.USERNAME_ENV_VAR;
|
||||||
|
const password = process.env.PASSWORD_ENV_VAR;
|
||||||
|
|
||||||
|
if (!username || !password) {
|
||||||
|
throw new Error("USERNAME_ENV_VAR or PASSWORD_ENV_VAR not set.");
|
||||||
|
}
|
||||||
|
|
||||||
|
const dbName = "uptime_db";
|
||||||
|
const userDb = db.getSiblingDB(dbName);
|
||||||
|
|
||||||
|
try {
|
||||||
|
userDb.createUser({
|
||||||
|
user: username,
|
||||||
|
pwd: password,
|
||||||
|
roles: [{ role: "readWrite", db: dbName }],
|
||||||
|
});
|
||||||
|
print(`✅ User '${username}' created successfully.`);
|
||||||
|
} catch (e) {
|
||||||
|
print(`⚠️ Failed to create user '${username}': ${e.message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await initReplicaSet();
|
||||||
|
await createAppUser();
|
||||||
Reference in new issue
Block a user