From a3d0dfd6650c6860a91d4b1a0118b9bd4a5863a2 Mon Sep 17 00:00:00 2001 From: Muhammad Ibrahim Date: Fri, 10 Oct 2025 21:52:57 +0100 Subject: [PATCH] Fixed entrypoint to handle better updating of Agent mechanism Updated Readme to show the --update flag --- README.md | 9 ++- docker/backend.docker-entrypoint.sh | 95 ++++++++++++++++++++++++++--- 2 files changed, 92 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 840d0f0..2cd6f83 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ PatchMon provides centralized patch management across diverse server environment ### API & Integrations - REST API under `/api/v1` with JWT auth -- **Proxmox LXC Auto-Enrollment** - Automatically discover and enroll LXC containers from Proxmox hosts ([Documentation](PROXMOX_AUTO_ENROLLMENT.md)) +- Proxmox LXC Auto-Enrollment - Automatically discover and enroll LXC containers from Proxmox hosts ### Security - Rate limiting for general, auth, and agent endpoints @@ -85,11 +85,16 @@ apt-get upgrade -y apt install curl -y ``` -#### Script +#### Install Script ```bash curl -fsSL -o setup.sh https://raw.githubusercontent.com/PatchMon/PatchMon/refs/heads/main/setup.sh && chmod +x setup.sh && bash setup.sh ``` +#### Update Script (--update flag) +```bash +curl -fsSL -o setup.sh https://raw.githubusercontent.com/PatchMon/PatchMon/refs/heads/main/setup.sh && chmod +x setup.sh && bash setup.sh --update +``` + #### Minimum specs for building : ##### CPU : 2 vCPU RAM : 2GB diff --git a/docker/backend.docker-entrypoint.sh b/docker/backend.docker-entrypoint.sh index 486f05d..9f1a59d 100755 --- a/docker/backend.docker-entrypoint.sh +++ b/docker/backend.docker-entrypoint.sh @@ -8,19 +8,94 @@ log() { echo "[$(date +'%Y-%m-%d %H:%M:%S')] $*" >&2 } -# Copy files from agents_backup to agents if agents directory is empty and no .sh files are present -if [ -d "/app/agents" ] && [ -z "$(find /app/agents -maxdepth 1 -type f -name '*.sh' | head -n 1)" ]; then - if [ -d "/app/agents_backup" ]; then - log "Agents directory is empty, copying from backup..." - cp -r /app/agents_backup/* /app/agents/ +# Function to extract version from agent script +get_agent_version() { + local file="$1" + if [ -f "$file" ]; then + grep -m 1 '^AGENT_VERSION=' "$file" | cut -d'"' -f2 2>/dev/null || echo "0.0.0" else - log "Warning: agents_backup directory not found" + echo "0.0.0" fi -else - log "Agents directory already contains files, skipping copy" -fi +} -log "Starting PatchMon Backend (${NODE_ENV:-production})..." +# Function to compare versions (returns 0 if $1 > $2) +version_greater() { + # Use sort -V for version comparison + test "$(printf '%s\n' "$1" "$2" | sort -V | tail -n1)" = "$1" && test "$1" != "$2" +} + +# Check and update agent files if necessary +update_agents() { + local backup_agent="/app/agents_backup/patchmon-agent.sh" + local current_agent="/app/agents/patchmon-agent.sh" + + # Check if agents directory exists + if [ ! -d "/app/agents" ]; then + log "ERROR: /app/agents directory not found" + return 1 + fi + + # Check if backup exists + if [ ! -d "/app/agents_backup" ]; then + log "WARNING: agents_backup directory not found, skipping agent update" + return 0 + fi + + # Get versions + local backup_version=$(get_agent_version "$backup_agent") + local current_version=$(get_agent_version "$current_agent") + + log "Agent version check:" + log " Image version: ${backup_version}" + log " Volume version: ${current_version}" + + # Determine if update is needed + local needs_update=0 + + # Case 1: No agents in volume (first time setup) + if [ -z "$(find /app/agents -maxdepth 1 -type f -name '*.sh' 2>/dev/null | head -n 1)" ]; then + log "Agents directory is empty - performing initial copy" + needs_update=1 + # Case 2: Backup version is newer + elif version_greater "$backup_version" "$current_version"; then + log "Newer agent version available (${backup_version} > ${current_version})" + needs_update=1 + else + log "Agents are up to date" + needs_update=0 + fi + + # Perform update if needed + if [ $needs_update -eq 1 ]; then + log "Updating agents to version ${backup_version}..." + + # Create backup of existing agents if they exist + if [ -f "$current_agent" ]; then + local backup_timestamp=$(date +%Y%m%d_%H%M%S) + local backup_name="/app/agents/patchmon-agent.sh.backup.${backup_timestamp}" + cp "$current_agent" "$backup_name" 2>/dev/null || true + log "Previous agent backed up to: $(basename $backup_name)" + fi + + # Copy new agents + cp -r /app/agents_backup/* /app/agents/ + + # Verify update + local new_version=$(get_agent_version "$current_agent") + if [ "$new_version" = "$backup_version" ]; then + log "✅ Agents successfully updated to version ${new_version}" + else + log "⚠️ Warning: Agent update may have failed (expected: ${backup_version}, got: ${new_version})" + fi + fi +} + +# Main execution +log "PatchMon Backend Container Starting..." +log "Environment: ${NODE_ENV:-production}" + +# Update agents (version-aware) +update_agents log "Running database migrations..." npx prisma migrate deploy