mongodb-control 13 KB

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