Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
267 changes: 267 additions & 0 deletions install/ubuntu24/uninstall.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
#!/usr/bin/env bash
set -euo pipefail

echo "---------------------------------------------------------"
echo "[UNINSTALL] Starting NetAlertX uninstallation for Ubuntu"
echo "---------------------------------------------------------"
echo

if [[ $EUID -ne 0 ]]; then
echo "[UNINSTALL] This script must be run as root. Please use 'sudo'."
exit 1
fi

SYSTEMD_UNIT_FILE="/etc/systemd/system/netalertx.service"
SYSTEMD_UNIT_DEFAULTS="/etc/default/netalertx"

DEFAULT_INSTALL_DIR="/app"
DEFAULT_INSTALL_SYSTEM_NAME="ubuntu24"
DEFAULT_WEB_UI_DIR="/var/www/html/netalertx"
DEFAULT_NGINX_CONF_FILE="netalertx.conf"
DEFAULT_VENV_DIR="/opt/netalertx-python"
DEFAULT_PHPVERSION="8.3"

if [ -f "$SYSTEMD_UNIT_DEFAULTS" ]; then
# shellcheck disable=SC1090
source "$SYSTEMD_UNIT_DEFAULTS"
fi

INSTALL_DIR="${INSTALL_DIR:-$DEFAULT_INSTALL_DIR}"
INSTALL_SYSTEM_NAME="${INSTALL_SYSTEM_NAME:-$DEFAULT_INSTALL_SYSTEM_NAME}"
INSTALLER_DIR="${INSTALLER_DIR:-${INSTALL_DIR}/install/${INSTALL_SYSTEM_NAME}}"
WEB_UI_DIR="${WEB_UI_DIR:-$DEFAULT_WEB_UI_DIR}"
NGINX_CONF_FILE="${NGINX_CONF_FILE:-$DEFAULT_NGINX_CONF_FILE}"
NGINX_CONFIG_FILE="/etc/nginx/conf.d/${NGINX_CONF_FILE}"
VENV_DIR="${VIRTUAL_ENV:-$DEFAULT_VENV_DIR}"
PHPVERSION="${PHPVERSION:-$DEFAULT_PHPVERSION}"
Comment on lines +35 to +36
Copy link
Contributor

@coderabbitai coderabbitai bot Oct 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Respect custom VENV_DIR from /etc/default
After sourcing /etc/default/netalertx, we expect any configured VENV_DIR to be honored, but the reassignment uses VIRTUAL_ENV instead, overriding the sourced value with the default path. On installs that changed the virtualenv location, the uninstall will miss the real venv and try to delete /opt/netalertx-python instead. Please preserve a pre-set VENV_DIR while still falling back to VIRTUAL_ENV and the default.

Apply this diff:

-  VENV_DIR="${VIRTUAL_ENV:-$DEFAULT_VENV_DIR}"
+  VENV_DIR="${VENV_DIR:-${VIRTUAL_ENV:-$DEFAULT_VENV_DIR}}"
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
VENV_DIR="${VIRTUAL_ENV:-$DEFAULT_VENV_DIR}"
PHPVERSION="${PHPVERSION:-$DEFAULT_PHPVERSION}"
VENV_DIR="${VENV_DIR:-${VIRTUAL_ENV:-$DEFAULT_VENV_DIR}}"
PHPVERSION="${PHPVERSION:-$DEFAULT_PHPVERSION}"
🤖 Prompt for AI Agents
In install/ubuntu24/uninstall.sh around lines 35 to 36, the script currently
overwrites any VENV_DIR set by /etc/default/netalertx by using VIRTUAL_ENV
first; change the assignment so it preserves an already-set VENV_DIR, falling
back to VIRTUAL_ENV then to DEFAULT_VENV_DIR. Update the logic to: if VENV_DIR
is non-empty use it, else use VIRTUAL_ENV if set, otherwise use
DEFAULT_VENV_DIR; implement this via a single shell assignment that prefers an
existing VENV_DIR, then VIRTUAL_ENV, then the default.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks a lot for the PR @gonzague - can you check these 2 comments (nginx restore and VENV_DIR) to see if valid concerns?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!


