mongodb-control 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #!/bin/bash
  2. # MongoDB Control Script
  3. # IMPORTANT: Migration is only triggered when MIGRATE_MONGODB=true environment variable is set
  4. # IMPORTANT: No snap settings or channel changes are made during migration
  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. # Migration checking and handling - controlled by MIGRATE_MONGODB environment variable
  13. MIGRATION_STATUS="${SNAP_COMMON}/mongodb-migration-status.json"
  14. MIGRATION_LOG="${SNAP_COMMON}/mongodb-migration-log.txt"
  15. REVERT_FILE="${SNAP_COMMON}/revert-mongodb-migration.txt"
  16. # Check if migration is needed
  17. check_migration_needed() {
  18. if [ -f "$MIGRATION_STATUS" ]; then
  19. local status=$(jq -r '.status' "$MIGRATION_STATUS" 2>/dev/null || echo "unknown")
  20. if [ "$status" = "completed" ]; then
  21. return 1
  22. elif [ "$status" = "running" ]; then
  23. return 0
  24. fi
  25. fi
  26. # Check if we have MongoDB 3 data
  27. if [ -d "${SNAP_COMMON}/wekan" ] && [ ! -f "${SNAP_COMMON}/mongodb-version-7" ]; then
  28. return 0
  29. fi
  30. return 1
  31. }
  32. # Handle migration - only if MIGRATE_MONGODB=true
  33. handle_migration() {
  34. echo "MongoDB migration needed, starting migration process..."
  35. # Stop Wekan (meteor) process before migration
  36. echo "Stopping Wekan (meteor) process for migration..."
  37. snapctl stop --disable ${SNAP_NAME}.wekan || true
  38. snapctl stop --disable ${SNAP_NAME} || true
  39. # Wait a moment for processes to stop
  40. sleep 2
  41. # Kill any remaining meteor/node processes
  42. pkill -f "node.*main.js" || true
  43. pkill -f "meteor" || true
  44. sleep 1
  45. # Start migration web interface in background
  46. $SNAP/bin/mongodb-migration-web &
  47. local web_pid=$!
  48. echo "$web_pid" > "${SNAP_COMMON}/migration-web.pid"
  49. # Run migration
  50. if $SNAP/bin/mongodb-migrate; then
  51. echo "MongoDB migration completed successfully"
  52. # Kill migration web interface
  53. if [ -f "${SNAP_COMMON}/migration-web.pid" ]; then
  54. local web_pid=$(cat "${SNAP_COMMON}/migration-web.pid")
  55. kill "$web_pid" 2>/dev/null || true
  56. rm -f "${SNAP_COMMON}/migration-web.pid"
  57. fi
  58. # Clean up temporary Node.js server file
  59. rm -f "${SNAP_COMMON}/migration-web-server.js"
  60. echo "Migration completed. Wekan will be restarted automatically."
  61. else
  62. echo "MongoDB migration failed"
  63. # Kill migration web interface
  64. if [ -f "${SNAP_COMMON}/migration-web.pid" ]; then
  65. local web_pid=$(cat "${SNAP_COMMON}/migration-web.pid")
  66. kill "$web_pid" 2>/dev/null || true
  67. rm -f "${SNAP_COMMON}/migration-web.pid"
  68. fi
  69. # Clean up temporary Node.js server file
  70. rm -f "${SNAP_COMMON}/migration-web-server.js"
  71. exit 1
  72. fi
  73. }
  74. # Check if revert is requested
  75. if [ -f "$REVERT_FILE" ]; then
  76. echo "Revert requested, stopping Wekan and MongoDB, then reverting migration..."
  77. # Stop Wekan (meteor) process before revert
  78. echo "Stopping Wekan (meteor) process for revert..."
  79. snapctl stop --disable ${SNAP_NAME}.wekan || true
  80. snapctl stop --disable ${SNAP_NAME} || true
  81. # Wait a moment for processes to stop
  82. sleep 2
  83. # Kill any remaining meteor/node processes
  84. pkill -f "node.*main.js" || true
  85. pkill -f "meteor" || true
  86. sleep 1
  87. # Stop MongoDB
  88. snapctl stop --disable ${SNAP_NAME}.mongodb
  89. # Run migration (which will handle revert)
  90. $SNAP/bin/mongodb-migrate
  91. exit $?
  92. fi
  93. # Check if migration is needed - only if MIGRATE_MONGODB=true
  94. if [ "$MIGRATE_MONGODB" = "true" ]; then
  95. if check_migration_needed; then
  96. handle_migration
  97. else
  98. echo "MIGRATE_MONGODB=true but no migration needed"
  99. fi
  100. else
  101. echo "MIGRATE_MONGODB not set to 'true' - skipping migration check"
  102. fi
  103. # make sure we have set minimum env variables for locale
  104. if [ -z "${LANG}" ]; then
  105. export LANG=en_US.UTF-8
  106. fi
  107. export LC_ALL=C
  108. # If CPU does not support AVX, use Qemu that supports AVX.
  109. # Migratemongo is at https://github.com/wekan/migratemongo
  110. # and at directory /snap/${SNAP_NAME}/current/migratemongo/avx
  111. # is bash scripts like mongod, mongosh check avx support and use Qemu if needed.
  112. export PATH=/snap/${SNAP_NAME}/current/migratemongo/avx:/snap/${SNAP_NAME}/current/usr/bin:/snap/${SNAP_NAME}/current/bin:${PATH}
  113. export LD_LIBRARY_PATH=/snap/${SNAP_NAME}/current/lib:/snap/${SNAP_NAME}/current/lib/x86_64-linux-gnu:${LD_LIBRARY_PATH}
  114. # If temporary settings log exists, delete it
  115. if [ -f ${SNAP_COMMON}/settings.log ]; then
  116. rm ${SNAP_COMMON}/settings.log
  117. fi
  118. #if test -f "$SNAP_COMMON/01-migrate-mongo3-to-mongo5.txt"; then
  119. # touch "$SNAP_COMMON/01-migrate-mongo3-to-mongo5.txt"
  120. # # Stop MongoDB
  121. # touch "$SNAP_COMMON/02-disable-mongo.txt"
  122. # snapctl stop --disable ${SNAP_NAME}.mongodb
  123. # touch "$SNAP_COMMON/03-eval-stop-mongo.txt"
  124. # mongo wekan --eval "db.getSiblingDB('admin').shutdownServer()" $BIND_OPTIONS
  125. # # Start MongoDB 4.4
  126. # touch "$SNAP_COMMON/04-start-mongo44.txt"
  127. # $SNAP/mongo44bin/mongod --dbpath $SNAP_COMMON --logpath $SNAP_COMMON/02_mongodb_log_while_migrate.txt --logappend --journal $MONGO_URL --quiet
  128. # # Wait MongoDB 4.4 to start
  129. # touch "$SNAP_COMMON/05-wait-2s-mongo44-start.txt"
  130. # sleep 2s
  131. # # Dump Old MongoDB 3.x database
  132. # touch "$SNAP_COMMON/06-dump-database.txt"
  133. # (cd $SNAP_COMMON && mongodump --port ${MONGODB_PORT})
  134. # # Stop MongoDB 4.4
  135. # touch "$SNAP_COMMON/07-stop-mongo44.txt"
  136. # $SNAP/mongo44bin/mongo wekan --eval "db.getSiblingDB('admin').shutdownServer()" $BIND_OPTIONS
  137. # # Wait MongoDB 4.4 to stop
  138. # touch "$SNAP_COMMON/08-wait-2s-mongo44-stop.txt"
  139. # sleep 2s
  140. # # Start MongoDB 5
  141. # touch "$SNAP_COMMON/09-start-mongo5.txt"
  142. # mongod --dbpath $SNAP_COMMON --logpath $SNAP_COMMON/10_mongodb_log_while_migrate.txt --logappend --journal $MONGO_URL --quiet
  143. # # Restore database
  144. # touch "$SNAP_COMMON/11-mongorestore-to-mongo5.txt"
  145. # (cd $SNAP_COMMON && mongorestore --port ${MONGODB_PORT})
  146. # # Wait 5s
  147. # touch "$SNAP_COMMON/12-wait-5s-after-restore.txt"
  148. # sleep 5s
  149. # # Shutdown mongodb
  150. # touch "$SNAP_COMMON/13-shutdown-mongodb.txt"
  151. # mongo wekan --eval "db.getSiblingDB('admin').shutdownServer()" $BIND_OPTIONS
  152. # touch "$SNAP_COMMON/14-wait-5s-after-mongo5-shutdown.txt"
  153. # sleep 5s
  154. # # Enable MongoDB 5
  155. # touch "$SNAP_COMMON/15-enable-mongo-5.txt"
  156. # snapctl start --enable ${SNAP_NAME}.mongodb
  157. #fi
  158. # When starting MongoDB, if logfile exist, delete it, because now uses syslog instead of logfile,
  159. # because syslog usually already has log rotation.
  160. # https://github.com/wekan/wekan-snap/issues/92
  161. #if test -f "$SNAP_COMMON/mongodb.log"; then
  162. # rm -f "$SNAP_COMMON/mongodb.log"
  163. #fi
  164. # Alternative: When starting MongoDB, and using logfile, truncate log to last 1000 lines of text.
  165. # 1) If file exists:
  166. #if test -f "$SNAP_COMMON/mongodb.log"; then
  167. # # 2) Copy last 1000 lines to variable loglast1000lines.
  168. # loglast1000lines=$(tail -1000 "$SNAP_COMMON/mongodb.log")
  169. # # 3) Copy variable to replace original MongoDB log.
  170. # echo "$loglast1000lines" > "$SNAP_COMMON/mongodb.log"
  171. # # 4) Set variable to be empty.
  172. # loglast1000lines=""
  173. #fi
  174. if [ -z "${MONGO_URL}" ]; then
  175. # start mongo deamon
  176. BIND_OPTIONS=""
  177. if [ "nill" != "${MONGODB_BIND_UNIX_SOCKET}" ] && [ "x" != "x${MONGODB_BIND_UNIX_SOCKET}" ]; then
  178. BIND_OPTIONS+=" --unixSocketPrefix ${MONGODB_BIND_UNIX_SOCKET}"
  179. fi
  180. # Newest MongoDB uses --host or --bind_ip
  181. if [ "x" != "x${MONGODB_BIND_IP}" ]; then
  182. BIND_OPTIONS+=" --bind_ip $MONGODB_BIND_IP"
  183. fi
  184. if [ "x" != "x${MONGODB_PORT}" ]; then
  185. BIND_OPTIONS+=" --port ${MONGODB_PORT}"
  186. fi
  187. if [ "syslog" == "${MONGO_LOG_DESTINATION}" ]; then
  188. echo "Sending mongodb logs to syslog"
  189. mongod --dbpath ${SNAP_COMMON} --syslog ${BIND_OPTIONS} --quiet
  190. exit 0
  191. fi
  192. if [ "snapcommon" == "${MONGO_LOG_DESTINATION}" ]; then
  193. echo "Sending mongodb logs to $SNAP_COMMON"
  194. mongod --dbpath ${SNAP_COMMON} --logpath ${SNAP_COMMON}/mongodb.log --logappend ${BIND_OPTIONS} --quiet
  195. fi
  196. if [ "devnull" == "${MONGO_LOG_DESTINATION}" ]; then
  197. echo "Sending mongodb logs to /dev/null"
  198. mongod --dbpath ${SNAP_COMMON} --logpath /dev/null ${BIND_OPTIONS} --quiet
  199. fi
  200. #echo "mongodb log destination: ${MONGO_LOG_DESTINATION}" >> "${SNAP_COMMON}/settings.log"
  201. # Disable MongoDB telemetry and free monitoring
  202. /snap/${SNAP_NAME}/current/bin/mongosh wekan --eval 'disableTelemetry();' --port ${MONGODB_PORT}
  203. /snap/${SNAP_NAME}/current/bin/mongosh wekan --eval 'db.disableFreeMonitoring();' --port ${MONGODB_PORT}
  204. # Snap: Disable apparmor="DENIED" at syslog
  205. # https://github.com/wekan/wekan/issues/4855
  206. /snap/${SNAP_NAME}/current/bin/mongosh wekan --eval 'db.adminCommand({ setParameter: 1, diagnosticDataCollectionEnabled: false});' --port ${MONGODB_PORT}
  207. # Drop indexes on database upgrade, when starting MongoDB
  208. #mongosh wekan --eval "db.getCollectionNames().forEach(function(col_name) { var coll = db.getCollection(col_name); coll.dropIndexes(); });" $BIND_OPTIONS
  209. # Set MongoDB feature compatibility version
  210. #mongosh wekan --eval 'db.adminCommand({ setFeatureCompatibilityVersion: "4.4" });' ${BIND_OPTIONS}
  211. # Delete incomplete uploads so that they would not prevent starting WeKan
  212. /snap/${SNAP_NAME}/current/bin/mongosh wekan --eval 'db.getCollection("cfs.attachments.filerecord").find( { "uploadedAt": { "$exists": true }, "copies.attachments" : null,"failures.copies.attachments.doneTrying" : {"$ne" : true}});' --port ${MONGODB_PORT}
  213. else
  214. if [ "syslog" == "${MONGO_LOG_DESTINATION}" ]; then
  215. echo "Sending mongodb logs to syslog"
  216. mongod --dbpath ${SNAP_COMMON} --syslog ${MONGO_URL} --quiet
  217. fi
  218. if [ "snapcommon" == "${MONGO_LOG_DESTINATION}" ]; then
  219. echo "Sending mongodb logs to ${SNAP_COMMON}"
  220. mongod --dbpath ${SNAP_COMMON} --logpath ${SNAP_COMMON}/mongodb.log --logappend ${MONGO_URL} --quiet
  221. fi
  222. if [ "devnull" == "${MONGO_LOG_DESTINATION}" ]; then
  223. echo "Sending mongodb logs to /dev/null"
  224. mongod --dbpath ${SNAP_COMMON} --logpath /dev/null ${MONGO_URL} --quiet
  225. fi
  226. # Disable MongoDB telemetry and free monitoring
  227. /snap/${SNAP_NAME}/current/bin/mongosh ${MONGO_URL} --eval 'disableTelemetry();'
  228. /snap/${SNAP_NAME}/current/bin/mongosh ${MONGO_URL} --eval 'db.disableFreeMonitoring();'
  229. # Snap: Disable apparmor="DENIED" at syslog
  230. # https://github.com/wekan/wekan/issues/4855
  231. /snap/${SNAP_NAME}/current/bin/mongosh ${MONGO_URL} --eval 'db.adminCommand({ setParameter: 1, diagnosticDataCollectionEnabled: false});'
  232. # Drop indexes on database upgrade, when starting MongoDB
  233. #mongosh wekan --eval "db.getCollectionNames().forEach(function(col_name) { var coll = db.getCollection(col_name); coll.dropIndexes(); });" $BIND_OPTIONS
  234. # Set MongoDB feature compatibility version
  235. #/snap/${SNAP_NAME}/current/bin/mongosh ${MONGO_URL} --eval 'db.adminCommand({ setFeatureCompatibilityVersion: "4.4" });'
  236. # Delete incomplete uploads so that they would not prevent starting WeKan
  237. /snap/${SNAP_NAME}/current/bin/mongosh ${MONGO_URL} --eval 'db.getCollection("cfs.attachments.filerecord").find( { "uploadedAt": { "$exists": true }, "copies.attachments" : null,"failures.copies.attachments.doneTrying" : {"$ne" : true}});'
  238. fi