29 lines
921 B
Bash
Executable File
29 lines
921 B
Bash
Executable File
#!/bin/bash
|
|
# Initiates the single-node replica set "rs0" if it does not exist yet and
|
|
# waits until the member is primary. Runs as a one-shot container.
|
|
set -euo pipefail
|
|
|
|
HOST="${MONGO_HOST:-al-mongo}"
|
|
PORT="${MONGO_PORT:-27017}"
|
|
|
|
echo "Waiting for MongoDB at ${HOST}:${PORT} ..."
|
|
until mongosh --host "${HOST}" --port "${PORT}" --quiet \
|
|
--eval 'db.adminCommand({ ping: 1 }).ok' >/dev/null 2>&1; do
|
|
sleep 2
|
|
done
|
|
|
|
if [ "$(mongosh --host "${HOST}" --port "${PORT}" --quiet \
|
|
--eval 'try { rs.status().ok } catch (e) { 0 }')" != "1" ]; then
|
|
echo "Initiating replica set rs0 ..."
|
|
mongosh --host "${HOST}" --port "${PORT}" --quiet \
|
|
--eval 'rs.initiate({ _id: "rs0", members: [{ _id: 0, host: "al-mongo:27017" }] })'
|
|
fi
|
|
|
|
echo "Waiting for primary ..."
|
|
until mongosh --host "${HOST}" --port "${PORT}" --quiet \
|
|
--eval 'db.hello().isWritablePrimary' | grep -q true; do
|
|
sleep 2
|
|
done
|
|
|
|
echo "Replica set rs0 is ready."
|