mongodb-control 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #!/bin/bash
  2. # MongoDB Control Script
  3. # Handles MongoDB server startup with automatic version detection and switching
  4. # Supports MongoDB versions 3-8 with automatic binary selection
  5. # get wekan/mongo settings
  6. source $SNAP/bin/wekan-read-settings
  7. if [ "true" == "${DISABLE_MONGODB}" ]; then
  8. echo "mongodb is disabled. Stop service"
  9. snapctl stop --disable ${SNAP_NAME}.mongodb
  10. exit 0
  11. fi
  12. # MongoDB Version Detection and Auto-Switching System
  13. # Detects MongoDB server version from connection attempts and switches to correct binary
  14. # MongoDB binary paths for different versions
  15. # Note: Currently only versions 3 and 7 are available in the snap package
  16. # Versions 4, 5, 6, 8 would need to be added to the snap package
  17. MONGO3_BIN="/snap/${SNAP_NAME}/current/migratemongo/bin"
  18. MONGO7_BIN="/snap/${SNAP_NAME}/current/bin"
  19. MONGO3_LIB="/snap/${SNAP_NAME}/current/migratemongo/lib"
  20. MONGO7_LIB="/snap/${SNAP_NAME}/current/usr/lib"
  21. # Future paths for additional versions (when added to snap package)
  22. MONGO4_BIN="/snap/${SNAP_NAME}/current/mongodb4/bin"
  23. MONGO5_BIN="/snap/${SNAP_NAME}/current/mongodb5/bin"
  24. MONGO6_BIN="/snap/${SNAP_NAME}/current/mongodb6/bin"
  25. MONGO8_BIN="/snap/${SNAP_NAME}/current/mongodb8/bin"
  26. # Version detection log
  27. VERSION_DETECTION_LOG="${SNAP_COMMON}/mongodb-version-detection.log"
  28. # Log version detection events
  29. log_version_detection() {
  30. echo "$(date): $1" >> "$VERSION_DETECTION_LOG"
  31. }
  32. # Detect MongoDB server version by attempting connection
  33. detect_mongodb_version() {
  34. local mongo_url="${MONGO_URL:-mongodb://127.0.0.1:27017/wekan}"
  35. local detected_version=""
  36. log_version_detection "Starting MongoDB version detection for: $mongo_url"
  37. # Try to connect with MongoDB 7 first (latest available)
  38. log_version_detection "Attempting connection with MongoDB 7 binary"
  39. 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
  40. detected_version="7"
  41. log_version_detection "Detected MongoDB 7.x server"
  42. 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
  43. # Check for wire protocol errors that indicate older version
  44. local error_output=$(timeout 10s /snap/${SNAP_NAME}/current/bin/mongosh --quiet --eval "db.runCommand({buildInfo: 1}).version" "$mongo_url" 2>&1)
  45. if echo "$error_output" | grep -q "protocol version 0\|wire protocol version 0"; then
  46. detected_version="3"
  47. log_version_detection "Detected MongoDB 3.x server (wire protocol 0)"
  48. elif echo "$error_output" | grep -q "protocol version 1\|wire protocol version 1"; then
  49. detected_version="3"
  50. log_version_detection "Detected MongoDB 3.x server (wire protocol 1)"
  51. elif echo "$error_output" | grep -q "protocol version 2\|wire protocol version 2"; then
  52. detected_version="3"
  53. log_version_detection "Detected MongoDB 3.x server (wire protocol 2)"
  54. elif echo "$error_output" | grep -q "protocol version 3\|wire protocol version 3"; then
  55. detected_version="3"
  56. log_version_detection "Detected MongoDB 3.x server (wire protocol 3)"
  57. elif echo "$error_output" | grep -q "protocol version 4\|wire protocol version 4"; then
  58. detected_version="4"
  59. log_version_detection "Detected MongoDB 4.x server (wire protocol 4)"
  60. elif echo "$error_output" | grep -q "protocol version 5\|wire protocol version 5"; then
  61. detected_version="5"
  62. log_version_detection "Detected MongoDB 5.x server (wire protocol 5)"
  63. elif echo "$error_output" | grep -q "protocol version 6\|wire protocol version 6"; then
  64. detected_version="6"
  65. log_version_detection "Detected MongoDB 6.x server (wire protocol 6)"
  66. elif echo "$error_output" | grep -q "protocol version 7\|wire protocol version 7"; then
  67. detected_version="7"
  68. log_version_detection "Detected MongoDB 7.x server (wire protocol 7)"
  69. elif echo "$error_output" | grep -q "protocol version 8\|wire protocol version 8"; then
  70. detected_version="8"
  71. log_version_detection "Detected MongoDB 8.x server (wire protocol 8)"
  72. else
  73. log_version_detection "Unknown wire protocol error: $error_output"
  74. fi
  75. else
  76. log_version_detection "No MongoDB server running or connection failed"
  77. fi
  78. echo "$detected_version"
  79. }
  80. # Switch to appropriate MongoDB binary based on detected version
  81. switch_mongodb_binary() {
  82. local version="$1"
  83. case "$version" in
  84. "3")
  85. if [ -f "/snap/${SNAP_NAME}/current/migratemongo/bin/mongod" ]; then
  86. log_version_detection "Switching to MongoDB 3.x binary"
  87. export PATH="${MONGO3_BIN}:${PATH}"
  88. export LD_LIBRARY_PATH="${MONGO3_LIB}:${MONGO3_LIB}/x86_64-linux-gnu:${LD_LIBRARY_PATH}"
  89. echo "3" > "${SNAP_COMMON}/mongodb-active-version"
  90. else
  91. log_version_detection "MongoDB 3.x binary not found, using default MongoDB 7.x"
  92. switch_mongodb_binary "7"
  93. fi
  94. ;;
  95. "4")
  96. if [ -f "/snap/${SNAP_NAME}/current/mongodb4/bin/mongod" ]; then
  97. log_version_detection "Switching to MongoDB 4.x binary"
  98. export PATH="${MONGO4_BIN}:${PATH}"
  99. echo "4" > "${SNAP_COMMON}/mongodb-active-version"
  100. else
  101. log_version_detection "MongoDB 4.x binary not found, using default MongoDB 7.x"
  102. switch_mongodb_binary "7"
  103. fi
  104. ;;
  105. "5")
  106. if [ -f "/snap/${SNAP_NAME}/current/mongodb5/bin/mongod" ]; then
  107. log_version_detection "Switching to MongoDB 5.x binary"
  108. export PATH="${MONGO5_BIN}:${PATH}"
  109. echo "5" > "${SNAP_COMMON}/mongodb-active-version"
  110. else
  111. log_version_detection "MongoDB 5.x binary not found, using default MongoDB 7.x"
  112. switch_mongodb_binary "7"
  113. fi
  114. ;;
  115. "6")
  116. if [ -f "/snap/${SNAP_NAME}/current/mongodb6/bin/mongod" ]; then
  117. log_version_detection "Switching to MongoDB 6.x binary"
  118. export PATH="${MONGO6_BIN}:${PATH}"
  119. echo "6" > "${SNAP_COMMON}/mongodb-active-version"
  120. else
  121. log_version_detection "MongoDB 6.x binary not found, using default MongoDB 7.x"
  122. switch_mongodb_binary "7"
  123. fi
  124. ;;
  125. "7"|"")
  126. log_version_detection "Using MongoDB 7.x binary (default)"
  127. export PATH="${MONGO7_BIN}:${PATH}"
  128. export LD_LIBRARY_PATH="${MONGO7_LIB}:${LD_LIBRARY_PATH}"
  129. echo "7" > "${SNAP_COMMON}/mongodb-active-version"
  130. ;;
  131. "8")
  132. if [ -f "/snap/${SNAP_NAME}/current/mongodb8/bin/mongod" ]; then
  133. log_version_detection "Switching to MongoDB 8.x binary"
  134. export PATH="${MONGO8_BIN}:${PATH}"
  135. echo "8" > "${SNAP_COMMON}/mongodb-active-version"
  136. else
  137. log_version_detection "MongoDB 8.x binary not found, using default MongoDB 7.x"
  138. switch_mongodb_binary "7"
  139. fi
  140. ;;
  141. *)
  142. log_version_detection "Unknown version $version, using default MongoDB 7.x"
  143. export PATH="${MONGO7_BIN}:${PATH}"
  144. export LD_LIBRARY_PATH="${MONGO7_LIB}:${LD_LIBRARY_PATH}"
  145. echo "7" > "${SNAP_COMMON}/mongodb-active-version"
  146. ;;
  147. esac
  148. }
  149. # Main version detection and switching logic
  150. setup_mongodb_version() {
  151. # Check if we have a cached version
  152. if [ -f "${SNAP_COMMON}/mongodb-active-version" ]; then
  153. local cached_version=$(cat "${SNAP_COMMON}/mongodb-active-version")
  154. log_version_detection "Using cached MongoDB version: $cached_version"
  155. switch_mongodb_binary "$cached_version"
  156. return
  157. fi
  158. # Detect version and switch
  159. local detected_version=$(detect_mongodb_version)
  160. if [ -n "$detected_version" ]; then
  161. switch_mongodb_binary "$detected_version"
  162. else
  163. # Default to MongoDB 7 if detection fails
  164. log_version_detection "Version detection failed, using default MongoDB 7.x"
  165. switch_mongodb_binary "7"
  166. fi
  167. }
  168. # Run version detection and setup
  169. setup_mongodb_version
  170. # make sure we have set minimum env variables for locale
  171. if [ -z "${LANG}" ]; then
  172. export LANG=en_US.UTF-8
  173. fi
  174. export LC_ALL=C
  175. # If CPU does not support AVX, use Qemu that supports AVX.
  176. # Migratemongo is at https://github.com/wekan/migratemongo
  177. # and at directory /snap/${SNAP_NAME}/current/migratemongo/avx
  178. # is bash scripts like mongod, mongosh check avx support and use Qemu if needed.
  179. export PATH=/snap/${SNAP_NAME}/current/migratemongo/avx:/snap/${SNAP_NAME}/current/usr/bin:/snap/${SNAP_NAME}/current/bin:${PATH}
  180. export LD_LIBRARY_PATH=/snap/${SNAP_NAME}/current/lib:/snap/${SNAP_NAME}/current/lib/x86_64-linux-gnu:${LD_LIBRARY_PATH}
  181. # If temporary settings log exists, delete it
  182. if [ -f ${SNAP_COMMON}/settings.log ]; then
  183. rm ${SNAP_COMMON}/settings.log
  184. fi
  185. # Set MongoDB log destination to snapcommon for log file detection
  186. export MONGO_LOG_DESTINATION="snapcommon"
  187. # Set MongoDB data directory
  188. export MONGO_DATA_DIR="${SNAP_COMMON}/wekan"
  189. # Create MongoDB data directory if it doesn't exist
  190. if [ ! -d "$MONGO_DATA_DIR" ]; then
  191. mkdir -p "$MONGO_DATA_DIR"
  192. chmod 755 "$MONGO_DATA_DIR"
  193. fi
  194. # Set MongoDB log file path
  195. export MONGO_LOG_FILE="${SNAP_COMMON}/mongodb.log"
  196. # Start MongoDB with appropriate version
  197. echo "Starting MongoDB with detected version..."
  198. log_version_detection "Starting MongoDB server"
  199. # Get the active version
  200. ACTIVE_VERSION=$(cat "${SNAP_COMMON}/mongodb-active-version" 2>/dev/null || echo "7")
  201. case "$ACTIVE_VERSION" in
  202. "3")
  203. echo "Starting MongoDB 3.x server..."
  204. log_version_detection "Starting MongoDB 3.x server"
  205. exec /snap/${SNAP_NAME}/current/migratemongo/bin/mongod \
  206. --dbpath="$MONGO_DATA_DIR" \
  207. --logpath="$MONGO_LOG_FILE" \
  208. --logappend \
  209. --bind_ip=127.0.0.1 \
  210. --port=27017 \
  211. --fork
  212. ;;
  213. "4")
  214. echo "Starting MongoDB 4.x server..."
  215. log_version_detection "Starting MongoDB 4.x server"
  216. exec /snap/${SNAP_NAME}/current/mongodb4/bin/mongod \
  217. --dbpath="$MONGO_DATA_DIR" \
  218. --logpath="$MONGO_LOG_FILE" \
  219. --logappend \
  220. --bind_ip=127.0.0.1 \
  221. --port=27017 \
  222. --fork
  223. ;;
  224. "5")
  225. echo "Starting MongoDB 5.x server..."
  226. log_version_detection "Starting MongoDB 5.x server"
  227. exec /snap/${SNAP_NAME}/current/mongodb5/bin/mongod \
  228. --dbpath="$MONGO_DATA_DIR" \
  229. --logpath="$MONGO_LOG_FILE" \
  230. --logappend \
  231. --bind_ip=127.0.0.1 \
  232. --port=27017 \
  233. --fork
  234. ;;
  235. "6")
  236. echo "Starting MongoDB 6.x server..."
  237. log_version_detection "Starting MongoDB 6.x server"
  238. exec /snap/${SNAP_NAME}/current/mongodb6/bin/mongod \
  239. --dbpath="$MONGO_DATA_DIR" \
  240. --logpath="$MONGO_LOG_FILE" \
  241. --logappend \
  242. --bind_ip=127.0.0.1 \
  243. --port=27017 \
  244. --fork
  245. ;;
  246. "7"|*)
  247. echo "Starting MongoDB 7.x server..."
  248. log_version_detection "Starting MongoDB 7.x server"
  249. exec /snap/${SNAP_NAME}/current/bin/mongod \
  250. --dbpath="$MONGO_DATA_DIR" \
  251. --logpath="$MONGO_LOG_FILE" \
  252. --logappend \
  253. --bind_ip=127.0.0.1 \
  254. --port=27017 \
  255. --fork
  256. ;;
  257. "8")
  258. echo "Starting MongoDB 8.x server..."
  259. log_version_detection "Starting MongoDB 8.x server"
  260. exec /snap/${SNAP_NAME}/current/mongodb8/bin/mongod \
  261. --dbpath="$MONGO_DATA_DIR" \
  262. --logpath="$MONGO_LOG_FILE" \
  263. --logappend \
  264. --bind_ip=127.0.0.1 \
  265. --port=27017 \
  266. --fork
  267. ;;
  268. esac