Files

41 lines
855 B
JavaScript
Raw Permalink Normal View History

2025-04-25 16:26:10 -07:00
function initiateReplicaSet() {
try {
rs.initiate({
_id: "rs0",
members: [{ _id: 0, host: "localhost:27017" }],
});
} catch (e) {
print("Replica set already initiated or error occurred: " + e);
}
}
2025-04-25 16:16:11 -07:00
function waitForPrimary() {
2025-04-25 16:09:02 -07:00
while (true) {
2025-04-25 16:26:10 -07:00
const isMaster = db.isMaster();
if (isMaster.ismaster) {
print("This node is now PRIMARY.");
2025-04-25 16:09:02 -07:00
break;
}
2025-04-25 16:26:10 -07:00
print("Waiting to become PRIMARY...");
sleep(2000);
2025-04-25 16:09:02 -07:00
}
}
2025-04-25 16:26:10 -07:00
function createUser() {
db = db.getSiblingDB("uptime_db");
2025-04-25 16:09:02 -07:00
2025-04-25 16:26:10 -07:00
var username = process.env.USERNAME_ENV_VAR;
var password = process.env.PASSWORD_ENV_VAR;
2025-04-25 16:09:02 -07:00
2025-04-25 16:26:10 -07:00
db.createUser({
user: username,
pwd: password,
roles: [{ role: "readWrite", db: "uptime_db" }],
});
2025-04-25 16:09:02 -07:00
2025-04-25 16:26:10 -07:00
print("User '" + username + "' created successfully");
}
2025-04-25 16:09:02 -07:00
2025-04-25 16:26:10 -07:00
initiateReplicaSet();
waitForPrimary();
createUser();