#!/bin/bash # ============================================================================== # WALLARM UNINSTALL SCRIPT - V1.0 (Native deployment) # ============================================================================== # Purpose: Safely remove a native (no-Docker) Wallarm filtering node # Features: # - Interactive confirmation with safety checks # - Removes Wallarm NGINX configuration (with backup) # - Restores disabled default NGINX sites # - Uninstalls Wallarm packages and repository definitions # - Removes /opt/wallarm node data # - Preserves user data and logs (with option to remove) # - DAU-friendly warnings and confirmations # ============================================================================== # Script location and shared library (colors, logging, validation) SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # shellcheck source=../common/wallarm-lib.sh source "$SCRIPT_DIR/../common/wallarm-lib.sh" # Strict error handling set -euo pipefail trap early_error_handler ERR # ============================================================================== # CONFIGURATION # ============================================================================== CONFIG_GLOB="/etc/nginx/conf.d/wallarm-*.conf" DEFAULT_BACKUP_GLOB="/etc/nginx/sites-enabled/*.bak" WALLARM_NODE_DIR="/opt/wallarm" # ============================================================================== # FUNCTIONS # ============================================================================== # Ask for confirmation confirm() { local prompt="$1" local default="${2:-n}" local options="[y/N]" if [ "$default" = "y" ]; then options="[Y/n]" fi echo -e -n "${YELLOW}${prompt} ${options}${NC} " read -r response case "$response" in [yY][eE][sS]|[yY]) return 0 ;; [nN][oO]|[nN]) return 1 ;; "") if [ "$default" = "y" ]; then return 0 else return 1 fi ;; *) return 1 ;; esac } # Detect the package manager in use detect_package_manager() { if command -v apt-get >/dev/null 2>&1; then echo "apt" elif command -v yum >/dev/null 2>&1; then echo "yum" elif command -v dnf >/dev/null 2>&1; then echo "dnf" elif command -v apk >/dev/null 2>&1; then echo "apk" else echo "unknown" fi } # Remove Wallarm NGINX configuration files remove_nginx_configs() { log_message "INFO" "Looking for Wallarm NGINX configurations..." local configs=() while IFS= read -r file; do configs+=("$file") done < <(ls $CONFIG_GLOB 2>/dev/null) if [ ${#configs[@]} -eq 0 ]; then log_message "INFO" "No Wallarm NGINX configurations found" return fi log_message "INFO" "Found ${#configs[@]} Wallarm NGINX configuration(s)" for file in "${configs[@]}"; do if confirm "Remove $file? (backup created)" "y"; then cp "$file" "$file.uninstall-$(date +%Y%m%d_%H%M%S).bak" rm -f "$file" log_message "SUCCESS" "Removed $file (backup created)" else log_message "INFO" "Skipping $file" fi done } # Restore any default NGINX sites we disabled during deployment restore_default_sites() { log_message "INFO" "Looking for disabled default NGINX sites..." local restored=0 while IFS= read -r backup; do if [ -f "$backup" ]; then local target="${backup%.bak}" log_message "INFO" "Restoring $target from $backup" if confirm "Restore $target?" "y"; then cp "$backup" "$target" rm -f "$backup" restored=$((restored + 1)) fi fi done < <(ls $DEFAULT_BACKUP_GLOB 2>/dev/null) if [ "$restored" -gt 0 ]; then log_message "SUCCESS" "Restored $restored default site(s)" else log_message "INFO" "No disabled default sites found" fi } # Reload NGINX to drop the removed configurations reload_nginx() { log_message "INFO" "Testing and reloading NGINX..." if sudo nginx -t 2>/dev/null; then sudo systemctl reload nginx 2>/dev/null || sudo nginx -s reload 2>/dev/null || true log_message "SUCCESS" "NGINX reloaded" else log_message "WARNING" "NGINX configuration test failed - reload skipped" echo -e "${YELLOW}Run 'sudo nginx -t' manually to diagnose the issue.${NC}" fi } # Uninstall Wallarm packages via the system package manager remove_wallarm_packages() { local pkg_manager pkg_manager=$(detect_package_manager) log_message "INFO" "Detected package manager: $pkg_manager" if ! confirm "Uninstall Wallarm packages? (wallarm-node, wallarm modules)" "y"; then log_message "INFO" "Skipping Wallarm package removal" return fi case "$pkg_manager" in "apt") log_message "INFO" "Removing Wallarm packages (apt)..." sudo apt-get purge -y "wallarm-node" "wallarm-node-nginx" "nginx-module-wallarm*" 2>/dev/null || true sudo apt-get autoremove -y 2>/dev/null || true ;; "yum") log_message "INFO" "Removing Wallarm packages (yum)..." sudo yum remove -y "wallarm-node" "nginx-module-wallarm*" 2>/dev/null || true ;; "dnf") log_message "INFO" "Removing Wallarm packages (dnf)..." sudo dnf remove -y "wallarm-node" "nginx-module-wallarm*" 2>/dev/null || true ;; "apk") log_message "INFO" "Removing Wallarm packages (apk)..." sudo apk del "wallarm-node" "nginx-module-wallarm" 2>/dev/null || true ;; *) log_message "WARNING" "Unknown package manager. Please remove Wallarm packages manually." echo -e "${YELLOW}Relevant packages usually include: wallarm-node, nginx-module-wallarm${NC}" return ;; esac log_message "SUCCESS" "Wallarm packages removed" } # Remove Wallarm repository definitions added by the installer remove_wallarm_repos() { log_message "INFO" "Removing Wallarm repository definitions..." local removed=0 while IFS= read -r file; do if [ -f "$file" ]; then sudo rm -f "$file" log_message "SUCCESS" "Removed repo file: $file" removed=$((removed + 1)) fi done < <(ls /etc/apt/sources.list.d/*wallarm* /etc/yum.repos.d/*wallarm* 2>/dev/null) if [ "$removed" -eq 0 ]; then log_message "INFO" "No Wallarm repository files found" fi } # Remove Wallarm node data directory remove_wallarm_data() { if [ -d "$WALLARM_NODE_DIR" ]; then log_message "WARNING" "Found Wallarm data directory: $WALLARM_NODE_DIR" log_message "WARNING" "This contains node registration and local data." if confirm "Remove $WALLARM_NODE_DIR? (node will need re-registration)" "n"; then sudo rm -rf "$WALLARM_NODE_DIR" log_message "SUCCESS" "Removed $WALLARM_NODE_DIR" else log_message "INFO" "Skipping removal of $WALLARM_NODE_DIR" fi else log_message "INFO" "No Wallarm data directory found" fi } # Remove Wallarm log files (optional) remove_wallarm_logs() { local log_dir="$HOME/logs" if [ -d "$log_dir" ]; then log_message "INFO" "Found log directory: $log_dir" if [ -z "$(ls -A "$log_dir" 2>/dev/null)" ]; then log_message "INFO" "Log directory is empty, removing..." sudo rmdir "$log_dir" 2>/dev/null || true else log_message "INFO" "Log directory contains files, preserving..." fi fi if [ -f ".env" ]; then if confirm "Remove .env preflight file?" "n"; then rm -f ".env" log_message "SUCCESS" "Removed .env file" fi fi } # ============================================================================== # MAIN # ============================================================================== main() { echo -e "${CYAN}${BOLD}" echo "╔══════════════════════════════════════════════════════════════╗" echo "║ WALLARM UNINSTALLATION (Native) ║" echo "╚══════════════════════════════════════════════════════════════╝" echo -e "${NC}" echo -e "${YELLOW}This script will remove the native Wallarm filtering node.${NC}" echo -e "${YELLOW}You will be asked for confirmation before each destructive operation.${NC}" echo "" if ! confirm "Do you want to continue with the uninstallation?" "n"; then log_message "INFO" "Uninstallation cancelled by user" exit 0 fi # Step 1: Remove Wallarm NGINX configurations echo "" echo -e "${CYAN}${BOLD}Step 1: Remove Wallarm NGINX configurations${NC}" remove_nginx_configs restore_default_sites reload_nginx # Step 2: Uninstall Wallarm packages echo "" echo -e "${CYAN}${BOLD}Step 2: Uninstall Wallarm packages${NC}" remove_wallarm_packages remove_wallarm_repos # Step 3: Remove Wallarm data echo "" echo -e "${CYAN}${BOLD}Step 3: Remove Wallarm data${NC}" remove_wallarm_data # Step 4: Cleanup echo "" echo -e "${CYAN}${BOLD}Step 4: Logs and files cleanup${NC}" if confirm "Remove Wallarm log files and .env?" "n"; then remove_wallarm_logs else log_message "INFO" "Skipping log/file cleanup" fi # Final message echo "" echo -e "${GREEN}${BOLD}╔══════════════════════════════════════════════════════════════╗${NC}" echo -e "${GREEN}${BOLD}║ UNINSTALLATION COMPLETE ║${NC}" echo -e "${GREEN}${BOLD}╚══════════════════════════════════════════════════════════════╝${NC}" echo "" echo -e "${GREEN}The native Wallarm filtering node has been removed.${NC}" echo "" echo -e "${YELLOW}Note:${NC}" echo -e " • NGINX itself was kept installed (may be used by other applications)" echo -e " • The Wallarm module may remain in NGINX's modules directory" echo -e " • Wallarm repositories were removed from your package sources" echo "" } # Run main function main "$@"