#!/bin/bash # MongoDB Control Script # Starts MongoDB 7.x server only # get wekan/mongo settings echo "Reading snap settings..." source $SNAP/bin/wekan-read-settings # Debug: Show what we got from snap settings echo "Snap settings loaded:" echo " MONGODB_BIND_IP: '${MONGODB_BIND_IP}'" echo " MONGODB_PORT: '${MONGODB_PORT}'" echo " MONGODB_BIND_UNIX_SOCKET: '${MONGODB_BIND_UNIX_SOCKET}'" # Debug: Check snap settings directly echo "Direct snap settings check:" echo " mongodb-port: $(snapctl get mongodb-port 2>/dev/null || echo 'not set')" echo " mongodb-bind-ip: $(snapctl get mongodb-bind-ip 2>/dev/null || echo 'not set')" echo " mongodb-bind-unix-socket: $(snapctl get mongodb-bind-unix-socket 2>/dev/null || echo 'not set')" if [ "true" == "${DISABLE_MONGODB}" ]; then echo "mongodb is disabled. Stop service" snapctl stop --disable ${SNAP_NAME}.mongodb exit 0 fi # make sure we have set minimum env variables for locale if [ -z "${LANG}" ]; then export LANG=en_US.UTF-8 fi export LC_ALL=C export PATH=/snap/${SNAP_NAME}/current/usr/bin:/snap/${SNAP_NAME}/current/bin:${PATH} export LD_LIBRARY_PATH=/snap/${SNAP_NAME}/current/lib:/snap/${SNAP_NAME}/current/lib/x86_64-linux-gnu:${LD_LIBRARY_PATH} # If temporary settings log exists, delete it if [ -f ${SNAP_COMMON}/settings.log ]; then rm ${SNAP_COMMON}/settings.log fi # Set MongoDB log destination to snapcommon for log file detection export MONGO_LOG_DESTINATION="snapcommon" # Set MongoDB data directory export MONGO_DATA_DIR="${SNAP_COMMON}" # Create MongoDB data directory if it doesn't exist if [ ! -d "$MONGO_DATA_DIR" ]; then mkdir -p "$MONGO_DATA_DIR" chmod 755 "$MONGO_DATA_DIR" fi # Handle migration from SNAP_COMMON/wekan back to SNAP_COMMON # This ensures data is preserved when switching from per-swimlane lists back to shared lists if [ ! -d "${SNAP_COMMON}/mongodb-migration-completed" ]; then echo "Checking for MongoDB data migration from SNAP_COMMON/wekan to SNAP_COMMON..." # Check if SNAP_COMMON/wekan exists and has MongoDB data if [ -d "${SNAP_COMMON}/wekan" ] && [ "$(ls -A ${SNAP_COMMON}/wekan 2>/dev/null)" ]; then echo "Found MongoDB data in SNAP_COMMON/wekan, migrating to SNAP_COMMON..." # Create backup directory for existing SNAP_COMMON data if [ "$(ls -A ${SNAP_COMMON} 2>/dev/null)" ]; then echo "Backing up existing SNAP_COMMON data to SNAP_COMMON/old-mongodb-move..." mkdir -p "${SNAP_COMMON}/old-mongodb-move" # Move all files except Caddyfile for file in "${SNAP_COMMON}"/*; do if [ -f "$file" ] && [ "$(basename "$file")" != "Caddyfile" ]; then mv "$file" "${SNAP_COMMON}/old-mongodb-move/" fi done # Move specific MongoDB directories if [ -d "${SNAP_COMMON}/journal" ]; then echo "Moving SNAP_COMMON/journal to old-mongodb-move..." mv "${SNAP_COMMON}/journal" "${SNAP_COMMON}/old-mongodb-move/" fi if [ -d "${SNAP_COMMON}/diagnostic.data" ]; then echo "Moving SNAP_COMMON/diagnostic.data to old-mongodb-move..." mv "${SNAP_COMMON}/diagnostic.data" "${SNAP_COMMON}/old-mongodb-move/" fi fi # Move MongoDB data files from SNAP_COMMON/wekan to SNAP_COMMON echo "Moving MongoDB data from SNAP_COMMON/wekan to SNAP_COMMON..." mv "${SNAP_COMMON}/wekan"/* "${SNAP_COMMON}/" 2>/dev/null || true # Rename SNAP_COMMON/wekan to SNAP_COMMON/old-wekan echo "Renaming SNAP_COMMON/wekan to SNAP_COMMON/old-wekan..." mv "${SNAP_COMMON}/wekan" "${SNAP_COMMON}/old-wekan" # Set proper permissions chmod 755 "${SNAP_COMMON}" chmod 755 "${SNAP_COMMON}/old-wekan" 2>/dev/null || true chmod 755 "${SNAP_COMMON}/old-mongodb-move" 2>/dev/null || true echo "MongoDB data migration completed successfully" else echo "No MongoDB data found in SNAP_COMMON/wekan, skipping migration" fi # Mark migration as completed touch "${SNAP_COMMON}/mongodb-migration-completed" echo "MongoDB migration marker created" fi # Set MongoDB log file path export MONGO_LOG_FILE="${SNAP_COMMON}/mongodb.log" # Build bind options from snap settings BIND_OPTIONS="" if [ "nill" != "${MONGODB_BIND_UNIX_SOCKET}" ] && [ -n "${MONGODB_BIND_UNIX_SOCKET}" ]; then BIND_OPTIONS+=" --unixSocketPrefix ${MONGODB_BIND_UNIX_SOCKET}" fi if [ -n "${MONGODB_BIND_IP}" ]; then BIND_OPTIONS+=" --bind_ip ${MONGODB_BIND_IP}" else BIND_OPTIONS+=" --bind_ip 127.0.0.1" fi if [ -n "${MONGODB_PORT}" ]; then BIND_OPTIONS+=" --port ${MONGODB_PORT}" else BIND_OPTIONS+=" --port 27019" fi # Debug: Show what settings we're using echo "MongoDB settings:" echo " MONGODB_BIND_IP: ${MONGODB_BIND_IP:-127.0.0.1}" echo " MONGODB_PORT: ${MONGODB_PORT:-27017}" echo " MONGODB_BIND_UNIX_SOCKET: ${MONGODB_BIND_UNIX_SOCKET:-not set}" echo " BIND_OPTIONS: ${BIND_OPTIONS}" # Check if MongoDB is already running check_mongodb_running() { local port="${MONGODB_PORT:-27017}" local bind_ip="${MONGODB_BIND_IP:-127.0.0.1}" # Check if MongoDB is already running on the configured port if netstat -tuln 2>/dev/null | grep -q ":${port} "; then echo "MongoDB is already running on port ${port}" return 0 fi # Alternative check using lsof if netstat is not available if command -v lsof >/dev/null 2>&1; then if lsof -i ":${port}" >/dev/null 2>&1; then echo "MongoDB is already running on port ${port} (detected via lsof)" return 0 fi fi # Check if there's a PID file and the process is still running if [ -f "${SNAP_COMMON}/mongodb.pid" ]; then local pid=$(cat "${SNAP_COMMON}/mongodb.pid" 2>/dev/null) if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then echo "MongoDB is already running (PID: $pid)" return 0 else # Remove stale PID file rm -f "${SNAP_COMMON}/mongodb.pid" fi fi return 1 } # Cleanup function to remove PID file on exit cleanup() { rm -f "${SNAP_COMMON}/mongodb.pid" } trap cleanup EXIT # Check if MongoDB is already running if check_mongodb_running; then echo "MongoDB is already running. Exiting to prevent multiple instances." exit 0 fi # Start MongoDB 7.x server echo "Starting MongoDB 7.x server..." # Create PID file echo $$ > "${SNAP_COMMON}/mongodb.pid" exec /snap/${SNAP_NAME}/current/bin/mongod \ --dbpath="$MONGO_DATA_DIR" \ --logpath="$MONGO_LOG_FILE" \ --logappend $BIND_OPTIONS