79 lines
3.1 KiB
Bash
79 lines
3.1 KiB
Bash
#!/usr/bin/env bash
|
|
# ADPanel privileged job: fully disable and remove MongoDB.
|
|
# Invoked via adpanel-priv: adpanel-priv job mongodb-disable [listen-port]
|
|
#
|
|
# Failure policy: every critical step (service stop, package purge, config and
|
|
# data removal) emits an ADP_MONGO_DISABLE_FAIL marker on stderr and sets
|
|
# FAILED=1. The script NEVER exits early — later cleanup still runs — and it
|
|
# stays idempotent: already-clean states (stopped service, purged packages,
|
|
# absent files) count as success, so a re-run after a partial failure attempts
|
|
# every step again. The runner exits non-zero when FAILED=1 so the caller can
|
|
# refuse to wipe the panel's MongoDB bookkeeping while cleanup is incomplete.
|
|
set -u
|
|
export DEBIAN_FRONTEND=noninteractive
|
|
|
|
FAILED=0
|
|
fail() { echo "ADP_MONGO_DISABLE_FAIL: $*" >&2; FAILED=1; }
|
|
|
|
# --- stop the service (critical: a live mongod must not survive "disabled") ---
|
|
if ! systemctl stop mongod 2>/dev/null; then
|
|
# Tolerate an already-stopped or already-removed unit (idempotent re-run);
|
|
# only a unit that is still active after the stop attempt is a failure.
|
|
if systemctl is-active mongod >/dev/null 2>&1; then
|
|
fail "could not stop the mongod service"
|
|
fi
|
|
fi
|
|
systemctl disable mongod 2>/dev/null
|
|
if systemctl is-enabled mongod >/dev/null 2>&1; then
|
|
fail "could not disable the mongod service (boot start still enabled)"
|
|
fi
|
|
pkill -9 -x mongod 2>/dev/null
|
|
sleep 1
|
|
if pgrep -x mongod >/dev/null 2>&1; then
|
|
fail "a mongod process is still running"
|
|
fi
|
|
|
|
# --- purge packages (critical; only purge what is actually installed so
|
|
# re-runs after a successful purge stay green) ---
|
|
PURGE_PKGS=""
|
|
for pkg in mongodb-org mongodb-org-server mongodb-org-mongos mongodb-org-shell mongodb-org-tools mongodb-mongosh; do
|
|
if dpkg-query -W -f='${Status}' "$pkg" 2>/dev/null | grep -q "installed"; then
|
|
PURGE_PKGS="$PURGE_PKGS $pkg"
|
|
fi
|
|
done
|
|
if [ -n "$PURGE_PKGS" ]; then
|
|
apt-get purge -y $PURGE_PKGS 2>/dev/null || fail "apt-get purge failed for:$PURGE_PKGS"
|
|
fi
|
|
apt-get autoremove -y 2>/dev/null || true
|
|
|
|
# --- remove data, logs and config (critical) ---
|
|
rm -rf /var/lib/mongodb /var/log/mongodb 2>/dev/null
|
|
if [ -e /var/lib/mongodb ] || [ -e /var/log/mongodb ]; then
|
|
fail "MongoDB data/log directories could not be removed"
|
|
fi
|
|
rm -f /etc/mongod.conf 2>/dev/null
|
|
if [ -e /etc/mongod.conf ]; then
|
|
fail "/etc/mongod.conf could not be removed"
|
|
fi
|
|
|
|
# Best-effort leftovers only: stale sockets, apt sources, keyrings, indexes.
|
|
rm -f /tmp/mongodb-*.sock 2>/dev/null || true
|
|
rm -f /etc/apt/sources.list.d/mongodb*.list 2>/dev/null || true
|
|
rm -f /usr/share/keyrings/mongodb*.gpg 2>/dev/null || true
|
|
apt-get update 2>/dev/null || true
|
|
|
|
systemctl daemon-reload 2>/dev/null || true
|
|
|
|
# Optional: remove the firewall rule for the former MongoDB port.
|
|
PORT="${1:-}"
|
|
if [ -n "$PORT" ] && [ "$PORT" = "${PORT//[!0-9]/}" ] && [ "$PORT" -ge 1 ] && [ "$PORT" -le 65535 ]; then
|
|
ufw delete allow "${PORT}/tcp" 2>/dev/null || true
|
|
iptables -D INPUT -p tcp --dport "$PORT" -j ACCEPT 2>/dev/null || true
|
|
fi
|
|
|
|
if [ "$FAILED" -ne 0 ]; then
|
|
echo "ADP_MONGO_DISABLE_FAIL: one or more critical cleanup steps failed" >&2
|
|
exit 1
|
|
fi
|
|
exit 0
|