mongodb-control-old 11 KB

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