mongodb-control 6.8 KB

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