server-up/apps/unifi-network/files/init-mongo.sh
Ramon 6f549357b1
All checks were successful
Deploy server-up (dev) / deploy (push) Successful in 5m44s
fix(unifi): herstel MongoDB-gebruiker
2026-08-06 09:36:37 +02:00

49 lines
1.2 KiB
Bash
Executable file

<% raw %>
#!/bin/bash
set -e
if which mongosh > /dev/null 2>&1; then
mongo_init_bin="mongosh"
else
mongo_init_bin="mongo"
fi
mongo_args=()
if [[ -n "${MONGO_HOST:-}" ]]; then
mongo_args=(
--host "${MONGO_HOST}"
--port "${MONGO_PORT:-27017}"
)
fi
"${mongo_init_bin}" "${mongo_args[@]}" <<EOF
const authDb = db.getSiblingDB("${MONGO_AUTHSOURCE}");
if (!authDb.auth("${MONGO_INITDB_ROOT_USERNAME}", "${MONGO_INITDB_ROOT_PASSWORD}")) {
quit(18);
}
const roles = [
{ db: "admin", role: "clusterMonitor" },
{ db: "${MONGO_DBNAME}", role: "dbOwner" },
{ db: "${MONGO_DBNAME}_stat", role: "dbOwner" },
{ db: "${MONGO_DBNAME}_audit", role: "dbOwner" },
{ db: "${MONGO_DBNAME}_restore", role: "dbOwner" }
];
function ensureUser(database) {
const userDb = db.getSiblingDB(database);
const existing = userDb.getUser("${MONGO_USER}");
if (existing) {
userDb.updateUser("${MONGO_USER}", { pwd: "${MONGO_PASS}", roles: roles });
} else {
userDb.createUser({ user: "${MONGO_USER}", pwd: "${MONGO_PASS}", roles: roles });
}
}
ensureUser("${MONGO_AUTHSOURCE}");
if ("${MONGO_AUTHSOURCE}" !== "${MONGO_DBNAME}") {
ensureUser("${MONGO_DBNAME}");
}
EOF
<% endraw %>