#!/usr/bin/env bash # ============================================================================= # PulseGrid Metric Agent Uninstall Script # ============================================================================= # # This script completely removes the PulseGrid Metric Agent from the system. # It will stop and remove the service, delete all files and directories, # and remove the system user and group. # # WARNING: This will permanently delete all agent data and configuration! # # ============================================================================= set -o errexit set -o nounset # ============================================================================= # CONFIGURATION SECTION # ============================================================================= # Installation paths DESTDIR="/usr/local/bin" CONFIG_DIR="/etc/pulsegrid-metric-agent" DATA_DIR="/var/lib/pulsegrid-metric-agent" # Service configuration SERVICE_NAME="pulsegrid-metric-agent" SERVICE_USER="pulsegrid-metric-agent" SERVICE_GROUP="pulsegrid-metric-agent" SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service" # ============================================================================= # COLOR OUTPUT CONFIGURATION # ============================================================================= # Color codes for terminal output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # ============================================================================= # SCRIPT EXECUTION # ============================================================================= echo -e "${BLUE}[HEADER]${NC} PulseGrid Metric Agent Uninstallation" echo -e "${GREEN}[INFO]${NC} This script will completely remove the PulseGrid Metric Agent from your system." echo -e "${YELLOW}[WARNING]${NC} WARNING: This action cannot be undone!" echo # ============================================================================= # STEP 1: CHECK ROOT PRIVILEGES # ============================================================================= echo -e "${GREEN}[INFO]${NC} Checking root privileges..." if [ "$EUID" -ne 0 ]; then echo -e "${RED}[ERROR]${NC} This script must be run as root (use sudo)" echo -e "${GREEN}[INFO]${NC} Usage: sudo $0" exit 1 fi echo -e "${GREEN}[INFO]${NC} Root privileges confirmed" # ============================================================================= # STEP 2: CHECK INSTALLATION # ============================================================================= echo -e "${GREEN}[INFO]${NC} Checking for PulseGrid Metric Agent installation..." found_components=() # Check if binary exists if [ -f "$DESTDIR/pulsegrid-metric-agent" ]; then found_components+=("Binary file: $DESTDIR/pulsegrid-metric-agent") fi # Check if config directory exists if [ -d "$CONFIG_DIR" ]; then found_components+=("Configuration directory: $CONFIG_DIR") fi # Check if data directory exists if [ -d "$DATA_DIR" ]; then found_components+=("Data directory: $DATA_DIR") fi # Check if service exists if [ -f "$SERVICE_FILE" ]; then found_components+=("Systemd service: $SERVICE_FILE") fi # Check if service is running if systemctl is-active --quiet "$SERVICE_NAME" 2>/dev/null; then found_components+=("Running service: $SERVICE_NAME") fi # Check if user exists if getent passwd "$SERVICE_USER" >/dev/null 2>&1; then found_components+=("System user: $SERVICE_USER") fi # Check if group exists if getent group "$SERVICE_GROUP" >/dev/null 2>&1; then found_components+=("System group: $SERVICE_GROUP") fi if [ ${#found_components[@]} -eq 0 ]; then echo -e "${YELLOW}[WARNING]${NC} No PulseGrid Metric Agent installation found" echo -e "${GREEN}[INFO]${NC} Nothing to uninstall" exit 0 else echo -e "${YELLOW}[WARNING]${NC} Found PulseGrid Metric Agent installation:" for component in "${found_components[@]}"; do echo " - $component" done echo echo -e "${YELLOW}[WARNING]${NC} WARNING: This will permanently delete all agent data and configuration!" echo -e "${YELLOW}[WARNING]${NC} This action cannot be undone!" echo fi # ============================================================================= # STEP 3: STOP AND DISABLE SERVICE # ============================================================================= echo -e "${BLUE}[STEP]${NC} Stopping and disabling the agent service..." # Stop service if running if systemctl is-active --quiet "$SERVICE_NAME" 2>/dev/null; then echo -e "${GREEN}[INFO]${NC} Stopping running service..." systemctl stop "$SERVICE_NAME" || true echo -e "${GREEN}[INFO]${NC} ✓ Service stopped" else echo -e "${GREEN}[INFO]${NC} ✓ Service is not running" fi # Disable service if enabled if systemctl is-enabled --quiet "$SERVICE_NAME" 2>/dev/null; then echo -e "${GREEN}[INFO]${NC} Disabling service..." systemctl disable "$SERVICE_NAME" || true echo -e "${GREEN}[INFO]${NC} ✓ Service disabled" else echo -e "${GREEN}[INFO]${NC} ✓ Service is not enabled" fi echo -e "${GREEN}[SUCCESS]${NC} Service stopped and disabled" # ============================================================================= # STEP 4: REMOVE SYSTEMD SERVICE FILE # ============================================================================= echo -e "${BLUE}[STEP]${NC} Removing systemd service file..." if [ -f "$SERVICE_FILE" ]; then echo -e "${GREEN}[INFO]${NC} Removing service file: $SERVICE_FILE" rm -f "$SERVICE_FILE" echo -e "${GREEN}[INFO]${NC} ✓ Service file removed" # Reload systemd daemon systemctl daemon-reload echo -e "${GREEN}[INFO]${NC} ✓ Systemd daemon reloaded" else echo -e "${GREEN}[INFO]${NC} ✓ Service file not found" fi echo -e "${GREEN}[SUCCESS]${NC} Systemd service file removed" # ============================================================================= # STEP 5: REMOVE BINARY FILE # ============================================================================= echo -e "${BLUE}[STEP]${NC} Removing agent binary..." if [ -f "$DESTDIR/pulsegrid-metric-agent" ]; then echo -e "${GREEN}[INFO]${NC} Removing binary file: $DESTDIR/pulsegrid-metric-agent" rm -f "$DESTDIR/pulsegrid-metric-agent" echo -e "${GREEN}[INFO]${NC} ✓ Binary file removed" else echo -e "${GREEN}[INFO]${NC} ✓ Binary file not found" fi echo -e "${GREEN}[SUCCESS]${NC} Agent binary removed" # ============================================================================= # STEP 6: REMOVE CONFIGURATION AND DATA DIRECTORIES # ============================================================================= echo -e "${BLUE}[STEP]${NC} Removing configuration and data directories..." # Remove configuration directory if [ -d "$CONFIG_DIR" ]; then echo -e "${GREEN}[INFO]${NC} Removing configuration directory: $CONFIG_DIR" rm -rf "$CONFIG_DIR" echo -e "${GREEN}[INFO]${NC} ✓ Configuration directory removed" else echo -e "${GREEN}[INFO]${NC} ✓ Configuration directory not found" fi # Remove data directory if [ -d "$DATA_DIR" ]; then echo -e "${GREEN}[INFO]${NC} Removing data directory: $DATA_DIR" rm -rf "$DATA_DIR" echo -e "${GREEN}[INFO]${NC} ✓ Data directory removed" else echo -e "${GREEN}[INFO]${NC} ✓ Data directory not found" fi echo -e "${GREEN}[SUCCESS]${NC} Configuration and data directories removed" # ============================================================================= # STEP 7: REMOVE SYSTEM USER AND GROUP # ============================================================================= echo -e "${BLUE}[STEP]${NC} Removing system user and group..." # Remove user if exists if getent passwd "$SERVICE_USER" >/dev/null 2>&1; then echo -e "${GREEN}[INFO]${NC} Removing system user: $SERVICE_USER" userdel "$SERVICE_USER" || true echo -e "${GREEN}[INFO]${NC} ✓ System user removed" else echo -e "${GREEN}[INFO]${NC} ✓ System user not found" fi # Remove group if exists if getent group "$SERVICE_GROUP" >/dev/null 2>&1; then echo -e "${GREEN}[INFO]${NC} Removing system group: $SERVICE_GROUP" groupdel "$SERVICE_GROUP" || true echo -e "${GREEN}[INFO]${NC} ✓ System group removed" else echo -e "${GREEN}[INFO]${NC} ✓ System group not found" fi echo -e "${GREEN}[SUCCESS]${NC} System user and group removed" # ============================================================================= # STEP 8: CLEANUP REMAINING FILES # ============================================================================= echo -e "${BLUE}[STEP]${NC} Cleaning up any remaining files..." remaining_files=() # Check for any remaining files in common locations if [ -f "$DESTDIR/pulsegrid-metric-agent" ]; then remaining_files+=("$DESTDIR/pulsegrid-metric-agent") fi if [ -d "$CONFIG_DIR" ]; then remaining_files+=("$CONFIG_DIR") fi if [ -d "$DATA_DIR" ]; then remaining_files+=("$DATA_DIR") fi if [ -f "$SERVICE_FILE" ]; then remaining_files+=("$SERVICE_FILE") fi if [ ${#remaining_files[@]} -gt 0 ]; then echo -e "${YELLOW}[WARNING]${NC} Found remaining files that could not be removed:" for file in "${remaining_files[@]}"; do echo " - $file" done echo -e "${GREEN}[INFO]${NC} You may need to remove these files manually" else echo -e "${GREEN}[INFO]${NC} ✓ All files cleaned up successfully" fi echo -e "${GREEN}[SUCCESS]${NC} Cleanup completed" # ============================================================================= # STEP 9: VERIFY UNINSTALLATION # ============================================================================= echo -e "${BLUE}[STEP]${NC} Verifying uninstallation..." remaining_components=() # Check if binary still exists if [ -f "$DESTDIR/pulsegrid-metric-agent" ]; then remaining_components+=("Binary file") fi # Check if config directory still exists if [ -d "$CONFIG_DIR" ]; then remaining_components+=("Configuration directory") fi # Check if data directory still exists if [ -d "$DATA_DIR" ]; then remaining_components+=("Data directory") fi # Check if service file still exists if [ -f "$SERVICE_FILE" ]; then remaining_components+=("Systemd service file") fi # Check if service is still running if systemctl is-active --quiet "$SERVICE_NAME" 2>/dev/null; then remaining_components+=("Running service") fi # Check if user still exists if getent passwd "$SERVICE_USER" >/dev/null 2>&1; then remaining_components+=("System user") fi # Check if group still exists if getent group "$SERVICE_GROUP" >/dev/null 2>&1; then remaining_components+=("System group") fi if [ ${#remaining_components[@]} -eq 0 ]; then echo -e "${GREEN}[SUCCESS]${NC} Uninstallation verification completed successfully!" echo -e "${GREEN}[INFO]${NC} All PulseGrid Metric Agent components have been removed" else echo -e "${YELLOW}[WARNING]${NC} Some components could not be removed:" for component in "${remaining_components[@]}"; do echo " - $component" done echo -e "${GREEN}[INFO]${NC} You may need to remove these components manually" fi # ============================================================================= # STEP 10: SHOW UNINSTALLATION SUMMARY # ============================================================================= echo echo -e "${BLUE}[HEADER]${NC} Uninstallation Summary" echo echo "PulseGrid Metric Agent has been successfully uninstalled!" echo echo "Removed Components:" echo " - Binary file: $DESTDIR/pulsegrid-metric-agent" echo " - Configuration directory: $CONFIG_DIR" echo " - Data directory: $DATA_DIR" echo " - Systemd service: $SERVICE_FILE" echo " - System user: $SERVICE_USER" echo " - System group: $SERVICE_GROUP" echo echo "Note: All agent data and configuration have been permanently deleted." echo echo -e "${GREEN}[SUCCESS]${NC} Uninstallation completed successfully!"