From 0396f8bb29d65c49bc7eb19087676b8877d7b649 Mon Sep 17 00:00:00 2001 From: Yury Kossakovsky Date: Thu, 28 Aug 2025 18:47:02 -0600 Subject: [PATCH] Enhance service configuration script for improved user interaction - Refactored prompts for OpenAI API key and n8n workflow import settings in `05_configure_services.sh` to streamline user experience. - Improved validation and handling of n8n worker count input, ensuring robust configuration. - Added logic to manage Cloudflare Tunnel token input more effectively, reusing existing tokens when available. - Updated logging for better clarity during configuration steps, enhancing overall maintainability of the script. --- scripts/05_configure_services.sh | 238 +++++++++++++++---------------- 1 file changed, 119 insertions(+), 119 deletions(-) diff --git a/scripts/05_configure_services.sh b/scripts/05_configure_services.sh index 762371f..0d63cc7 100644 --- a/scripts/05_configure_services.sh +++ b/scripts/05_configure_services.sh @@ -37,143 +37,143 @@ write_env_var() { log_info "Configuring service options in .env..." -# --------------------------- -# OPENAI_API_KEY (optional) -# --------------------------- -EXISTING_OPENAI_API_KEY="$(read_env_var OPENAI_API_KEY)" -if [ -z "$EXISTING_OPENAI_API_KEY" ]; then - echo "" - echo "OpenAI API Key (optional). This key will be used for:" - echo " - Supabase: AI services to help with writing SQL queries, statements, and policies" - echo " - Crawl4AI: Default LLM configuration for web crawling capabilities" - echo " You can skip this by leaving it empty." - echo "" - read -p "OpenAI API Key: " INPUT_OPENAI_API_KEY - write_env_var OPENAI_API_KEY "$INPUT_OPENAI_API_KEY" -else - # Keep as-is if set (even if user wants to change later, they can edit .env) - log_info "OPENAI_API_KEY already set in .env; leaving unchanged." +# Prompt for OpenAI API key (optional) +if [[ ! -v existing_env_vars[OPENAI_API_KEY] || -z "${existing_env_vars[OPENAI_API_KEY]}" ]]; then + echo "" + echo "OpenAI API Key (optional). This key will be used for:" + echo " - Supabase: AI services to help with writing SQL queries, statements, and policies" + echo " - Crawl4AI: Default LLM configuration for web crawling capabilities" + echo " You can skip this by leaving it empty." fi -# --------------------------- -# RUN_N8N_IMPORT (300 workflows) -# --------------------------- -EXISTING_RUN_N8N_IMPORT="$(read_env_var RUN_N8N_IMPORT)" -if [ -n "$EXISTING_RUN_N8N_IMPORT" ]; then - echo "" - read -p "Change n8n workflows import setting? Current: $EXISTING_RUN_N8N_IMPORT (y/N): " CHANGE_IMPORT - if [[ "$CHANGE_IMPORT" =~ ^[Yy]$ ]]; then - echo "" - echo "Do you want to import 300 ready-made workflows for n8n? This process may take about 30 minutes to complete." - echo "" - read -p "Import workflows? (y/n): " import_workflow_choice - if [[ "$import_workflow_choice" =~ ^[Yy]$ ]]; then - write_env_var RUN_N8N_IMPORT "true" +if [[ -v existing_env_vars[OPENAI_API_KEY] ]]; then # -v checks if variable is set (even if empty) + OPENAI_API_KEY="${existing_env_vars[OPENAI_API_KEY]}" + if [[ -n "$OPENAI_API_KEY" ]]; then : # Fix: Add null command for empty 'then' block else - write_env_var RUN_N8N_IMPORT "false" + log_info "Found empty OpenAI API Key in .env. You can provide one now or leave empty." + echo "" + read -p "OpenAI API Key: " OPENAI_API_KEY # Allow update if it was empty fi - else - log_info "Keeping existing RUN_N8N_IMPORT value: $EXISTING_RUN_N8N_IMPORT" - fi else - echo "" - echo "Do you want to import 300 ready-made workflows for n8n? This process may take about 30 minutes to complete." - echo "" - read -p "Import workflows? (y/n): " import_workflow_choice - if [[ "$import_workflow_choice" =~ ^[Yy]$ ]]; then - write_env_var RUN_N8N_IMPORT "true" - else - write_env_var RUN_N8N_IMPORT "false" - fi + echo "" + read -p "OpenAI API Key: " OPENAI_API_KEY fi -# --------------------------- -# N8N_WORKER_COUNT -# --------------------------- -EXISTING_N8N_WORKER_COUNT="$(read_env_var N8N_WORKER_COUNT)" -if [ -n "$EXISTING_N8N_WORKER_COUNT" ]; then - echo "" - read -p "Do you want to change the number of n8n workers? Current: $EXISTING_N8N_WORKER_COUNT. (Enter new number, or press Enter to keep current): " N8N_WORKER_COUNT_INPUT_RAW - if [ -z "$N8N_WORKER_COUNT_INPUT_RAW" ]; then - log_info "Keeping N8N_WORKER_COUNT at $EXISTING_N8N_WORKER_COUNT." - else - if [[ "$N8N_WORKER_COUNT_INPUT_RAW" =~ ^0*[1-9][0-9]*$ ]]; then - N8N_WORKER_COUNT_TEMP="$((10#$N8N_WORKER_COUNT_INPUT_RAW))" - if [ "$N8N_WORKER_COUNT_TEMP" -ge 1 ]; then - echo "" - read -p "Update n8n workers to $N8N_WORKER_COUNT_TEMP? (y/N): " confirm_change - if [[ "$confirm_change" =~ ^[Yy]$ ]]; then - write_env_var N8N_WORKER_COUNT "$N8N_WORKER_COUNT_TEMP" +# Logic for n8n workflow import (RUN_N8N_IMPORT) +echo "" + +final_run_n8n_import_decision="false" + +echo "Do you want to import 300 ready-made workflows for n8n? This process may take about 30 minutes to complete." +echo "" +read -p "Import workflows? (y/n): " import_workflow_choice + +if [[ "$import_workflow_choice" =~ ^[Yy]$ ]]; then + final_run_n8n_import_decision="true" +else + final_run_n8n_import_decision="false" +fi + +# Prompt for number of n8n workers +echo "" # Add a newline for better formatting +log_info "Configuring n8n worker count..." +if [[ -n "${existing_env_vars[N8N_WORKER_COUNT]}" ]]; then + N8N_WORKER_COUNT_CURRENT="${existing_env_vars[N8N_WORKER_COUNT]}" + echo "" + read -p "Do you want to change the number of n8n workers? Current: $N8N_WORKER_COUNT_CURRENT. (Enter new number, or press Enter to keep current): " N8N_WORKER_COUNT_INPUT_RAW + if [[ -z "$N8N_WORKER_COUNT_INPUT_RAW" ]]; then + N8N_WORKER_COUNT="$N8N_WORKER_COUNT_CURRENT" + else + # Validate the new input + if [[ "$N8N_WORKER_COUNT_INPUT_RAW" =~ ^0*[1-9][0-9]*$ ]]; then + N8N_WORKER_COUNT_TEMP="$((10#$N8N_WORKER_COUNT_INPUT_RAW))" # Sanitize (e.g. 01 -> 1) + if [[ "$N8N_WORKER_COUNT_TEMP" -ge 1 ]]; then + echo "" + read -p "Update n8n workers to $N8N_WORKER_COUNT_TEMP? (y/N): " confirm_change + if [[ "$confirm_change" =~ ^[Yy]$ ]]; then + N8N_WORKER_COUNT="$N8N_WORKER_COUNT_TEMP" + else + N8N_WORKER_COUNT="$N8N_WORKER_COUNT_CURRENT" + log_info "Change declined. Keeping N8N_WORKER_COUNT at $N8N_WORKER_COUNT." + fi + else # Should not happen with regex but as a safeguard + log_warning "Invalid input '$N8N_WORKER_COUNT_INPUT_RAW'. Number must be positive. Keeping $N8N_WORKER_COUNT_CURRENT." + N8N_WORKER_COUNT="$N8N_WORKER_COUNT_CURRENT" + fi else - log_info "Change declined. Keeping N8N_WORKER_COUNT at $EXISTING_N8N_WORKER_COUNT." + log_warning "Invalid input '$N8N_WORKER_COUNT_INPUT_RAW'. Please enter a positive integer. Keeping $N8N_WORKER_COUNT_CURRENT." + N8N_WORKER_COUNT="$N8N_WORKER_COUNT_CURRENT" fi - else - log_warning "Invalid input '$N8N_WORKER_COUNT_INPUT_RAW'. Number must be positive. Keeping $EXISTING_N8N_WORKER_COUNT." - fi - else - log_warning "Invalid input '$N8N_WORKER_COUNT_INPUT_RAW'. Please enter a positive integer. Keeping $EXISTING_N8N_WORKER_COUNT." fi - fi else - while true; do - echo "" - read -p "Enter the number of n8n workers to run (e.g., 1, 2, 3; default is 1): " N8N_WORKER_COUNT_INPUT_RAW - N8N_WORKER_COUNT_CANDIDATE="${N8N_WORKER_COUNT_INPUT_RAW:-1}" - if [[ "$N8N_WORKER_COUNT_CANDIDATE" =~ ^0*[1-9][0-9]*$ ]]; then - N8N_WORKER_COUNT_VALIDATED="$((10#$N8N_WORKER_COUNT_CANDIDATE))" - if [ "$N8N_WORKER_COUNT_VALIDATED" -ge 1 ]; then + while true; do echo "" - read -p "Run $N8N_WORKER_COUNT_VALIDATED n8n worker(s)? (y/N): " confirm_workers - if [[ "$confirm_workers" =~ ^[Yy]$ ]]; then - write_env_var N8N_WORKER_COUNT "$N8N_WORKER_COUNT_VALIDATED" - break - else - log_info "Please try entering the number of workers again." - fi - else - log_error "Number of workers must be a positive integer." >&2 - fi - else - log_error "Invalid input '$N8N_WORKER_COUNT_CANDIDATE'. Please enter a positive integer (e.g., 1, 2)." >&2 - fi - done -fi + read -p "Enter the number of n8n workers to run (e.g., 1, 2, 3; default is 1): " N8N_WORKER_COUNT_INPUT_RAW + N8N_WORKER_COUNT_CANDIDATE="${N8N_WORKER_COUNT_INPUT_RAW:-1}" # Default to 1 if empty + if [[ "$N8N_WORKER_COUNT_CANDIDATE" =~ ^0*[1-9][0-9]*$ ]]; then + N8N_WORKER_COUNT_VALIDATED="$((10#$N8N_WORKER_COUNT_CANDIDATE))" + if [[ "$N8N_WORKER_COUNT_VALIDATED" -ge 1 ]]; then + echo "" + read -p "Run $N8N_WORKER_COUNT_VALIDATED n8n worker(s)? (y/N): " confirm_workers + if [[ "$confirm_workers" =~ ^[Yy]$ ]]; then + N8N_WORKER_COUNT="$N8N_WORKER_COUNT_VALIDATED" + break + else + log_info "Please try entering the number of workers again." + fi + else # Should not be reached if regex is correct + log_error "Number of workers must be a positive integer." >&2 + fi + else + log_error "Invalid input '$N8N_WORKER_COUNT_CANDIDATE'. Please enter a positive integer (e.g., 1, 2)." >&2 + fi + done +fi +# Ensure N8N_WORKER_COUNT is definitely set (should be by logic above) +N8N_WORKER_COUNT="${N8N_WORKER_COUNT:-1}" # --------------------------- # Cloudflare Tunnel Token (if cloudflare-tunnel profile is active) # --------------------------- -CURRENT_PROFILES_VALUE="" -if grep -q "^COMPOSE_PROFILES=" "$ENV_FILE"; then - CURRENT_PROFILES_VALUE=$(grep "^COMPOSE_PROFILES=" "$ENV_FILE" | cut -d'=' -f2- | sed 's/^"//' | sed 's/"$//') -fi - -if [ -n "$CURRENT_PROFILES_VALUE" ]; then - PROFILES_CSV=",$CURRENT_PROFILES_VALUE," -else - PROFILES_CSV="," # no profiles -fi - -if [[ "$PROFILES_CSV" == *",cloudflare-tunnel,"* ]]; then - EXISTING_CF_TOKEN="$(read_env_var CLOUDFLARE_TUNNEL_TOKEN)" - if [ -n "$EXISTING_CF_TOKEN" ]; then - log_info "Cloudflare Tunnel token found in .env; reusing it." - else - log_info "Cloudflare Tunnel profile is active. Please provide your Cloudflare Tunnel token." - echo "" - read -p "Cloudflare Tunnel Token: " input_cf_token - write_env_var CLOUDFLARE_TUNNEL_TOKEN "$input_cf_token" - if [ -n "$input_cf_token" ]; then - log_success "Cloudflare Tunnel token saved to .env." - echo "" - echo "🔒 After confirming the tunnel works, consider closing ports 80, 443, and 7687 in your firewall." - else - log_warning "Cloudflare Tunnel token was left empty. You can set it later in .env." +# If Cloudflare Tunnel is selected, prompt for the token and write to .env +cloudflare_selected=0 +for profile in "${selected_profiles[@]}"; do + if [ "$profile" == "cloudflare-tunnel" ]; then + cloudflare_selected=1 + break fi - fi -else - log_info "Cloudflare Tunnel profile is not active; skipping Cloudflare token configuration." -fi +done +if [ $cloudflare_selected -eq 1 ]; then + existing_cf_token="" + if grep -q "^CLOUDFLARE_TUNNEL_TOKEN=" "$ENV_FILE"; then + existing_cf_token=$(grep "^CLOUDFLARE_TUNNEL_TOKEN=" "$ENV_FILE" | cut -d'=' -f2- | sed 's/^\"//' | sed 's/\"$//') + fi + + if [ -n "$existing_cf_token" ]; then + log_info "Cloudflare Tunnel token found in .env; reusing it." + # Do not prompt; keep existing token as-is + else + log_info "Cloudflare Tunnel selected. Please provide your Cloudflare Tunnel token." + echo "" + read -p "Cloudflare Tunnel Token: " input_cf_token + token_to_write="$input_cf_token" + + # Update the .env with the token (may be empty if user skipped) + if grep -q "^CLOUDFLARE_TUNNEL_TOKEN=" "$ENV_FILE"; then + sed -i.bak "/^CLOUDFLARE_TUNNEL_TOKEN=/d" "$ENV_FILE" + fi + echo "CLOUDFLARE_TUNNEL_TOKEN=\"$token_to_write\"" >> "$ENV_FILE" + + if [ -n "$token_to_write" ]; then + log_success "Cloudflare Tunnel token saved to .env." + echo "" + echo "🔒 After confirming the tunnel works, consider closing ports 80, 443, and 7687 in your firewall." + else + log_warning "Cloudflare Tunnel token was left empty. You can set it later in .env." + fi + fi +fi log_success "Service configuration complete. .env updated at $ENV_FILE" exit 0