From 08c500855e9538efbb22f47de3a5dfbd86f4b65f Mon Sep 17 00:00:00 2001 From: taylanbakircioglu Date: Mon, 16 Feb 2026 14:16:07 +0300 Subject: [PATCH] fix: rewrite uninstall scripts - simple and reliable Replaced overcomplicated 380-line scripts with clean ~125-line versions. Steps: stop service, kill processes (excluding self), remove binary, remove config/logs/temps, verify. No emojis, no fragile helpers. Co-authored-by: Cursor --- .../agent_scripts/uninstall-agent-linux.sh | 435 ++++-------------- .../agent_scripts/uninstall-agent-macos.sh | 405 ++++------------ utils/uninstall-agent-linux.sh | 435 ++++-------------- utils/uninstall-agent-macos.sh | 405 ++++------------ 4 files changed, 362 insertions(+), 1318 deletions(-) diff --git a/backend/utils/agent_scripts/uninstall-agent-linux.sh b/backend/utils/agent_scripts/uninstall-agent-linux.sh index 1714184..cdc1181 100755 --- a/backend/utils/agent_scripts/uninstall-agent-linux.sh +++ b/backend/utils/agent_scripts/uninstall-agent-linux.sh @@ -1,380 +1,127 @@ #!/bin/bash -# HAProxy Agent Uninstaller for Linux - Complete Agent Cleanup +# HAProxy Agent Uninstaller for Linux # Run with: sudo ./uninstall-agent-linux.sh -# Removes ALL agent components while keeping HAProxy intact -# -# IMPORTANT: 'set -e' intentionally NOT used here. -# safe_remove returns non-zero for missing files which would cause -# premature exit before cleaning up config, logs, and binaries. +# Removes agent service, binary, config, logs. HAProxy untouched. -echo "==========================================" -echo "HAProxy Agent Uninstaller for Linux v6.0" -echo "Complete Agent Cleanup" -echo "==========================================" - -# Check root permissions if [[ $EUID -ne 0 ]]; then - echo "ERROR: This script must be run as root" - echo " Please run: sudo $0" + echo "ERROR: Run as root: sudo $0" exit 1 fi +echo "==========================================" +echo "HAProxy Agent Uninstaller for Linux" +echo "==========================================" echo "" -echo "Starting complete agent cleanup (HAProxy untouched)..." -echo "" - -# -------------------------------------------- -# Helper: safely remove a file or directory -# Always returns 0 so the script never exits early -# -------------------------------------------- -safe_remove() { - local path="$1" - local desc="$2" - - if [[ -e "$path" ]]; then - rm -rf "$path" 2>/dev/null || true - if [[ ! -e "$path" ]]; then - echo " [REMOVED] $desc" - else - echo " [WARN] Failed to remove $desc" - fi - else - echo " [SKIP] $desc (not found)" - fi - return 0 -} - -# -------------------------------------------- -# 1. Terminate all agent processes -# -------------------------------------------- -echo "[1/7] Terminating agent processes..." - -killed_count=0 -patterns=( - "haproxy-agent" - "/usr/local/bin/haproxy-agent" - "/usr/bin/haproxy-agent" - "haproxy-agent.service" - "haproxy-agent daemon" - "bash.*haproxy-agent" - "nohup.*haproxy-agent" -) SELF_PID=$$ -for pattern in "${patterns[@]}"; do - pids=$(pgrep -f "$pattern" 2>/dev/null || true) - if [[ -n "$pids" ]]; then - # Filter out our own PID and parent PID to avoid killing ourselves - filtered_pids="" - for p in $pids; do - if [[ "$p" != "$SELF_PID" && "$p" != "$PPID" ]]; then - filtered_pids="$filtered_pids $p" - fi - done - filtered_pids=$(echo "$filtered_pids" | xargs) - if [[ -n "$filtered_pids" ]]; then - echo " Killing processes matching: $pattern (PIDs: $filtered_pids)" - kill -TERM $filtered_pids 2>/dev/null || true - sleep 2 - # Force kill if still running - remaining=$(pgrep -f "$pattern" 2>/dev/null || true) - if [[ -n "$remaining" ]]; then - rem_filtered="" - for p in $remaining; do - if [[ "$p" != "$SELF_PID" && "$p" != "$PPID" ]]; then - rem_filtered="$rem_filtered $p" - fi - done - rem_filtered=$(echo "$rem_filtered" | xargs) - if [[ -n "$rem_filtered" ]]; then - echo " Force killing remaining: $rem_filtered" - kill -KILL $rem_filtered 2>/dev/null || true - sleep 1 - fi - fi - killed_count=$((killed_count + $(echo "$filtered_pids" | wc -w))) - fi - fi +# --- 1. Stop and remove systemd service --- +echo "[1/5] Stopping systemd service..." +for svc in haproxy-agent.service haproxy.agent.service; do + systemctl stop "$svc" 2>/dev/null + systemctl disable "$svc" 2>/dev/null + systemctl unmask "$svc" 2>/dev/null done +for f in \ + /etc/systemd/system/haproxy-agent.service \ + /etc/systemd/system/haproxy.agent.service \ + /lib/systemd/system/haproxy-agent.service \ + /usr/lib/systemd/system/haproxy-agent.service \ + /etc/systemd/user/haproxy-agent.service; do + rm -f "$f" 2>/dev/null && echo " Removed $f" +done +systemctl daemon-reload 2>/dev/null +systemctl reset-failed 2>/dev/null +echo " Done" +echo "" -# Final sweep (exclude self) -final_pids=$(pgrep -f "haproxy-agent" 2>/dev/null || true) -if [[ -n "$final_pids" ]]; then - final_filtered="" - for p in $final_pids; do +# --- 2. Kill agent processes (exclude self) --- +echo "[2/5] Killing agent processes..." +agent_pids=$(pgrep -f "haproxy-agent" 2>/dev/null || true) +if [[ -n "$agent_pids" ]]; then + for p in $agent_pids; do if [[ "$p" != "$SELF_PID" && "$p" != "$PPID" ]]; then - final_filtered="$final_filtered $p" + kill -9 "$p" 2>/dev/null && echo " Killed PID $p" fi done - final_filtered=$(echo "$final_filtered" | xargs) - if [[ -n "$final_filtered" ]]; then - echo " Final cleanup: force killing PIDs $final_filtered" - kill -KILL $final_filtered 2>/dev/null || true - fi -fi - -if [[ $killed_count -gt 0 ]]; then - echo " Terminated $killed_count agent process(es)" + sleep 1 else echo " No agent processes found" fi echo "" -# -------------------------------------------- -# 2. Stop and remove systemd services -# -------------------------------------------- -echo "[2/7] Cleaning up systemd services..." - -services=( - "haproxy-agent.service" - "haproxy.agent.service" -) - -for svc in "${services[@]}"; do - if systemctl is-active "$svc" &>/dev/null; then - echo " Stopping: $svc" - systemctl stop "$svc" 2>/dev/null || true - fi - if systemctl is-enabled "$svc" &>/dev/null; then - echo " Disabling: $svc" - systemctl disable "$svc" 2>/dev/null || true - fi - # Unmask in case it was masked previously, then mask to prevent reactivation - systemctl unmask "$svc" 2>/dev/null || true - systemctl mask "$svc" 2>/dev/null || true +# --- 3. Remove binary --- +echo "[3/5] Removing agent binary..." +for f in \ + /usr/local/bin/haproxy-agent \ + /usr/bin/haproxy-agent \ + /usr/sbin/haproxy-agent \ + /sbin/haproxy-agent \ + /opt/bin/haproxy-agent; do + rm -f "$f" 2>/dev/null && echo " Removed $f" done - -# Remove service unit files -service_files=( - "/etc/systemd/system/haproxy-agent.service" - "/etc/systemd/system/haproxy.agent.service" - "/lib/systemd/system/haproxy-agent.service" - "/usr/lib/systemd/system/haproxy-agent.service" - "/etc/systemd/user/haproxy-agent.service" -) - -for sf in "${service_files[@]}"; do - safe_remove "$sf" "systemd unit: $sf" -done - -# Reload systemd -systemctl daemon-reload 2>/dev/null || true -systemctl reset-failed 2>/dev/null || true -echo " systemd daemon reloaded" +echo " Done" echo "" -# -------------------------------------------- -# 3. Remove agent binaries -# -------------------------------------------- -echo "[3/7] Removing agent binaries..." - -bin_paths=( - "/usr/local/bin/haproxy-agent" - "/usr/bin/haproxy-agent" - "/bin/haproxy-agent" - "/usr/sbin/haproxy-agent" - "/sbin/haproxy-agent" - "/opt/bin/haproxy-agent" - "/usr/local/sbin/haproxy-agent" -) - -for bp in "${bin_paths[@]}"; do - safe_remove "$bp" "binary: $bp" +# --- 4. Remove config, logs, PID files, temp files --- +echo "[4/5] Removing config, logs, and temp files..." +for d in \ + /etc/haproxy-agent \ + /var/log/haproxy-agent \ + /var/lib/haproxy-agent \ + /usr/local/etc/haproxy-agent \ + /usr/local/share/haproxy-agent \ + /usr/local/share/haproxy-agent-backups \ + /opt/haproxy-agent; do + rm -rf "$d" 2>/dev/null && echo " Removed $d" done - -# Deep search for any remaining binaries -found_extra=$(find /usr /opt /bin /sbin -name "haproxy-agent" -type f 2>/dev/null || true) -if [[ -n "$found_extra" ]]; then - while IFS= read -r extra; do - safe_remove "$extra" "binary (deep search): $extra" - done <<< "$found_extra" +for f in \ + /var/run/haproxy-agent.pid \ + /run/haproxy-agent.pid \ + /tmp/haproxy-agent.pid; do + rm -f "$f" 2>/dev/null && echo " Removed $f" +done +# Clean up agent temp files in /tmp +find /tmp -maxdepth 1 -name "haproxy-agent*" -exec rm -rf {} \; 2>/dev/null +find /tmp -maxdepth 1 -name "heartbeat_payload_*" -exec rm -f {} \; 2>/dev/null +find /tmp -maxdepth 1 -name "heartbeat_response_*" -exec rm -f {} \; 2>/dev/null +find /tmp -maxdepth 1 -name "haproxy-failed-*" -exec rm -f {} \; 2>/dev/null +find /tmp -maxdepth 1 -name "haproxy_new_*" -exec rm -f {} \; 2>/dev/null +find /tmp -maxdepth 1 -name "haproxy-merged*" -exec rm -f {} \; 2>/dev/null +find /tmp -maxdepth 1 -name "ssl_cert_*" -exec rm -f {} \; 2>/dev/null +rm -f /tmp/agent_debug.log /tmp/debug_agent.log /tmp/haproxy-new-config.cfg 2>/dev/null +# Clean cron +if grep -q "haproxy-agent" /etc/cron.d/haproxy-agent 2>/dev/null; then + rm -f /etc/cron.d/haproxy-agent 2>/dev/null && echo " Removed cron job" fi -echo "" - -# -------------------------------------------- -# 4. Remove ALL agent configuration and data -# -------------------------------------------- -echo "[4/7] Removing agent configuration and data..." - -config_dirs=( - "/etc/haproxy-agent" - "/usr/local/etc/haproxy-agent" - "/var/lib/haproxy-agent" - "/opt/haproxy-agent" - "/usr/local/share/haproxy-agent" - "/usr/local/share/haproxy-agent-backups" -) - -for cd in "${config_dirs[@]}"; do - safe_remove "$cd" "config dir: $cd" -done - -log_dirs=( - "/var/log/haproxy-agent" - "/usr/local/var/log/haproxy-agent" - "/tmp/haproxy-agent-logs" - "/var/log/syslog.d/haproxy-agent" -) - -for ld in "${log_dirs[@]}"; do - safe_remove "$ld" "log dir: $ld" -done - -pid_files=( - "/var/run/haproxy-agent.pid" - "/tmp/haproxy-agent.pid" - "/usr/local/var/run/haproxy-agent.pid" - "/run/haproxy-agent.pid" -) - -for pf in "${pid_files[@]}"; do - safe_remove "$pf" "PID file: $pf" -done -echo "" - -# -------------------------------------------- -# 5. Remove temporary files, markers, sockets -# -------------------------------------------- -echo "[5/7] Cleaning up temporary files and markers..." - -# Fixed-name temp files -fixed_temps=( - "/tmp/agent_debug.log" - "/tmp/debug_agent.log" - "/tmp/fixed_agent_test.log" - "/tmp/agent-startup-with-config.sh" - "/tmp/haproxy-new-config.cfg" -) - -for ft in "${fixed_temps[@]}"; do - safe_remove "$ft" "temp: $ft" -done - -# Wildcard patterns - files -wildcard_file_patterns=( - "haproxy-agent-*" - "daemon-test-agent*.sh" - "haproxy-failed-*.cfg" - "haproxy_new_*.cfg" - "haproxy-merged*.cfg" - "haproxy-global*.cfg" - "haproxy-defaults*.cfg" - "haproxy-listen*.cfg" - "haproxy-agent-upgrade-complete-*" - "haproxy-agent-upgrade-test-*" - "haproxy-agent-*.pid" - "haproxy-agent-ssl-sync-*" - "heartbeat_payload_*.json" - "heartbeat_response_*.txt" - "ssl_cert_*.pem" -) - -for wp in "${wildcard_file_patterns[@]}"; do - for search_dir in "/tmp" "/var/tmp"; do - if [[ -d "$search_dir" ]]; then - found=$(find "$search_dir" -maxdepth 1 -name "$wp" 2>/dev/null || true) - if [[ -n "$found" ]]; then - while IFS= read -r f; do - safe_remove "$f" "temp: $f" - done <<< "$found" - fi - fi - done -done - -# Wildcard patterns - sockets -for search_dir in "/tmp" "/var/run" "/run"; do - if [[ -d "$search_dir" ]]; then - found_sockets=$(find "$search_dir" -maxdepth 1 -name "haproxy-agent-*" -type s 2>/dev/null || true) - if [[ -n "$found_sockets" ]]; then - while IFS= read -r sock; do - safe_remove "$sock" "socket: $sock" - done <<< "$found_sockets" - fi - fi -done -echo "" - -# -------------------------------------------- -# 6. Clean up cron jobs -# -------------------------------------------- -echo "[6/7] Cleaning up cron jobs..." - -cron_files=( - "/etc/cron.d/haproxy-agent" - "/etc/crontab" - "/var/spool/cron/root" - "/var/spool/cron/crontabs/root" -) - -for cf in "${cron_files[@]}"; do +for cf in /etc/crontab /var/spool/cron/root /var/spool/cron/crontabs/root; do if [[ -f "$cf" ]] && grep -q "haproxy-agent" "$cf" 2>/dev/null; then - cp "$cf" "${cf}.backup.$(date +%Y%m%d_%H%M%S)" 2>/dev/null || true - sed -i '/haproxy-agent/d' "$cf" 2>/dev/null || true - echo " [CLEANED] Removed haproxy-agent entries from $cf" + sed -i '/haproxy-agent/d' "$cf" 2>/dev/null && echo " Cleaned $cf" fi done +echo " Done" echo "" -# -------------------------------------------- -# 7. Verification -# -------------------------------------------- -echo "[7/7] Verifying cleanup..." - -remaining_procs=$(pgrep -f "haproxy-agent" 2>/dev/null | wc -l || echo "0") -remaining_svcs=$(systemctl list-units --all 2>/dev/null | grep -c "haproxy-agent" || echo "0") -remaining_bins=$(find /usr /opt /bin /sbin -name "haproxy-agent" -type f 2>/dev/null | wc -l || echo "0") -remaining_conf=$(test -d "/etc/haproxy-agent" && echo "1" || echo "0") - -echo "" -if [[ $remaining_procs -eq 0 && $remaining_svcs -eq 0 && $remaining_bins -eq 0 && $remaining_conf -eq 0 ]]; then - echo "CLEANUP VERIFIED - Agent completely removed." -else - echo "WARNING: Some agent remnants detected:" - [[ $remaining_procs -gt 0 ]] && echo " - $remaining_procs agent process(es) still running" - [[ $remaining_svcs -gt 0 ]] && echo " - $remaining_svcs agent service(s) still loaded" - [[ $remaining_bins -gt 0 ]] && echo " - $remaining_bins agent binary(ies) still found" - [[ $remaining_conf -gt 0 ]] && echo " - /etc/haproxy-agent/ config directory still exists" - - if [[ $remaining_bins -gt 0 ]]; then - echo " Remaining binaries:" - find /usr /opt /bin /sbin -name "haproxy-agent" -type f 2>/dev/null | while read -r b; do - echo " - $b" - done - fi - - if [[ $remaining_procs -gt 0 ]]; then - echo " Remaining processes:" - pgrep -f "haproxy-agent" 2>/dev/null | while read -r pid; do - ps -p "$pid" -o pid,ppid,command 2>/dev/null || true - done - fi +# --- 5. Verify --- +echo "[5/5] Verifying..." +ok=true +if pgrep -f "haproxy-agent" 2>/dev/null | grep -v "^${SELF_PID}$" | grep -v "^${PPID}$" | grep -q .; then + echo " WARNING: Agent processes still running" + ok=false +fi +if [[ -d /etc/haproxy-agent ]]; then + echo " WARNING: /etc/haproxy-agent still exists" + ok=false +fi +if [[ -f /usr/local/bin/haproxy-agent ]]; then + echo " WARNING: /usr/local/bin/haproxy-agent still exists" + ok=false +fi +if $ok; then + echo " Agent completely removed." fi echo "" echo "==========================================" -echo "HAProxy Agent Uninstall Complete" +echo "Done. HAProxy itself was not touched." echo "==========================================" -echo "" -echo "What was removed:" -echo " - All agent processes (including daemon mode)" -echo " - All systemd services (stopped, disabled, masked)" -echo " - All agent binaries" -echo " - All agent config files (including config.json)" -echo " - All agent logs" -echo " - All agent temp files, markers, and sockets" -echo " - Agent-related cron entries" -echo "" -echo "What was NOT touched:" -echo " - HAProxy service" -echo " - HAProxy configuration (/etc/haproxy/haproxy.cfg)" -echo " - HAProxy SSL certificates" -echo " - HAProxy logs" -echo "" -echo "System is ready for fresh agent installation." -echo "" -echo "Verify with:" -echo " ps aux | grep haproxy-agent" -echo " systemctl status haproxy-agent" -echo " ls -la /etc/haproxy-agent/" diff --git a/backend/utils/agent_scripts/uninstall-agent-macos.sh b/backend/utils/agent_scripts/uninstall-agent-macos.sh index 2538277..20a9e34 100755 --- a/backend/utils/agent_scripts/uninstall-agent-macos.sh +++ b/backend/utils/agent_scripts/uninstall-agent-macos.sh @@ -1,347 +1,122 @@ #!/bin/bash -# HAProxy Agent Uninstaller for macOS - Complete Agent Cleanup +# HAProxy Agent Uninstaller for macOS # Run with: sudo ./uninstall-agent-macos.sh -# Removes ALL agent components while keeping HAProxy intact -# -# IMPORTANT: 'set -e' intentionally NOT used here. -# safe_remove returns non-zero for missing files which would cause -# premature exit before cleaning up config, logs, and binaries. +# Removes agent service, binary, config, logs. HAProxy untouched. -echo "==========================================" -echo "HAProxy Agent Uninstaller for macOS v6.0" -echo "Complete Agent Cleanup" -echo "==========================================" - -# Check root permissions if [[ $EUID -ne 0 ]]; then - echo "ERROR: This script must be run as root" - echo " Please run: sudo $0" + echo "ERROR: Run as root: sudo $0" exit 1 fi +echo "==========================================" +echo "HAProxy Agent Uninstaller for macOS" +echo "==========================================" echo "" -echo "Starting complete agent cleanup (HAProxy untouched)..." -echo "" - -# -------------------------------------------- -# Helper: safely remove a file or directory -# Always returns 0 so the script never exits early -# -------------------------------------------- -safe_remove() { - local path="$1" - local desc="$2" - - if [[ -e "$path" ]]; then - rm -rf "$path" 2>/dev/null || true - if [[ ! -e "$path" ]]; then - echo " [REMOVED] $desc" - else - echo " [WARN] Failed to remove $desc" - fi - else - echo " [SKIP] $desc (not found)" - fi - return 0 -} - -# -------------------------------------------- -# 1. Terminate all agent processes -# -------------------------------------------- -echo "[1/6] Terminating agent processes..." - -killed_count=0 -patterns=( - "haproxy-agent" - "/usr/local/bin/haproxy-agent" - "/opt/homebrew/bin/haproxy-agent" - "com.haproxy.agent" - "haproxy-agent daemon" - "bash.*haproxy-agent" - "nohup.*haproxy-agent" -) SELF_PID=$$ -for pattern in "${patterns[@]}"; do - pids=$(pgrep -f "$pattern" 2>/dev/null || true) - if [[ -n "$pids" ]]; then - # Filter out our own PID and parent PID to avoid killing ourselves - filtered_pids="" - for p in $pids; do - if [[ "$p" != "$SELF_PID" && "$p" != "$PPID" ]]; then - filtered_pids="$filtered_pids $p" - fi - done - filtered_pids=$(echo "$filtered_pids" | xargs) - if [[ -n "$filtered_pids" ]]; then - echo " Killing processes matching: $pattern (PIDs: $filtered_pids)" - kill -TERM $filtered_pids 2>/dev/null || true - sleep 2 - # Force kill if still running - remaining=$(pgrep -f "$pattern" 2>/dev/null || true) - if [[ -n "$remaining" ]]; then - rem_filtered="" - for p in $remaining; do - if [[ "$p" != "$SELF_PID" && "$p" != "$PPID" ]]; then - rem_filtered="$rem_filtered $p" - fi - done - rem_filtered=$(echo "$rem_filtered" | xargs) - if [[ -n "$rem_filtered" ]]; then - echo " Force killing remaining: $rem_filtered" - kill -KILL $rem_filtered 2>/dev/null || true - sleep 1 - fi - fi - killed_count=$((killed_count + $(echo "$filtered_pids" | wc -w))) - fi - fi +# --- 1. Stop and remove launchd service --- +echo "[1/5] Stopping launchd service..." +for svc in com.haproxy.agent haproxy.agent haproxy-agent com.haproxy-agent; do + launchctl stop "$svc" 2>/dev/null + launchctl unload "/Library/LaunchDaemons/$svc.plist" 2>/dev/null + launchctl unload "/Library/LaunchAgents/$svc.plist" 2>/dev/null + launchctl remove "$svc" 2>/dev/null done +for svc in com.haproxy.agent haproxy.agent haproxy-agent com.haproxy-agent; do + for f in \ + "/Library/LaunchDaemons/$svc.plist" \ + "/Library/LaunchAgents/$svc.plist" \ + "$HOME/Library/LaunchAgents/$svc.plist"; do + rm -f "$f" 2>/dev/null && echo " Removed $f" + done +done +echo " Done" +echo "" -# Final sweep (exclude self) -final_pids=$(pgrep -f "haproxy-agent" 2>/dev/null || true) -if [[ -n "$final_pids" ]]; then - final_filtered="" - for p in $final_pids; do +# --- 2. Kill agent processes (exclude self) --- +echo "[2/5] Killing agent processes..." +agent_pids=$(pgrep -f "haproxy-agent" 2>/dev/null || true) +if [[ -n "$agent_pids" ]]; then + for p in $agent_pids; do if [[ "$p" != "$SELF_PID" && "$p" != "$PPID" ]]; then - final_filtered="$final_filtered $p" + kill -9 "$p" 2>/dev/null && echo " Killed PID $p" fi done - final_filtered=$(echo "$final_filtered" | xargs) - if [[ -n "$final_filtered" ]]; then - echo " Final cleanup: force killing PIDs $final_filtered" - kill -KILL $final_filtered 2>/dev/null || true - fi -fi - -if [[ $killed_count -gt 0 ]]; then - echo " Terminated $killed_count agent process(es)" + sleep 1 else echo " No agent processes found" fi echo "" -# -------------------------------------------- -# 2. Stop and remove launchd services -# -------------------------------------------- -echo "[2/6] Cleaning up launchd services..." - -services=( - "com.haproxy.agent" - "haproxy.agent" - "haproxy-agent" - "com.haproxy-agent" -) - -for svc in "${services[@]}"; do - if launchctl list 2>/dev/null | grep -q "$svc"; then - echo " Stopping and removing: $svc" - launchctl stop "$svc" 2>/dev/null || true - launchctl unload "/Library/LaunchDaemons/$svc.plist" 2>/dev/null || true - launchctl unload "/Library/LaunchAgents/$svc.plist" 2>/dev/null || true - launchctl remove "$svc" 2>/dev/null || true - fi -done - -# Remove plist files -for svc in "${services[@]}"; do - safe_remove "/Library/LaunchDaemons/$svc.plist" "LaunchDaemon: $svc" - safe_remove "/Library/LaunchAgents/$svc.plist" "LaunchAgent: $svc" - safe_remove "$HOME/Library/LaunchAgents/$svc.plist" "User LaunchAgent: $svc" +# --- 3. Remove binary --- +echo "[3/5] Removing agent binary..." +for f in \ + /usr/local/bin/haproxy-agent \ + /opt/homebrew/bin/haproxy-agent \ + /usr/bin/haproxy-agent \ + /opt/homebrew/sbin/haproxy-agent \ + /usr/sbin/haproxy-agent; do + rm -f "$f" 2>/dev/null && echo " Removed $f" done +echo " Done" echo "" -# -------------------------------------------- -# 3. Remove agent binaries -# -------------------------------------------- -echo "[3/6] Removing agent binaries..." - -bin_paths=( - "/usr/local/bin/haproxy-agent" - "/opt/homebrew/bin/haproxy-agent" - "/usr/bin/haproxy-agent" - "/bin/haproxy-agent" - "/opt/homebrew/sbin/haproxy-agent" - "/usr/sbin/haproxy-agent" - "/sbin/haproxy-agent" -) - -for bp in "${bin_paths[@]}"; do - safe_remove "$bp" "binary: $bp" +# --- 4. Remove config, logs, PID files, temp files --- +echo "[4/5] Removing config, logs, and temp files..." +for d in \ + /etc/haproxy-agent \ + /var/log/haproxy-agent \ + /var/lib/haproxy-agent \ + /usr/local/etc/haproxy-agent \ + /opt/homebrew/etc/haproxy-agent \ + /usr/local/var/log/haproxy-agent \ + /opt/homebrew/var/log/haproxy-agent \ + /usr/local/share/haproxy-agent \ + /usr/local/share/haproxy-agent-backups \ + /opt/homebrew/share/haproxy-agent \ + /opt/homebrew/share/haproxy-agent-backups; do + rm -rf "$d" 2>/dev/null && echo " Removed $d" done +for f in \ + /var/run/haproxy-agent.pid \ + /tmp/haproxy-agent.pid \ + /usr/local/var/run/haproxy-agent.pid \ + /opt/homebrew/var/run/haproxy-agent.pid; do + rm -f "$f" 2>/dev/null && echo " Removed $f" +done +# Clean up agent temp files in /tmp +find /tmp -maxdepth 1 -name "haproxy-agent*" -exec rm -rf {} \; 2>/dev/null +find /tmp -maxdepth 1 -name "heartbeat_payload_*" -exec rm -f {} \; 2>/dev/null +find /tmp -maxdepth 1 -name "heartbeat_response_*" -exec rm -f {} \; 2>/dev/null +find /tmp -maxdepth 1 -name "haproxy-failed-*" -exec rm -f {} \; 2>/dev/null +find /tmp -maxdepth 1 -name "haproxy_new_*" -exec rm -f {} \; 2>/dev/null +find /tmp -maxdepth 1 -name "haproxy-merged*" -exec rm -f {} \; 2>/dev/null +find /tmp -maxdepth 1 -name "ssl_cert_*" -exec rm -f {} \; 2>/dev/null +rm -f /tmp/agent_debug.log /tmp/debug_agent.log /tmp/haproxy-new-config.cfg 2>/dev/null +echo " Done" +echo "" -# Deep search for any remaining binaries -found_extra=$(find /usr /opt /Applications -name "haproxy-agent" -type f 2>/dev/null || true) -if [[ -n "$found_extra" ]]; then - while IFS= read -r extra; do - safe_remove "$extra" "binary (deep search): $extra" - done <<< "$found_extra" +# --- 5. Verify --- +echo "[5/5] Verifying..." +ok=true +if pgrep -f "haproxy-agent" 2>/dev/null | grep -v "^${SELF_PID}$" | grep -v "^${PPID}$" | grep -q .; then + echo " WARNING: Agent processes still running" + ok=false fi -echo "" - -# -------------------------------------------- -# 4. Remove ALL agent configuration and data -# -------------------------------------------- -echo "[4/6] Removing agent configuration and data..." - -config_dirs=( - "/etc/haproxy-agent" - "/opt/homebrew/etc/haproxy-agent" - "/usr/local/etc/haproxy-agent" - "/var/lib/haproxy-agent" - "/usr/local/share/haproxy-agent" - "/usr/local/share/haproxy-agent-backups" - "/opt/homebrew/share/haproxy-agent" - "/opt/homebrew/share/haproxy-agent-backups" -) - -for cd in "${config_dirs[@]}"; do - safe_remove "$cd" "config dir: $cd" -done - -log_dirs=( - "/var/log/haproxy-agent" - "/opt/homebrew/var/log/haproxy-agent" - "/usr/local/var/log/haproxy-agent" - "/tmp/haproxy-agent-logs" -) - -for ld in "${log_dirs[@]}"; do - safe_remove "$ld" "log dir: $ld" -done - -pid_files=( - "/var/run/haproxy-agent.pid" - "/tmp/haproxy-agent.pid" - "/usr/local/var/run/haproxy-agent.pid" - "/opt/homebrew/var/run/haproxy-agent.pid" -) - -for pf in "${pid_files[@]}"; do - safe_remove "$pf" "PID file: $pf" -done -echo "" - -# -------------------------------------------- -# 5. Remove temporary files, markers, sockets -# -------------------------------------------- -echo "[5/6] Cleaning up temporary files and markers..." - -# Fixed-name temp files -fixed_temps=( - "/tmp/agent_debug.log" - "/tmp/debug_agent.log" - "/tmp/fixed_agent_test.log" - "/tmp/agent-startup-with-config.sh" - "/tmp/haproxy-new-config.cfg" -) - -for ft in "${fixed_temps[@]}"; do - safe_remove "$ft" "temp: $ft" -done - -# Wildcard patterns - files -wildcard_file_patterns=( - "haproxy-agent-*" - "daemon-test-agent*.sh" - "haproxy-failed-*.cfg" - "haproxy_new_*.cfg" - "haproxy-merged*.cfg" - "haproxy-global*.cfg" - "haproxy-defaults*.cfg" - "haproxy-listen*.cfg" - "haproxy-agent-upgrade-complete-*" - "haproxy-agent-upgrade-test-*" - "haproxy-agent-*.pid" - "haproxy-agent-ssl-sync-*" - "heartbeat_payload_*.json" - "heartbeat_response_*.txt" - "ssl_cert_*.pem" -) - -for wp in "${wildcard_file_patterns[@]}"; do - for search_dir in "/tmp" "/var/tmp"; do - if [[ -d "$search_dir" ]]; then - found=$(find "$search_dir" -maxdepth 1 -name "$wp" 2>/dev/null || true) - if [[ -n "$found" ]]; then - while IFS= read -r f; do - safe_remove "$f" "temp: $f" - done <<< "$found" - fi - fi - done -done - -# Wildcard patterns - sockets -for search_dir in "/tmp" "/var/run"; do - if [[ -d "$search_dir" ]]; then - found_sockets=$(find "$search_dir" -maxdepth 1 -name "haproxy-agent-*" -type s 2>/dev/null || true) - if [[ -n "$found_sockets" ]]; then - while IFS= read -r sock; do - safe_remove "$sock" "socket: $sock" - done <<< "$found_sockets" - fi - fi -done -echo "" - -# -------------------------------------------- -# 6. Verification -# -------------------------------------------- -echo "[6/6] Verifying cleanup..." - -remaining_procs=$(pgrep -f "haproxy-agent" 2>/dev/null | wc -l || echo "0") -remaining_svcs=$(launchctl list 2>/dev/null | grep -c "haproxy.agent\|haproxy-agent" || echo "0") -remaining_bins=$(find /usr /opt /Applications -name "haproxy-agent" -type f 2>/dev/null | wc -l || echo "0") -remaining_conf=$(test -d "/etc/haproxy-agent" && echo "1" || echo "0") - -echo "" -if [[ $remaining_procs -eq 0 && $remaining_svcs -eq 0 && $remaining_bins -eq 0 && $remaining_conf -eq 0 ]]; then - echo "CLEANUP VERIFIED - Agent completely removed." -else - echo "WARNING: Some agent remnants detected:" - [[ $remaining_procs -gt 0 ]] && echo " - $remaining_procs agent process(es) still running" - [[ $remaining_svcs -gt 0 ]] && echo " - $remaining_svcs agent service(s) still loaded" - [[ $remaining_bins -gt 0 ]] && echo " - $remaining_bins agent binary(ies) still found" - [[ $remaining_conf -gt 0 ]] && echo " - /etc/haproxy-agent/ config directory still exists" - - if [[ $remaining_bins -gt 0 ]]; then - echo " Remaining binaries:" - find /usr /opt /Applications -name "haproxy-agent" -type f 2>/dev/null | while read -r b; do - echo " - $b" - done - fi - - if [[ $remaining_procs -gt 0 ]]; then - echo " Remaining processes:" - pgrep -f "haproxy-agent" 2>/dev/null | while read -r pid; do - ps -p "$pid" -o pid,ppid,command 2>/dev/null || true - done - fi +if [[ -d /etc/haproxy-agent ]]; then + echo " WARNING: /etc/haproxy-agent still exists" + ok=false +fi +if [[ -f /usr/local/bin/haproxy-agent ]]; then + echo " WARNING: /usr/local/bin/haproxy-agent still exists" + ok=false +fi +if $ok; then + echo " Agent completely removed." fi echo "" echo "==========================================" -echo "HAProxy Agent Uninstall Complete" +echo "Done. HAProxy itself was not touched." echo "==========================================" -echo "" -echo "What was removed:" -echo " - All agent processes (including daemon mode)" -echo " - All launchd services (stopped and unloaded)" -echo " - All agent binaries" -echo " - All agent config files (including config.json)" -echo " - All agent logs" -echo " - All agent temp files, markers, and sockets" -echo "" -echo "What was NOT touched:" -echo " - HAProxy service" -echo " - HAProxy configuration" -echo " - HAProxy SSL certificates" -echo " - HAProxy logs" -echo "" -echo "System is ready for fresh agent installation." -echo "" -echo "Verify with:" -echo " ps aux | grep haproxy-agent" -echo " launchctl list | grep haproxy" -echo " ls -la /etc/haproxy-agent/" diff --git a/utils/uninstall-agent-linux.sh b/utils/uninstall-agent-linux.sh index 1714184..cdc1181 100755 --- a/utils/uninstall-agent-linux.sh +++ b/utils/uninstall-agent-linux.sh @@ -1,380 +1,127 @@ #!/bin/bash -# HAProxy Agent Uninstaller for Linux - Complete Agent Cleanup +# HAProxy Agent Uninstaller for Linux # Run with: sudo ./uninstall-agent-linux.sh -# Removes ALL agent components while keeping HAProxy intact -# -# IMPORTANT: 'set -e' intentionally NOT used here. -# safe_remove returns non-zero for missing files which would cause -# premature exit before cleaning up config, logs, and binaries. +# Removes agent service, binary, config, logs. HAProxy untouched. -echo "==========================================" -echo "HAProxy Agent Uninstaller for Linux v6.0" -echo "Complete Agent Cleanup" -echo "==========================================" - -# Check root permissions if [[ $EUID -ne 0 ]]; then - echo "ERROR: This script must be run as root" - echo " Please run: sudo $0" + echo "ERROR: Run as root: sudo $0" exit 1 fi +echo "==========================================" +echo "HAProxy Agent Uninstaller for Linux" +echo "==========================================" echo "" -echo "Starting complete agent cleanup (HAProxy untouched)..." -echo "" - -# -------------------------------------------- -# Helper: safely remove a file or directory -# Always returns 0 so the script never exits early -# -------------------------------------------- -safe_remove() { - local path="$1" - local desc="$2" - - if [[ -e "$path" ]]; then - rm -rf "$path" 2>/dev/null || true - if [[ ! -e "$path" ]]; then - echo " [REMOVED] $desc" - else - echo " [WARN] Failed to remove $desc" - fi - else - echo " [SKIP] $desc (not found)" - fi - return 0 -} - -# -------------------------------------------- -# 1. Terminate all agent processes -# -------------------------------------------- -echo "[1/7] Terminating agent processes..." - -killed_count=0 -patterns=( - "haproxy-agent" - "/usr/local/bin/haproxy-agent" - "/usr/bin/haproxy-agent" - "haproxy-agent.service" - "haproxy-agent daemon" - "bash.*haproxy-agent" - "nohup.*haproxy-agent" -) SELF_PID=$$ -for pattern in "${patterns[@]}"; do - pids=$(pgrep -f "$pattern" 2>/dev/null || true) - if [[ -n "$pids" ]]; then - # Filter out our own PID and parent PID to avoid killing ourselves - filtered_pids="" - for p in $pids; do - if [[ "$p" != "$SELF_PID" && "$p" != "$PPID" ]]; then - filtered_pids="$filtered_pids $p" - fi - done - filtered_pids=$(echo "$filtered_pids" | xargs) - if [[ -n "$filtered_pids" ]]; then - echo " Killing processes matching: $pattern (PIDs: $filtered_pids)" - kill -TERM $filtered_pids 2>/dev/null || true - sleep 2 - # Force kill if still running - remaining=$(pgrep -f "$pattern" 2>/dev/null || true) - if [[ -n "$remaining" ]]; then - rem_filtered="" - for p in $remaining; do - if [[ "$p" != "$SELF_PID" && "$p" != "$PPID" ]]; then - rem_filtered="$rem_filtered $p" - fi - done - rem_filtered=$(echo "$rem_filtered" | xargs) - if [[ -n "$rem_filtered" ]]; then - echo " Force killing remaining: $rem_filtered" - kill -KILL $rem_filtered 2>/dev/null || true - sleep 1 - fi - fi - killed_count=$((killed_count + $(echo "$filtered_pids" | wc -w))) - fi - fi +# --- 1. Stop and remove systemd service --- +echo "[1/5] Stopping systemd service..." +for svc in haproxy-agent.service haproxy.agent.service; do + systemctl stop "$svc" 2>/dev/null + systemctl disable "$svc" 2>/dev/null + systemctl unmask "$svc" 2>/dev/null done +for f in \ + /etc/systemd/system/haproxy-agent.service \ + /etc/systemd/system/haproxy.agent.service \ + /lib/systemd/system/haproxy-agent.service \ + /usr/lib/systemd/system/haproxy-agent.service \ + /etc/systemd/user/haproxy-agent.service; do + rm -f "$f" 2>/dev/null && echo " Removed $f" +done +systemctl daemon-reload 2>/dev/null +systemctl reset-failed 2>/dev/null +echo " Done" +echo "" -# Final sweep (exclude self) -final_pids=$(pgrep -f "haproxy-agent" 2>/dev/null || true) -if [[ -n "$final_pids" ]]; then - final_filtered="" - for p in $final_pids; do +# --- 2. Kill agent processes (exclude self) --- +echo "[2/5] Killing agent processes..." +agent_pids=$(pgrep -f "haproxy-agent" 2>/dev/null || true) +if [[ -n "$agent_pids" ]]; then + for p in $agent_pids; do if [[ "$p" != "$SELF_PID" && "$p" != "$PPID" ]]; then - final_filtered="$final_filtered $p" + kill -9 "$p" 2>/dev/null && echo " Killed PID $p" fi done - final_filtered=$(echo "$final_filtered" | xargs) - if [[ -n "$final_filtered" ]]; then - echo " Final cleanup: force killing PIDs $final_filtered" - kill -KILL $final_filtered 2>/dev/null || true - fi -fi - -if [[ $killed_count -gt 0 ]]; then - echo " Terminated $killed_count agent process(es)" + sleep 1 else echo " No agent processes found" fi echo "" -# -------------------------------------------- -# 2. Stop and remove systemd services -# -------------------------------------------- -echo "[2/7] Cleaning up systemd services..." - -services=( - "haproxy-agent.service" - "haproxy.agent.service" -) - -for svc in "${services[@]}"; do - if systemctl is-active "$svc" &>/dev/null; then - echo " Stopping: $svc" - systemctl stop "$svc" 2>/dev/null || true - fi - if systemctl is-enabled "$svc" &>/dev/null; then - echo " Disabling: $svc" - systemctl disable "$svc" 2>/dev/null || true - fi - # Unmask in case it was masked previously, then mask to prevent reactivation - systemctl unmask "$svc" 2>/dev/null || true - systemctl mask "$svc" 2>/dev/null || true +# --- 3. Remove binary --- +echo "[3/5] Removing agent binary..." +for f in \ + /usr/local/bin/haproxy-agent \ + /usr/bin/haproxy-agent \ + /usr/sbin/haproxy-agent \ + /sbin/haproxy-agent \ + /opt/bin/haproxy-agent; do + rm -f "$f" 2>/dev/null && echo " Removed $f" done - -# Remove service unit files -service_files=( - "/etc/systemd/system/haproxy-agent.service" - "/etc/systemd/system/haproxy.agent.service" - "/lib/systemd/system/haproxy-agent.service" - "/usr/lib/systemd/system/haproxy-agent.service" - "/etc/systemd/user/haproxy-agent.service" -) - -for sf in "${service_files[@]}"; do - safe_remove "$sf" "systemd unit: $sf" -done - -# Reload systemd -systemctl daemon-reload 2>/dev/null || true -systemctl reset-failed 2>/dev/null || true -echo " systemd daemon reloaded" +echo " Done" echo "" -# -------------------------------------------- -# 3. Remove agent binaries -# -------------------------------------------- -echo "[3/7] Removing agent binaries..." - -bin_paths=( - "/usr/local/bin/haproxy-agent" - "/usr/bin/haproxy-agent" - "/bin/haproxy-agent" - "/usr/sbin/haproxy-agent" - "/sbin/haproxy-agent" - "/opt/bin/haproxy-agent" - "/usr/local/sbin/haproxy-agent" -) - -for bp in "${bin_paths[@]}"; do - safe_remove "$bp" "binary: $bp" +# --- 4. Remove config, logs, PID files, temp files --- +echo "[4/5] Removing config, logs, and temp files..." +for d in \ + /etc/haproxy-agent \ + /var/log/haproxy-agent \ + /var/lib/haproxy-agent \ + /usr/local/etc/haproxy-agent \ + /usr/local/share/haproxy-agent \ + /usr/local/share/haproxy-agent-backups \ + /opt/haproxy-agent; do + rm -rf "$d" 2>/dev/null && echo " Removed $d" done - -# Deep search for any remaining binaries -found_extra=$(find /usr /opt /bin /sbin -name "haproxy-agent" -type f 2>/dev/null || true) -if [[ -n "$found_extra" ]]; then - while IFS= read -r extra; do - safe_remove "$extra" "binary (deep search): $extra" - done <<< "$found_extra" +for f in \ + /var/run/haproxy-agent.pid \ + /run/haproxy-agent.pid \ + /tmp/haproxy-agent.pid; do + rm -f "$f" 2>/dev/null && echo " Removed $f" +done +# Clean up agent temp files in /tmp +find /tmp -maxdepth 1 -name "haproxy-agent*" -exec rm -rf {} \; 2>/dev/null +find /tmp -maxdepth 1 -name "heartbeat_payload_*" -exec rm -f {} \; 2>/dev/null +find /tmp -maxdepth 1 -name "heartbeat_response_*" -exec rm -f {} \; 2>/dev/null +find /tmp -maxdepth 1 -name "haproxy-failed-*" -exec rm -f {} \; 2>/dev/null +find /tmp -maxdepth 1 -name "haproxy_new_*" -exec rm -f {} \; 2>/dev/null +find /tmp -maxdepth 1 -name "haproxy-merged*" -exec rm -f {} \; 2>/dev/null +find /tmp -maxdepth 1 -name "ssl_cert_*" -exec rm -f {} \; 2>/dev/null +rm -f /tmp/agent_debug.log /tmp/debug_agent.log /tmp/haproxy-new-config.cfg 2>/dev/null +# Clean cron +if grep -q "haproxy-agent" /etc/cron.d/haproxy-agent 2>/dev/null; then + rm -f /etc/cron.d/haproxy-agent 2>/dev/null && echo " Removed cron job" fi -echo "" - -# -------------------------------------------- -# 4. Remove ALL agent configuration and data -# -------------------------------------------- -echo "[4/7] Removing agent configuration and data..." - -config_dirs=( - "/etc/haproxy-agent" - "/usr/local/etc/haproxy-agent" - "/var/lib/haproxy-agent" - "/opt/haproxy-agent" - "/usr/local/share/haproxy-agent" - "/usr/local/share/haproxy-agent-backups" -) - -for cd in "${config_dirs[@]}"; do - safe_remove "$cd" "config dir: $cd" -done - -log_dirs=( - "/var/log/haproxy-agent" - "/usr/local/var/log/haproxy-agent" - "/tmp/haproxy-agent-logs" - "/var/log/syslog.d/haproxy-agent" -) - -for ld in "${log_dirs[@]}"; do - safe_remove "$ld" "log dir: $ld" -done - -pid_files=( - "/var/run/haproxy-agent.pid" - "/tmp/haproxy-agent.pid" - "/usr/local/var/run/haproxy-agent.pid" - "/run/haproxy-agent.pid" -) - -for pf in "${pid_files[@]}"; do - safe_remove "$pf" "PID file: $pf" -done -echo "" - -# -------------------------------------------- -# 5. Remove temporary files, markers, sockets -# -------------------------------------------- -echo "[5/7] Cleaning up temporary files and markers..." - -# Fixed-name temp files -fixed_temps=( - "/tmp/agent_debug.log" - "/tmp/debug_agent.log" - "/tmp/fixed_agent_test.log" - "/tmp/agent-startup-with-config.sh" - "/tmp/haproxy-new-config.cfg" -) - -for ft in "${fixed_temps[@]}"; do - safe_remove "$ft" "temp: $ft" -done - -# Wildcard patterns - files -wildcard_file_patterns=( - "haproxy-agent-*" - "daemon-test-agent*.sh" - "haproxy-failed-*.cfg" - "haproxy_new_*.cfg" - "haproxy-merged*.cfg" - "haproxy-global*.cfg" - "haproxy-defaults*.cfg" - "haproxy-listen*.cfg" - "haproxy-agent-upgrade-complete-*" - "haproxy-agent-upgrade-test-*" - "haproxy-agent-*.pid" - "haproxy-agent-ssl-sync-*" - "heartbeat_payload_*.json" - "heartbeat_response_*.txt" - "ssl_cert_*.pem" -) - -for wp in "${wildcard_file_patterns[@]}"; do - for search_dir in "/tmp" "/var/tmp"; do - if [[ -d "$search_dir" ]]; then - found=$(find "$search_dir" -maxdepth 1 -name "$wp" 2>/dev/null || true) - if [[ -n "$found" ]]; then - while IFS= read -r f; do - safe_remove "$f" "temp: $f" - done <<< "$found" - fi - fi - done -done - -# Wildcard patterns - sockets -for search_dir in "/tmp" "/var/run" "/run"; do - if [[ -d "$search_dir" ]]; then - found_sockets=$(find "$search_dir" -maxdepth 1 -name "haproxy-agent-*" -type s 2>/dev/null || true) - if [[ -n "$found_sockets" ]]; then - while IFS= read -r sock; do - safe_remove "$sock" "socket: $sock" - done <<< "$found_sockets" - fi - fi -done -echo "" - -# -------------------------------------------- -# 6. Clean up cron jobs -# -------------------------------------------- -echo "[6/7] Cleaning up cron jobs..." - -cron_files=( - "/etc/cron.d/haproxy-agent" - "/etc/crontab" - "/var/spool/cron/root" - "/var/spool/cron/crontabs/root" -) - -for cf in "${cron_files[@]}"; do +for cf in /etc/crontab /var/spool/cron/root /var/spool/cron/crontabs/root; do if [[ -f "$cf" ]] && grep -q "haproxy-agent" "$cf" 2>/dev/null; then - cp "$cf" "${cf}.backup.$(date +%Y%m%d_%H%M%S)" 2>/dev/null || true - sed -i '/haproxy-agent/d' "$cf" 2>/dev/null || true - echo " [CLEANED] Removed haproxy-agent entries from $cf" + sed -i '/haproxy-agent/d' "$cf" 2>/dev/null && echo " Cleaned $cf" fi done +echo " Done" echo "" -# -------------------------------------------- -# 7. Verification -# -------------------------------------------- -echo "[7/7] Verifying cleanup..." - -remaining_procs=$(pgrep -f "haproxy-agent" 2>/dev/null | wc -l || echo "0") -remaining_svcs=$(systemctl list-units --all 2>/dev/null | grep -c "haproxy-agent" || echo "0") -remaining_bins=$(find /usr /opt /bin /sbin -name "haproxy-agent" -type f 2>/dev/null | wc -l || echo "0") -remaining_conf=$(test -d "/etc/haproxy-agent" && echo "1" || echo "0") - -echo "" -if [[ $remaining_procs -eq 0 && $remaining_svcs -eq 0 && $remaining_bins -eq 0 && $remaining_conf -eq 0 ]]; then - echo "CLEANUP VERIFIED - Agent completely removed." -else - echo "WARNING: Some agent remnants detected:" - [[ $remaining_procs -gt 0 ]] && echo " - $remaining_procs agent process(es) still running" - [[ $remaining_svcs -gt 0 ]] && echo " - $remaining_svcs agent service(s) still loaded" - [[ $remaining_bins -gt 0 ]] && echo " - $remaining_bins agent binary(ies) still found" - [[ $remaining_conf -gt 0 ]] && echo " - /etc/haproxy-agent/ config directory still exists" - - if [[ $remaining_bins -gt 0 ]]; then - echo " Remaining binaries:" - find /usr /opt /bin /sbin -name "haproxy-agent" -type f 2>/dev/null | while read -r b; do - echo " - $b" - done - fi - - if [[ $remaining_procs -gt 0 ]]; then - echo " Remaining processes:" - pgrep -f "haproxy-agent" 2>/dev/null | while read -r pid; do - ps -p "$pid" -o pid,ppid,command 2>/dev/null || true - done - fi +# --- 5. Verify --- +echo "[5/5] Verifying..." +ok=true +if pgrep -f "haproxy-agent" 2>/dev/null | grep -v "^${SELF_PID}$" | grep -v "^${PPID}$" | grep -q .; then + echo " WARNING: Agent processes still running" + ok=false +fi +if [[ -d /etc/haproxy-agent ]]; then + echo " WARNING: /etc/haproxy-agent still exists" + ok=false +fi +if [[ -f /usr/local/bin/haproxy-agent ]]; then + echo " WARNING: /usr/local/bin/haproxy-agent still exists" + ok=false +fi +if $ok; then + echo " Agent completely removed." fi echo "" echo "==========================================" -echo "HAProxy Agent Uninstall Complete" +echo "Done. HAProxy itself was not touched." echo "==========================================" -echo "" -echo "What was removed:" -echo " - All agent processes (including daemon mode)" -echo " - All systemd services (stopped, disabled, masked)" -echo " - All agent binaries" -echo " - All agent config files (including config.json)" -echo " - All agent logs" -echo " - All agent temp files, markers, and sockets" -echo " - Agent-related cron entries" -echo "" -echo "What was NOT touched:" -echo " - HAProxy service" -echo " - HAProxy configuration (/etc/haproxy/haproxy.cfg)" -echo " - HAProxy SSL certificates" -echo " - HAProxy logs" -echo "" -echo "System is ready for fresh agent installation." -echo "" -echo "Verify with:" -echo " ps aux | grep haproxy-agent" -echo " systemctl status haproxy-agent" -echo " ls -la /etc/haproxy-agent/" diff --git a/utils/uninstall-agent-macos.sh b/utils/uninstall-agent-macos.sh index 2538277..20a9e34 100755 --- a/utils/uninstall-agent-macos.sh +++ b/utils/uninstall-agent-macos.sh @@ -1,347 +1,122 @@ #!/bin/bash -# HAProxy Agent Uninstaller for macOS - Complete Agent Cleanup +# HAProxy Agent Uninstaller for macOS # Run with: sudo ./uninstall-agent-macos.sh -# Removes ALL agent components while keeping HAProxy intact -# -# IMPORTANT: 'set -e' intentionally NOT used here. -# safe_remove returns non-zero for missing files which would cause -# premature exit before cleaning up config, logs, and binaries. +# Removes agent service, binary, config, logs. HAProxy untouched. -echo "==========================================" -echo "HAProxy Agent Uninstaller for macOS v6.0" -echo "Complete Agent Cleanup" -echo "==========================================" - -# Check root permissions if [[ $EUID -ne 0 ]]; then - echo "ERROR: This script must be run as root" - echo " Please run: sudo $0" + echo "ERROR: Run as root: sudo $0" exit 1 fi +echo "==========================================" +echo "HAProxy Agent Uninstaller for macOS" +echo "==========================================" echo "" -echo "Starting complete agent cleanup (HAProxy untouched)..." -echo "" - -# -------------------------------------------- -# Helper: safely remove a file or directory -# Always returns 0 so the script never exits early -# -------------------------------------------- -safe_remove() { - local path="$1" - local desc="$2" - - if [[ -e "$path" ]]; then - rm -rf "$path" 2>/dev/null || true - if [[ ! -e "$path" ]]; then - echo " [REMOVED] $desc" - else - echo " [WARN] Failed to remove $desc" - fi - else - echo " [SKIP] $desc (not found)" - fi - return 0 -} - -# -------------------------------------------- -# 1. Terminate all agent processes -# -------------------------------------------- -echo "[1/6] Terminating agent processes..." - -killed_count=0 -patterns=( - "haproxy-agent" - "/usr/local/bin/haproxy-agent" - "/opt/homebrew/bin/haproxy-agent" - "com.haproxy.agent" - "haproxy-agent daemon" - "bash.*haproxy-agent" - "nohup.*haproxy-agent" -) SELF_PID=$$ -for pattern in "${patterns[@]}"; do - pids=$(pgrep -f "$pattern" 2>/dev/null || true) - if [[ -n "$pids" ]]; then - # Filter out our own PID and parent PID to avoid killing ourselves - filtered_pids="" - for p in $pids; do - if [[ "$p" != "$SELF_PID" && "$p" != "$PPID" ]]; then - filtered_pids="$filtered_pids $p" - fi - done - filtered_pids=$(echo "$filtered_pids" | xargs) - if [[ -n "$filtered_pids" ]]; then - echo " Killing processes matching: $pattern (PIDs: $filtered_pids)" - kill -TERM $filtered_pids 2>/dev/null || true - sleep 2 - # Force kill if still running - remaining=$(pgrep -f "$pattern" 2>/dev/null || true) - if [[ -n "$remaining" ]]; then - rem_filtered="" - for p in $remaining; do - if [[ "$p" != "$SELF_PID" && "$p" != "$PPID" ]]; then - rem_filtered="$rem_filtered $p" - fi - done - rem_filtered=$(echo "$rem_filtered" | xargs) - if [[ -n "$rem_filtered" ]]; then - echo " Force killing remaining: $rem_filtered" - kill -KILL $rem_filtered 2>/dev/null || true - sleep 1 - fi - fi - killed_count=$((killed_count + $(echo "$filtered_pids" | wc -w))) - fi - fi +# --- 1. Stop and remove launchd service --- +echo "[1/5] Stopping launchd service..." +for svc in com.haproxy.agent haproxy.agent haproxy-agent com.haproxy-agent; do + launchctl stop "$svc" 2>/dev/null + launchctl unload "/Library/LaunchDaemons/$svc.plist" 2>/dev/null + launchctl unload "/Library/LaunchAgents/$svc.plist" 2>/dev/null + launchctl remove "$svc" 2>/dev/null done +for svc in com.haproxy.agent haproxy.agent haproxy-agent com.haproxy-agent; do + for f in \ + "/Library/LaunchDaemons/$svc.plist" \ + "/Library/LaunchAgents/$svc.plist" \ + "$HOME/Library/LaunchAgents/$svc.plist"; do + rm -f "$f" 2>/dev/null && echo " Removed $f" + done +done +echo " Done" +echo "" -# Final sweep (exclude self) -final_pids=$(pgrep -f "haproxy-agent" 2>/dev/null || true) -if [[ -n "$final_pids" ]]; then - final_filtered="" - for p in $final_pids; do +# --- 2. Kill agent processes (exclude self) --- +echo "[2/5] Killing agent processes..." +agent_pids=$(pgrep -f "haproxy-agent" 2>/dev/null || true) +if [[ -n "$agent_pids" ]]; then + for p in $agent_pids; do if [[ "$p" != "$SELF_PID" && "$p" != "$PPID" ]]; then - final_filtered="$final_filtered $p" + kill -9 "$p" 2>/dev/null && echo " Killed PID $p" fi done - final_filtered=$(echo "$final_filtered" | xargs) - if [[ -n "$final_filtered" ]]; then - echo " Final cleanup: force killing PIDs $final_filtered" - kill -KILL $final_filtered 2>/dev/null || true - fi -fi - -if [[ $killed_count -gt 0 ]]; then - echo " Terminated $killed_count agent process(es)" + sleep 1 else echo " No agent processes found" fi echo "" -# -------------------------------------------- -# 2. Stop and remove launchd services -# -------------------------------------------- -echo "[2/6] Cleaning up launchd services..." - -services=( - "com.haproxy.agent" - "haproxy.agent" - "haproxy-agent" - "com.haproxy-agent" -) - -for svc in "${services[@]}"; do - if launchctl list 2>/dev/null | grep -q "$svc"; then - echo " Stopping and removing: $svc" - launchctl stop "$svc" 2>/dev/null || true - launchctl unload "/Library/LaunchDaemons/$svc.plist" 2>/dev/null || true - launchctl unload "/Library/LaunchAgents/$svc.plist" 2>/dev/null || true - launchctl remove "$svc" 2>/dev/null || true - fi -done - -# Remove plist files -for svc in "${services[@]}"; do - safe_remove "/Library/LaunchDaemons/$svc.plist" "LaunchDaemon: $svc" - safe_remove "/Library/LaunchAgents/$svc.plist" "LaunchAgent: $svc" - safe_remove "$HOME/Library/LaunchAgents/$svc.plist" "User LaunchAgent: $svc" +# --- 3. Remove binary --- +echo "[3/5] Removing agent binary..." +for f in \ + /usr/local/bin/haproxy-agent \ + /opt/homebrew/bin/haproxy-agent \ + /usr/bin/haproxy-agent \ + /opt/homebrew/sbin/haproxy-agent \ + /usr/sbin/haproxy-agent; do + rm -f "$f" 2>/dev/null && echo " Removed $f" done +echo " Done" echo "" -# -------------------------------------------- -# 3. Remove agent binaries -# -------------------------------------------- -echo "[3/6] Removing agent binaries..." - -bin_paths=( - "/usr/local/bin/haproxy-agent" - "/opt/homebrew/bin/haproxy-agent" - "/usr/bin/haproxy-agent" - "/bin/haproxy-agent" - "/opt/homebrew/sbin/haproxy-agent" - "/usr/sbin/haproxy-agent" - "/sbin/haproxy-agent" -) - -for bp in "${bin_paths[@]}"; do - safe_remove "$bp" "binary: $bp" +# --- 4. Remove config, logs, PID files, temp files --- +echo "[4/5] Removing config, logs, and temp files..." +for d in \ + /etc/haproxy-agent \ + /var/log/haproxy-agent \ + /var/lib/haproxy-agent \ + /usr/local/etc/haproxy-agent \ + /opt/homebrew/etc/haproxy-agent \ + /usr/local/var/log/haproxy-agent \ + /opt/homebrew/var/log/haproxy-agent \ + /usr/local/share/haproxy-agent \ + /usr/local/share/haproxy-agent-backups \ + /opt/homebrew/share/haproxy-agent \ + /opt/homebrew/share/haproxy-agent-backups; do + rm -rf "$d" 2>/dev/null && echo " Removed $d" done +for f in \ + /var/run/haproxy-agent.pid \ + /tmp/haproxy-agent.pid \ + /usr/local/var/run/haproxy-agent.pid \ + /opt/homebrew/var/run/haproxy-agent.pid; do + rm -f "$f" 2>/dev/null && echo " Removed $f" +done +# Clean up agent temp files in /tmp +find /tmp -maxdepth 1 -name "haproxy-agent*" -exec rm -rf {} \; 2>/dev/null +find /tmp -maxdepth 1 -name "heartbeat_payload_*" -exec rm -f {} \; 2>/dev/null +find /tmp -maxdepth 1 -name "heartbeat_response_*" -exec rm -f {} \; 2>/dev/null +find /tmp -maxdepth 1 -name "haproxy-failed-*" -exec rm -f {} \; 2>/dev/null +find /tmp -maxdepth 1 -name "haproxy_new_*" -exec rm -f {} \; 2>/dev/null +find /tmp -maxdepth 1 -name "haproxy-merged*" -exec rm -f {} \; 2>/dev/null +find /tmp -maxdepth 1 -name "ssl_cert_*" -exec rm -f {} \; 2>/dev/null +rm -f /tmp/agent_debug.log /tmp/debug_agent.log /tmp/haproxy-new-config.cfg 2>/dev/null +echo " Done" +echo "" -# Deep search for any remaining binaries -found_extra=$(find /usr /opt /Applications -name "haproxy-agent" -type f 2>/dev/null || true) -if [[ -n "$found_extra" ]]; then - while IFS= read -r extra; do - safe_remove "$extra" "binary (deep search): $extra" - done <<< "$found_extra" +# --- 5. Verify --- +echo "[5/5] Verifying..." +ok=true +if pgrep -f "haproxy-agent" 2>/dev/null | grep -v "^${SELF_PID}$" | grep -v "^${PPID}$" | grep -q .; then + echo " WARNING: Agent processes still running" + ok=false fi -echo "" - -# -------------------------------------------- -# 4. Remove ALL agent configuration and data -# -------------------------------------------- -echo "[4/6] Removing agent configuration and data..." - -config_dirs=( - "/etc/haproxy-agent" - "/opt/homebrew/etc/haproxy-agent" - "/usr/local/etc/haproxy-agent" - "/var/lib/haproxy-agent" - "/usr/local/share/haproxy-agent" - "/usr/local/share/haproxy-agent-backups" - "/opt/homebrew/share/haproxy-agent" - "/opt/homebrew/share/haproxy-agent-backups" -) - -for cd in "${config_dirs[@]}"; do - safe_remove "$cd" "config dir: $cd" -done - -log_dirs=( - "/var/log/haproxy-agent" - "/opt/homebrew/var/log/haproxy-agent" - "/usr/local/var/log/haproxy-agent" - "/tmp/haproxy-agent-logs" -) - -for ld in "${log_dirs[@]}"; do - safe_remove "$ld" "log dir: $ld" -done - -pid_files=( - "/var/run/haproxy-agent.pid" - "/tmp/haproxy-agent.pid" - "/usr/local/var/run/haproxy-agent.pid" - "/opt/homebrew/var/run/haproxy-agent.pid" -) - -for pf in "${pid_files[@]}"; do - safe_remove "$pf" "PID file: $pf" -done -echo "" - -# -------------------------------------------- -# 5. Remove temporary files, markers, sockets -# -------------------------------------------- -echo "[5/6] Cleaning up temporary files and markers..." - -# Fixed-name temp files -fixed_temps=( - "/tmp/agent_debug.log" - "/tmp/debug_agent.log" - "/tmp/fixed_agent_test.log" - "/tmp/agent-startup-with-config.sh" - "/tmp/haproxy-new-config.cfg" -) - -for ft in "${fixed_temps[@]}"; do - safe_remove "$ft" "temp: $ft" -done - -# Wildcard patterns - files -wildcard_file_patterns=( - "haproxy-agent-*" - "daemon-test-agent*.sh" - "haproxy-failed-*.cfg" - "haproxy_new_*.cfg" - "haproxy-merged*.cfg" - "haproxy-global*.cfg" - "haproxy-defaults*.cfg" - "haproxy-listen*.cfg" - "haproxy-agent-upgrade-complete-*" - "haproxy-agent-upgrade-test-*" - "haproxy-agent-*.pid" - "haproxy-agent-ssl-sync-*" - "heartbeat_payload_*.json" - "heartbeat_response_*.txt" - "ssl_cert_*.pem" -) - -for wp in "${wildcard_file_patterns[@]}"; do - for search_dir in "/tmp" "/var/tmp"; do - if [[ -d "$search_dir" ]]; then - found=$(find "$search_dir" -maxdepth 1 -name "$wp" 2>/dev/null || true) - if [[ -n "$found" ]]; then - while IFS= read -r f; do - safe_remove "$f" "temp: $f" - done <<< "$found" - fi - fi - done -done - -# Wildcard patterns - sockets -for search_dir in "/tmp" "/var/run"; do - if [[ -d "$search_dir" ]]; then - found_sockets=$(find "$search_dir" -maxdepth 1 -name "haproxy-agent-*" -type s 2>/dev/null || true) - if [[ -n "$found_sockets" ]]; then - while IFS= read -r sock; do - safe_remove "$sock" "socket: $sock" - done <<< "$found_sockets" - fi - fi -done -echo "" - -# -------------------------------------------- -# 6. Verification -# -------------------------------------------- -echo "[6/6] Verifying cleanup..." - -remaining_procs=$(pgrep -f "haproxy-agent" 2>/dev/null | wc -l || echo "0") -remaining_svcs=$(launchctl list 2>/dev/null | grep -c "haproxy.agent\|haproxy-agent" || echo "0") -remaining_bins=$(find /usr /opt /Applications -name "haproxy-agent" -type f 2>/dev/null | wc -l || echo "0") -remaining_conf=$(test -d "/etc/haproxy-agent" && echo "1" || echo "0") - -echo "" -if [[ $remaining_procs -eq 0 && $remaining_svcs -eq 0 && $remaining_bins -eq 0 && $remaining_conf -eq 0 ]]; then - echo "CLEANUP VERIFIED - Agent completely removed." -else - echo "WARNING: Some agent remnants detected:" - [[ $remaining_procs -gt 0 ]] && echo " - $remaining_procs agent process(es) still running" - [[ $remaining_svcs -gt 0 ]] && echo " - $remaining_svcs agent service(s) still loaded" - [[ $remaining_bins -gt 0 ]] && echo " - $remaining_bins agent binary(ies) still found" - [[ $remaining_conf -gt 0 ]] && echo " - /etc/haproxy-agent/ config directory still exists" - - if [[ $remaining_bins -gt 0 ]]; then - echo " Remaining binaries:" - find /usr /opt /Applications -name "haproxy-agent" -type f 2>/dev/null | while read -r b; do - echo " - $b" - done - fi - - if [[ $remaining_procs -gt 0 ]]; then - echo " Remaining processes:" - pgrep -f "haproxy-agent" 2>/dev/null | while read -r pid; do - ps -p "$pid" -o pid,ppid,command 2>/dev/null || true - done - fi +if [[ -d /etc/haproxy-agent ]]; then + echo " WARNING: /etc/haproxy-agent still exists" + ok=false +fi +if [[ -f /usr/local/bin/haproxy-agent ]]; then + echo " WARNING: /usr/local/bin/haproxy-agent still exists" + ok=false +fi +if $ok; then + echo " Agent completely removed." fi echo "" echo "==========================================" -echo "HAProxy Agent Uninstall Complete" +echo "Done. HAProxy itself was not touched." echo "==========================================" -echo "" -echo "What was removed:" -echo " - All agent processes (including daemon mode)" -echo " - All launchd services (stopped and unloaded)" -echo " - All agent binaries" -echo " - All agent config files (including config.json)" -echo " - All agent logs" -echo " - All agent temp files, markers, and sockets" -echo "" -echo "What was NOT touched:" -echo " - HAProxy service" -echo " - HAProxy configuration" -echo " - HAProxy SSL certificates" -echo " - HAProxy logs" -echo "" -echo "System is ready for fresh agent installation." -echo "" -echo "Verify with:" -echo " ps aux | grep haproxy-agent" -echo " launchctl list | grep haproxy" -echo " ls -la /etc/haproxy-agent/"