diff --git a/config.sh b/config.sh index f6722bd..f5b2f28 100755 --- a/config.sh +++ b/config.sh @@ -26,6 +26,7 @@ TUNNEL_CONFIG_FILE="$TUNNEL_CONFIG_DIR/tunnel-config.yml" SERVICES_YAML="$SCRIPT_DIR/configs/homepage/services.yaml" MAIN_HTML="$SCRIPT_DIR/mkdocs/docs/overrides/main.html" MAP_ENV_FILE="$SCRIPT_DIR/map/.env" # Add the map's .env file path +DOCKER_COMPOSE_FILE="$SCRIPT_DIR/docker-compose.yml" echo "Looking for .env file at: $ENV_FILE" @@ -651,6 +652,111 @@ EOL return 0 } +# Function to get instance identifier +get_instance_identifier() { + # Try to get from directory name first + local dir_name=$(basename "$SCRIPT_DIR") + local default_instance="" + + # Extract potential instance name from directory + if [[ "$dir_name" =~ changemaker\.lite\.(.+)$ ]]; then + default_instance="${BASH_REMATCH[1]}" + elif [[ "$dir_name" != "changemaker.lite" ]]; then + default_instance="$dir_name" + fi + + echo "" + echo "=== Instance Configuration ===" + echo "To run multiple Changemaker instances on the same machine," + echo "each instance needs a unique identifier for containers and networks." + echo "" + + if [ -n "$default_instance" ]; then + echo "Detected potential instance name from directory: $default_instance" + read -p "Use this instance identifier? [Y/n]: " use_detected + if [[ ! "$use_detected" =~ ^[Nn]$ ]]; then + echo "$default_instance" + return 0 + fi + fi + + read -p "Enter instance identifier (letters, numbers, hyphens only) [default: main]: " instance_id + + # Validate and sanitize instance identifier + if [ -z "$instance_id" ]; then + instance_id="main" + fi + + # Remove invalid characters and convert to lowercase + instance_id=$(echo "$instance_id" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9-]//g') + + if [ -z "$instance_id" ]; then + instance_id="main" + fi + + echo "$instance_id" +} + +# Function to update docker-compose.yml with unique names +update_docker_compose_names() { + local instance_id=$1 + + if [ -z "$instance_id" ] || [ "$instance_id" = "main" ]; then + echo "Using default container names (no instance suffix)" + return 0 + fi + + echo "Updating docker-compose.yml with instance identifier: $instance_id" + + # Create a backup of the docker-compose.yml file + local timestamp=$(date +"%Y%m%d_%H%M%S") + local backup_file="${DOCKER_COMPOSE_FILE}.backup_${timestamp}" + cp "$DOCKER_COMPOSE_FILE" "$backup_file" + echo "Created backup of docker-compose.yml at $backup_file" + + # Create temporary file for modifications + local temp_file=$(mktemp) + + # Update container names, network names, and volume names + sed -e "s/container_name: \([^-]*\)$/container_name: \1-${instance_id}/g" \ + -e "s/container_name: \([^-]*\)-changemaker$/container_name: \1-changemaker-${instance_id}/g" \ + -e "s/container_name: \([^-]*\)_\([^-]*\)$/container_name: \1_\2_${instance_id}/g" \ + -e "s/container_name: \([^-]*\)_\([^-]*\)_changemaker$/container_name: \1_\2_changemaker_${instance_id}/g" \ + -e "s/networks:/networks:/g" \ + -e "s/changemaker-lite:/changemaker-lite-${instance_id}:/g" \ + -e "s/- changemaker-lite$/- changemaker-lite-${instance_id}/g" \ + -e "s/driver: bridge$/driver: bridge/g" \ + -e "s/volumes:/volumes:/g" \ + -e "s/listmonk-data:/listmonk-data-${instance_id}:/g" \ + -e "s/n8n_data:/n8n_data_${instance_id}:/g" \ + -e "s/nc_data:/nc_data_${instance_id}:/g" \ + -e "s/db_data:/db_data_${instance_id}:/g" \ + -e "s/gitea_data:/gitea_data_${instance_id}:/g" \ + -e "s/mysql_data:/mysql_data_${instance_id}:/g" \ + "$DOCKER_COMPOSE_FILE" > "$temp_file" + + # Replace the original file + mv "$temp_file" "$DOCKER_COMPOSE_FILE" + + echo "āœ… Updated docker-compose.yml with instance-specific names:" + echo " - Container names: *-${instance_id}" + echo " - Network name: changemaker-lite-${instance_id}" + echo " - Volume names: *-${instance_id}" + + return 0 +} + +# Function to update .env file with instance identifier +update_env_instance_config() { + local instance_id=$1 + + if [ -n "$instance_id" ] && [ "$instance_id" != "main" ]; then + update_env_var "INSTANCE_ID" "$instance_id" + update_env_var "COMPOSE_PROJECT_NAME" "changemaker-lite-${instance_id}" + echo "Updated .env with instance configuration" + fi +} + # Initialize a new .env file if it doesn't exist if [ ! -f "$ENV_FILE" ]; then echo "No .env file found. Creating a new one from scratch." @@ -674,6 +780,12 @@ echo -e "\n\nWelcome to Changemaker Config!\n" echo "This script will help you configure your .env file for Changemaker." echo "Please provide the following information:" +# Get instance identifier and update docker-compose.yml +echo -e "\n---- Instance Configuration ----" +instance_identifier=$(get_instance_identifier) +update_docker_compose_names "$instance_identifier" +update_env_instance_config "$instance_identifier" + # Domain configuration read -p "Enter your domain name (without protocol, e.g., example.com): " domain_name @@ -830,6 +942,7 @@ echo "Secure passwords generated and updated." echo -e "\nāœ… Configuration completed successfully!" echo "Your .env file has been configured with:" +echo "- Instance ID: $instance_identifier" echo "- Domain: $domain_name" echo "- Cookie Domain: .$domain_name" echo "- Allowed Origins: https://map.$domain_name,http://localhost:3000" @@ -846,6 +959,10 @@ echo "======================================" echo "Next Steps:" echo "======================================" echo "" +if [ -n "$instance_identifier" ] && [ "$instance_identifier" != "main" ]; then + echo "Instance: $instance_identifier" + echo "" +fi echo "1. Start services locally:" echo " docker compose up -d" echo ""