mongodb-control 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #!/bin/bash
  2. # MongoDB Control Script
  3. # Starts MongoDB 7.x server only
  4. # get wekan/mongo settings
  5. echo "Reading snap settings..."
  6. source $SNAP/bin/wekan-read-settings
  7. # Debug: Show what we got from snap settings
  8. echo "Snap settings loaded:"
  9. echo " MONGODB_BIND_IP: '${MONGODB_BIND_IP}'"
  10. echo " MONGODB_PORT: '${MONGODB_PORT}'"
  11. echo " MONGODB_BIND_UNIX_SOCKET: '${MONGODB_BIND_UNIX_SOCKET}'"
  12. # Debug: Check snap settings directly
  13. echo "Direct snap settings check:"
  14. echo " mongodb-port: $(snapctl get mongodb-port 2>/dev/null || echo 'not set')"
  15. echo " mongodb-bind-ip: $(snapctl get mongodb-bind-ip 2>/dev/null || echo 'not set')"
  16. echo " mongodb-bind-unix-socket: $(snapctl get mongodb-bind-unix-socket 2>/dev/null || echo 'not set')"
  17. if [ "true" == "${DISABLE_MONGODB}" ]; then
  18. echo "mongodb is disabled. Stop service"
  19. snapctl stop --disable ${SNAP_NAME}.mongodb
  20. exit 0
  21. fi
  22. # make sure we have set minimum env variables for locale
  23. if [ -z "${LANG}" ]; then
  24. export LANG=en_US.UTF-8
  25. fi
  26. export LC_ALL=C
  27. export PATH=/snap/${SNAP_NAME}/current/usr/bin:/snap/${SNAP_NAME}/current/bin:${PATH}
  28. export LD_LIBRARY_PATH=/snap/${SNAP_NAME}/current/lib:/snap/${SNAP_NAME}/current/lib/x86_64-linux-gnu:${LD_LIBRARY_PATH}
  29. # If temporary settings log exists, delete it
  30. if [ -f ${SNAP_COMMON}/settings.log ]; then
  31. rm ${SNAP_COMMON}/settings.log
  32. fi
  33. # Set MongoDB log destination to snapcommon for log file detection
  34. export MONGO_LOG_DESTINATION="snapcommon"
  35. # Set MongoDB data directory
  36. export MONGO_DATA_DIR="${SNAP_COMMON}"
  37. # Create MongoDB data directory if it doesn't exist
  38. if [ ! -d "$MONGO_DATA_DIR" ]; then
  39. mkdir -p "$MONGO_DATA_DIR"
  40. chmod 755 "$MONGO_DATA_DIR"
  41. fi
  42. # Handle migration from SNAP_COMMON/wekan back to SNAP_COMMON
  43. # This ensures data is preserved when switching from per-swimlane lists back to shared lists
  44. if [ ! -d "${SNAP_COMMON}/mongodb-migration-completed" ]; then
  45. echo "Checking for MongoDB data migration from SNAP_COMMON/wekan to SNAP_COMMON..."
  46. # Check if SNAP_COMMON/wekan exists and has MongoDB data
  47. if [ -d "${SNAP_COMMON}/wekan" ] && [ "$(ls -A ${SNAP_COMMON}/wekan 2>/dev/null)" ]; then
  48. echo "Found MongoDB data in SNAP_COMMON/wekan, migrating to SNAP_COMMON..."
  49. # Create backup directory for existing SNAP_COMMON data
  50. if [ "$(ls -A ${SNAP_COMMON} 2>/dev/null)" ]; then
  51. echo "Backing up existing SNAP_COMMON data to SNAP_COMMON/old-mongodb-move..."
  52. mkdir -p "${SNAP_COMMON}/old-mongodb-move"
  53. # Move all files except Caddyfile
  54. for file in "${SNAP_COMMON}"/*; do
  55. if [ -f "$file" ] && [ "$(basename "$file")" != "Caddyfile" ]; then
  56. mv "$file" "${SNAP_COMMON}/old-mongodb-move/"
  57. fi
  58. done
  59. # Move specific MongoDB directories
  60. if [ -d "${SNAP_COMMON}/journal" ]; then
  61. echo "Moving SNAP_COMMON/journal to old-mongodb-move..."
  62. mv "${SNAP_COMMON}/journal" "${SNAP_COMMON}/old-mongodb-move/"
  63. fi
  64. if [ -d "${SNAP_COMMON}/diagnostic.data" ]; then
  65. echo "Moving SNAP_COMMON/diagnostic.data to old-mongodb-move..."
  66. mv "${SNAP_COMMON}/diagnostic.data" "${SNAP_COMMON}/old-mongodb-move/"
  67. fi
  68. fi
  69. # Move MongoDB data files from SNAP_COMMON/wekan to SNAP_COMMON
  70. echo "Moving MongoDB data from SNAP_COMMON/wekan to SNAP_COMMON..."
  71. mv "${SNAP_COMMON}/wekan"/* "${SNAP_COMMON}/" 2>/dev/null || true
  72. # Rename SNAP_COMMON/wekan to SNAP_COMMON/old-wekan
  73. echo "Renaming SNAP_COMMON/wekan to SNAP_COMMON/old-wekan..."
  74. mv "${SNAP_COMMON}/wekan" "${SNAP_COMMON}/old-wekan"
  75. # Set proper permissions
  76. chmod 755 "${SNAP_COMMON}"
  77. chmod 755 "${SNAP_COMMON}/old-wekan" 2>/dev/null || true
  78. chmod 755 "${SNAP_COMMON}/old-mongodb-move" 2>/dev/null || true
  79. echo "MongoDB data migration completed successfully"
  80. else
  81. echo "No MongoDB data found in SNAP_COMMON/wekan, skipping migration"
  82. fi
  83. # Mark migration as completed
  84. touch "${SNAP_COMMON}/mongodb-migration-completed"
  85. echo "MongoDB migration marker created"
  86. fi
  87. # Set MongoDB log file path
  88. export MONGO_LOG_FILE="${SNAP_COMMON}/mongodb.log"
  89. # Build bind options from snap settings
  90. BIND_OPTIONS=""
  91. if [ "nill" != "${MONGODB_BIND_UNIX_SOCKET}" ] && [ -n "${MONGODB_BIND_UNIX_SOCKET}" ]; then
  92. BIND_OPTIONS+=" --unixSocketPrefix ${MONGODB_BIND_UNIX_SOCKET}"
  93. fi
  94. if [ -n "${MONGODB_BIND_IP}" ]; then
  95. BIND_OPTIONS+=" --bind_ip ${MONGODB_BIND_IP}"
  96. else
  97. BIND_OPTIONS+=" --bind_ip 127.0.0.1"
  98. fi
  99. if [ -n "${MONGODB_PORT}" ]; then
  100. BIND_OPTIONS+=" --port ${MONGODB_PORT}"
  101. else
  102. BIND_OPTIONS+=" --port 27019"
  103. fi
  104. # Debug: Show what settings we're using
  105. echo "MongoDB settings:"
  106. echo " MONGODB_BIND_IP: ${MONGODB_BIND_IP:-127.0.0.1}"
  107. echo " MONGODB_PORT: ${MONGODB_PORT:-27017}"
  108. echo " MONGODB_BIND_UNIX_SOCKET: ${MONGODB_BIND_UNIX_SOCKET:-not set}"
  109. echo " BIND_OPTIONS: ${BIND_OPTIONS}"
  110. # Check if MongoDB is already running
  111. check_mongodb_running() {
  112. local port="${MONGODB_PORT:-27017}"
  113. local bind_ip="${MONGODB_BIND_IP:-127.0.0.1}"
  114. # Check if MongoDB is already running on the configured port
  115. if netstat -tuln 2>/dev/null | grep -q ":${port} "; then
  116. echo "MongoDB is already running on port ${port}"
  117. return 0
  118. fi
  119. # Alternative check using lsof if netstat is not available
  120. if command -v lsof >/dev/null 2>&1; then
  121. if lsof -i ":${port}" >/dev/null 2>&1; then
  122. echo "MongoDB is already running on port ${port} (detected via lsof)"
  123. return 0
  124. fi
  125. fi
  126. # Check if there's a PID file and the process is still running
  127. if [ -f "${SNAP_COMMON}/mongodb.pid" ]; then
  128. local pid=$(cat "${SNAP_COMMON}/mongodb.pid" 2>/dev/null)
  129. if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
  130. echo "MongoDB is already running (PID: $pid)"
  131. return 0
  132. else
  133. # Remove stale PID file
  134. rm -f "${SNAP_COMMON}/mongodb.pid"
  135. fi
  136. fi
  137. return 1
  138. }
  139. # Cleanup function to remove PID file on exit
  140. cleanup() {
  141. rm -f "${SNAP_COMMON}/mongodb.pid"
  142. }
  143. trap cleanup EXIT
  144. # Check if MongoDB is already running
  145. if check_mongodb_running; then
  146. echo "MongoDB is already running. Exiting to prevent multiple instances."
  147. exit 0
  148. fi
  149. # Start MongoDB 7.x server
  150. echo "Starting MongoDB 7.x server..."
  151. # Create PID file
  152. echo $$ > "${SNAP_COMMON}/mongodb.pid"
  153. exec /snap/${SNAP_NAME}/current/bin/mongod \
  154. --dbpath="$MONGO_DATA_DIR" \
  155. --logpath="$MONGO_LOG_FILE" \
  156. --logappend $BIND_OPTIONS