mongodb-control 7.5 KB

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