From ae5cd1aab2eff6bf3fed65f70054f73115ba77ae Mon Sep 17 00:00:00 2001 From: Yury Kossakovsky Date: Thu, 22 May 2025 11:22:11 -0600 Subject: [PATCH] Enhance n8n import handling in apply_update.sh and update secrets generation - Added logic to determine the N8N_WORKFLOWS_IMPORTED_EVER variable based on existing environment values and user input. - Updated the apply_update.sh script to check if workflows have been imported previously, modifying the RUN_N8N_IMPORT and N8N_WORKFLOWS_IMPORTED_EVER variables accordingly. - Improved user prompts for importing n8n workflows, ensuring a smoother configuration experience. --- scripts/03_generate_secrets.sh | 20 ++++++++++-- scripts/apply_update.sh | 57 +++++++++++++++++++++++----------- 2 files changed, 57 insertions(+), 20 deletions(-) diff --git a/scripts/03_generate_secrets.sh b/scripts/03_generate_secrets.sh index 61f8a55..fd0e150 100755 --- a/scripts/03_generate_secrets.sh +++ b/scripts/03_generate_secrets.sh @@ -187,6 +187,20 @@ else fi fi +# Determine N8N_WORKFLOWS_IMPORTED_EVER based on RUN_N8N_IMPORT and existing values +N8N_WORKFLOWS_IMPORTED_EVER_VALUE="false" # Default to false +if [[ -n "${existing_env_vars[N8N_WORKFLOWS_IMPORTED_EVER]}" ]]; then + N8N_WORKFLOWS_IMPORTED_EVER_VALUE="${existing_env_vars[N8N_WORKFLOWS_IMPORTED_EVER]}" + log_info "Using existing N8N_WORKFLOWS_IMPORTED_EVER value from .env: $N8N_WORKFLOWS_IMPORTED_EVER_VALUE" +else + # If N8N_WORKFLOWS_IMPORTED_EVER is not in .env, set it based on RUN_N8N_IMPORT choice + if [[ "$RUN_N8N_IMPORT" == "true" ]]; then + N8N_WORKFLOWS_IMPORTED_EVER_VALUE="true" + fi + # No else needed, it's already defaulted to false + log_info "Setting N8N_WORKFLOWS_IMPORTED_EVER based on current import choice (or default): $N8N_WORKFLOWS_IMPORTED_EVER_VALUE" +fi + # Prompt for number of n8n workers echo "" # Add a newline for better formatting log_info "Configuring n8n worker count..." @@ -307,6 +321,7 @@ generated_values["PROMETHEUS_USERNAME"]="$USER_EMAIL" generated_values["SEARXNG_USERNAME"]="$USER_EMAIL" generated_values["LANGFUSE_INIT_USER_EMAIL"]="$USER_EMAIL" generated_values["N8N_WORKER_COUNT"]="$N8N_WORKER_COUNT" +generated_values["N8N_WORKFLOWS_IMPORTED_EVER"]="$N8N_WORKFLOWS_IMPORTED_EVER_VALUE" if [[ -n "$OPENAI_API_KEY" ]]; then generated_values["OPENAI_API_KEY"]="$OPENAI_API_KEY" fi @@ -327,6 +342,7 @@ found_vars["SEARXNG_USERNAME"]=0 found_vars["OPENAI_API_KEY"]=0 found_vars["LANGFUSE_INIT_USER_EMAIL"]=0 found_vars["N8N_WORKER_COUNT"]=0 +found_vars["N8N_WORKFLOWS_IMPORTED_EVER"]=0 # Read template, substitute domain, generate initial values while IFS= read -r line || [[ -n "$line" ]]; do @@ -372,7 +388,7 @@ while IFS= read -r line || [[ -n "$line" ]]; do # This 'else' block is for lines from template not covered by existing values or VARS_TO_GENERATE. # Check if it is one of the user input vars - these are handled by found_vars later if not in template. is_user_input_var=0 # Reset for each line - user_input_vars=("FLOWISE_USERNAME" "DASHBOARD_USERNAME" "LETSENCRYPT_EMAIL" "RUN_N8N_IMPORT" "PROMETHEUS_USERNAME" "SEARXNG_USERNAME" "OPENAI_API_KEY" "LANGFUSE_INIT_USER_EMAIL" "N8N_WORKER_COUNT") + user_input_vars=("FLOWISE_USERNAME" "DASHBOARD_USERNAME" "LETSENCRYPT_EMAIL" "RUN_N8N_IMPORT" "PROMETHEUS_USERNAME" "SEARXNG_USERNAME" "OPENAI_API_KEY" "LANGFUSE_INIT_USER_EMAIL" "N8N_WORKER_COUNT" "N8N_WORKFLOWS_IMPORTED_EVER") for uivar in "${user_input_vars[@]}"; do if [[ "$varName" == "$uivar" ]]; then is_user_input_var=1 @@ -462,7 +478,7 @@ else fi # Add any custom variables that weren't found in the template -for var in "FLOWISE_USERNAME" "DASHBOARD_USERNAME" "LETSENCRYPT_EMAIL" "RUN_N8N_IMPORT" "OPENAI_API_KEY" "PROMETHEUS_USERNAME" "SEARXNG_USERNAME" "LANGFUSE_INIT_USER_EMAIL" "N8N_WORKER_COUNT"; do +for var in "FLOWISE_USERNAME" "DASHBOARD_USERNAME" "LETSENCRYPT_EMAIL" "RUN_N8N_IMPORT" "OPENAI_API_KEY" "PROMETHEUS_USERNAME" "SEARXNG_USERNAME" "LANGFUSE_INIT_USER_EMAIL" "N8N_WORKER_COUNT" "N8N_WORKFLOWS_IMPORTED_EVER"; do if [[ ${found_vars["$var"]} -eq 0 && -v generated_values["$var"] ]]; then # Before appending, check if it's already in TMP_ENV_FILE to avoid duplicates if ! grep -q -E "^${var}=" "$TMP_ENV_FILE"; then diff --git a/scripts/apply_update.sh b/scripts/apply_update.sh index c946281..fd78b50 100755 --- a/scripts/apply_update.sh +++ b/scripts/apply_update.sh @@ -40,24 +40,45 @@ $COMPOSE_CMD pull || { log_error "Failed to pull Docker images. Check network co # Ask user about n8n import and modify .env file if [ -f "$ENV_FILE" ]; then - echo "" - read -p "Import n8n workflow? (y/n). Enter 'n' if you did it already: " import_choice - case "$import_choice" in - [yY] | [yY][eE][sS] ) - # Use a temporary file for sed portability - sed 's/^RUN_N8N_IMPORT=.*/RUN_N8N_IMPORT=true/' "$ENV_FILE" > "${ENV_FILE}.tmp" && mv "${ENV_FILE}.tmp" "$ENV_FILE" || { - log_error "Failed to set RUN_N8N_IMPORT in $ENV_FILE. Check permissions." - rm -f "${ENV_FILE}.tmp" # Clean up temp file on failure - } - ;; - * ) - # Use a temporary file for sed portability - sed 's/^RUN_N8N_IMPORT=.*/RUN_N8N_IMPORT=false/' "$ENV_FILE" > "${ENV_FILE}.tmp" && mv "${ENV_FILE}.tmp" "$ENV_FILE" || { - log_error "Failed to set RUN_N8N_IMPORT in $ENV_FILE. Check permissions." - rm -f "${ENV_FILE}.tmp" # Clean up temp file on failure - } - ;; - esac + N8N_WORKFLOWS_IMPORTED_EVER=$(grep "^N8N_WORKFLOWS_IMPORTED_EVER=" "$ENV_FILE" | cut -d'=' -f2 | tr -d '"' || echo "false") + + if [[ "$N8N_WORKFLOWS_IMPORTED_EVER" == "true" ]]; then + log_info "n8n workflows have been imported previously. Skipping import prompt." + # Use a temporary file for sed portability + sed 's/^RUN_N8N_IMPORT=.*/RUN_N8N_IMPORT=false/' "$ENV_FILE" > "${ENV_FILE}.tmp" && mv "${ENV_FILE}.tmp" "$ENV_FILE" || { + log_error "Failed to set RUN_N8N_IMPORT=false in $ENV_FILE. Check permissions." + rm -f "${ENV_FILE}.tmp" + } + else + echo "" + read -p "Import n8n workflow? (y/n) : " import_choice + case "$import_choice" in + [yY] | [yY][eE][sS] ) + # Use a temporary file for sed portability + sed 's/^RUN_N8N_IMPORT=.*/RUN_N8N_IMPORT=true/' "$ENV_FILE" > "${ENV_FILE}.tmp" && mv "${ENV_FILE}.tmp" "$ENV_FILE" || { + log_error "Failed to set RUN_N8N_IMPORT=true in $ENV_FILE. Check permissions." + rm -f "${ENV_FILE}.tmp" + } + # Update N8N_WORKFLOWS_IMPORTED_EVER to true + if grep -q "^N8N_WORKFLOWS_IMPORTED_EVER=" "$ENV_FILE"; then + sed 's/^N8N_WORKFLOWS_IMPORTED_EVER=.*/N8N_WORKFLOWS_IMPORTED_EVER=true/' "$ENV_FILE" > "${ENV_FILE}.tmp" && mv "${ENV_FILE}.tmp" "$ENV_FILE" || { + log_error "Failed to set N8N_WORKFLOWS_IMPORTED_EVER=true in $ENV_FILE. Check permissions." + rm -f "${ENV_FILE}.tmp" + } + else + echo "N8N_WORKFLOWS_IMPORTED_EVER=true" >> "$ENV_FILE" + fi + ;; + * ) + # Use a temporary file for sed portability + sed 's/^RUN_N8N_IMPORT=.*/RUN_N8N_IMPORT=false/' "$ENV_FILE" > "${ENV_FILE}.tmp" && mv "${ENV_FILE}.tmp" "$ENV_FILE" || { + log_error "Failed to set RUN_N8N_IMPORT=false in $ENV_FILE. Check permissions." + rm -f "${ENV_FILE}.tmp" + } + # N8N_WORKFLOWS_IMPORTED_EVER remains false (or its current state if already in file) + ;; + esac + fi # Ask user about n8n worker count if grep -q "^N8N_WORKER_COUNT=" "$ENV_FILE"; then