Skip to content

Commit 1f8b015

Browse files
fix: update script with mongodb connection string
fix: update script with mongodb connection string
2 parents 8a06ac6 + 8a009ea commit 1f8b015

File tree

6 files changed

+89
-17
lines changed

6 files changed

+89
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Changelog
22

33
### Changed
4+
- create MONGODB connection string from environment variables instead of full MONGO_URI variable
45
- create REDIS connection string from environment variables instead of full REDIS_URL variable
56

67
## 1.1.0

backend/Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ ENV FLASK_DEBUG production
99
# Copy scripts
1010
COPY ./flask_start.sh /flask_start.sh
1111
COPY ./celery_start.sh /celery_start.sh
12-
COPY ./construct-redis-url.sh /app/construct-redis-url.sh
12+
COPY construct-connection-strings.sh /app/construct-connection-strings.sh
1313

1414
# Set permissions BEFORE USER switch
15-
RUN chmod +x /flask_start.sh /celery_start.sh /app/construct-redis-url.sh
15+
RUN chmod +x /flask_start.sh /celery_start.sh /app/construct-connection-strings.sh
1616

1717
# Switch to non-root
1818
USER 10000:10000
1919

2020
EXPOSE 5000
2121

22-
# Use flask_start.sh which sources construct-redis-url.sh
22+
# Use flask_start.sh which sources construct-connection-strings.sh
2323
CMD ["/flask_start.sh"]

backend/SC4SNMP_UI_backend/__init__.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import sys
2+
import time
3+
14
from flask import Flask
25
from pymongo import MongoClient
6+
from pymongo.errors import ConnectionFailure, ServerSelectionTimeoutError
37
import os
48
import logging
59
from celery import Celery
@@ -11,6 +15,63 @@
1115
__version__ = "1.1.2-beta.1"
1216

1317
MONGO_URI = os.getenv("MONGO_URI")
18+
19+
20+
21+
def wait_for_mongodb_replicaset(logger, max_retries=120, retry_interval=5):
22+
"""
23+
Wait for MongoDB to be ready before starting the application.
24+
For replica sets, waits for PRIMARY to be elected.
25+
"""
26+
mongo_mode = os.getenv("MONGODB_MODE", "standalone").lower()
27+
if mongo_mode == "standalone":
28+
logger.info("MongoDB is in standalone mode, skipping ReplicaSet wait")
29+
return
30+
31+
mongo_uri = os.getenv("MONGO_URI")
32+
33+
if not mongo_uri:
34+
logger.warning("MONGO_URI not set, exiting application")
35+
sys.exit(1)
36+
37+
logger.info(f"Waiting for MongoDB ReplicaSet to be ready and elect the primary...")
38+
39+
for attempt in range(1, max_retries + 1):
40+
try:
41+
# Try to connect
42+
client = MongoClient(
43+
mongo_uri, serverSelectionTimeoutMS=5000, connectTimeoutMS=5000
44+
)
45+
46+
# Execute a simple operation to verify PRIMARY exists
47+
client.admin.command("ping")
48+
49+
# For replica sets, verify PRIMARY exists
50+
if "replicaSet=" in mongo_uri:
51+
if client.primary is None:
52+
raise Exception("No PRIMARY elected yet")
53+
logger.info(f"PRIMARY found: {client.primary}")
54+
55+
client.close()
56+
logger.info("MongoDB is ready")
57+
return
58+
59+
except (ServerSelectionTimeoutError, ConnectionFailure, Exception) as e:
60+
if attempt >= max_retries:
61+
logger.info(
62+
f"MongoDB not ready after {max_retries * retry_interval}s"
63+
)
64+
logger.info(f" Error: {e}")
65+
sys.exit(1)
66+
67+
if attempt % 6 == 0: # Print every 30 seconds
68+
logger.info(
69+
f" Still waiting... ({attempt}/{max_retries}) - {e.__class__.__name__}"
70+
)
71+
72+
time.sleep(retry_interval)
73+
74+
wait_for_mongodb_replicaset(logging.getLogger())
1475
mongo_client = MongoClient(MONGO_URI)
1576

1677
VALUES_DIRECTORY = os.getenv("VALUES_DIRECTORY", "")

backend/celery_start.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
set -e
33

44
# Source Redis URL construction
5-
. /app/construct-redis-url.sh
5+
. /app/construct-connection-strings.sh
66

77
set -o errexit
88
set -o nounset
Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,19 @@ if [ -z "$REDIS_URL" ] || [ -z "$CELERY_BROKER_URL" ]; then
2121