echo "[UNINSTALL] Target install dir : ${INSTALL_DIR}"
echo "[UNINSTALL] Web UI location : ${WEB_UI_DIR}"
echo "[UNINSTALL] Nginx config file : ${NGINX_CONFIG_FILE}"
echo "[UNINSTALL] Python venv : ${VENV_DIR}"
echo

PURGE_DATA=false
PURGE_PACKAGES=false

for arg in "$@"; do
case "$arg" in
--purge-data)
PURGE_DATA=true
;;
--purge-packages)
PURGE_PACKAGES=true
;;
--help)
cat <<'USAGE'
Usage: uninstall.sh [--purge-data] [--purge-packages]
--purge-data Remove the application directory without prompting.
--purge-packages Purge packages that were installed by install.sh without prompting.
USAGE
exit 0
;;
*)
echo "[UNINSTALL] Unknown option: $arg" >&2
echo "[UNINSTALL] Use --help to see available options." >&2
exit 1
;;
esac
done

echo "---------------------------------------------------------"
echo "[UNINSTALL] Stopping services"
echo "---------------------------------------------------------"
systemctl stop netalertx 2>/dev/null || true
systemctl disable netalertx 2>/dev/null || true
systemctl reset-failed netalertx 2>/dev/null || true

echo
echo "---------------------------------------------------------"
echo "[UNINSTALL] Removing systemd unit and defaults"
echo "---------------------------------------------------------"
rm -f "$SYSTEMD_UNIT_FILE"
rm -f "$SYSTEMD_UNIT_DEFAULTS"
systemctl daemon-reload
echo "[UNINSTALL] Systemd cleanup complete"

echo
echo "---------------------------------------------------------"
echo "[UNINSTALL] Unmounting tmpfs mounts"
echo "---------------------------------------------------------"
if command -v mountpoint >/dev/null 2>&1; then
for mount_point in "${INSTALL_DIR}/log" "${INSTALL_DIR}/api"; do
if [ -d "$mount_point" ] && mountpoint -q "$mount_point"; then
umount "$mount_point" || true
echo "[UNINSTALL] Unmounted ${mount_point}"
fi
done
else
echo "[UNINSTALL] mountpoint utility not available; skipping tmpfs checks."
fi

echo
echo "---------------------------------------------------------"
echo "[UNINSTALL] Cleaning nginx and web artifacts"
echo "---------------------------------------------------------"
NEED_NGINX_RELOAD=false

if [ -e "$WEB_UI_DIR" ]; then
TARGET="$(readlink -f "$WEB_UI_DIR" 2>/dev/null || echo "$WEB_UI_DIR")"
if [ "$TARGET" = "${INSTALL_DIR}/front" ]; then
rm -rf "$WEB_UI_DIR"
echo "[UNINSTALL] Removed web UI link ${WEB_UI_DIR}"
fi
fi

if [ -e "$NGINX_CONFIG_FILE" ]; then
TARGET="$(readlink -f "$NGINX_CONFIG_FILE" 2>/dev/null || echo "")"
if [ -n "$TARGET" ] && [[ "$TARGET" == "${INSTALLER_DIR}/"* ]]; then
rm -f "$NGINX_CONFIG_FILE"
NEED_NGINX_RELOAD=true
echo "[UNINSTALL] Removed nginx config ${NGINX_CONFIG_FILE}"
elif grep -qi "netalertx" "$NGINX_CONFIG_FILE" 2>/dev/null; then
rm -f "$NGINX_CONFIG_FILE"
NEED_NGINX_RELOAD=true
echo "[UNINSTALL] Removed nginx config ${NGINX_CONFIG_FILE}"
fi
fi

