Files
n8n-install/scripts/update.sh
Yury Kossakovsky 8b08442815 refactor: extract telemetry and database modules
- create scripts/telemetry.sh with all telemetry functions
- rename init_databases.sh to databases.sh and convert to library
- remove code duplication in init_all_databases()
- update all scripts to source new modules
- update documentation references
2025-12-25 18:22:05 -07:00

129 lines
4.3 KiB
Bash
Executable File

#!/bin/bash
# =============================================================================
# update.sh - Main update orchestrator
# =============================================================================
# Performs a full system and service update:
# 1. Backs up user-customizable directories (e.g., python-runner/)
# 2. Pulls latest changes from the git repository (git reset --hard + pull)
# 3. Restores backed up directories to preserve user modifications
# 4. Updates Ubuntu system packages (apt-get update && upgrade)
# 5. Delegates to apply_update.sh for service updates
#
# This two-stage approach ensures apply_update.sh itself gets updated before
# running, so new update logic is always applied.
#
# Preserved directories: Defined in PRESERVE_DIRS array in utils.sh.
# These directories contain user-customizable content that survives git reset.
#
# Usage: make update OR sudo bash scripts/update.sh
# =============================================================================
set -e
# Source the utilities file and initialize paths
source "$(dirname "$0")/utils.sh"
init_paths
# Source telemetry functions
source "$SCRIPT_DIR/telemetry.sh"
# Setup error telemetry trap for tracking failures
setup_error_telemetry_trap
# Global variable to track backup path for cleanup
BACKUP_PATH=""
# Cleanup function for interrupted updates
cleanup_on_exit() {
local exit_code=$?
if [ -n "$BACKUP_PATH" ] && [ -d "$BACKUP_PATH" ]; then
log_warning "Cleaning up backup directory: $BACKUP_PATH"
rm -rf "$BACKUP_PATH"
fi
exit $exit_code
}
trap cleanup_on_exit INT TERM
# Path to the apply_update.sh script
APPLY_UPDATE_SCRIPT="$SCRIPT_DIR/apply_update.sh"
# Check if apply update script exists
if [ ! -f "$APPLY_UPDATE_SCRIPT" ]; then
log_error "Crucial update script $APPLY_UPDATE_SCRIPT not found. Cannot proceed."
exit 1
fi
log_info "Starting update process..."
set_telemetry_stage "git_update"
# Pull the latest repository changes
log_info "Pulling latest repository changes..."
# Check if git is installed
if ! command -v git &> /dev/null; then
log_warning "'git' command not found. Skipping repository update."
# Decide if we should proceed without git pull or exit. Exiting is safer.
log_error "Cannot proceed with update without git. Please install git."
exit 1
# Or, if allowing update without pull:
# log_warning "Proceeding without pulling latest changes..."
else
# Change to project root for git pull
cd "$PROJECT_ROOT" || { log_error "Failed to change directory to $PROJECT_ROOT"; exit 1; }
# Ensure git pull.rebase is configured (fallback for older installations)
if [ -z "$(git config --global pull.rebase)" ]; then
git config --global pull.rebase true
fi
# Backup user-customizable directories before git reset (uses PRESERVE_DIRS from utils.sh)
if ! BACKUP_PATH=$(backup_preserved_dirs); then
log_error "Backup failed. Aborting update to prevent data loss."
exit 1
fi
if [ -n "$BACKUP_PATH" ]; then
log_info "Backup created at: $BACKUP_PATH"
fi
# Git operations
if ! git reset --hard HEAD; then
log_error "Git reset failed."
restore_preserved_dirs "$BACKUP_PATH"
exit 1
fi
if ! git pull; then
log_error "Git pull failed."
restore_preserved_dirs "$BACKUP_PATH"
exit 1
fi
# Restore user-customizable directories after git pull
if ! restore_preserved_dirs "$BACKUP_PATH"; then
log_error "Failed to restore user directories from backup."
log_error "Backup may still be available at: $BACKUP_PATH"
BACKUP_PATH="" # Prevent cleanup from deleting it
exit 1
fi
# Clear backup path after successful restore
BACKUP_PATH=""
fi
# Update Ubuntu packages before running apply_update
set_telemetry_stage "git_system_packages"
log_info "Updating system packages..."
if command -v apt-get &> /dev/null; then
sudo apt-get update && sudo apt-get upgrade -y
log_info "System packages updated successfully."
else
log_warning "'apt-get' not found. Skipping system package update. This is normal on non-debian systems."
fi
# Execute the rest of the update process using the (potentially updated) apply_update.sh
# Note: apply_update.sh has its own error telemetry trap and stages
bash "$APPLY_UPDATE_SCRIPT"
exit 0