2222
if [ -n "$REDIS_PASSWORD" ]; then
2323
SENTINEL_SCHEME="sentinel://:${REDIS_PASSWORD}@${REDIS_SENTINEL_SERVICE}:${REDIS_SENTINEL_PORT}"
24-
REDBEAT_SCHEME="redis-sentinel://:${REDIS_PASSWORD}@${REDIS_SENTINEL_SERVICE}:${REDIS_SENTINEL_PORT}"
2524
REDIS_HA_CHECK="redis://:${REDIS_PASSWORD}@${REDIS_HEADLESS_SERVICE}:${REDIS_PORT}"
2625
else
2726
SENTINEL_SCHEME="sentinel://${REDIS_SENTINEL_SERVICE}:${REDIS_SENTINEL_PORT}"
28-
REDBEAT_SCHEME="redis-sentinel://${REDIS_SENTINEL_SERVICE}:${REDIS_SENTINEL_PORT}"
2927
REDIS_HA_CHECK="redis://${REDIS_HEADLESS_SERVICE}:${REDIS_PORT}"
30-
3128
fi
3229

30+
REDBEAT_SCHEME="redis-sentinel://${REDIS_SENTINEL_SERVICE}:${REDIS_SENTINEL_PORT}"
31+
3332
# Celery broker uses sentinel://
3433
: "${CELERY_BROKER_URL:=${SENTINEL_SCHEME}/${CELERY_DB}#master_name=${REDIS_MASTER_NAME}}"
3534

3635
# RedBeat uses redis-sentinel:// with master_name query
3736
: "${REDIS_URL:=${REDBEAT_SCHEME}/${REDIS_DB}#master_name=${REDIS_MASTER_NAME}}"
38-
SENTINEL_CHECK="redis://${REDIS_SENTINEL_SERVICE}:${REDIS_SENTINEL_PORT}"
39-
# For healthcheck / wait-for-dep - space-separated list
40-
REDIS_DEPENDENCIES="${SENTINEL_CHECK} ${REDIS_HA_CHECK}"
4137

4238
else
4339
# Standalone mode
@@ -53,16 +49,30 @@ if [ -z "$REDIS_URL" ] || [ -z "$CELERY_BROKER_URL" ]; then
5349

5450
: "${REDIS_URL:=$BASE/${REDIS_DB:-1}}"
5551
: "${CELERY_BROKER_URL:=$BASE/${CELERY_DB:-0}}"
56-
57-
# For healthcheck / wait-for-dep - space-separated list
58-
REDIS_DEPENDENCIES="${REDIS_URL} ${CELERY_BROKER_URL}"
5952
fi
6053

61-
echo "Constructed REDIS_URL: $REDIS_URL"
62-
echo "Constructed CELERY_BROKER_URL: $CELERY_BROKER_URL"
54+
# Build MongoDB URI from environment variables
55+
if [ -n "$MONGODB_PASSWORD" ]; then
56+
# With authentication
57+
if [ -n "$MONGODB_REPLICA_SET" ]; then
58+
# Replica set
59+
export MONGO_URI="mongodb://${MONGODB_USERNAME}:${MONGODB_PASSWORD}@${MONGODB_HOST}/${MONGODB_DATABASE}?replicaSet=${MONGODB_REPLICA_SET}&authSource=${MONGODB_AUTH_SOURCE:-admin}&readPreference=primary"
60+
else
61+
# Standalone
62+
export MONGO_URI="mongodb://${MONGODB_USERNAME}:${MONGODB_PASSWORD}@${MONGODB_HOST}:${MONGODB_PORT}/${MONGODB_DATABASE}?authSource=admin"
63+
fi
64+
else
65+
# Without authentication
66+
if [ -n "$MONGODB_REPLICA_SET" ]; then
67+
export MONGO_URI="mongodb://${MONGODB_HOST}/${MONGODB_DATABASE}?replicaSet=${MONGODB_REPLICA_SET}&authSource=admin&retryWrites=false"
68+
else
69+
export MONGO_URI="mongodb://${MONGODB_HOST}:${MONGODB_PORT}/${MONGODB_DATABASE}?authSource=admin"
70+
fi
71+
fi
6372

73+
export MONGO_URI
6474
export REDIS_URL
6575
export CELERY_BROKER_URL
66-
export REDIS_DEPENDENCIES
6776
export REDIS_MODE
77+
6878
fi

backend/flask_start.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
set -e
33

44
# Source Redis URL construction
5-
. /app/construct-redis-url.sh
5+
. /app/construct-connection-strings.sh
66

77
set -o errexit
88
set -o nounset

0 commit comments

Comments
 (0)