if [ -f /etc/nginx/sites-available/default.bkp_netalertx ]; then
if [ -e /etc/nginx/sites-enabled/default ]; then
rm -f /etc/nginx/sites-enabled/default
fi
mv /etc/nginx/sites-available/default.bkp_netalertx /etc/nginx/sites-enabled/default
NEED_NGINX_RELOAD=true
echo "[UNINSTALL] Restored nginx default site"
elif [ ! -e /etc/nginx/sites-enabled/default ] && [ -f /etc/nginx/sites-available/default ];
then
ln -s ../sites-available/default /etc/nginx/sites-enabled/default
NEED_NGINX_RELOAD=true
echo "[UNINSTALL] Re-enabled nginx default site"
fi
Comment on lines +129 to +141
Copy link
Contributor

@coderabbitai coderabbitai bot Oct 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Restore nginx default site to sites-available, not sites-enabled
Moving default.bkp_netalertx directly into /etc/nginx/sites-enabled/default leaves /etc/nginx/sites-available/default missing and replaces the usual symlink with a plain file. That breaks the standard Debian/Ubuntu nginx layout and complicates future site management. Instead, move the backup back into sites-available and (re)create the symlink in sites-enabled.

Apply this diff:

-    mv /etc/nginx/sites-available/default.bkp_netalertx /etc/nginx/sites-enabled/default
-    NEED_NGINX_RELOAD=true
-    echo "[UNINSTALL] Restored nginx default site"
+    mv /etc/nginx/sites-available/default.bkp_netalertx /etc/nginx/sites-available/default
+    if [ ! -e /etc/nginx/sites-enabled/default ]; then
+      ln -s ../sites-available/default /etc/nginx/sites-enabled/default
+    fi
+    NEED_NGINX_RELOAD=true
+    echo "[UNINSTALL] Restored nginx default site"
🤖 Prompt for AI Agents
In install/ubuntu24/uninstall.sh around lines 129-141, the script currently
moves default.bkp_netalertx into /etc/nginx/sites-enabled/default which replaces
the normal symlink with a regular file; instead move the backup into
/etc/nginx/sites-available/default (restoring the file there), then ensure
/etc/nginx/sites-enabled/default is a symlink to ../sites-available/default
(remove any existing regular file or stale symlink first, then create the
symlink), set NEED_NGINX_RELOAD=true and echo the restored message; do the
analogous fix in the elif branch by creating the symlink to
../sites-available/default rather than linking incorrectly.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should not include packages or remove anything other than the files from this repository. you could wreck someone's php, nginx, avahi, or other. the only files you should touch are the /app files.

Additionaly, there's no official support for hardware installs. The HW installs make copies of the GitHub instead of installing directly from source, making them outside the realm of anything which should be included. HW installs will be removed from source and moved to documentation shortly.

I recommend converting this to a sectional uninstall doc.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adamoutler, understood. You're absolutely right that the uninstall script in its current form is too invasive and could potentially damage user systems by touching nginx, PHP, systemd, and other system packages that may be shared with other applications.

The key concerns I'm noting:

  1. Safety: Removing or modifying system packages (nginx, PHP, avahi) could break other applications or services on the user's system
  2. Scope: The script should only handle files from this repository (specifically /app directory)
  3. Policy: Hardware installs aren't officially supported and will be moved to documentation

Converting this to a sectional uninstall documentation makes much more sense. The documentation could:

  • Provide step-by-step instructions for users who want to manually uninstall
  • Clearly scope what files belong to NetAlertX (primarily /app contents)
  • Let users decide what system-level changes to reverse (nginx config, systemd units, etc.)
  • Include warnings about not removing shared system packages

This approach gives users control while avoiding the risk of automated removal damaging their systems.


✏️ Learnings added
Learnt from: adamoutler
PR: jokob-sk/NetAlertX#1214
File: install/ubuntu24/uninstall.sh:129-141
Timestamp: 2025-10-10T22:16:02.739Z
Learning: In the NetAlertX project, hardware installs are not officially supported. Hardware installation methods will be moved from source code to documentation.

Learnt from: adamoutler
PR: jokob-sk/NetAlertX#1214
File: install/ubuntu24/uninstall.sh:129-141
Timestamp: 2025-10-10T22:16:02.740Z
Learning: NetAlertX uninstall procedures should only remove files from the repository (specifically /app files) and should not touch system packages like PHP, nginx, avahi, or other shared system components to avoid damaging user systems.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great points @adamoutler

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!


