tweaks to the scripts
All checks were successful
Check scripts syntax / check-scripts-syntax (push) Successful in 41s

This commit is contained in:
2025-08-24 20:55:14 -03:00
parent 45567b2242
commit eb8ca78f4f
3 changed files with 58 additions and 121 deletions

View File

@@ -4,7 +4,6 @@
#
# Description: Automatically updates Docker containers and manages Docker images
# Features:
# - Self-updating capability
# - Updates all Docker Compose projects in /root/docker
# - Skips containers with .ignore file
# - Removes obsolete Docker Compose version attributes
@@ -13,6 +12,23 @@
# Version: 2.0
set -euo pipefail # Exit on error, undefined vars, and pipe failures
HOSTNAME=$(cat /etc/hostname)
NOTIFY_URL_ERROR="http://notify.haven/template/notify/error"
NOTIFY_URL_UPDATE="http://notify.haven/template/notify/update"
send_error_notification() {
local message="$1"
local critical="$2"
curl -s -X POST "$NOTIFY_URL_ERROR" \
-H "Content-Type: application/json" \
-d "{\"caller\": \"$HOSTNAME\", \"message\": \"$message\", \"critical\": $critical}"
}
send_update_notification() {
local message="$1"
local script_time="$2"
curl -s -X POST "$NOTIFY_URL_UPDATE" \
-H "Content-Type: application/json" \
-d "{\"host\": \"$HOSTNAME\", \"asset\": \"Docker containers\", \"time\": $script_time}"
}
#==============================================================================
# CONFIGURATION
@@ -28,8 +44,6 @@ readonly LIGHT_GREY='\033[0;37m'
readonly YELLOW='\033[1;33m'
# Script configuration
readonly SCRIPT_NAME="docker-updater.sh"
readonly SERVER_BASE_URL="https://git.ivanch.me/ivanch/server-scripts/raw/branch/main"
readonly DOCKER_FOLDER="/root/docker"
readonly COMPOSE_FILES=("docker-compose.yml" "docker-compose.yaml" "compose.yaml" "compose.yml")
@@ -43,11 +57,18 @@ readonly AUTO_UPDATE_ENABLED=true
# Print formatted log messages
log_info() { echo -e "${LIGHT_GREY}[i] $1${NC}"; }
log_success() { echo -e "${LIGHT_GREEN}[✓] $1${NC}"; }
log_warning() { echo -e "${YELLOW}[!] $1${NC}"; }
log_error() { echo -e "${RED}[x] $1${NC}" >&2; }
log_step() { echo -e "${LIGHT_BLUE}[i] $1${NC}"; }
log_container() { echo -e "${LIGHT_BLUE}[$1] $2${NC}"; }
log_warning() {
echo -e "${YELLOW}[!] $1${NC}";
send_error_notification "$1" false
}
log_error() {
echo -e "${RED}[x] $1${NC}" >&2;
send_error_notification "$1" true
}
# Exit with error message
die() {
log_error "$1"
@@ -86,69 +107,6 @@ get_url_hash() {
curl -s "$url" 2>/dev/null | sha256sum | awk '{print $1}' || echo ""
}
# Check if server file is accessible
check_server_connectivity() {
local url="$1"
curl -s --head "$url" | head -n 1 | grep -E "HTTP/[12] [23].." >/dev/null 2>&1
}
#==============================================================================
# AUTO-UPDATE FUNCTIONALITY
#==============================================================================
# Perform self-update if newer version is available
perform_self_update() {
if [[ "$AUTO_UPDATE_ENABLED" != "true" ]]; then
log_info "Auto-update is disabled"
return 0
fi
local server_url="$SERVER_BASE_URL/$SCRIPT_NAME"
log_step "Checking for script updates..."
# Check if server file is accessible
if ! check_server_connectivity "$server_url"; then
log_warning "Cannot connect to update server, continuing with current version"
return 0
fi
# Compare local and server file hashes
local local_hash
local server_hash
local_hash=$(get_file_hash "$SCRIPT_NAME")
server_hash=$(get_url_hash "$server_url")
if [[ -z "$local_hash" || -z "$server_hash" ]]; then
log_warning "Cannot determine file hashes, skipping update"
return 0
fi
if [[ "$local_hash" != "$server_hash" ]]; then
log_info "Update available, downloading new version..."
# Create backup of current script
local backup_file="${SCRIPT_NAME}.backup.$(date +%s)"
cp "$SCRIPT_NAME" "$backup_file" || die "Failed to create backup"
# Download updated script
if curl -s -o "$SCRIPT_NAME" "$server_url"; then
chmod +x "$SCRIPT_NAME" || die "Failed to set executable permissions"
log_success "Script updated successfully"
log_step "Running updated script..."
exec ./"$SCRIPT_NAME" "$@"
else
# Restore backup on failure
mv "$backup_file" "$SCRIPT_NAME"
die "Failed to download updated script"
fi
else
log_success "Script is already up to date"
fi
}
#==============================================================================
# DOCKER COMPOSE MANAGEMENT
#==============================================================================
@@ -315,6 +273,8 @@ cleanup_docker_resources() {
#==============================================================================
main() {
START_TIME=$(date +%s)
log_step "Starting Docker Container Updater"
echo
@@ -332,6 +292,12 @@ main() {
echo
log_success "Docker container update process completed!"
END_TIME=$(date +%s)
DURATION=$((END_TIME - START_TIME))
log_info "Total duration: $DURATION seconds"
send_update_notification $DURATION
}
# Execute main function with all arguments