mongodb-control 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. # make sure we have set minimum env variables for locale
  10. if [ -z "${LANG}" ]; then
  11. export LANG=en_US.UTF-8
  12. fi
  13. export LC_ALL=C
  14. export PATH=/snap/${SNAP_NAME}/current/usr/bin:/snap/${SNAP_NAME}/current/bin:${PATH}
  15. export LD_LIBRARY_PATH=/snap/${SNAP_NAME}/current/lib:/snap/${SNAP_NAME}/current/lib/x86_64-linux-gnu:${LD_LIBRARY_PATH}
  16. echo "DISABLE_MONGODB=${DISABLE_MONGODB}" > "${SNAP_COMMON}/settings.log"
  17. echo "LC_ALL=${LC_ALL}" >> "${SNAP_COMMON}/settings.log"
  18. echo "LANG=${LANG}" >> "${SNAP_COMMON}/settings.log"
  19. echo "PATH=${PATH}" >> "${SNAP_COMMON}/settings.log"
  20. echo "SNAP_COMMON=${SNAP_COMMON}" >> "{$SNAP_COMMON}/settings.log"
  21. echo "LD_LIBRARY_PATH=${LD_LIBRARY_PATH}" >> "${SNAP_COMMON}/settings.log"
  22. #if test -f "$SNAP_COMMON/01-migrate-mongo3-to-mongo5.txt"; then
  23. # touch "$SNAP_COMMON/01-migrate-mongo3-to-mongo5.txt"
  24. # # Stop MongoDB
  25. # touch "$SNAP_COMMON/02-disable-mongo.txt"
  26. # snapctl stop --disable ${SNAP_NAME}.mongodb
  27. # touch "$SNAP_COMMON/03-eval-stop-mongo.txt"
  28. # mongo wekan --eval "db.getSiblingDB('admin').shutdownServer()" $BIND_OPTIONS
  29. # # Start MongoDB 4.4
  30. # touch "$SNAP_COMMON/04-start-mongo44.txt"
  31. # $SNAP/mongo44bin/mongod --dbpath $SNAP_COMMON --logpath $SNAP_COMMON/02_mongodb_log_while_migrate.txt --logappend --journal $MONGO_URL --quiet
  32. # # Wait MongoDB 4.4 to start
  33. # touch "$SNAP_COMMON/05-wait-2s-mongo44-start.txt"
  34. # sleep 2s
  35. # # Dump Old MongoDB 3.x database
  36. # touch "$SNAP_COMMON/06-dump-database.txt"
  37. # (cd $SNAP_COMMON && mongodump --port ${MONGODB_PORT})
  38. # # Stop MongoDB 4.4
  39. # touch "$SNAP_COMMON/07-stop-mongo44.txt"
  40. # $SNAP/mongo44bin/mongo wekan --eval "db.getSiblingDB('admin').shutdownServer()" $BIND_OPTIONS
  41. # # Wait MongoDB 4.4 to stop
  42. # touch "$SNAP_COMMON/08-wait-2s-mongo44-stop.txt"
  43. # sleep 2s
  44. # # Start MongoDB 5
  45. # touch "$SNAP_COMMON/09-start-mongo5.txt"
  46. # mongod --dbpath $SNAP_COMMON --logpath $SNAP_COMMON/10_mongodb_log_while_migrate.txt --logappend --journal $MONGO_URL --quiet
  47. # # Restore database
  48. # touch "$SNAP_COMMON/11-mongorestore-to-mongo5.txt"
  49. # (cd $SNAP_COMMON && mongorestore --port ${MONGODB_PORT})
  50. # # Wait 5s
  51. # touch "$SNAP_COMMON/12-wait-5s-after-restore.txt"
  52. # sleep 5s
  53. # # Shutdown mongodb
  54. # touch "$SNAP_COMMON/13-shutdown-mongodb.txt"
  55. # mongo wekan --eval "db.getSiblingDB('admin').shutdownServer()" $BIND_OPTIONS
  56. # touch "$SNAP_COMMON/14-wait-5s-after-mongo5-shutdown.txt"
  57. # sleep 5s
  58. # # Enable MongoDB 5
  59. # touch "$SNAP_COMMON/15-enable-mongo-5.txt"
  60. # snapctl start --enable ${SNAP_NAME}.mongodb
  61. #fi
  62. # When starting MongoDB, if logfile exist, delete it, because now uses syslog instead of logfile,
  63. # because syslog usually already has log rotation.
  64. # https://github.com/wekan/wekan-snap/issues/92
  65. #if test -f "$SNAP_COMMON/mongodb.log"; then
  66. # rm -f "$SNAP_COMMON/mongodb.log"
  67. #fi
  68. # Alternative: When starting MongoDB, and using logfile, truncate log to last 1000 lines of text.
  69. # 1) If file exists:
  70. #if test -f "$SNAP_COMMON/mongodb.log"; then
  71. # # 2) Copy last 1000 lines to variable loglast1000lines.
  72. # loglast1000lines=$(tail -1000 "$SNAP_COMMON/mongodb.log")
  73. # # 3) Copy variable to replace original MongoDB log.
  74. # echo "$loglast1000lines" > "$SNAP_COMMON/mongodb.log"
  75. # # 4) Set variable to be empty.
  76. # loglast1000lines=""
  77. #fi
  78. echo "MONGO_URL=${MONGO_URL}" >> "${SNAP_COMMON}/settings.log"
  79. if [ -z "${MONGO_URL}" ]; then
  80. # start mongo deamon
  81. BIND_OPTIONS=""
  82. if [ "nill" != "${MONGODB_BIND_UNIX_SOCKET}" ] && [ "x" != "x${MONGODB_BIND_UNIX_SOCKET}" ]; then
  83. BIND_OPTIONS+=" --unixSocketPrefix ${MONGODB_BIND_UNIX_SOCKET}"
  84. echo "BIND_OPTIONS+= --unixSocketPrefix ${MONGODB_BIND_UNIX_SOCKET}" >> "${SNAP_COMMON}/settings.log"
  85. fi
  86. if [ "x" != "x${MONGODB_BIND_IP}" ]; then
  87. BIND_OPTIONS+=" --bind_ip $MONGODB_BIND_IP"
  88. echo "BIND_OPTIONS+= --bind_ip ${MONGODB_BIND_IP}" >> "${SNAP_COMMON}/settings.log"
  89. fi
  90. if [ "x" != "x${MONGODB_PORT}" ]; then
  91. BIND_OPTIONS+=" --port ${MONGODB_PORT}"
  92. echo "BIND_OPTIONS+= --port ${MONGODB_PORT}" >> "${SNAP_COMMON}/settings.log"
  93. fi
  94. echo "mongodb bind options: ${BIND_OPTIONS}"
  95. echo "mongodb bind options: ${BIND_OPTIONS}" >> "${SNAP_COMMON}/settings.log"
  96. if [ "syslog" == "${MONGO_LOG_DESTINATION}" ]; then
  97. echo "Sending mongodb logs to syslog"
  98. mongod --dbpath $SNAP_COMMON --syslog --journal $BIND_OPTIONS --quiet
  99. exit 0
  100. fi
  101. echo mongodb log destination: ${MONGO_LOG_DESTINATION} >> "${SNAP_COMMON}/settings.log"
  102. if [ "snapcommon" == "${MONGO_LOG_DESTINATION}" ]; then
  103. echo "Sending mongodb logs to $SNAP_COMMON"
  104. mongod --dbpath $SNAP_COMMON --logpath $SNAP_COMMON/mongodb.log --logappend --journal $BIND_OPTIONS --quiet
  105. fi
  106. echo mongodb log destination: ${MONGO_LOG_DESTINATION} >> "${SNAP_COMMON}/settings.log"
  107. if [ "devnull" == "${MONGO_LOG_DESTINATION}" ]; then
  108. echo "Sending mongodb logs to /dev/null"
  109. mongod --dbpath $SNAP_COMMON --logpath /dev/null --journal $BIND_OPTIONS --quiet
  110. fi
  111. echo mongodb log destination: ${MONGO_LOG_DESTINATION} >> "${SNAP_COMMON}/settings.log"
  112. else
  113. if [ "syslog" == "${MONGO_LOG_DESTINATION}" ]; then
  114. echo "Sending mongodb logs to syslog"
  115. mongod --dbpath ${SNAP_COMMON} --syslog --journal ${MONGO_URL} --quiet
  116. fi
  117. if [ "snapcommon" == "${MONGO_LOG_DESTINATION}" ]; then
  118. echo "Sending mongodb logs to ${SNAP_COMMON}"
  119. mongod --dbpath ${SNAP_COMMON} --logpath ${SNAP_COMMON}/mongodb.log --logappend --journal ${MONGO_URL} --quiet
  120. fi
  121. if [ "devnull" == "${MONGO_LOG_DESTINATION}" ]; then
  122. echo "Sending mongodb logs to /dev/null"
  123. mongod --dbpath ${SNAP_COMMON} --logpath /dev/null --journal ${MONGO_URL} --quiet
  124. fi
  125. BIND_OPTIONS=${MONGO_URL}
  126. fi
  127. # Disable MongoDB telemetry and free monitoring
  128. mongosh wekan --eval 'disableTelemetry();' ${BIND_OPTIONS}
  129. echo "mongosh wekan --eval 'disableTelemetry();' ${BIND_OPTIONS}" >> "${SNAP_COMMON}/settings.log"
  130. mongosh wekan --eval 'db.disableFreeMonitoring();' ${BIND_OPTIONS}
  131. echo mongosh wekan --eval 'db.disableFreeMonitoring();' ${BIND_OPTIONS} >> "${SNAP_COMMON}/settings.log"
  132. # Snap: Disable apparmor="DENIED" at syslog
  133. # https://github.com/wekan/wekan/issues/4855
  134. mongosh wekan --eval 'db.adminCommand({ setParameter: 1, diagnosticDataCollectionEnabled: false});' ${BIND_OPTIONS}
  135. echo mongosh wekan --eval 'db.adminCommand({ setParameter: 1, diagnosticDataCollectionEnabled: false});' ${BIND_OPTIONS} >> "${SNAP_COMMON}/settings.log"
  136. # Drop indexes on database upgrade, when starting MongoDB
  137. #mongosh wekan --eval "db.getCollectionNames().forEach(function(col_name) { var coll = db.getCollection(col_name); coll.dropIndexes(); });" $BIND_OPTIONS
  138. mongosh wekan --eval 'db.adminCommand({ setFeatureCompatibilityVersion: "4.4" });' ${BIND_OPTIONS}
  139. echo mongosh wekan --eval 'db.adminCommand({ setFeatureCompatibilityVersion: "4.4" });' ${BIND_OPTIONS} >> "${SNAP_COMMON}/settings.log"
  140. # Delete incomplete uploads so that they would not prevent starting WeKan
  141. mongosh wekan --eval 'db.getCollection("cfs.attachments.filerecord").find( { "uploadedAt": { "$exists": true }, "copies.attachments" : null,"failures.copies.attachments.doneTrying" : {"$ne" : true}});' ${BIND_OPTIONS}
  142. echo mongosh wekan --eval 'db.getCollection("cfs.attachments.filerecord").find( { "uploadedAt": { "$exists": true }, "copies.attachments" : null,"failures.copies.attachments.doneTrying" : {"$ne" : true}});' ${BIND_OPTIONS} >> "${SNAP_COMMON}/settings.log"