mongodb-control 9.8 KB

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