fix: remove emojis from all agent scripts and fix install/uninstall bugs

- Remove all emoji characters from install scripts (linux, macos)
- Rewrite uninstall scripts: remove set -e, fix safe_remove to always
  return 0, replace ((var++)) with $((var + 1)), fix local keyword
  usage, add comprehensive cleanup including config.json
- Fix install script QUIET_MODE/DAEMON_MODE detection to correctly
  handle interactive runs when old config.json exists

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
taylanbakircioglu
2026-02-16 13:58:42 +03:00
parent 6349f1ad7c
commit 5cec6cf85a
6 changed files with 1217 additions and 1427 deletions
+37 -20
View File
@@ -13,8 +13,14 @@ HAPROXY_CONFIG_PATH="{{HAPROXY_CONFIG_PATH}}"
# Quick daemon mode detection (before installer UI)
# Suppress installer messages if running in daemon mode
# CRITICAL FIX: Only check config.json for daemon mode when NOT running interactively
# Previous bug: config.json from old install caused script to silently enter daemon mode
# when user ran ./install-agent.sh manually, producing no output at all
QUIET_MODE=false
if [[ "$1" == "daemon" || "$SKIP_TO_DAEMON" == "true" || -f "/etc/haproxy-agent/config.json" ]]; then
if [[ "$1" == "daemon" || "$SKIP_TO_DAEMON" == "true" ]]; then
QUIET_MODE=true
elif [[ -f "/etc/haproxy-agent/config.json" ]] && [[ ! -t 0 ]]; then
# Config exists AND not running from terminal (systemd/non-interactive)
QUIET_MODE=true
fi
@@ -26,7 +32,7 @@ if [[ "$QUIET_MODE" != "true" ]]; then
echo "With Version Management & Auto-Upgrade"
echo "=========================================="
echo ""
echo "⚠️ IMPORTANT WARNING ⚠️"
echo "IMPORTANT WARNING"
echo "=========================================="
echo ""
echo "After agent installation, ALL your existing frontend and backend"
@@ -35,11 +41,11 @@ if [[ "$QUIET_MODE" != "true" ]]; then
echo ""
echo "An automatic backup will be created: haproxy.cfg.initial-backup"
echo ""
echo "📋 REQUIRED STEPS AFTER INSTALLATION:"
echo "REQUIRED STEPS AFTER INSTALLATION:"
echo " 1. Login to HAProxy OpenManager interface"
echo " 2. Use the BULK IMPORT feature to restore your configuration"
echo ""
echo "⚠️ IMPORTANT: After installation, ALL configuration changes MUST be"
echo "IMPORTANT: After installation, ALL configuration changes MUST be"
echo " made through the HAProxy OpenManager interface only."
echo " Manual changes will be overwritten on the next update."
echo ""
@@ -50,8 +56,8 @@ if [[ "$QUIET_MODE" != "true" ]]; then
if [[ -f "$HAPROXY_CONFIG_PATH" ]]; then
BACKUP_PATH="${HAPROXY_CONFIG_PATH}.initial-backup"
if [[ ! -f "$BACKUP_PATH" ]]; then
echo "📦 Creating backup: $BACKUP_PATH"
cp "$HAPROXY_CONFIG_PATH" "$BACKUP_PATH" && echo "Backup created successfully" || echo "⚠️ Backup failed"
echo "Creating backup: $BACKUP_PATH"
cp "$HAPROXY_CONFIG_PATH" "$BACKUP_PATH" && echo "Backup created successfully" || echo "WARNING: Backup failed"
echo ""
fi
fi
@@ -229,9 +235,20 @@ if [[ "$SKIP_TO_DAEMON" == "true" ]]; then
fi
# Method 3: Config file exists (upgrade scenario)
if [[ -f "/etc/haproxy-agent/config.json" ]]; then
log "INFO" "DAEMON: Existing config file detected"
# CRITICAL FIX: Only enter daemon mode from config.json when NOT running interactively
# If user runs ./install-agent.sh from terminal with old config.json present,
# we should show the installer, not silently enter daemon mode
if [[ -f "/etc/haproxy-agent/config.json" ]] && [[ ! -t 0 ]]; then
log "INFO" "DAEMON: Existing config file detected (non-interactive mode)"
DAEMON_MODE=true
elif [[ -f "/etc/haproxy-agent/config.json" ]] && [[ -t 0 ]]; then
log "INFO" "INSTALLER: Old config.json found but running interactively - showing installer"
echo ""
echo "WARNING: Previous agent installation detected!"
echo " Config file found: /etc/haproxy-agent/config.json"
echo ""
echo " The old agent configuration will be cleaned up during installation."
echo ""
fi
# Method 4: Legacy environment variable
@@ -381,7 +398,7 @@ if [[ "$SKIP_TO_DAEMON" != "true" ]]; then
# CRITICAL: Clean installation - Remove existing agent components
if [[ "$QUIET_MODE" != "true" ]]; then
echo ""
echo "🧹 Performing pre-installation cleanup..."
echo "Performing pre-installation cleanup..."
fi
# Function to safely remove files/directories
@@ -390,7 +407,7 @@ safe_remove() {
local desc="$2"
if [[ -e "$path" ]]; then
[[ "$QUIET_MODE" != "true" ]] && echo " 🗑️ Removing $desc..."
[[ "$QUIET_MODE" != "true" ]] && echo " Removing $desc..."
rm -rf "$path" 2>/dev/null || true
[[ "$QUIET_MODE" != "true" ]] && echo " $desc removed"
return 0
@@ -398,7 +415,7 @@ safe_remove() {
}
# 1. Stop and kill all existing agent processes
[[ "$QUIET_MODE" != "true" ]] && echo "🔪 Terminating existing HAProxy Agent processes..."
[[ "$QUIET_MODE" != "true" ]] && echo "Terminating existing HAProxy Agent processes..."
KILLED_COUNT=0
for pattern in "haproxy-agent" "/usr/local/bin/haproxy-agent" "haproxy-agent.service"; do
PIDS=$(pgrep -f "$pattern" 2>/dev/null || true)
@@ -418,7 +435,7 @@ else
fi
# 2. Stop and disable existing systemd services
[[ "$QUIET_MODE" != "true" ]] && echo "🛑 Cleaning up existing systemd services..."
[[ "$QUIET_MODE" != "true" ]] && echo "Cleaning up existing systemd services..."
if systemctl is-enabled haproxy-agent >/dev/null 2>&1; then
[[ "$QUIET_MODE" != "true" ]] && echo " Stopping and disabling haproxy-agent service"
systemctl stop haproxy-agent 2>/dev/null || true
@@ -427,7 +444,7 @@ if systemctl is-enabled haproxy-agent >/dev/null 2>&1; then
fi
# 3. Remove existing files and directories
[[ "$QUIET_MODE" != "true" ]] && echo "🗑️ Removing existing agent files..."
[[ "$QUIET_MODE" != "true" ]] && echo "Removing existing agent files..."
safe_remove "/etc/systemd/system/haproxy-agent.service" "systemd service file"
safe_remove "/usr/local/bin/haproxy-agent" "agent binary"
safe_remove "/etc/haproxy-agent" "configuration directory"
@@ -456,21 +473,21 @@ fi # End of cleanup section
# Detect architecture
ARCH=$(uname -m)
case "$ARCH" in
"x86_64") ARCH_SUFFIX="linux-amd64"; [[ "$QUIET_MODE" != "true" ]] && echo "🖥️ Detected: Linux AMD64" ;;
"aarch64"|"arm64") ARCH_SUFFIX="linux-arm64"; [[ "$QUIET_MODE" != "true" ]] && echo "🦾 Detected: Linux ARM64" ;;
"x86_64") ARCH_SUFFIX="linux-amd64"; [[ "$QUIET_MODE" != "true" ]] && echo "Detected: Linux AMD64" ;;
"aarch64"|"arm64") ARCH_SUFFIX="linux-arm64"; [[ "$QUIET_MODE" != "true" ]] && echo "Detected: Linux ARM64" ;;
*) ARCH_SUFFIX="linux-unknown"; log "WARN" "Unknown architecture: $ARCH" ;;
esac
# Display configuration only in interactive mode
if [[ "$QUIET_MODE" != "true" ]]; then
echo "📋 Agent Configuration:"
echo "Agent Configuration:"
echo " Management URL: $MANAGEMENT_URL"
echo " Cluster ID: $CLUSTER_ID"
echo " Agent Name: $AGENT_NAME"
echo " Hostname: $HOSTNAME"
echo " Architecture: $ARCH ($ARCH_SUFFIX)"
echo ""
echo "📋 HAProxy Integration:"
echo "HAProxy Integration:"
echo " Config Path: $HAPROXY_CONFIG_PATH"
echo " Stats Socket: $STATS_SOCKET_PATH"
echo " Service Name: $HAPROXY_SERVICE_NAME"
@@ -667,7 +684,7 @@ if [[ "$SKIP_TO_DAEMON" != "true" ]]; then
# CRITICAL: Final cleanup as root (clean any remaining files)
echo ""
echo "🧹 Final cleanup as root (removing any remaining files)..."
echo "Final cleanup as root (removing any remaining files)..."
safe_remove "/etc/systemd/system/haproxy-agent.service" "existing systemd service file"
safe_remove "/usr/local/bin/haproxy-agent" "existing agent binary"
safe_remove "/etc/haproxy-agent" "existing configuration directory"
@@ -2120,7 +2137,7 @@ CLUSTER_POOL_ID=$(curl -k -s -X GET "$MANAGEMENT_URL/api/clusters/$CLUSTER_ID" \
[[ -z "$CLUSTER_POOL_ID" || "$CLUSTER_POOL_ID" == "null" ]] && CLUSTER_POOL_ID=1
# Create configuration
echo "⚙️ Creating configuration..."
echo "Creating configuration..."
cat > "$CONFIG_DIR/config.json" << CONFIG_EOF
{
"management": {
@@ -2213,7 +2230,7 @@ if [[ "$SERVICE_STATUS" == "active" ]]; then
echo " • Wait for configuration update tasks from management UI"
echo " • Apply HAProxy config changes to: $HAPROXY_CONFIG_PATH"
echo ""
echo "Your Linux HAProxy instance is now centrally manageable!"
echo "Your Linux HAProxy instance is now centrally manageable!"
echo ""
echo "Next Steps:"
echo " 1. Check agent logs: tail -f $LOG_DIR/agent.log"
+24 -19
View File
@@ -13,8 +13,13 @@ HAPROXY_CONFIG_PATH="{{HAPROXY_CONFIG_PATH}}"
# Quick daemon mode detection (before installer UI)
# Suppress installer messages if running in daemon mode
# CRITICAL FIX: Only check config.json for daemon mode when NOT running interactively
# Previous bug: config.json from old install caused script to silently enter daemon mode
QUIET_MODE=false
if [[ "$1" == "daemon" || "$SKIP_TO_DAEMON" == "true" || -f "/etc/haproxy-agent/config.json" ]]; then
if [[ "$1" == "daemon" || "$SKIP_TO_DAEMON" == "true" ]]; then
QUIET_MODE=true
elif [[ -f "/etc/haproxy-agent/config.json" ]] && [[ ! -t 0 ]]; then
# Config exists AND not running from terminal (launchd/non-interactive)
QUIET_MODE=true
fi
@@ -26,7 +31,7 @@ if [[ "$QUIET_MODE" != "true" ]]; then
echo "With Version Management & Auto-Upgrade"
echo "=========================================="
echo ""
echo "⚠️ IMPORTANT WARNING ⚠️"
echo "IMPORTANT WARNING"
echo "=========================================="
echo ""
echo "After agent installation, ALL your existing frontend and backend"
@@ -35,11 +40,11 @@ if [[ "$QUIET_MODE" != "true" ]]; then
echo ""
echo "An automatic backup will be created: haproxy.cfg.initial-backup"
echo ""
echo "📋 REQUIRED STEPS AFTER INSTALLATION:"
echo "REQUIRED STEPS AFTER INSTALLATION:"
echo " 1. Login to HAProxy OpenManager interface"
echo " 2. Use the BULK IMPORT feature to restore your configuration"
echo ""
echo "⚠️ IMPORTANT: After installation, ALL configuration changes MUST be"
echo "IMPORTANT: After installation, ALL configuration changes MUST be"
echo " made through the HAProxy OpenManager interface only."
echo " Manual changes will be overwritten on the next update."
echo ""
@@ -50,8 +55,8 @@ if [[ "$QUIET_MODE" != "true" ]]; then
if [[ -f "$HAPROXY_CONFIG_PATH" ]]; then
BACKUP_PATH="${HAPROXY_CONFIG_PATH}.initial-backup"
if [[ ! -f "$BACKUP_PATH" ]]; then
echo "📦 Creating backup: $BACKUP_PATH"
cp "$HAPROXY_CONFIG_PATH" "$BACKUP_PATH" && echo "Backup created successfully" || echo "⚠️ Backup failed"
echo "Creating backup: $BACKUP_PATH"
cp "$HAPROXY_CONFIG_PATH" "$BACKUP_PATH" && echo "Backup created successfully" || echo "WARNING: Backup failed"
echo ""
fi
fi
@@ -247,7 +252,7 @@ if [[ "$SKIP_TO_DAEMON" != "true" ]]; then
# CRITICAL: Clean installation - Remove existing agent components
if [[ "$QUIET_MODE" != "true" ]]; then
echo ""
echo "🧹 Performing pre-installation cleanup..."
echo "Performing pre-installation cleanup..."
fi
# Function to safely remove files/directories
@@ -256,7 +261,7 @@ safe_remove() {
local desc="$2"
if [[ -e "$path" ]]; then
[[ "$QUIET_MODE" != "true" ]] && echo " 🗑️ Removing $desc..."
[[ "$QUIET_MODE" != "true" ]] && echo " Removing $desc..."
rm -rf "$path" 2>/dev/null || true
[[ "$QUIET_MODE" != "true" ]] && echo " $desc removed"
return 0
@@ -264,7 +269,7 @@ safe_remove() {
}
# 1. Stop and kill all existing agent processes
[[ "$QUIET_MODE" != "true" ]] && echo "🔪 Terminating existing HAProxy Agent processes..."
[[ "$QUIET_MODE" != "true" ]] && echo "Terminating existing HAProxy Agent processes..."
KILLED_COUNT=0
for pattern in "haproxy-agent" "/usr/local/bin/haproxy-agent" "com.haproxy.agent"; do
PIDS=$(pgrep -f "$pattern" 2>/dev/null || true)
@@ -284,7 +289,7 @@ else
fi
# 2. Stop and unload existing launchd services
[[ "$QUIET_MODE" != "true" ]] && echo "🛑 Cleaning up existing launchd services..."
[[ "$QUIET_MODE" != "true" ]] && echo "Cleaning up existing launchd services..."
for service in "com.haproxy.agent" "homebrew.mxcl.haproxy"; do
if launchctl list 2>/dev/null | grep -q "$service"; then
[[ "$QUIET_MODE" != "true" ]] && echo " Stopping service: $service"
@@ -297,7 +302,7 @@ done
# 3. Remove existing files and directories (requires root for some)
if [[ $EUID -eq 0 ]]; then
[[ "$QUIET_MODE" != "true" ]] && echo "🗑️ Removing existing agent files (as root)..."
[[ "$QUIET_MODE" != "true" ]] && echo "Removing existing agent files (as root)..."
safe_remove "/Library/LaunchDaemons/com.haproxy.agent.plist" "LaunchDaemon plist"
safe_remove "/Library/LaunchAgents/com.haproxy.agent.plist" "LaunchAgent plist"
safe_remove "/usr/local/bin/haproxy-agent" "agent binary"
@@ -332,22 +337,22 @@ fi # End of cleanup section
ARCH=$(uname -m)
if [[ "$ARCH" == "arm64" ]]; then
ARCH_SUFFIX="darwin-arm64"
[[ "$QUIET_MODE" != "true" ]] && echo "🍎 Detected: Apple Silicon (ARM64)"
[[ "$QUIET_MODE" != "true" ]] && echo "Detected: Apple Silicon (ARM64)"
else
ARCH_SUFFIX="darwin-amd64"
[[ "$QUIET_MODE" != "true" ]] && echo "🖥️ Detected: Intel Mac (AMD64)"
[[ "$QUIET_MODE" != "true" ]] && echo "Detected: Intel Mac (AMD64)"
fi
# Display configuration only in interactive mode
if [[ "$QUIET_MODE" != "true" ]]; then
echo "📋 Agent Configuration:"
echo "Agent Configuration:"
echo " Management URL: $MANAGEMENT_URL"
echo " Cluster ID: $CLUSTER_ID"
echo " Agent Name: $AGENT_NAME"
echo " Hostname: $HOSTNAME"
echo " Architecture: $ARCH ($ARCH_SUFFIX)"
echo ""
echo "📋 HAProxy Integration:"
echo "HAProxy Integration:"
echo " Config Path: $HAPROXY_CONFIG_PATH"
echo " Stats Socket: $STATS_SOCKET_PATH"
echo " Service Name: $HAPROXY_SERVICE_NAME"
@@ -423,7 +428,7 @@ if [[ "$SKIP_TO_DAEMON" != "true" && $EUID -ne 0 ]]; then
install_dependencies_user
echo ""
echo "📋 Dependencies installed. Now run with root privileges for agent service setup:"
echo "Dependencies installed. Now run with root privileges for agent service setup:"
echo " sudo $0"
exit 0
fi
@@ -574,7 +579,7 @@ if [[ "$SKIP_TO_DAEMON" != "true" ]]; then
# CRITICAL: Final cleanup as root (clean any remaining files)
echo ""
echo "🧹 Final cleanup as root (removing any remaining files)..."
echo "Final cleanup as root (removing any remaining files)..."
safe_remove "/Library/LaunchDaemons/com.haproxy.agent.plist" "LaunchDaemon plist"
safe_remove "/Library/LaunchAgents/com.haproxy.agent.plist" "LaunchAgent plist"
safe_remove "/usr/local/bin/haproxy-agent" "existing agent binary"
@@ -2053,7 +2058,7 @@ CLUSTER_POOL_ID=$(curl -k -s -X GET "$MANAGEMENT_URL/api/clusters/$CLUSTER_ID" \
[[ -z "$CLUSTER_POOL_ID" || "$CLUSTER_POOL_ID" == "null" ]] && CLUSTER_POOL_ID=1
# Create configuration
echo "⚙️ Creating configuration..."
echo "Creating configuration..."
cat > "$CONFIG_DIR/config.json" << CONFIG_EOF
{
"management": {
@@ -2151,7 +2156,7 @@ if [[ "$SERVICE_STATUS" == "active" ]]; then
echo " • Wait for configuration update tasks from management UI"
echo " • Apply HAProxy config changes to: $HAPROXY_CONFIG_PATH"
echo ""
echo "Your macOS HAProxy instance is now centrally manageable!"
echo "Your macOS HAProxy instance is now centrally manageable!"
echo ""
echo "Next Steps:"
echo " 1. Check agent logs: tail -f $LOG_DIR/agent.log"
@@ -1,418 +1,350 @@
#!/bin/bash
# HAProxy Agent Uninstaller for Linux - Complete Agent Cleanup
# Run with: sudo ./uninstall-agent-linux.sh
# Updated for daemon mode and modern agent cleanup
set -e
# 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.
echo "=========================================="
echo "HAProxy Agent Uninstaller for Linux v5.0"
echo "Complete Agent Cleanup Edition"
echo "With Daemon Mode & Modern Agent Support"
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"
exit 1
echo "ERROR: This script must be run as root"
echo " Please run: sudo $0"
exit 1
fi
echo "🔍 Performing COMPLETE agent cleanup (HAProxy untouched)..."
echo ""
echo "Starting complete agent cleanup (HAProxy untouched)..."
echo ""
# Function to safely remove files/directories
# --------------------------------------------
# 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
echo "🗑️ Removing $desc..."
rm -rf "$path" 2>/dev/null || {
echo "⚠️ Failed to remove $desc (may not exist or no permission)"
return 1
}
echo "$desc removed"
return 0
else
echo "$desc not found"
return 1
fi
}
# Function to find and kill daemon processes
kill_daemon_processes() {
echo "🔪 Terminating ALL HAProxy Agent processes (including daemon mode)..."
local killed_count=0
# Enhanced process patterns for daemon mode
local patterns=(
"haproxy-agent"
"/usr/local/bin/haproxy-agent"
"/usr/bin/haproxy-agent"
"haproxy-agent.service"
"haproxy-agent daemon"
"bash.*haproxy-agent"
"nohup.*haproxy-agent"
)
for pattern in "${patterns[@]}"; do
local pids=$(pgrep -f "$pattern" 2>/dev/null || true)
if [[ -n "$pids" ]]; then
echo " 🎯 Killing processes matching: $pattern"
echo " 📋 PIDs: $pids"
# First try TERM signal
kill -TERM $pids 2>/dev/null || true
sleep 3
# Check if still running, then use KILL
local remaining_pids=$(pgrep -f "$pattern" 2>/dev/null || true)
if [[ -n "$remaining_pids" ]]; then
echo " 💀 Force killing remaining PIDs: $remaining_pids"
kill -KILL $remaining_pids 2>/dev/null || true
sleep 1
fi
killed_count=$((killed_count + $(echo "$pids" | wc -w)))
fi
done
# Also check for any remaining haproxy-agent processes
local final_check=$(pgrep -f "haproxy-agent" 2>/dev/null || true)
if [[ -n "$final_check" ]]; then
echo " 🔥 Final cleanup: killing PIDs $final_check"
kill -KILL $final_check 2>/dev/null || true
fi
if [[ $killed_count -gt 0 ]]; then
echo "✅ Terminated $killed_count agent processes"
else
echo "️ No agent processes found"
fi
}
# Function to clean up systemd services
cleanup_systemd_services() {
echo "🛑 Cleaning up agent systemd services..."
local services=(
"haproxy-agent"
"haproxy-agent.service"
"haproxy.agent"
"haproxy.agent.service"
)
for service in "${services[@]}"; do
# Remove .service if not already present
local service_name="$service"
[[ "$service_name" != *.service ]] && service_name="${service_name}.service"
# Check if service exists and is active
if systemctl is-active "$service_name" &>/dev/null; then
echo " 🛑 Stopping service: $service_name"
systemctl stop "$service_name" 2>/dev/null || true
fi
# Check if service is enabled
if systemctl is-enabled "$service_name" &>/dev/null; then
echo " 🚫 Disabling service: $service_name"
systemctl disable "$service_name" 2>/dev/null || true
fi
# Mask the service to prevent reactivation
systemctl mask "$service_name" 2>/dev/null || true
echo " ✅ Service $service_name cleaned up"
done
# Remove service files
local 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 service_file in "${service_files[@]}"; do
safe_remove "$service_file" "systemd service file ($service_file)"
done
# Reload systemd daemon
echo "🔄 Reloading systemd daemon..."
systemctl daemon-reload 2>/dev/null || true
systemctl reset-failed 2>/dev/null || true
}
# Function to find and remove all agent binaries dynamically
remove_agent_binaries() {
echo "🔍 Searching for agent binaries in all possible locations..."
# Common binary locations for Linux
local search_paths=(
"/usr/local/bin"
"/usr/bin"
"/bin"
"/usr/sbin"
"/sbin"
"/opt/bin"
"/usr/local/sbin"
)
local found_count=0
for path in "${search_paths[@]}"; do
if [[ -f "$path/haproxy-agent" ]]; then
safe_remove "$path/haproxy-agent" "agent binary ($path)"
((found_count++))
fi
done
# Deep search for any remaining haproxy-agent binaries
echo "🔍 Deep search for any remaining haproxy-agent binaries..."
local found_binaries
found_binaries=$(find /usr /opt /bin /sbin 2>/dev/null -name "haproxy-agent" -type f 2>/dev/null || true)
if [[ -n "$found_binaries" ]]; then
while IFS= read -r binary; do
if [[ -f "$binary" ]]; then
safe_remove "$binary" "found agent binary ($binary)"
((found_count++))
fi
done <<< "$found_binaries"
fi
if [[ $found_count -eq 0 ]]; then
echo "️ No agent binaries found"
else
echo "✅ Removed $found_count agent binaries"
fi
}
# Function to clean up configuration and data
cleanup_agent_data() {
echo "🧹 Cleaning up agent configuration and data..."
# Configuration directories
local 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 config_dir in "${config_dirs[@]}"; do
safe_remove "$config_dir" "agent configuration directory ($config_dir)"
done
# Log directories
local log_dirs=(
"/var/log/haproxy-agent"
"/usr/local/var/log/haproxy-agent"
"/tmp/haproxy-agent-logs"
"/var/log/syslog.d/haproxy-agent"
)
for log_dir in "${log_dirs[@]}"; do
safe_remove "$log_dir" "agent log directory ($log_dir)"
done
# PID files
local pid_files=(
"/var/run/haproxy-agent.pid"
"/tmp/haproxy-agent.pid"
"/usr/local/var/run/haproxy-agent.pid"
"/run/haproxy-agent.pid"
)
for pid_file in "${pid_files[@]}"; do
safe_remove "$pid_file" "agent PID file ($pid_file)"
done
}
# Function to clean up temporary files
cleanup_temp_files() {
echo "🧹 Cleaning up agent-specific temporary and debug files..."
local temp_patterns=(
"/tmp/agent_debug.log"
"/tmp/debug_agent.log"
"/tmp/fixed_agent_test.log"
"/tmp/haproxy-agent-*"
"/tmp/agent-startup-with-config.sh"
"/tmp/daemon-test-agent*.sh"
"/var/tmp/haproxy-agent-*"
"/tmp/haproxy-failed-*.cfg"
"/tmp/haproxy_new_*.cfg"
"/tmp/haproxy-new-config.cfg"
"/tmp/haproxy-merged*.cfg"
"/tmp/haproxy-global*.cfg"
"/tmp/haproxy-defaults*.cfg"
"/tmp/haproxy-listen*.cfg"
)
for pattern in "${temp_patterns[@]}"; do
if [[ "$pattern" == *"*"* ]]; then
# Handle wildcard patterns
local base_dir=$(dirname "$pattern")
local file_pattern=$(basename "$pattern")
if [[ -d "$base_dir" ]]; then
local found_files=$(find "$base_dir" -maxdepth 1 -name "$file_pattern" -type f 2>/dev/null || true)
if [[ -n "$found_files" ]]; then
while IFS= read -r file; do
[[ -f "$file" ]] && safe_remove "$file" "agent temp file ($file)"
done <<< "$found_files"
fi
fi
rm -rf "$path" 2>/dev/null || true
if [[ ! -e "$path" ]]; then
echo " [REMOVED] $desc"
else
safe_remove "$pattern" "agent temp file ($(basename "$pattern"))"
echo " [WARN] Failed to remove $desc"
fi
done
# Clean up agent upgrade markers and PID files
echo "🧹 Cleaning up agent upgrade markers..."
local marker_patterns=(
"/tmp/haproxy-agent-upgrade-complete-*"
"/tmp/haproxy-agent-upgrade-test-*"
"/tmp/haproxy-agent-*.pid"
)
for pattern in "${marker_patterns[@]}"; do
local marker_dir=$(dirname "$pattern")
local marker_name=$(basename "$pattern")
if [[ -d "$marker_dir" ]]; then
local found_markers=$(find "$marker_dir" -maxdepth 1 -name "$marker_name" -type f 2>/dev/null || true)
if [[ -n "$found_markers" ]]; then
while IFS= read -r marker; do
[[ -f "$marker" ]] && safe_remove "$marker" "agent marker file ($marker)"
done <<< "$found_markers"
fi
fi
done
# Clean up agent-specific sockets
echo "🧹 Cleaning up agent-specific sockets..."
local socket_patterns=(
"/tmp/haproxy-agent-*"
"/var/run/haproxy-agent-*"
"/run/haproxy-agent-*"
)
for pattern in "${socket_patterns[@]}"; do
local socket_dir=$(dirname "$pattern")
local socket_name=$(basename "$pattern")
if [[ -d "$socket_dir" ]]; then
local found_sockets=$(find "$socket_dir" -name "$socket_name" -type s 2>/dev/null || true)
if [[ -n "$found_sockets" ]]; then
while IFS= read -r socket; do
safe_remove "$socket" "agent socket ($socket)"
done <<< "$found_sockets"
fi
fi
done
else
echo " [SKIP] $desc (not found)"
fi
return 0
}
# Function to clean up cron jobs
cleanup_cron_jobs() {
echo "🧹 Cleaning up agent-related cron jobs..."
# Check for haproxy-agent related cron jobs
local cron_files=(
"/etc/cron.d/haproxy-agent"
"/etc/crontab"
"/var/spool/cron/root"
"/var/spool/cron/crontabs/root"
)
for cron_file in "${cron_files[@]}"; do
if [[ -f "$cron_file" ]]; then
if grep -q "haproxy-agent" "$cron_file" 2>/dev/null; then
echo " 🕐 Found haproxy-agent references in $cron_file"
# Create backup before modifying
cp "$cron_file" "${cron_file}.backup.$(date +%Y%m%d_%H%M%S)" 2>/dev/null || true
# Remove lines containing haproxy-agent
sed -i '/haproxy-agent/d' "$cron_file" 2>/dev/null || true
echo " ✅ Cleaned haproxy-agent entries from $cron_file"
fi
# --------------------------------------------
# 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"
)
for pattern in "${patterns[@]}"; do
pids=$(pgrep -f "$pattern" 2>/dev/null || true)
if [[ -n "$pids" ]]; then
echo " Killing processes matching: $pattern (PIDs: $pids)"
kill -TERM $pids 2>/dev/null || true
sleep 2
# Force kill if still running
remaining=$(pgrep -f "$pattern" 2>/dev/null || true)
if [[ -n "$remaining" ]]; then
echo " Force killing remaining: $remaining"
kill -KILL $remaining 2>/dev/null || true
sleep 1
fi
done
}
killed_count=$((killed_count + $(echo "$pids" | wc -w)))
fi
done
# Main cleanup execution
echo "🚀 Starting complete agent cleanup process..."
# Final sweep
final_pids=$(pgrep -f "haproxy-agent" 2>/dev/null || true)
if [[ -n "$final_pids" ]]; then
echo " Final cleanup: force killing PIDs $final_pids"
kill -KILL $final_pids 2>/dev/null || true
fi
# 1. Kill all processes (including daemon mode)
kill_daemon_processes
if [[ $killed_count -gt 0 ]]; then
echo " Terminated $killed_count agent process(es)"
else
echo " No agent processes found"
fi
echo ""
# 2. Stop and clean systemd services
cleanup_systemd_services
# --------------------------------------------
# 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
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 ""
# --------------------------------------------
# 3. Remove agent binaries
remove_agent_binaries
# --------------------------------------------
echo "[3/7] Removing agent binaries..."
# 4. Clean up configuration and data
cleanup_agent_data
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"
)
# 5. Clean up temporary files
cleanup_temp_files
for bp in "${bin_paths[@]}"; do
safe_remove "$bp" "binary: $bp"
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"
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
cleanup_cron_jobs
# --------------------------------------------
echo "[6/7] Cleaning up cron jobs..."
# 7. Final comprehensive verification
echo "🔍 Final agent cleanup verification..."
local remaining_processes=$(pgrep -f "haproxy-agent" 2>/dev/null | wc -l || echo "0")
local remaining_services=$(systemctl list-units --all 2>/dev/null | grep -c "haproxy-agent" || echo "0")
local remaining_binaries=$(find /usr /opt /bin /sbin 2>/dev/null -name "haproxy-agent" -type f 2>/dev/null | wc -l || echo "0")
cron_files=(
"/etc/cron.d/haproxy-agent"
"/etc/crontab"
"/var/spool/cron/root"
"/var/spool/cron/crontabs/root"
)
for cf in "${cron_files[@]}"; 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"
fi
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_processes -eq 0 && $remaining_services -eq 0 && $remaining_binaries -eq 0 ]]; then
echo "✅ COMPLETE AGENT CLEANUP VERIFIED - Agent completely removed!"
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 "⚠️ Some agent remnants detected:"
[[ $remaining_processes -gt 0 ]] && echo " - $remaining_processes agent processes still running"
[[ $remaining_services -gt 0 ]] && echo " - $remaining_services agent services still loaded"
[[ $remaining_binaries -gt 0 ]] && echo " - $remaining_binaries agent binaries still found"
if [[ $remaining_binaries -gt 0 ]]; then
echo " 📋 Remaining agent binaries:"
find /usr /opt /bin /sbin 2>/dev/null -name "haproxy-agent" -type f 2>/dev/null | while read -r binary; do
echo " - $binary"
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_processes -gt 0 ]]; then
echo " 📋 Remaining agent processes:"
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 [[ $remaining_services -gt 0 ]]; then
echo " 📋 Remaining agent services:"
systemctl list-units --all 2>/dev/null | grep "haproxy-agent" || true
fi
fi
echo ""
echo "🎉 HAProxy Agent Uninstaller Completed!"
echo "=========================================="
echo "HAProxy Agent Uninstall Complete"
echo "=========================================="
echo ""
echo "📋 Complete cleanup summary:"
echo " • ✅ All agent processes terminated (including daemon mode)"
echo " • ✅ All agent systemd services stopped, disabled, and masked"
echo " • ✅ All agent binaries found and removed (deep search)"
echo " • ✅ All agent configs, logs, and data cleaned"
echo " • ✅ All agent-specific temp files and sockets removed"
echo " • ✅ Agent-related cron jobs cleaned"
echo " • ✅ Systemd daemon reloaded"
echo " • ✅ HAProxy installation and configs UNTOUCHED"
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 "🚀 System ready for fresh agent installation!"
echo "What was NOT touched:"
echo " - HAProxy service"
echo " - HAProxy configuration (/etc/haproxy/haproxy.cfg)"
echo " - HAProxy SSL certificates"
echo " - HAProxy logs"
echo ""
echo "💡 Tips:"
echo " • HAProxy service and configurations remain intact"
echo " • Run 'ps aux | grep haproxy-agent' to verify no processes remain"
echo " • Run 'systemctl list-units | grep haproxy' to verify no services remain"
echo " • Run 'systemctl status haproxy-agent' should show 'Unit not found'"
echo " • Agent can be reinstalled using the management UI"
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/"
@@ -1,343 +1,289 @@
#!/bin/bash
# HAProxy Agent Uninstaller for macOS - Complete Agent Cleanup
# Run with: sudo ./uninstall-agent-macos.sh
# Updated for daemon mode and modern agent cleanup
set -e
# 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.
echo "=========================================="
echo "HAProxy Agent Uninstaller for macOS v5.0"
echo "Complete Agent Cleanup Edition"
echo "With Daemon Mode & Modern Agent Support"
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"
exit 1
echo "ERROR: This script must be run as root"
echo " Please run: sudo $0"
exit 1
fi
echo "🔍 Performing COMPLETE agent cleanup (HAProxy untouched)..."
echo ""
echo "Starting complete agent cleanup (HAProxy untouched)..."
echo ""
# Function to safely remove files/directories
# --------------------------------------------
# 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
echo "🗑️ Removing $desc..."
rm -rf "$path" 2>/dev/null || {
echo "⚠️ Failed to remove $desc (may not exist or no permission)"
return 1
}
echo "$desc removed"
return 0
else
echo "$desc not found"
return 1
fi
}
# Function to find and kill daemon processes
kill_daemon_processes() {
echo "🔪 Terminating ALL HAProxy Agent processes (including daemon mode)..."
local killed_count=0
# Enhanced process patterns for daemon mode
local 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"
)
for pattern in "${patterns[@]}"; do
local pids=$(pgrep -f "$pattern" 2>/dev/null || true)
if [[ -n "$pids" ]]; then
echo " 🎯 Killing processes matching: $pattern"
echo " 📋 PIDs: $pids"
# First try TERM signal
kill -TERM $pids 2>/dev/null || true
sleep 3
# Check if still running, then use KILL
local remaining_pids=$(pgrep -f "$pattern" 2>/dev/null || true)
if [[ -n "$remaining_pids" ]]; then
echo " 💀 Force killing remaining PIDs: $remaining_pids"
kill -KILL $remaining_pids 2>/dev/null || true
sleep 1
fi
killed_count=$((killed_count + $(echo "$pids" | wc -w)))
fi
done
# Also check for any remaining haproxy-agent processes
local final_check=$(pgrep -f "haproxy-agent" 2>/dev/null || true)
if [[ -n "$final_check" ]]; then
echo " 🔥 Final cleanup: killing PIDs $final_check"
kill -KILL $final_check 2>/dev/null || true
fi
if [[ $killed_count -gt 0 ]]; then
echo "✅ Terminated $killed_count agent processes"
else
echo "️ No agent processes found"
fi
}
# Function to find and remove all agent binaries dynamically
remove_agent_binaries() {
echo "🔍 Searching for agent binaries in all possible locations..."
# Common binary locations
local search_paths=(
"/usr/local/bin"
"/opt/homebrew/bin"
"/usr/bin"
"/bin"
"/opt/homebrew/sbin"
"/usr/sbin"
"/sbin"
)
local found_count=0
for path in "${search_paths[@]}"; do
if [[ -f "$path/haproxy-agent" ]]; then
safe_remove "$path/haproxy-agent" "agent binary ($path)"
((found_count++))
fi
done
# Deep search for any remaining haproxy-agent binaries
echo "🔍 Deep search for any remaining haproxy-agent binaries..."
local found_binaries
found_binaries=$(find /usr /opt /Applications 2>/dev/null -name "haproxy-agent" -type f 2>/dev/null || true)
if [[ -n "$found_binaries" ]]; then
while IFS= read -r binary; do
if [[ -f "$binary" ]]; then
safe_remove "$binary" "found agent binary ($binary)"
((found_count++))
fi
done <<< "$found_binaries"
fi
if [[ $found_count -eq 0 ]]; then
echo "️ No agent binaries found"
else
echo "✅ Removed $found_count agent binaries"
fi
}
# Function to clean up launchd services
cleanup_launchd_services() {
echo "🛑 Cleaning up agent launchd services..."
local services=(
"com.haproxy.agent"
"haproxy.agent"
"haproxy-agent"
"com.haproxy-agent"
)
for service in "${services[@]}"; do
# Check if service is loaded
if launchctl list 2>/dev/null | grep -q "$service"; then
echo " 🛑 Stopping service: $service"
launchctl stop "$service" 2>/dev/null || true
launchctl unload "/Library/LaunchDaemons/$service.plist" 2>/dev/null || true
launchctl unload "/Library/LaunchAgents/$service.plist" 2>/dev/null || true
launchctl remove "$service" 2>/dev/null || true
echo " ✅ Service $service cleaned up"
fi
done
# Remove plist files
for service in "${services[@]}"; do
safe_remove "/Library/LaunchDaemons/$service.plist" "LaunchDaemon plist ($service)"
safe_remove "/Library/LaunchAgents/$service.plist" "LaunchAgent plist ($service)"
safe_remove "~/Library/LaunchAgents/$service.plist" "User LaunchAgent plist ($service)"
done
}
# Function to clean up configuration and data
cleanup_agent_data() {
echo "🧹 Cleaning up agent configuration and data..."
# Configuration directories
local 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 config_dir in "${config_dirs[@]}"; do
safe_remove "$config_dir" "agent configuration directory ($config_dir)"
done
# Log directories
local log_dirs=(
"/var/log/haproxy-agent"
"/opt/homebrew/var/log/haproxy-agent"
"/usr/local/var/log/haproxy-agent"
"/tmp/haproxy-agent-logs"
)
for log_dir in "${log_dirs[@]}"; do
safe_remove "$log_dir" "agent log directory ($log_dir)"
done
# PID files
local 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 pid_file in "${pid_files[@]}"; do
safe_remove "$pid_file" "agent PID file ($pid_file)"
done
}
# Function to clean up temporary files
cleanup_temp_files() {
echo "🧹 Cleaning up agent-specific temporary and debug files..."
local temp_patterns=(
"/tmp/agent_debug.log"
"/tmp/debug_agent.log"
"/tmp/fixed_agent_test.log"
"/tmp/haproxy-agent-*"
"/tmp/agent-startup-with-config.sh"
"/tmp/daemon-test-agent*.sh"
"/var/tmp/haproxy-agent-*"
"/tmp/haproxy-failed-*.cfg"
"/tmp/haproxy_new_*.cfg"
"/tmp/haproxy-new-config.cfg"
"/tmp/haproxy-merged*.cfg"
"/tmp/haproxy-global*.cfg"
"/tmp/haproxy-defaults*.cfg"
"/tmp/haproxy-listen*.cfg"
)
for pattern in "${temp_patterns[@]}"; do
if [[ "$pattern" == *"*"* ]]; then
# Handle wildcard patterns
local base_dir=$(dirname "$pattern")
local file_pattern=$(basename "$pattern")
if [[ -d "$base_dir" ]]; then
local found_files=$(find "$base_dir" -maxdepth 1 -name "$file_pattern" -type f 2>/dev/null || true)
if [[ -n "$found_files" ]]; then
while IFS= read -r file; do
[[ -f "$file" ]] && safe_remove "$file" "agent temp file ($file)"
done <<< "$found_files"
fi
fi
rm -rf "$path" 2>/dev/null || true
if [[ ! -e "$path" ]]; then
echo " [REMOVED] $desc"
else
safe_remove "$pattern" "agent temp file ($(basename "$pattern"))"
echo " [WARN] Failed to remove $desc"
fi
done
# Clean up agent upgrade markers and PID files
echo "🧹 Cleaning up agent upgrade markers..."
local marker_patterns=(
"/tmp/haproxy-agent-upgrade-complete-*"
"/tmp/haproxy-agent-upgrade-test-*"
"/tmp/haproxy-agent-*.pid"
)
for pattern in "${marker_patterns[@]}"; do
local marker_dir=$(dirname "$pattern")
local marker_name=$(basename "$pattern")
if [[ -d "$marker_dir" ]]; then
local found_markers=$(find "$marker_dir" -maxdepth 1 -name "$marker_name" -type f 2>/dev/null || true)
if [[ -n "$found_markers" ]]; then
while IFS= read -r marker; do
[[ -f "$marker" ]] && safe_remove "$marker" "agent marker file ($marker)"
done <<< "$found_markers"
fi
fi
done
# Clean up agent-specific sockets
echo "🧹 Cleaning up agent-specific sockets..."
local socket_patterns=(
"/tmp/haproxy-agent-*"
"/var/run/haproxy-agent-*"
)
for pattern in "${socket_patterns[@]}"; do
local socket_dir=$(dirname "$pattern")
local socket_name=$(basename "$pattern")
if [[ -d "$socket_dir" ]]; then
local found_sockets=$(find "$socket_dir" -name "$socket_name" -type s 2>/dev/null || true)
if [[ -n "$found_sockets" ]]; then
while IFS= read -r socket; do
safe_remove "$socket" "agent socket ($socket)"
done <<< "$found_sockets"
fi
fi
done
else
echo " [SKIP] $desc (not found)"
fi
return 0
}
# Main cleanup execution
echo "🚀 Starting complete agent cleanup process..."
# --------------------------------------------
# 1. Terminate all agent processes
# --------------------------------------------
echo "[1/6] Terminating agent processes..."
# 1. Kill all processes (including daemon mode)
kill_daemon_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"
)
# 2. Stop and clean launchd services
cleanup_launchd_services
for pattern in "${patterns[@]}"; do
pids=$(pgrep -f "$pattern" 2>/dev/null || true)
if [[ -n "$pids" ]]; then
echo " Killing processes matching: $pattern (PIDs: $pids)"
kill -TERM $pids 2>/dev/null || true
sleep 2
# Force kill if still running
remaining=$(pgrep -f "$pattern" 2>/dev/null || true)
if [[ -n "$remaining" ]]; then
echo " Force killing remaining: $remaining"
kill -KILL $remaining 2>/dev/null || true
sleep 1
fi
killed_count=$((killed_count + $(echo "$pids" | wc -w)))
fi
done
# Final sweep
final_pids=$(pgrep -f "haproxy-agent" 2>/dev/null || true)
if [[ -n "$final_pids" ]]; then
echo " Final cleanup: force killing PIDs $final_pids"
kill -KILL $final_pids 2>/dev/null || true
fi
if [[ $killed_count -gt 0 ]]; then
echo " Terminated $killed_count agent process(es)"
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"
done
echo ""
# --------------------------------------------
# 3. Remove agent binaries
remove_agent_binaries
# --------------------------------------------
echo "[3/6] Removing agent binaries..."
# 4. Clean up configuration and data
cleanup_agent_data
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"
)
# 5. Clean up temporary files
cleanup_temp_files
for bp in "${bin_paths[@]}"; do
safe_remove "$bp" "binary: $bp"
done
# 6. Reload launchctl to clean up daemon list
echo "🔄 Refreshing system services..."
launchctl load /System/Library/LaunchDaemons/com.apple.periodic-daily.plist 2>/dev/null || true
# 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"
fi
echo ""
# 7. Final comprehensive verification
echo "🔍 Final agent cleanup verification..."
local remaining_processes=$(pgrep -f "haproxy-agent" 2>/dev/null | wc -l || echo "0")
local remaining_services=$(launchctl list 2>/dev/null | grep -c "haproxy.agent\|haproxy-agent" || echo "0")
local remaining_binaries=$(find /usr /opt /Applications 2>/dev/null -name "haproxy-agent" -type f 2>/dev/null | wc -l || echo "0")
# --------------------------------------------
# 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_processes -eq 0 && $remaining_services -eq 0 && $remaining_binaries -eq 0 ]]; then
echo "✅ COMPLETE AGENT CLEANUP VERIFIED - Agent completely removed!"
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 "⚠️ Some agent remnants detected:"
[[ $remaining_processes -gt 0 ]] && echo " - $remaining_processes agent processes still running"
[[ $remaining_services -gt 0 ]] && echo " - $remaining_services agent services still loaded"
[[ $remaining_binaries -gt 0 ]] && echo " - $remaining_binaries agent binaries still found"
if [[ $remaining_binaries -gt 0 ]]; then
echo " 📋 Remaining agent binaries:"
find /usr /opt /Applications 2>/dev/null -name "haproxy-agent" -type f 2>/dev/null | while read -r binary; do
echo " - $binary"
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_processes -gt 0 ]]; then
echo " 📋 Remaining agent processes:"
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
@@ -345,21 +291,27 @@ else
fi
echo ""
echo "🎉 HAProxy Agent Uninstaller Completed!"
echo "=========================================="
echo "HAProxy Agent Uninstall Complete"
echo "=========================================="
echo ""
echo "📋 Complete cleanup summary:"
echo " • ✅ All agent processes terminated (including daemon mode)"
echo " • ✅ All agent launchd services removed and unloaded"
echo " • ✅ All agent binaries found and removed (deep search)"
echo " • ✅ All agent configs, logs, and data cleaned"
echo " • ✅ All agent-specific temp files and sockets removed"
echo " • ✅ System services refreshed"
echo " • ✅ HAProxy installation and configs UNTOUCHED"
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 "🚀 System ready for fresh agent installation!"
echo "What was NOT touched:"
echo " - HAProxy service"
echo " - HAProxy configuration"
echo " - HAProxy SSL certificates"
echo " - HAProxy logs"
echo ""
echo "💡 Tips:"
echo " • HAProxy service and configurations remain intact"
echo " • Run 'ps aux | grep haproxy-agent' to verify no processes remain"
echo " • Run 'launchctl list | grep haproxy' to verify no services remain"
echo " • Agent can be reinstalled using the management UI"
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/"
+304 -372
View File
@@ -1,418 +1,350 @@
#!/bin/bash
# HAProxy Agent Uninstaller for Linux - Complete Agent Cleanup
# Run with: sudo ./uninstall-agent-linux.sh
# Updated for daemon mode and modern agent cleanup
set -e
# 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.
echo "=========================================="
echo "HAProxy Agent Uninstaller for Linux v5.0"
echo "Complete Agent Cleanup Edition"
echo "With Daemon Mode & Modern Agent Support"
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"
exit 1
echo "ERROR: This script must be run as root"
echo " Please run: sudo $0"
exit 1
fi
echo "🔍 Performing COMPLETE agent cleanup (HAProxy untouched)..."
echo ""
echo "Starting complete agent cleanup (HAProxy untouched)..."
echo ""
# Function to safely remove files/directories
# --------------------------------------------
# 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
echo "🗑️ Removing $desc..."
rm -rf "$path" 2>/dev/null || {
echo "⚠️ Failed to remove $desc (may not exist or no permission)"
return 1
}
echo "$desc removed"
return 0
else
echo "$desc not found"
return 1
fi
}
# Function to find and kill daemon processes
kill_daemon_processes() {
echo "🔪 Terminating ALL HAProxy Agent processes (including daemon mode)..."
local killed_count=0
# Enhanced process patterns for daemon mode
local patterns=(
"haproxy-agent"
"/usr/local/bin/haproxy-agent"
"/usr/bin/haproxy-agent"
"haproxy-agent.service"
"haproxy-agent daemon"
"bash.*haproxy-agent"
"nohup.*haproxy-agent"
)
for pattern in "${patterns[@]}"; do
local pids=$(pgrep -f "$pattern" 2>/dev/null || true)
if [[ -n "$pids" ]]; then
echo " 🎯 Killing processes matching: $pattern"
echo " 📋 PIDs: $pids"
# First try TERM signal
kill -TERM $pids 2>/dev/null || true
sleep 3
# Check if still running, then use KILL
local remaining_pids=$(pgrep -f "$pattern" 2>/dev/null || true)
if [[ -n "$remaining_pids" ]]; then
echo " 💀 Force killing remaining PIDs: $remaining_pids"
kill -KILL $remaining_pids 2>/dev/null || true
sleep 1
fi
killed_count=$((killed_count + $(echo "$pids" | wc -w)))
fi
done
# Also check for any remaining haproxy-agent processes
local final_check=$(pgrep -f "haproxy-agent" 2>/dev/null || true)
if [[ -n "$final_check" ]]; then
echo " 🔥 Final cleanup: killing PIDs $final_check"
kill -KILL $final_check 2>/dev/null || true
fi
if [[ $killed_count -gt 0 ]]; then
echo "✅ Terminated $killed_count agent processes"
else
echo "️ No agent processes found"
fi
}
# Function to clean up systemd services
cleanup_systemd_services() {
echo "🛑 Cleaning up agent systemd services..."
local services=(
"haproxy-agent"
"haproxy-agent.service"
"haproxy.agent"
"haproxy.agent.service"
)
for service in "${services[@]}"; do
# Remove .service if not already present
local service_name="$service"
[[ "$service_name" != *.service ]] && service_name="${service_name}.service"
# Check if service exists and is active
if systemctl is-active "$service_name" &>/dev/null; then
echo " 🛑 Stopping service: $service_name"
systemctl stop "$service_name" 2>/dev/null || true
fi
# Check if service is enabled
if systemctl is-enabled "$service_name" &>/dev/null; then
echo " 🚫 Disabling service: $service_name"
systemctl disable "$service_name" 2>/dev/null || true
fi
# Mask the service to prevent reactivation
systemctl mask "$service_name" 2>/dev/null || true
echo " ✅ Service $service_name cleaned up"
done
# Remove service files
local 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 service_file in "${service_files[@]}"; do
safe_remove "$service_file" "systemd service file ($service_file)"
done
# Reload systemd daemon
echo "🔄 Reloading systemd daemon..."
systemctl daemon-reload 2>/dev/null || true
systemctl reset-failed 2>/dev/null || true
}
# Function to find and remove all agent binaries dynamically
remove_agent_binaries() {
echo "🔍 Searching for agent binaries in all possible locations..."
# Common binary locations for Linux
local search_paths=(
"/usr/local/bin"
"/usr/bin"
"/bin"
"/usr/sbin"
"/sbin"
"/opt/bin"
"/usr/local/sbin"
)
local found_count=0
for path in "${search_paths[@]}"; do
if [[ -f "$path/haproxy-agent" ]]; then
safe_remove "$path/haproxy-agent" "agent binary ($path)"
((found_count++))
fi
done
# Deep search for any remaining haproxy-agent binaries
echo "🔍 Deep search for any remaining haproxy-agent binaries..."
local found_binaries
found_binaries=$(find /usr /opt /bin /sbin 2>/dev/null -name "haproxy-agent" -type f 2>/dev/null || true)
if [[ -n "$found_binaries" ]]; then
while IFS= read -r binary; do
if [[ -f "$binary" ]]; then
safe_remove "$binary" "found agent binary ($binary)"
((found_count++))
fi
done <<< "$found_binaries"
fi
if [[ $found_count -eq 0 ]]; then
echo "️ No agent binaries found"
else
echo "✅ Removed $found_count agent binaries"
fi
}
# Function to clean up configuration and data
cleanup_agent_data() {
echo "🧹 Cleaning up agent configuration and data..."
# Configuration directories
local 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 config_dir in "${config_dirs[@]}"; do
safe_remove "$config_dir" "agent configuration directory ($config_dir)"
done
# Log directories
local log_dirs=(
"/var/log/haproxy-agent"
"/usr/local/var/log/haproxy-agent"
"/tmp/haproxy-agent-logs"
"/var/log/syslog.d/haproxy-agent"
)
for log_dir in "${log_dirs[@]}"; do
safe_remove "$log_dir" "agent log directory ($log_dir)"
done
# PID files
local pid_files=(
"/var/run/haproxy-agent.pid"
"/tmp/haproxy-agent.pid"
"/usr/local/var/run/haproxy-agent.pid"
"/run/haproxy-agent.pid"
)
for pid_file in "${pid_files[@]}"; do
safe_remove "$pid_file" "agent PID file ($pid_file)"
done
}
# Function to clean up temporary files
cleanup_temp_files() {
echo "🧹 Cleaning up agent-specific temporary and debug files..."
local temp_patterns=(
"/tmp/agent_debug.log"
"/tmp/debug_agent.log"
"/tmp/fixed_agent_test.log"
"/tmp/haproxy-agent-*"
"/tmp/agent-startup-with-config.sh"
"/tmp/daemon-test-agent*.sh"
"/var/tmp/haproxy-agent-*"
"/tmp/haproxy-failed-*.cfg"
"/tmp/haproxy_new_*.cfg"
"/tmp/haproxy-new-config.cfg"
"/tmp/haproxy-merged*.cfg"
"/tmp/haproxy-global*.cfg"
"/tmp/haproxy-defaults*.cfg"
"/tmp/haproxy-listen*.cfg"
)
for pattern in "${temp_patterns[@]}"; do
if [[ "$pattern" == *"*"* ]]; then
# Handle wildcard patterns
local base_dir=$(dirname "$pattern")
local file_pattern=$(basename "$pattern")
if [[ -d "$base_dir" ]]; then
local found_files=$(find "$base_dir" -maxdepth 1 -name "$file_pattern" -type f 2>/dev/null || true)
if [[ -n "$found_files" ]]; then
while IFS= read -r file; do
[[ -f "$file" ]] && safe_remove "$file" "agent temp file ($file)"
done <<< "$found_files"
fi
fi
rm -rf "$path" 2>/dev/null || true
if [[ ! -e "$path" ]]; then
echo " [REMOVED] $desc"
else
safe_remove "$pattern" "agent temp file ($(basename "$pattern"))"
echo " [WARN] Failed to remove $desc"
fi
done
# Clean up agent upgrade markers and PID files
echo "🧹 Cleaning up agent upgrade markers..."
local marker_patterns=(
"/tmp/haproxy-agent-upgrade-complete-*"
"/tmp/haproxy-agent-upgrade-test-*"
"/tmp/haproxy-agent-*.pid"
)
for pattern in "${marker_patterns[@]}"; do
local marker_dir=$(dirname "$pattern")
local marker_name=$(basename "$pattern")
if [[ -d "$marker_dir" ]]; then
local found_markers=$(find "$marker_dir" -maxdepth 1 -name "$marker_name" -type f 2>/dev/null || true)
if [[ -n "$found_markers" ]]; then
while IFS= read -r marker; do
[[ -f "$marker" ]] && safe_remove "$marker" "agent marker file ($marker)"
done <<< "$found_markers"
fi
fi
done
# Clean up agent-specific sockets
echo "🧹 Cleaning up agent-specific sockets..."
local socket_patterns=(
"/tmp/haproxy-agent-*"
"/var/run/haproxy-agent-*"
"/run/haproxy-agent-*"
)
for pattern in "${socket_patterns[@]}"; do
local socket_dir=$(dirname "$pattern")
local socket_name=$(basename "$pattern")
if [[ -d "$socket_dir" ]]; then
local found_sockets=$(find "$socket_dir" -name "$socket_name" -type s 2>/dev/null || true)
if [[ -n "$found_sockets" ]]; then
while IFS= read -r socket; do
safe_remove "$socket" "agent socket ($socket)"
done <<< "$found_sockets"
fi
fi
done
else
echo " [SKIP] $desc (not found)"
fi
return 0
}
# Function to clean up cron jobs
cleanup_cron_jobs() {
echo "🧹 Cleaning up agent-related cron jobs..."
# Check for haproxy-agent related cron jobs
local cron_files=(
"/etc/cron.d/haproxy-agent"
"/etc/crontab"
"/var/spool/cron/root"
"/var/spool/cron/crontabs/root"
)
for cron_file in "${cron_files[@]}"; do
if [[ -f "$cron_file" ]]; then
if grep -q "haproxy-agent" "$cron_file" 2>/dev/null; then
echo " 🕐 Found haproxy-agent references in $cron_file"
# Create backup before modifying
cp "$cron_file" "${cron_file}.backup.$(date +%Y%m%d_%H%M%S)" 2>/dev/null || true
# Remove lines containing haproxy-agent
sed -i '/haproxy-agent/d' "$cron_file" 2>/dev/null || true
echo " ✅ Cleaned haproxy-agent entries from $cron_file"
fi
# --------------------------------------------
# 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"
)
for pattern in "${patterns[@]}"; do
pids=$(pgrep -f "$pattern" 2>/dev/null || true)
if [[ -n "$pids" ]]; then
echo " Killing processes matching: $pattern (PIDs: $pids)"
kill -TERM $pids 2>/dev/null || true
sleep 2
# Force kill if still running
remaining=$(pgrep -f "$pattern" 2>/dev/null || true)
if [[ -n "$remaining" ]]; then
echo " Force killing remaining: $remaining"
kill -KILL $remaining 2>/dev/null || true
sleep 1
fi
done
}
killed_count=$((killed_count + $(echo "$pids" | wc -w)))
fi
done
# Main cleanup execution
echo "🚀 Starting complete agent cleanup process..."
# Final sweep
final_pids=$(pgrep -f "haproxy-agent" 2>/dev/null || true)
if [[ -n "$final_pids" ]]; then
echo " Final cleanup: force killing PIDs $final_pids"
kill -KILL $final_pids 2>/dev/null || true
fi
# 1. Kill all processes (including daemon mode)
kill_daemon_processes
if [[ $killed_count -gt 0 ]]; then
echo " Terminated $killed_count agent process(es)"
else
echo " No agent processes found"
fi
echo ""
# 2. Stop and clean systemd services
cleanup_systemd_services
# --------------------------------------------
# 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
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 ""
# --------------------------------------------
# 3. Remove agent binaries
remove_agent_binaries
# --------------------------------------------
echo "[3/7] Removing agent binaries..."
# 4. Clean up configuration and data
cleanup_agent_data
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"
)
# 5. Clean up temporary files
cleanup_temp_files
for bp in "${bin_paths[@]}"; do
safe_remove "$bp" "binary: $bp"
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"
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
cleanup_cron_jobs
# --------------------------------------------
echo "[6/7] Cleaning up cron jobs..."
# 7. Final comprehensive verification
echo "🔍 Final agent cleanup verification..."
local remaining_processes=$(pgrep -f "haproxy-agent" 2>/dev/null | wc -l || echo "0")
local remaining_services=$(systemctl list-units --all 2>/dev/null | grep -c "haproxy-agent" || echo "0")
local remaining_binaries=$(find /usr /opt /bin /sbin 2>/dev/null -name "haproxy-agent" -type f 2>/dev/null | wc -l || echo "0")
cron_files=(
"/etc/cron.d/haproxy-agent"
"/etc/crontab"
"/var/spool/cron/root"
"/var/spool/cron/crontabs/root"
)
for cf in "${cron_files[@]}"; 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"
fi
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_processes -eq 0 && $remaining_services -eq 0 && $remaining_binaries -eq 0 ]]; then
echo "✅ COMPLETE AGENT CLEANUP VERIFIED - Agent completely removed!"
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 "⚠️ Some agent remnants detected:"
[[ $remaining_processes -gt 0 ]] && echo " - $remaining_processes agent processes still running"
[[ $remaining_services -gt 0 ]] && echo " - $remaining_services agent services still loaded"
[[ $remaining_binaries -gt 0 ]] && echo " - $remaining_binaries agent binaries still found"
if [[ $remaining_binaries -gt 0 ]]; then
echo " 📋 Remaining agent binaries:"
find /usr /opt /bin /sbin 2>/dev/null -name "haproxy-agent" -type f 2>/dev/null | while read -r binary; do
echo " - $binary"
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_processes -gt 0 ]]; then
echo " 📋 Remaining agent processes:"
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 [[ $remaining_services -gt 0 ]]; then
echo " 📋 Remaining agent services:"
systemctl list-units --all 2>/dev/null | grep "haproxy-agent" || true
fi
fi
echo ""
echo "🎉 HAProxy Agent Uninstaller Completed!"
echo "=========================================="
echo "HAProxy Agent Uninstall Complete"
echo "=========================================="
echo ""
echo "📋 Complete cleanup summary:"
echo " • ✅ All agent processes terminated (including daemon mode)"
echo " • ✅ All agent systemd services stopped, disabled, and masked"
echo " • ✅ All agent binaries found and removed (deep search)"
echo " • ✅ All agent configs, logs, and data cleaned"
echo " • ✅ All agent-specific temp files and sockets removed"
echo " • ✅ Agent-related cron jobs cleaned"
echo " • ✅ Systemd daemon reloaded"
echo " • ✅ HAProxy installation and configs UNTOUCHED"
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 "🚀 System ready for fresh agent installation!"
echo "What was NOT touched:"
echo " - HAProxy service"
echo " - HAProxy configuration (/etc/haproxy/haproxy.cfg)"
echo " - HAProxy SSL certificates"
echo " - HAProxy logs"
echo ""
echo "💡 Tips:"
echo " • HAProxy service and configurations remain intact"
echo " • Run 'ps aux | grep haproxy-agent' to verify no processes remain"
echo " • Run 'systemctl list-units | grep haproxy' to verify no services remain"
echo " • Run 'systemctl status haproxy-agent' should show 'Unit not found'"
echo " • Agent can be reinstalled using the management UI"
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/"
+274 -322
View File
@@ -1,343 +1,289 @@
#!/bin/bash
# HAProxy Agent Uninstaller for macOS - Complete Agent Cleanup
# Run with: sudo ./uninstall-agent-macos.sh
# Updated for daemon mode and modern agent cleanup
set -e
# 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.
echo "=========================================="
echo "HAProxy Agent Uninstaller for macOS v5.0"
echo "Complete Agent Cleanup Edition"
echo "With Daemon Mode & Modern Agent Support"
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"
exit 1
echo "ERROR: This script must be run as root"
echo " Please run: sudo $0"
exit 1
fi
echo "🔍 Performing COMPLETE agent cleanup (HAProxy untouched)..."
echo ""
echo "Starting complete agent cleanup (HAProxy untouched)..."
echo ""
# Function to safely remove files/directories
# --------------------------------------------
# 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
echo "🗑️ Removing $desc..."
rm -rf "$path" 2>/dev/null || {
echo "⚠️ Failed to remove $desc (may not exist or no permission)"
return 1
}
echo "$desc removed"
return 0
else
echo "$desc not found"
return 1
fi
}
# Function to find and kill daemon processes
kill_daemon_processes() {
echo "🔪 Terminating ALL HAProxy Agent processes (including daemon mode)..."
local killed_count=0
# Enhanced process patterns for daemon mode
local 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"
)
for pattern in "${patterns[@]}"; do
local pids=$(pgrep -f "$pattern" 2>/dev/null || true)
if [[ -n "$pids" ]]; then
echo " 🎯 Killing processes matching: $pattern"
echo " 📋 PIDs: $pids"
# First try TERM signal
kill -TERM $pids 2>/dev/null || true
sleep 3
# Check if still running, then use KILL
local remaining_pids=$(pgrep -f "$pattern" 2>/dev/null || true)
if [[ -n "$remaining_pids" ]]; then
echo " 💀 Force killing remaining PIDs: $remaining_pids"
kill -KILL $remaining_pids 2>/dev/null || true
sleep 1
fi
killed_count=$((killed_count + $(echo "$pids" | wc -w)))
fi
done
# Also check for any remaining haproxy-agent processes
local final_check=$(pgrep -f "haproxy-agent" 2>/dev/null || true)
if [[ -n "$final_check" ]]; then
echo " 🔥 Final cleanup: killing PIDs $final_check"
kill -KILL $final_check 2>/dev/null || true
fi
if [[ $killed_count -gt 0 ]]; then
echo "✅ Terminated $killed_count agent processes"
else
echo "️ No agent processes found"
fi
}
# Function to find and remove all agent binaries dynamically
remove_agent_binaries() {
echo "🔍 Searching for agent binaries in all possible locations..."
# Common binary locations
local search_paths=(
"/usr/local/bin"
"/opt/homebrew/bin"
"/usr/bin"
"/bin"
"/opt/homebrew/sbin"
"/usr/sbin"
"/sbin"
)
local found_count=0
for path in "${search_paths[@]}"; do
if [[ -f "$path/haproxy-agent" ]]; then
safe_remove "$path/haproxy-agent" "agent binary ($path)"
((found_count++))
fi
done
# Deep search for any remaining haproxy-agent binaries
echo "🔍 Deep search for any remaining haproxy-agent binaries..."
local found_binaries
found_binaries=$(find /usr /opt /Applications 2>/dev/null -name "haproxy-agent" -type f 2>/dev/null || true)
if [[ -n "$found_binaries" ]]; then
while IFS= read -r binary; do
if [[ -f "$binary" ]]; then
safe_remove "$binary" "found agent binary ($binary)"
((found_count++))
fi
done <<< "$found_binaries"
fi
if [[ $found_count -eq 0 ]]; then
echo "️ No agent binaries found"
else
echo "✅ Removed $found_count agent binaries"
fi
}
# Function to clean up launchd services
cleanup_launchd_services() {
echo "🛑 Cleaning up agent launchd services..."
local services=(
"com.haproxy.agent"
"haproxy.agent"
"haproxy-agent"
"com.haproxy-agent"
)
for service in "${services[@]}"; do
# Check if service is loaded
if launchctl list 2>/dev/null | grep -q "$service"; then
echo " 🛑 Stopping service: $service"
launchctl stop "$service" 2>/dev/null || true
launchctl unload "/Library/LaunchDaemons/$service.plist" 2>/dev/null || true
launchctl unload "/Library/LaunchAgents/$service.plist" 2>/dev/null || true
launchctl remove "$service" 2>/dev/null || true
echo " ✅ Service $service cleaned up"
fi
done
# Remove plist files
for service in "${services[@]}"; do
safe_remove "/Library/LaunchDaemons/$service.plist" "LaunchDaemon plist ($service)"
safe_remove "/Library/LaunchAgents/$service.plist" "LaunchAgent plist ($service)"
safe_remove "~/Library/LaunchAgents/$service.plist" "User LaunchAgent plist ($service)"
done
}
# Function to clean up configuration and data
cleanup_agent_data() {
echo "🧹 Cleaning up agent configuration and data..."
# Configuration directories
local 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 config_dir in "${config_dirs[@]}"; do
safe_remove "$config_dir" "agent configuration directory ($config_dir)"
done
# Log directories
local log_dirs=(
"/var/log/haproxy-agent"
"/opt/homebrew/var/log/haproxy-agent"
"/usr/local/var/log/haproxy-agent"
"/tmp/haproxy-agent-logs"
)
for log_dir in "${log_dirs[@]}"; do
safe_remove "$log_dir" "agent log directory ($log_dir)"
done
# PID files
local 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 pid_file in "${pid_files[@]}"; do
safe_remove "$pid_file" "agent PID file ($pid_file)"
done
}
# Function to clean up temporary files
cleanup_temp_files() {
echo "🧹 Cleaning up agent-specific temporary and debug files..."
local temp_patterns=(
"/tmp/agent_debug.log"
"/tmp/debug_agent.log"
"/tmp/fixed_agent_test.log"
"/tmp/haproxy-agent-*"
"/tmp/agent-startup-with-config.sh"
"/tmp/daemon-test-agent*.sh"
"/var/tmp/haproxy-agent-*"
"/tmp/haproxy-failed-*.cfg"
"/tmp/haproxy_new_*.cfg"
"/tmp/haproxy-new-config.cfg"
"/tmp/haproxy-merged*.cfg"
"/tmp/haproxy-global*.cfg"
"/tmp/haproxy-defaults*.cfg"
"/tmp/haproxy-listen*.cfg"
)
for pattern in "${temp_patterns[@]}"; do
if [[ "$pattern" == *"*"* ]]; then
# Handle wildcard patterns
local base_dir=$(dirname "$pattern")
local file_pattern=$(basename "$pattern")
if [[ -d "$base_dir" ]]; then
local found_files=$(find "$base_dir" -maxdepth 1 -name "$file_pattern" -type f 2>/dev/null || true)
if [[ -n "$found_files" ]]; then
while IFS= read -r file; do
[[ -f "$file" ]] && safe_remove "$file" "agent temp file ($file)"
done <<< "$found_files"
fi
fi
rm -rf "$path" 2>/dev/null || true
if [[ ! -e "$path" ]]; then
echo " [REMOVED] $desc"
else
safe_remove "$pattern" "agent temp file ($(basename "$pattern"))"
echo " [WARN] Failed to remove $desc"
fi
done
# Clean up agent upgrade markers and PID files
echo "🧹 Cleaning up agent upgrade markers..."
local marker_patterns=(
"/tmp/haproxy-agent-upgrade-complete-*"
"/tmp/haproxy-agent-upgrade-test-*"
"/tmp/haproxy-agent-*.pid"
)
for pattern in "${marker_patterns[@]}"; do
local marker_dir=$(dirname "$pattern")
local marker_name=$(basename "$pattern")
if [[ -d "$marker_dir" ]]; then
local found_markers=$(find "$marker_dir" -maxdepth 1 -name "$marker_name" -type f 2>/dev/null || true)
if [[ -n "$found_markers" ]]; then
while IFS= read -r marker; do
[[ -f "$marker" ]] && safe_remove "$marker" "agent marker file ($marker)"
done <<< "$found_markers"
fi
fi
done
# Clean up agent-specific sockets
echo "🧹 Cleaning up agent-specific sockets..."
local socket_patterns=(
"/tmp/haproxy-agent-*"
"/var/run/haproxy-agent-*"
)
for pattern in "${socket_patterns[@]}"; do
local socket_dir=$(dirname "$pattern")
local socket_name=$(basename "$pattern")
if [[ -d "$socket_dir" ]]; then
local found_sockets=$(find "$socket_dir" -name "$socket_name" -type s 2>/dev/null || true)
if [[ -n "$found_sockets" ]]; then
while IFS= read -r socket; do
safe_remove "$socket" "agent socket ($socket)"
done <<< "$found_sockets"
fi
fi
done
else
echo " [SKIP] $desc (not found)"
fi
return 0
}
# Main cleanup execution
echo "🚀 Starting complete agent cleanup process..."
# --------------------------------------------
# 1. Terminate all agent processes
# --------------------------------------------
echo "[1/6] Terminating agent processes..."
# 1. Kill all processes (including daemon mode)
kill_daemon_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"
)
# 2. Stop and clean launchd services
cleanup_launchd_services
for pattern in "${patterns[@]}"; do
pids=$(pgrep -f "$pattern" 2>/dev/null || true)
if [[ -n "$pids" ]]; then
echo " Killing processes matching: $pattern (PIDs: $pids)"
kill -TERM $pids 2>/dev/null || true
sleep 2
# Force kill if still running
remaining=$(pgrep -f "$pattern" 2>/dev/null || true)
if [[ -n "$remaining" ]]; then
echo " Force killing remaining: $remaining"
kill -KILL $remaining 2>/dev/null || true
sleep 1
fi
killed_count=$((killed_count + $(echo "$pids" | wc -w)))
fi
done
# Final sweep
final_pids=$(pgrep -f "haproxy-agent" 2>/dev/null || true)
if [[ -n "$final_pids" ]]; then
echo " Final cleanup: force killing PIDs $final_pids"
kill -KILL $final_pids 2>/dev/null || true
fi
if [[ $killed_count -gt 0 ]]; then
echo " Terminated $killed_count agent process(es)"
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"
done
echo ""
# --------------------------------------------
# 3. Remove agent binaries
remove_agent_binaries
# --------------------------------------------
echo "[3/6] Removing agent binaries..."
# 4. Clean up configuration and data
cleanup_agent_data
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"
)
# 5. Clean up temporary files
cleanup_temp_files
for bp in "${bin_paths[@]}"; do
safe_remove "$bp" "binary: $bp"
done
# 6. Reload launchctl to clean up daemon list
echo "🔄 Refreshing system services..."
launchctl load /System/Library/LaunchDaemons/com.apple.periodic-daily.plist 2>/dev/null || true
# 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"
fi
echo ""
# 7. Final comprehensive verification
echo "🔍 Final agent cleanup verification..."
local remaining_processes=$(pgrep -f "haproxy-agent" 2>/dev/null | wc -l || echo "0")
local remaining_services=$(launchctl list 2>/dev/null | grep -c "haproxy.agent\|haproxy-agent" || echo "0")
local remaining_binaries=$(find /usr /opt /Applications 2>/dev/null -name "haproxy-agent" -type f 2>/dev/null | wc -l || echo "0")
# --------------------------------------------
# 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_processes -eq 0 && $remaining_services -eq 0 && $remaining_binaries -eq 0 ]]; then
echo "✅ COMPLETE AGENT CLEANUP VERIFIED - Agent completely removed!"
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 "⚠️ Some agent remnants detected:"
[[ $remaining_processes -gt 0 ]] && echo " - $remaining_processes agent processes still running"
[[ $remaining_services -gt 0 ]] && echo " - $remaining_services agent services still loaded"
[[ $remaining_binaries -gt 0 ]] && echo " - $remaining_binaries agent binaries still found"
if [[ $remaining_binaries -gt 0 ]]; then
echo " 📋 Remaining agent binaries:"
find /usr /opt /Applications 2>/dev/null -name "haproxy-agent" -type f 2>/dev/null | while read -r binary; do
echo " - $binary"
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_processes -gt 0 ]]; then
echo " 📋 Remaining agent processes:"
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
@@ -345,21 +291,27 @@ else
fi
echo ""
echo "🎉 HAProxy Agent Uninstaller Completed!"
echo "=========================================="
echo "HAProxy Agent Uninstall Complete"
echo "=========================================="
echo ""
echo "📋 Complete cleanup summary:"
echo " • ✅ All agent processes terminated (including daemon mode)"
echo " • ✅ All agent launchd services removed and unloaded"
echo " • ✅ All agent binaries found and removed (deep search)"
echo " • ✅ All agent configs, logs, and data cleaned"
echo " • ✅ All agent-specific temp files and sockets removed"
echo " • ✅ System services refreshed"
echo " • ✅ HAProxy installation and configs UNTOUCHED"
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 "🚀 System ready for fresh agent installation!"
echo "What was NOT touched:"
echo " - HAProxy service"
echo " - HAProxy configuration"
echo " - HAProxy SSL certificates"
echo " - HAProxy logs"
echo ""
echo "💡 Tips:"
echo " • HAProxy service and configurations remain intact"
echo " • Run 'ps aux | grep haproxy-agent' to verify no processes remain"
echo " • Run 'launchctl list | grep haproxy' to verify no services remain"
echo " • Agent can be reinstalled using the management UI"
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/"