-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Key improvements made: Added strict error handling with set -euo pipefail Modularized code into functions for better organization Made variables readonly and grouped them logically Added root user check Enhanced error handling and logging Added cleanup trap for error reporting Improved service management checks Added systemctl daemon-reload after service removal Made text styling more portable Added WARN level to logging Improved directory/file existence checks
- Loading branch information
1 parent
9afe078
commit 890bb31
Showing
1 changed file
with
141 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,116 +1,160 @@ | ||
#!/bin/bash | ||
|
||
set -euo pipefail # Enable strict error handling | ||
|
||
# Define the timestamp for backup and log filenames | ||
TIMESTAMP=$(date +"%Y%m%d%H%M%S") | ||
|
||
# Log file for uninstallation (with timestamp) | ||
LOGFILE="lxc_uninstall_${TIMESTAMP}.log" | ||
|
||
# Define text styles and emojis | ||
BOLD=$(tput bold) | ||
RESET=$(tput sgr0) | ||
GREEN=$(tput setaf 2) | ||
RED=$(tput setaf 1) | ||
CHECKMARK="\xE2\x9C\x85" # ✔️ | ||
CROSSMARK="\xE2\x9D\x8C" # ❌ | ||
THANKS="\xF0\x9F\x99\x8F" # 🙏 | ||
URL="\xF0\x9F\x94\x97" # 🔗 | ||
|
||
# Log function | ||
readonly TIMESTAMP=$(date +"%Y%m%d%H%M%S") | ||
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
readonly LOGFILE="/var/log/lxc_uninstall_${TIMESTAMP}.log" | ||
|
||
# Define directories and files | ||
readonly BACKUP_DIR="/etc/lxc_autoscale/backups" | ||
readonly CONFIG_FILE="/etc/lxc_autoscale/lxc_autoscale.yaml" | ||
readonly INSTALL_DIRS=( | ||
"/usr/local/bin/lxc_autoscale" | ||
"/etc/lxc_autoscale" | ||
"/var/lib/lxc_autoscale" | ||
) | ||
readonly LOG_FILES=( | ||
"/var/log/lxc_autoscale.log" | ||
"/var/log/lxc_autoscale.json" | ||
) | ||
|
||
# Define text styles and emojis using printf for better portability | ||
readonly BOLD=$(tput bold 2>/dev/null || printf '') | ||
readonly RESET=$(tput sgr0 2>/dev/null || printf '') | ||
readonly GREEN=$(tput setaf 2 2>/dev/null || printf '') | ||
readonly RED=$(tput setaf 1 2>/dev/null || printf '') | ||
readonly YELLOW=$(tput setaf 3 2>/dev/null || printf '') | ||
readonly CHECKMARK=$'\xe2\x9c\x85' | ||
readonly CROSSMARK=$'\xe2\x9d\x8c' | ||
readonly THANKS=$'\xf0\x9f\x99\x8f' | ||
readonly URL=$'\xf0\x9f\x94\x97' | ||
|
||
# Trap errors and cleanup | ||
cleanup() { | ||
local exit_code=$? | ||
if [ $exit_code -ne 0 ]; then | ||
log "ERROR" "Script failed with exit code: $exit_code" | ||
log "ERROR" "Check $LOGFILE for details" | ||
fi | ||
exit $exit_code | ||
} | ||
trap cleanup EXIT | ||
|
||
# Enhanced logging function with severity levels | ||
log() { | ||
local level="$1" | ||
local message="$2" | ||
local timestamp | ||
timestamp=$(date +"%Y-%m-%d %H:%M:%S") | ||
|
||
case $level in | ||
"INFO") | ||
echo -e "${timestamp} [${GREEN}${level}${RESET}] ${message}" | tee -a "$LOGFILE" | ||
printf "%s [%s%s%s] %s\n" "$timestamp" "${GREEN}" "$level" "${RESET}" "$message" | tee -a "$LOGFILE" | ||
;; | ||
"WARN") | ||
printf "%s [%s%s%s] %s\n" "$timestamp" "${YELLOW}" "$level" "${RESET}" "$message" | tee -a "$LOGFILE" | ||
;; | ||
"ERROR") | ||
echo -e "${timestamp} [${RED}${level}${RESET}] ${message}" | tee -a "$LOGFILE" | ||
printf "%s [%s%s%s] %s\n" "$timestamp" "${RED}" "$level" "${RESET}" "$message" | tee -a "$LOGFILE" | ||
;; | ||
esac | ||
} | ||
|
||
log "INFO" "Starting LXC AutoScale uninstallation..." | ||
|
||
# Backup the LXC AutoScale configuration file | ||
log "INFO" "Backing up the LXC AutoScale configuration file..." | ||
BACKUP_DIR="/etc/lxc_autoscale/backups" | ||
CONFIG_FILE="/etc/lxc_autoscale/lxc_autoscale.yaml" | ||
BACKUP_FILE="${BACKUP_DIR}/lxc_autoscale_backup_${TIMESTAMP}.yaml" | ||
|
||
if [ ! -d "$BACKUP_DIR" ]; then | ||
mkdir -p "$BACKUP_DIR" | ||
log "INFO" "Created backup directory at $BACKUP_DIR." | ||
fi | ||
|
||
if cp "$CONFIG_FILE" "$BACKUP_FILE"; then | ||
log "INFO" "${CHECKMARK} Successfully backed up $CONFIG_FILE to $BACKUP_FILE." | ||
else | ||
log "ERROR" "${CROSSMARK} Failed to backup $CONFIG_FILE." | ||
fi | ||
|
||
# Kill running LXC AutoScale processes | ||
log "INFO" "Killing any running LXC AutoScale processes..." | ||
if pkill -9 -f lxc_autoscale; then | ||
log "INFO" "${CHECKMARK} Successfully killed LXC AutoScale processes." | ||
else | ||
log "ERROR" "${CROSSMARK} Failed to kill LXC AutoScale processes or no processes found." | ||
fi | ||
|
||
# Stop and disable the LXC AutoScale service | ||
log "INFO" "Stopping and disabling the LXC AutoScale service..." | ||
if systemctl stop lxc_autoscale.service && systemctl disable lxc_autoscale.service; then | ||
log "INFO" "${CHECKMARK} Successfully stopped and disabled lxc_autoscale.service." | ||
else | ||
log "ERROR" "${CROSSMARK} Failed to stop or disable lxc_autoscale.service, or it was not found." | ||
fi | ||
|
||
# Remove the LXC AutoScale service file | ||
log "INFO" "Removing the LXC AutoScale service file..." | ||
if rm -f /etc/systemd/system/lxc_autoscale.service; then | ||
log "INFO" "${CHECKMARK} Successfully removed /etc/systemd/system/lxc_autoscale.service." | ||
else | ||
log "ERROR" "${CROSSMARK} Failed to remove /etc/systemd/system/lxc_autoscale.service, or it was not found." | ||
fi | ||
|
||
# Remove the LXC AutoScale script files from /usr/local/bin/lxc_autoscale/ | ||
log "INFO" "Removing the LXC AutoScale script files..." | ||
if rm -rf /usr/local/bin/lxc_autoscale/; then | ||
log "INFO" "${CHECKMARK} Successfully removed /usr/local/bin/lxc_autoscale/ directory and its contents." | ||
else | ||
log "ERROR" "${CROSSMARK} Failed to remove /usr/local/bin/lxc_autoscale/ or it was not found." | ||
fi | ||
|
||
# Remove the LXC AutoScale configuration directory | ||
log "INFO" "Removing the LXC AutoScale configuration directory..." | ||
if rm -rf /etc/lxc_autoscale/; then | ||
log "INFO" "${CHECKMARK} Successfully removed /etc/lxc_autoscale/." | ||
else | ||
log "ERROR" "${CROSSMARK} Failed to remove /etc/lxc_autoscale/, or it was not found." | ||
fi | ||
|
||
# Remove the LXC AutoScale data directory | ||
log "INFO" "Removing the LXC AutoScale data directory..." | ||
if rm -rf /var/lib/lxc_autoscale/; then | ||
log "INFO" "${CHECKMARK} Successfully removed /var/lib/lxc_autoscale/." | ||
else | ||
log "ERROR" "${CROSSMARK} Failed to remove /var/lib/lxc_autoscale/, or it was not found." | ||
fi | ||
|
||
# Remove the LXC AutoScale log files | ||
log "INFO" "Removing the LXC AutoScale log files..." | ||
LOG_FILES=("/var/log/lxc_autoscale.log" "/var/log/lxc_autoscale.json") | ||
for log_file in "${LOG_FILES[@]}"; do | ||
if rm -f "$log_file"; then | ||
log "INFO" "${CHECKMARK} Successfully removed $log_file." | ||
# Check if running as root | ||
check_root() { | ||
if [ "$(id -u)" -ne 0 ]; then | ||
log "ERROR" "This script must be run as root" | ||
exit 1 | ||
fi | ||
} | ||
|
||
# Backup configuration | ||
backup_config() { | ||
if [ ! -f "$CONFIG_FILE" ]; then | ||
log "WARN" "Configuration file not found at $CONFIG_FILE" | ||
return 0 | ||
} | ||
|
||
mkdir -p "$BACKUP_DIR" || { | ||
log "ERROR" "Failed to create backup directory at $BACKUP_DIR" | ||
return 1 | ||
} | ||
|
||
local backup_file="${BACKUP_DIR}/lxc_autoscale_backup_${TIMESTAMP}.yaml" | ||
if cp "$CONFIG_FILE" "$backup_file"; then | ||
log "INFO" "${CHECKMARK} Configuration backed up to $backup_file" | ||
return 0 | ||
else | ||
log "ERROR" "${CROSSMARK} Failed to backup configuration" | ||
return 1 | ||
fi | ||
} | ||
|
||
# Stop services and processes | ||
stop_services() { | ||
# Kill processes | ||
if pgrep -f lxc_autoscale >/dev/null; then | ||
pkill -9 -f lxc_autoscale && \ | ||
log "INFO" "${CHECKMARK} Killed LXC AutoScale processes" || \ | ||
log "ERROR" "${CROSSMARK} Failed to kill processes" | ||
else | ||
log "INFO" "No running LXC AutoScale processes found" | ||
fi | ||
|
||
# Stop and disable service | ||
if systemctl is-active lxc_autoscale.service >/dev/null 2>&1; then | ||
systemctl stop lxc_autoscale.service && \ | ||
systemctl disable lxc_autoscale.service && \ | ||
log "INFO" "${CHECKMARK} Stopped and disabled service" || \ | ||
log "ERROR" "${CROSSMARK} Failed to stop/disable service" | ||
else | ||
log "ERROR" "${CROSSMARK} Failed to remove $log_file, or it was not found." | ||
log "INFO" "Service not active or not found" | ||
fi | ||
done | ||
} | ||
|
||
# Remove installation files | ||
remove_files() { | ||
# Remove service file | ||
rm -f /etc/systemd/system/lxc_autoscale.service && \ | ||
log "INFO" "${CHECKMARK} Removed service file" || \ | ||
log "WARN" "Service file not found or couldn't be removed" | ||
|
||
# Remove installation directories | ||
for dir in "${INSTALL_DIRS[@]}"; do | ||
if [ -d "$dir" ]; then | ||
rm -rf "$dir" && \ | ||
log "INFO" "${CHECKMARK} Removed $dir" || \ | ||
log "ERROR" "${CROSSMARK} Failed to remove $dir" | ||
else | ||
log "INFO" "Directory $dir not found" | ||
fi | ||
done | ||
|
||
# Remove log files | ||
for log_file in "${LOG_FILES[@]}"; do | ||
if [ -f "$log_file" ]; then | ||
rm -f "$log_file" && \ | ||
log "INFO" "${CHECKMARK} Removed $log_file" || \ | ||
log "ERROR" "${CROSSMARK} Failed to remove $log_file" | ||
fi | ||
done | ||
} | ||
|
||
# Main execution | ||
main() { | ||
check_root | ||
log "INFO" "Starting LXC AutoScale uninstallation..." | ||
|
||
backup_config | ||
stop_services | ||
remove_files | ||
systemctl daemon-reload # Reload systemd after service removal | ||
|
||
log "INFO" "LXC AutoScale uninstallation complete!" | ||
log "INFO" "${THANKS} ${BOLD}Thank you for using LXC AutoScale!${RESET}" | ||
log "INFO" "${URL} ${BOLD}Repository: https://github.com/fabriziosalmi/proxmox-lxc-autoscale${RESET}" | ||
} | ||
|
||
log "INFO" "LXC AutoScale uninstallation complete!" | ||
log "INFO" "${THANKS} ${BOLD}Thank you for using LXC AutoScale!${RESET}" | ||
log "INFO" "${BOLD}For more information and updates, visit the repository:${RESET}" | ||
log "INFO" "${URL} ${BOLD}https://github.com/fabriziosalmi/proxmox-lxc-autoscale${RESET}" | ||
main "$@" |