mongodb-control 7.7 KB

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