echo
echo "---------------------------------------------------------"
echo "[UNINSTALL] Removing application files"
echo "---------------------------------------------------------"

if [ -d "$VENV_DIR" ]; then
rm -rf "$VENV_DIR"
echo "[UNINSTALL] Removed virtual environment ${VENV_DIR}"
fi

if [ -d "$INSTALL_DIR" ]; then
if [ "$PURGE_DATA" = false ]; then
if [ -t 0 ]; then
echo "[UNINSTALL] ${INSTALL_DIR} contains NetAlertX binaries, config, and database."
read -r -p "[UNINSTALL] Type 'purge' to delete it now, or press Enter to keep it:
" answer
if [ "$answer" = "purge" ]; then
PURGE_DATA=true
else
echo "[UNINSTALL] Keeping ${INSTALL_DIR}"
fi
else
echo "[UNINSTALL] Skipping removal of ${INSTALL_DIR}; rerun with --purge-data to
delete it."
fi
fi

if [ "$PURGE_DATA" = true ]; then
if [[ "$INSTALL_DIR" == "/" ]]; then
echo "[UNINSTALL] Refusing to remove root directory."
else
rm -rf "$INSTALL_DIR"
echo "[UNINSTALL] Removed ${INSTALL_DIR}"
fi
fi
fi

if [ "$NEED_NGINX_RELOAD" = true ] && command -v nginx >/dev/null 2>&1; then
echo
echo "---------------------------------------------------------"
echo "[UNINSTALL] Reloading nginx"
echo "---------------------------------------------------------"
if nginx -t; then
systemctl reload nginx 2>/dev/null || systemctl restart nginx 2>/dev/null || true
else
echo "[UNINSTALL] nginx config test failed; please fix manually before reloading."
fi
fi

echo
echo "---------------------------------------------------------"
echo "[UNINSTALL] Package cleanup"
echo "---------------------------------------------------------"

PACKAGES_TO_PURGE=(
git
tini
libwww-perl
perl
build-essential
sqlite3
net-tools
python3
python3-venv
python3-dev
python3-pip
dnsutils
mtr
arp-scan
snmp
iproute2
nmap
usbutils
traceroute
nbtscan
avahi-daemon
avahi-utils
nginx-core
php${PHPVERSION}
php${PHPVERSION}-sqlite3
php
php-fpm
php-cgi
php${PHPVERSION}-fpm
php-sqlite3
php-curl
php-cli
)

if command -v apt-get >/dev/null 2>&1; then
if [ "$PURGE_PACKAGES" = false ]; then
if [ -t 0 ]; then
echo "[UNINSTALL] The installer may have added these packages:"
printf ' - %s\n' "${PACKAGES_TO_PURGE[@]}"
echo "[UNINSTALL] Removing them can affect other applications that rely on them."
read -r -p "[UNINSTALL] Type 'purge' to remove these packages now, or press Enter to keep them: " package_answer
if [ "$package_answer" = "purge" ]; then
PURGE_PACKAGES=true
else
echo "[UNINSTALL] Keeping installed packages."
fi
else
echo "[UNINSTALL] Packages installed by the original script were left in place."
echo "[UNINSTALL] Re-run with --purge-packages to remove them automatically."
fi
fi

if [ "$PURGE_PACKAGES" = true ]; then
echo "[UNINSTALL] Removing installer packages via apt-get purge."
if ! apt-get purge -y "${PACKAGES_TO_PURGE[@]}"; then
echo "[UNINSTALL] Failed to purge some packages; please review the output above." >&2
else
apt-get autoremove -y
echo "[UNINSTALL] Package purge complete."
fi
fi
else
echo "[UNINSTALL] apt-get not found; skipping package removal."
fi

echo
echo "---------------------------------------------------------"
echo "[UNINSTALL] NetAlertX removal complete"
echo "---------------------------------------------------------"
echo "[UNINSTALL] Thank you for using NetAlertX."