update logic for deploy on chicken egg
This commit is contained in:
parent
61a1340eef
commit
d29806ae19
1 changed files with 24 additions and 27 deletions
|
|
@ -15,38 +15,35 @@ else
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# 2. WAIT FOR MONGO TO BE READY
|
STATUS_JSON=$(docker exec -t "mongo-db${KOMODO_NODE_ID}" mongosh --quiet --eval "rs.status()" 2>/dev/null)
|
||||||
# We need the container to be up before we can run mongosh commands
|
IS_INIT=$(echo "$STATUS_JSON" | grep -c "ok: 1")
|
||||||
echo "⏳ Waiting for MongoDB container (${mongo-db$KOMODO_NODE_ID}) to be ready..."
|
|
||||||
MAX_RETRIES=10
|
if [ "$IS_INIT" -gt 0 ]; then
|
||||||
COUNT=0
|
echo "✅ MongoDB is already part of a replica set. Nothing to do."
|
||||||
until docker exec "mongo-db$KOMODO_NODE_ID" mongosh --quiet --eval "db.adminCommand('ping')" &>/dev/null; do
|
exit 0
|
||||||
((COUNT++))
|
|
||||||
if [ $COUNT -ge $MAX_RETRIES ]; then
|
|
||||||
echo "❌ Timeout: MongoDB container is not responding."
|
|
||||||
exit 1
|
|
||||||
fi
|
fi
|
||||||
sleep 2
|
|
||||||
done
|
|
||||||
|
|
||||||
# 3. CHECK-THEN-INITIATE LOGIC
|
if [ "$KOMODO_NODE_ID" == "1" ]; then
|
||||||
# Ask the DB if it is already part of a replica set
|
echo "👑 Node 1 detected. Initiating as Primary for 'rs${LXC_NUM}'..."
|
||||||
IS_INIT=$(docker exec -t "mongo-db$KOMODO_NODE_ID" mongosh --quiet --eval "rs.status().ok" 2>/dev/null)
|
|
||||||
|
|
||||||
if [ "$IS_INIT" == "1" ]; then
|
# Initiate with just self to guarantee Primary status
|
||||||
echo "✅ MongoDB Replica Set 'rs${LXC_NUM}' is already initialized. Skipping."
|
docker exec -t "mongo-db1" mongosh --eval "
|
||||||
else
|
|
||||||
echo "⚠️ Replica Set not found. Initializing 'rs${LXC_NUM}' now..."
|
|
||||||
|
|
||||||
# Use your defined PRI/SEC variables for the members
|
|
||||||
docker exec -t "mongo-db$KOMODO_NODE_ID" mongosh --eval "
|
|
||||||
rs.initiate({
|
rs.initiate({
|
||||||
_id: 'rs${LXC_NUM}',
|
_id: 'rs${LXC_NUM}',
|
||||||
members: [
|
members: [{ _id: 0, host: 'mongo-db1:27017' }]
|
||||||
{ _id: 0, host: 'mongo-db1:27017' },
|
|
||||||
{ _id: 1, host: 'mongo-db2:27017' }
|
|
||||||
]
|
|
||||||
})"
|
})"
|
||||||
|
|
||||||
echo "🚀 Initialization command sent! Transitioning to Primary..."
|
echo "⏳ Waiting for election..."
|
||||||
|
sleep 5
|
||||||
|
|
||||||
|
# Pre-authorize the partner
|
||||||
|
docker exec -t "mongo-db1" mongosh --eval "rs.add('mongo-db2:27017')"
|
||||||
|
echo "🚀 Node 1 is now Primary and waiting for Node 2."
|
||||||
|
|
||||||
|
else
|
||||||
|
echo "🏃 Node 2 detected. Attempting to handshake with Node 1..."
|
||||||
|
|
||||||
|
# Node 2 doesn't initiate. It just waits for Node 1 to be ready.
|
||||||
|
# Node 1 already ran rs.add('mongo-db2'), so Node 2 just needs to be up.
|
||||||
|
echo "🤝 Node 2 is online. Synchronization should start automatically via Node 1's config."
|
||||||
fi
|
fi
|
||||||
Loading…
Reference in a new issue