mongodb-control 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/bin/bash
  2. # MongoDB Control Script
  3. # Starts MongoDB 7.x server only
  4. # get wekan/mongo settings
  5. echo "Reading snap settings..."
  6. source $SNAP/bin/wekan-read-settings
  7. # Debug: Show what we got from snap settings
  8. echo "Snap settings loaded:"
  9. echo " MONGODB_BIND_IP: '${MONGODB_BIND_IP}'"
  10. echo " MONGODB_PORT: '${MONGODB_PORT}'"
  11. echo " MONGODB_BIND_UNIX_SOCKET: '${MONGODB_BIND_UNIX_SOCKET}'"
  12. # Debug: Check snap settings directly
  13. echo "Direct snap settings check:"
  14. echo " mongodb-port: $(snapctl get mongodb-port 2>/dev/null || echo 'not set')"
  15. echo " mongodb-bind-ip: $(snapctl get mongodb-bind-ip 2>/dev/null || echo 'not set')"
  16. echo " mongodb-bind-unix-socket: $(snapctl get mongodb-bind-unix-socket 2>/dev/null || echo 'not set')"
  17. if [ "true" == "${DISABLE_MONGODB}" ]; then
  18. echo "mongodb is disabled. Stop service"
  19. snapctl stop --disable ${SNAP_NAME}.mongodb
  20. exit 0
  21. fi
  22. # make sure we have set minimum env variables for locale
  23. if [ -z "${LANG}" ]; then
  24. export LANG=en_US.UTF-8
  25. fi
  26. export LC_ALL=C
  27. export PATH=/snap/${SNAP_NAME}/current/usr/bin:/snap/${SNAP_NAME}/current/bin:${PATH}
  28. export LD_LIBRARY_PATH=/snap/${SNAP_NAME}/current/lib:/snap/${SNAP_NAME}/current/lib/x86_64-linux-gnu:${LD_LIBRARY_PATH}
  29. # If temporary settings log exists, delete it
  30. if [ -f ${SNAP_COMMON}/settings.log ]; then
  31. rm ${SNAP_COMMON}/settings.log
  32. fi
  33. # Set MongoDB log destination to snapcommon for log file detection
  34. export MONGO_LOG_DESTINATION="snapcommon"
  35. # Set MongoDB data directory
  36. export MONGO_DATA_DIR="${SNAP_COMMON}"
  37. # Create MongoDB data directory if it doesn't exist
  38. if [ ! -d "$MONGO_DATA_DIR" ]; then
  39. mkdir -p "$MONGO_DATA_DIR"
  40. chmod 755 "$MONGO_DATA_DIR"
  41. fi
  42. # Set MongoDB log file path
  43. export MONGO_LOG_FILE="${SNAP_COMMON}/mongodb.log"
  44. # Build bind options from snap settings
  45. BIND_OPTIONS=""
  46. if [ "nill" != "${MONGODB_BIND_UNIX_SOCKET}" ] && [ -n "${MONGODB_BIND_UNIX_SOCKET}" ]; then
  47. BIND_OPTIONS+=" --unixSocketPrefix ${MONGODB_BIND_UNIX_SOCKET}"
  48. fi
  49. if [ -n "${MONGODB_BIND_IP}" ]; then
  50. BIND_OPTIONS+=" --bind_ip ${MONGODB_BIND_IP}"
  51. else
  52. BIND_OPTIONS+=" --bind_ip 127.0.0.1"
  53. fi
  54. if [ -n "${MONGODB_PORT}" ]; then
  55. BIND_OPTIONS+=" --port ${MONGODB_PORT}"
  56. else
  57. BIND_OPTIONS+=" --port 27019"
  58. fi
  59. # Debug: Show what settings we're using
  60. echo "MongoDB settings:"
  61. echo " MONGODB_BIND_IP: ${MONGODB_BIND_IP:-127.0.0.1}"
  62. echo " MONGODB_PORT: ${MONGODB_PORT:-27017}"
  63. echo " MONGODB_BIND_UNIX_SOCKET: ${MONGODB_BIND_UNIX_SOCKET:-not set}"
  64. echo " BIND_OPTIONS: ${BIND_OPTIONS}"
  65. # Check if MongoDB is already running
  66. check_mongodb_running() {
  67. local port="${MONGODB_PORT:-27017}"
  68. local bind_ip="${MONGODB_BIND_IP:-127.0.0.1}"
  69. # Check if MongoDB is already running on the configured port
  70. if netstat -tuln 2>/dev/null | grep -q ":${port} "; then
  71. echo "MongoDB is already running on port ${port}"
  72. return 0
  73. fi
  74. # Alternative check using lsof if netstat is not available
  75. if command -v lsof >/dev/null 2>&1; then
  76. if lsof -i ":${port}" >/dev/null 2>&1; then
  77. echo "MongoDB is already running on port ${port} (detected via lsof)"
  78. return 0
  79. fi
  80. fi
  81. # Check if there's a PID file and the process is still running
  82. if [ -f "${SNAP_COMMON}/mongodb.pid" ]; then
  83. local pid=$(cat "${SNAP_COMMON}/mongodb.pid" 2>/dev/null)
  84. if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
  85. echo "MongoDB is already running (PID: $pid)"
  86. return 0
  87. else
  88. # Remove stale PID file
  89. rm -f "${SNAP_COMMON}/mongodb.pid"
  90. fi
  91. fi
  92. return 1
  93. }
  94. # Cleanup function to remove PID file on exit
  95. cleanup() {
  96. rm -f "${SNAP_COMMON}/mongodb.pid"
  97. }
  98. trap cleanup EXIT
  99. # Check if MongoDB is already running
  100. if check_mongodb_running; then
  101. echo "MongoDB is already running. Exiting to prevent multiple instances."
  102. exit 0
  103. fi
  104. # Start MongoDB 7.x server
  105. echo "Starting MongoDB 7.x server..."
  106. # Create PID file
  107. echo $$ > "${SNAP_COMMON}/mongodb.pid"
  108. exec /snap/${SNAP_NAME}/current/bin/mongod \
  109. --dbpath="$MONGO_DATA_DIR" \
  110. --logpath="$MONGO_LOG_FILE" \
  111. --logappend $BIND_OPTIONS