mongodb-control-new 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #!/bin/bash
  2. # MongoDB Control Script
  3. # Handles MongoDB server startup with automatic version detection and switching
  4. # get wekan/mongo settings
  5. source $SNAP/bin/wekan-read-settings
  6. if [ "true" == "${DISABLE_MONGODB}" ]; then
  7. echo "mongodb is disabled. Stop service"
  8. snapctl stop --disable ${SNAP_NAME}.mongodb
  9. exit 0
  10. fi
  11. # MongoDB Version Detection and Auto-Switching System
  12. # Detects MongoDB server version from connection attempts and switches to correct binary
  13. # MongoDB binary paths for different versions
  14. MONGO3_BIN="/snap/${SNAP_NAME}/current/migratemongo/bin"
  15. MONGO7_BIN="/snap/${SNAP_NAME}/current/bin"
  16. MONGO3_LIB="/snap/${SNAP_NAME}/current/migratemongo/lib"
  17. MONGO7_LIB="/snap/${SNAP_NAME}/current/usr/lib"
  18. # Version detection log
  19. VERSION_DETECTION_LOG="${SNAP_COMMON}/mongodb-version-detection.log"
  20. # Log version detection events
  21. log_version_detection() {
  22. echo "$(date): $1" >> "$VERSION_DETECTION_LOG"
  23. }
  24. # Detect MongoDB server version by attempting connection
  25. detect_mongodb_version() {
  26. local mongo_url="${MONGO_URL:-mongodb://127.0.0.1:27017/wekan}"
  27. local detected_version=""
  28. log_version_detection "Starting MongoDB version detection for: $mongo_url"
  29. # Try to connect with MongoDB 7 first (latest)
  30. log_version_detection "Attempting connection with MongoDB 7 binary"
  31. if timeout 10s /snap/${SNAP_NAME}/current/bin/mongosh --quiet --eval "db.runCommand({buildInfo: 1}).version" "$mongo_url" 2>/dev/null | grep -q "7\."; then
  32. detected_version="7"
  33. log_version_detection "Detected MongoDB 7.x server"
  34. elif timeout 10s /snap/${SNAP_NAME}/current/bin/mongosh --quiet --eval "db.runCommand({buildInfo: 1}).version" "$mongo_url" 2>&1 | grep -q "protocol version\|wire protocol"; then
  35. # Check for wire protocol errors that indicate older version
  36. local error_output=$(timeout 10s /snap/${SNAP_NAME}/current/bin/mongosh --quiet --eval "db.runCommand({buildInfo: 1}).version" "$mongo_url" 2>&1)
  37. if echo "$error_output" | grep -q "protocol version 0\|wire protocol version 0"; then
  38. detected_version="3"
  39. log_version_detection "Detected MongoDB 3.x server (wire protocol 0)"
  40. elif echo "$error_output" | grep -q "protocol version 1\|wire protocol version 1"; then
  41. detected_version="3"
  42. log_version_detection "Detected MongoDB 3.x server (wire protocol 1)"
  43. elif echo "$error_output" | grep -q "protocol version 2\|wire protocol version 2"; then
  44. detected_version="3"
  45. log_version_detection "Detected MongoDB 3.x server (wire protocol 2)"
  46. elif echo "$error_output" | grep -q "protocol version 3\|wire protocol version 3"; then
  47. detected_version="3"
  48. log_version_detection "Detected MongoDB 3.x server (wire protocol 3)"
  49. else
  50. log_version_detection "Unknown wire protocol error: $error_output"
  51. fi
  52. else
  53. log_version_detection "No MongoDB server running or connection failed"
  54. fi
  55. echo "$detected_version"
  56. }
  57. # Switch to appropriate MongoDB binary based on detected version
  58. switch_mongodb_binary() {
  59. local version="$1"
  60. case "$version" in
  61. "3")
  62. log_version_detection "Switching to MongoDB 3.x binary"
  63. export PATH="${MONGO3_BIN}:${PATH}"
  64. export LD_LIBRARY_PATH="${MONGO3_LIB}:${MONGO3_LIB}/x86_64-linux-gnu:${LD_LIBRARY_PATH}"
  65. echo "3" > "${SNAP_COMMON}/mongodb-active-version"
  66. ;;
  67. "7"|"")
  68. log_version_detection "Using MongoDB 7.x binary (default)"
  69. export PATH="${MONGO7_BIN}:${PATH}"
  70. export LD_LIBRARY_PATH="${MONGO7_LIB}:${LD_LIBRARY_PATH}"
  71. echo "7" > "${SNAP_COMMON}/mongodb-active-version"
  72. ;;
  73. *)
  74. log_version_detection "Unknown version $version, using default MongoDB 7.x"
  75. export PATH="${MONGO7_BIN}:${PATH}"
  76. export LD_LIBRARY_PATH="${MONGO7_LIB}:${LD_LIBRARY_PATH}"
  77. echo "7" > "${SNAP_COMMON}/mongodb-active-version"
  78. ;;
  79. esac
  80. }
  81. # Main version detection and switching logic
  82. setup_mongodb_version() {
  83. # Check if we have a cached version
  84. if [ -f "${SNAP_COMMON}/mongodb-active-version" ]; then
  85. local cached_version=$(cat "${SNAP_COMMON}/mongodb-active-version")
  86. log_version_detection "Using cached MongoDB version: $cached_version"
  87. switch_mongodb_binary "$cached_version"
  88. return
  89. fi
  90. # Detect version and switch
  91. local detected_version=$(detect_mongodb_version)
  92. if [ -n "$detected_version" ]; then
  93. switch_mongodb_binary "$detected_version"
  94. else
  95. # Default to MongoDB 7 if detection fails
  96. log_version_detection "Version detection failed, using default MongoDB 7.x"
  97. switch_mongodb_binary "7"
  98. fi
  99. }
  100. # Run version detection and setup
  101. setup_mongodb_version
  102. # make sure we have set minimum env variables for locale
  103. if [ -z "${LANG}" ]; then
  104. export LANG=en_US.UTF-8
  105. fi
  106. export LC_ALL=C
  107. # If CPU does not support AVX, use Qemu that supports AVX.
  108. # Migratemongo is at https://github.com/wekan/migratemongo
  109. # and at directory /snap/${SNAP_NAME}/current/migratemongo/avx
  110. # is bash scripts like mongod, mongosh check avx support and use Qemu if needed.
  111. export PATH=/snap/${SNAP_NAME}/current/migratemongo/avx:/snap/${SNAP_NAME}/current/usr/bin:/snap/${SNAP_NAME}/current/bin:${PATH}
  112. export LD_LIBRARY_PATH=/snap/${SNAP_NAME}/current/lib:/snap/${SNAP_NAME}/current/lib/x86_64-linux-gnu:${LD_LIBRARY_PATH}
  113. # If temporary settings log exists, delete it
  114. if [ -f ${SNAP_COMMON}/settings.log ]; then
  115. rm ${SNAP_COMMON}/settings.log
  116. fi
  117. # Set MongoDB log destination to snapcommon for log file detection
  118. export MONGO_LOG_DESTINATION="snapcommon"
  119. # Set MongoDB data directory
  120. export MONGO_DATA_DIR="${SNAP_COMMON}/wekan"
  121. # Create MongoDB data directory if it doesn't exist
  122. if [ ! -d "$MONGO_DATA_DIR" ]; then
  123. mkdir -p "$MONGO_DATA_DIR"
  124. chmod 755 "$MONGO_DATA_DIR"
  125. fi
  126. # Set MongoDB log file path
  127. export MONGO_LOG_FILE="${SNAP_COMMON}/mongodb.log"
  128. # Start MongoDB with appropriate version
  129. echo "Starting MongoDB with detected version..."
  130. log_version_detection "Starting MongoDB server"
  131. # Get the active version
  132. ACTIVE_VERSION=$(cat "${SNAP_COMMON}/mongodb-active-version" 2>/dev/null || echo "7")
  133. if [ "$ACTIVE_VERSION" = "3" ]; then
  134. echo "Starting MongoDB 3.x server..."
  135. log_version_detection "Starting MongoDB 3.x server"
  136. exec /snap/${SNAP_NAME}/current/migratemongo/bin/mongod \
  137. --dbpath="$MONGO_DATA_DIR" \
  138. --logpath="$MONGO_LOG_FILE" \
  139. --logappend \
  140. --bind_ip=127.0.0.1 \
  141. --port=27017 \
  142. --fork
  143. else
  144. echo "Starting MongoDB 7.x server..."
  145. log_version_detection "Starting MongoDB 7.x server"
  146. exec /snap/${SNAP_NAME}/current/bin/mongod \
  147. --dbpath="$MONGO_DATA_DIR" \
  148. --logpath="$MONGO_LOG_FILE" \
  149. --logappend \
  150. --bind_ip=127.0.0.1 \
  151. --port=27017 \
  152. --fork
  153. fi