Files
n8n-install/scripts/apply_update.sh
Yury Kossakovsky 20106f21f8 feat: fix file ownership after sudo installation
add step 8 to installation that restores file ownership to the invoking
user when running with sudo. also adds cleanup for legacy n8n worker
containers during updates.

- add 08_fix_permissions.sh script to detect real user and fix ownership
- add get_real_user() utility with multiple fallback detection methods
- add cleanup_legacy_n8n_workers() to remove old container naming format
- set restrictive permissions (600) on .env file for security
2025-12-16 16:07:08 -07:00

108 lines
4.4 KiB
Bash
Executable File

#!/bin/bash
# =============================================================================
# apply_update.sh - Service update and restart logic
# =============================================================================
# Called by update.sh after git pull. Performs the actual service updates:
# 1. Updates .env with any new variables (03_generate_secrets.sh --update)
# 2. Runs service selection wizard (04_wizard.sh) to update profiles
# 3. Configures services (05_configure_services.sh)
# 4. Pulls latest Docker images for selected services
# 5. Restarts all services (06_run_services.sh)
# 6. Displays final report (07_final_report.sh)
#
# Handles multiple compose files: main, n8n-workers, Supabase, and Dify.
#
# Usage: Called automatically by update.sh (not typically run directly)
# =============================================================================
set -e
# Source the utilities file and initialize paths
source "$(dirname "$0")/utils.sh"
init_paths
# Set the compose command explicitly to use docker compose subcommand
COMPOSE_CMD="docker compose"
# Path to the 06_run_services.sh script
RUN_SERVICES_SCRIPT="$SCRIPT_DIR/06_run_services.sh"
# Check if run services script exists
require_file "$RUN_SERVICES_SCRIPT" "$RUN_SERVICES_SCRIPT not found."
cd "$PROJECT_ROOT"
# --- Call 03_generate_secrets.sh in update mode ---
log_info "Ensuring .env file is up-to-date with all variables..."
bash "$SCRIPT_DIR/03_generate_secrets.sh" --update || {
log_error "Failed to update .env configuration via 03_generate_secrets.sh. Update process cannot continue."
exit 1
}
log_success ".env file updated successfully."
# --- End of .env update by 03_generate_secrets.sh ---
# --- Run Service Selection Wizard FIRST to get updated profiles ---
log_info "Running Service Selection Wizard to update service choices..."
bash "$SCRIPT_DIR/04_wizard.sh" || {
log_error "Service Selection Wizard failed. Update process cannot continue."
exit 1
}
log_success "Service selection updated."
# --- End of Service Selection Wizard ---
# --- Configure Services (prompts and .env updates) ---
log_info "Configuring services (.env updates for optional inputs)..."
bash "$SCRIPT_DIR/05_configure_services.sh" || {
log_error "Configure Services failed. Update process cannot continue."
exit 1
}
log_success "Service configuration completed."
# Clean up legacy n8n worker containers from old naming convention
cleanup_legacy_n8n_workers
# Pull latest versions of selected containers based on updated .env
log_info "Pulling latest versions of selected containers..."
COMPOSE_FILES_FOR_PULL=("-f" "$PROJECT_ROOT/docker-compose.yml")
# Check if n8n workers file exists (generated by 05_configure_services.sh)
N8N_WORKERS_COMPOSE_FILE="$PROJECT_ROOT/docker-compose.n8n-workers.yml"
if [ -f "$N8N_WORKERS_COMPOSE_FILE" ]; then
COMPOSE_FILES_FOR_PULL+=("-f" "$N8N_WORKERS_COMPOSE_FILE")
fi
# Check if Supabase directory and its docker-compose.yml exist
SUPABASE_DOCKER_DIR="$PROJECT_ROOT/supabase/docker"
SUPABASE_COMPOSE_FILE_PATH="$SUPABASE_DOCKER_DIR/docker-compose.yml"
if [ -d "$SUPABASE_DOCKER_DIR" ] && [ -f "$SUPABASE_COMPOSE_FILE_PATH" ]; then
COMPOSE_FILES_FOR_PULL+=("-f" "$SUPABASE_COMPOSE_FILE_PATH")
fi
# Check if Dify directory and its docker-compose.yaml exist
DIFY_DOCKER_DIR="$PROJECT_ROOT/dify/docker"
DIFY_COMPOSE_FILE_PATH="$DIFY_DOCKER_DIR/docker-compose.yaml"
if [ -d "$DIFY_DOCKER_DIR" ] && [ -f "$DIFY_COMPOSE_FILE_PATH" ]; then
COMPOSE_FILES_FOR_PULL+=("-f" "$DIFY_COMPOSE_FILE_PATH")
fi
# Use the project name "localai" for consistency.
# This command WILL respect COMPOSE_PROFILES from the .env file (updated by the wizard above).
$COMPOSE_CMD -p "localai" "${COMPOSE_FILES_FOR_PULL[@]}" pull --ignore-buildable || {
log_error "Failed to pull Docker images for selected services. Check network connection and Docker Hub status."
exit 1
}
# Start services using the 06_run_services.sh script
log_info "Running Services..."
bash "$RUN_SERVICES_SCRIPT" || { log_error "Failed to start services. Check logs for details."; exit 1; }
log_success "Update application completed successfully!"
# --- Display Final Report with Credentials ---
bash "$SCRIPT_DIR/07_final_report.sh" || {
log_warning "Failed to display the final report. This does not affect the update."
# We don't exit 1 here as the update itself was successful.
}
# --- End of Final Report ---
exit 0