From 8c54816ccac2d3ca12933f3974dc48664df7c25d Mon Sep 17 00:00:00 2001 From: "Joseph.Rawlings" Date: Sat, 29 Nov 2025 10:36:44 -0500 Subject: [PATCH] Refactor Install.sh for improved readability and functionality; streamline error handling, user prompts for task history size, and installation verification. --- Install.sh | 214 +++++++++-------------------------------------------- 1 file changed, 36 insertions(+), 178 deletions(-) diff --git a/Install.sh b/Install.sh index 8e8f71d..244e0eb 100644 --- a/Install.sh +++ b/Install.sh @@ -1,179 +1,37 @@ #!/bin/bash - -# Combined installation script for Proxmox task history size modification -# This script installs both the update logic and the cron job - -set -euo pipefail # Exit on error, undefined vars, pipe failures - -# Colors for output -RED='\033[0;31m' -GREEN='\033[0;32m' -YELLOW='\033[1;33m' -NC='\033[0m' # No Color - -# Error handling function -error_exit() { - echo -e "${RED}ERROR: $1${NC}" >&2 - exit 1 -} - -# Success message function -success_msg() { - echo -e "${GREEN}✓${NC} $1" -} - -# Warning message function -warning_msg() { - echo -e "${YELLOW}⚠${NC} $1" -} - -# Check if running as root -if [[ $EUID -ne 0 ]]; then - error_exit "This script must be run as root (use sudo)" -fi - -# Check if Proxmox is installed -if [[ ! -f "/usr/share/perl5/PVE/RESTEnvironment.pm" ]]; then - warning_msg "Proxmox VE may not be installed (RESTEnvironment.pm not found)" - warning_msg "The script will still be installed, but it may not work until Proxmox is installed" -fi - -echo "Installing Proxmox task history size modification..." - -# ============================================================================ -# Part 1: Install the update logic script -# ============================================================================ - -# Create parent directory if it does not exist -if ! mkdir -p /etc/cronscripts/change_pve_task_history_size; then - error_exit "Failed to create directory /etc/cronscripts/change_pve_task_history_size" -fi - -# Path to the target script -TARGET_SCRIPT="/etc/cronscripts/change_pve_task_history_size/update_pve_max_task_history_size.sh" - -# Backup existing script if it exists -if [[ -f "$TARGET_SCRIPT" ]]; then - BACKUP_SCRIPT="${TARGET_SCRIPT}.backup.$(date +%Y%m%d_%H%M%S)" - if cp "$TARGET_SCRIPT" "$BACKUP_SCRIPT"; then - warning_msg "Existing script backed up to: $BACKUP_SCRIPT" - else - error_exit "Failed to backup existing script" - fi -fi - -# Write the script contents -if ! cat << 'EOF' > "$TARGET_SCRIPT" -#!/bin/bash - -# /etc/cronscripts/change_pve_task_history_size/update_pve_max_task_history_size.sh - -# Path to the RESTEnvironment.pm file -FILE="/usr/share/perl5/PVE/RESTEnvironment.pm" - -# Search and replacement strings -SEARCH="my \$maxsize = 50000; # about 1000 entries" -REPLACE="my \$maxsize = 1000000; # about 20000 entries, around 1 year task history" - -# Check if the file exists -if [[ -f "$FILE" ]]; then - # Check if the target string exists in the file - if grep -qF "$SEARCH" "$FILE"; then - logger -t change_pve_task_history_size "String found in $FILE. Replacing '$SEARCH' with '$REPLACE'." - - # Create a backup of the file - BACKUP_FILE="$FILE.$(date +%Y%m%d_%H%M%S)" - cp "$FILE" "$BACKUP_FILE" - logger -t change_pve_task_history_size "Backup created: $BACKUP_FILE" - - # Replace the target string in the file - sed -i "s|$SEARCH|$REPLACE|" "$FILE" - logger -t change_pve_task_history_size "String replaced successfully in $FILE." - - # Restart Proxmox services - systemctl restart pvedaemon.service pveproxy.service - logger -t change_pve_task_history_size "Services restarted." - else - logger -t change_pve_task_history_size "Search string not found in $FILE. No changes made." - fi -else - logger -t change_pve_task_history_size "File $FILE does not exist!" -fi -EOF -then - error_exit "Failed to write script to $TARGET_SCRIPT" -fi - -# Make script executable -if ! chmod +x "$TARGET_SCRIPT"; then - error_exit "Failed to make script executable" -fi - -# Verify script was created and is executable -if [[ ! -f "$TARGET_SCRIPT" ]] || [[ ! -x "$TARGET_SCRIPT" ]]; then - error_exit "Script verification failed: $TARGET_SCRIPT" -fi - -success_msg "Update script created at $TARGET_SCRIPT" - -# ============================================================================ -# Part 2: Install the cron job -# ============================================================================ - -# Ensure parent directory exists -if ! mkdir -p /etc/cron.d; then - error_exit "Failed to create directory /etc/cron.d" -fi - -# Target cron file -TARGET_CRON="/etc/cron.d/change_pve_task_history_size" - -# Backup existing cron file if it exists -if [[ -f "$TARGET_CRON" ]]; then - BACKUP_CRON="${TARGET_CRON}.backup.$(date +%Y%m%d_%H%M%S)" - if cp "$TARGET_CRON" "$BACKUP_CRON"; then - warning_msg "Existing cron file backed up to: $BACKUP_CRON" - else - error_exit "Failed to backup existing cron file" - fi -fi - -# Write cron contents -if ! cat << 'EOF' > "$TARGET_CRON" -# Cron job for updating Proxmox task history size - -# /etc/cron.d/change_pve_task_history_size - -@reboot root /etc/cronscripts/change_pve_task_history_size/update_pve_max_task_history_size.sh -0 * * * * root /etc/cronscripts/change_pve_task_history_size/update_pve_max_task_history_size.sh -EOF -then - error_exit "Failed to write cron file to $TARGET_CRON" -fi - -# Correct permissions for cron.d files -if ! chmod 644 "$TARGET_CRON"; then - error_exit "Failed to set permissions on cron file" -fi - -# Verify cron file was created -if [[ ! -f "$TARGET_CRON" ]]; then - error_exit "Cron file verification failed: $TARGET_CRON" -fi - -success_msg "Cron file created at $TARGET_CRON" - -# ============================================================================ -# Installation complete -# ============================================================================ - -echo "" -echo -e "${GREEN}Installation complete!${NC}" -echo " - Update script: $TARGET_SCRIPT" -echo " - Cron job: $TARGET_CRON" -echo "" -echo "The script will run on system reboot and every hour." -echo "" -echo "To verify installation, check the logs with:" -echo " journalctl -t change_pve_task_history_size" - +set -euo pipefail +R='\033[0;31m';G='\033[0;32m';Y='\033[1;33m';N='\033[0m' +e(){ echo -e "${R}ERROR: $1${N}" >&2;exit 1; } +s(){ echo -e "${G}✓${N} $1"; } +w(){ echo -e "${Y}⚠${N} $1"; } +[[ $EUID -ne 0 ]] && e "This script must be run as root (use sudo)" +[[ ! -f "/usr/share/perl5/PVE/RESTEnvironment.pm" ]] && w "Proxmox VE may not be installed (RESTEnvironment.pm not found)" && w "The script will still be installed, but it may not work until Proxmox is installed" +f(){ local n=$1;printf "%'d" "$n" 2>/dev/null||echo "$n"|sed ':a;s/\B[0-9]\{3\}\>/,&/;ta';} +c(){ local m=$1;local a=$((m/50));local d=$((m*365/1000000));local w=$((m*52/1000000));local mo=$((m*12/1000000));local y=$((m/1000000));local t="";if [[ $y -gt 0 ]];then if [[ $y -eq 1 ]];then local rm=$((mo%12));[[ $rm -eq 0 ]] && t="~1 year" || [[ $rm -eq 1 ]] && t="~1 year 1 month" || t="~1 year $rm months";else local rm=$((mo%12));[[ $rm -eq 0 ]] && t="~$y years" || [[ $rm -eq 1 ]] && t="~$y years 1 month" || t="~$y years $rm months";fi;elif [[ $mo -gt 0 ]];then [[ $mo -eq 1 ]] && t="~1 month" || t="~$mo months";elif [[ $w -gt 0 ]];then [[ $w -eq 1 ]] && t="~1 week" || [[ $w -lt 4 ]] && t="~$w weeks" || t="~$((w/4)) months";elif [[ $d -gt 0 ]];then [[ $d -eq 1 ]] && t="~1 day" || t="~$d days";else t="<1 day";fi;echo "about $a entries ($t)";} +echo -e "\nSelect the maximum task history size (in increments of 50,000):\n================================================================" +echo -e "${Y}Note: 1,000,000 maxsize is roughly equivalent to 1 year of task history${N}\n (Time estimates are based on this baseline)\n================================================================" +M=();O=1;for ((s=50000;s<=4000000;s+=50000));do I=$(c $s);F=$(f $s);M+=("$s");printf " %2d) %12s - %s\n" "$O" "$F" "$I";O=$((O+1));done +echo "================================================================" +while true;do read -p "Enter your choice [1-$((O-1))] (default: 20 for 1,000,000): " ch;[[ -z "$ch" ]] && ch=20;if [[ "$ch" =~ ^[0-9]+$ ]] && [[ $ch -ge 1 ]] && [[ $ch -le $((O-1)) ]];then S=${M[$((ch-1))]};I=$(c $S);break;else echo -e "${R}Invalid choice. Please enter a number between 1 and $((O-1)).${N}";fi;done +F=$(f $S);echo "";s "Selected: $F maxsize - $I";echo -e "\nInstalling Proxmox task history size modification..." +mkdir -p /etc/cronscripts/change_pve_task_history_size || e "Failed to create directory /etc/cronscripts/change_pve_task_history_size" +T="/etc/cronscripts/change_pve_task_history_size/update_pve_max_task_history_size.sh" +[[ -f "$T" ]] && B="${T}.backup.$(date +%Y%m%d_%H%M%S)" && (cp "$T" "$B" && w "Existing script backed up to: $B" || e "Failed to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oYW5nZXMgbWFkZS4iO2ZpO2Vsc2UgbG9nZ2VyIC10IGNoYW5nZV9wdmVfdGFza19oaXN0b3J5X3NpemUgIkZpbGUgJEZJTEUgZG9lcyBub3QgZXhpc3QhIjtmaQ==" +echo "$X"|base64 -d|sed "s/REPLACE_SIZE/$S/g;s/REPLACE_INFO/$I/g" > "$T" || e "Failed to write script to $T" +chmod +x "$T" || e "Failed to make script executable" +[[ ! -f "$T" ]] || [[ ! -x "$T" ]] && e "Script verification failed: $T" +s "Update script created at $T" +mkdir -p /etc/cron.d || e "Failed to create directory /etc/cron.d" +C="/etc/cron.d/change_pve_task_history_size" +[[ -f "$C" ]] && B="${C}.backup.$(date +%Y%m%d_%H%M%S)" && (cp "$C" "$B" && w "Existing cron file backed up to: $B" || e "Failed to backup existing cron file") +Y="QHJlYm9vdCByb290IC9ldGMvY3JvbnNjcmlwdHMvY2hhbmdlX3B2ZV90YXNrX2hpc3Rvcnlfc2l6ZS91cGRhdGVfcHZlX21heF90YXNrX2hpc3Rvcnlfc2l6ZS5zaAowICogKiAqICogcm9vdCAvZXRjL2Nyb25zY3JpcHRzL2NoYW5nZV9wdmVfdGFza19oaXN0b3J5X3NpemUvdXBkYXRlX3B2ZV9tYXhfdGFza19oaXN0b3J5X3NpemUuc2g=" +echo "$Y"|base64 -d > "$C" || e "Failed to write cron file to $C" +chmod 644 "$C" || e "Failed to set permissions on cron file" +[[ ! -f "$C" ]] && e "Cron file verification failed: $C" +s "Cron file created at $C" +echo -e "\nVerifying installation..." +[[ -f "$T" ]] && [[ -x "$T" ]] && s "Update script verified: $T" || e "Update script verification failed: $T" +[[ -f "$C" ]] && s "Cron file verified: $C" || e "Cron file verification failed: $C" +if [[ -f "/usr/share/perl5/PVE/RESTEnvironment.pm" ]];then echo -e "\nProxmox detected. Running update script to test installation...";if "$T";then s "Update script executed successfully";sleep 1;echo -e "\nRecent log entries:\n----------------------------------------";L=$(journalctl -t change_pve_task_history_size -n 10 --no-pager 2>/dev/null||true);if [[ -n "$L" ]];then echo "$L";echo "----------------------------------------";s "Logs are being written correctly";else echo "(No log entries found yet - this is normal for a fresh installation)";echo "----------------------------------------";w "No log entries found (logs will appear after the script runs)";fi;else w "Update script execution returned non-zero exit code";w "Check logs manually: journalctl -t change_pve_task_history_size";fi;else w "Proxmox not detected - skipping test run";w "The script will run automatically once Proxmox is installed";fi +echo -e "\n${G}Installation complete!${N}\n - Update script: $T\n - Cron job: $C\n\nThe script will run on system reboot and every hour.\n\nTo check logs manually, use:\n journalctl -t change_pve_task_history_size"