diff --git a/config.sh b/config.sh index 3287cdf..b9193a1 100755 --- a/config.sh +++ b/config.sh @@ -80,9 +80,147 @@ backup_env_file() { fi } +# Function to get all used ports on the system +get_used_ports() { + local used_ports=() + + # Try different methods to get used ports, in order of preference + if command -v ss >/dev/null 2>&1; then + # Use ss (preferred, modern replacement for netstat) + while IFS= read -r line; do + if [[ "$line" =~ :([0-9]+)[[:space:]] ]]; then + used_ports+=("${BASH_REMATCH[1]}") + fi + done < <(ss -tuln 2>/dev/null | grep -E ':[0-9]+[[:space:]]') + elif command -v netstat >/dev/null 2>&1; then + # Use netstat (fallback) + while IFS= read -r line; do + if [[ "$line" =~ :([0-9]+)[[:space:]] ]]; then + used_ports+=("${BASH_REMATCH[1]}") + fi + done < <(netstat -tuln 2>/dev/null | grep -E ':[0-9]+[[:space:]]') + elif command -v lsof >/dev/null 2>&1; then + # Use lsof (another fallback) + while IFS= read -r line; do + if [[ "$line" =~ :([0-9]+)[[:space:]] ]]; then + used_ports+=("${BASH_REMATCH[1]}") + fi + done < <(lsof -i -P -n 2>/dev/null | grep LISTEN | grep -E ':[0-9]+[[:space:]]') + else + echo "Warning: No port scanning tools available (ss, netstat, or lsof)" + return 1 + fi + + # Remove duplicates and sort + printf '%s\n' "${used_ports[@]}" | sort -nu +} + +# Function to check if a specific port is available +is_port_available() { + local port=$1 + local used_ports_list=$2 + + # Check if port is in the used ports list + if echo "$used_ports_list" | grep -q "^$port$"; then + return 1 # Port is in use + else + return 0 # Port is available + fi +} + +# Function to find next available port starting from a given port +find_next_available_port() { + local start_port=$1 + local used_ports_list=$2 + local max_attempts=${3:-100} # Maximum ports to check + + for ((i=0; i&2 + return 1 +} + +# Function to initialize available ports for services +initialize_available_ports() { + echo "Scanning system for used ports to avoid conflicts..." + + # Get list of all used ports + local used_ports_list + used_ports_list=$(get_used_ports) + + if [ $? -ne 0 ] || [ -z "$used_ports_list" ]; then + echo "Warning: Could not scan system ports. Using default ports." + echo "You may need to manually resolve conflicts later." + return 1 + fi + + echo "Found $(echo "$used_ports_list" | wc -l) ports currently in use" + + # Define desired starting ports for each service + local -A service_ports=( + ["CODE_SERVER_PORT"]=8888 + ["LISTMONK_PORT"]=9000 + ["LISTMONK_DB_PORT"]=5432 + ["MKDOCS_PORT"]=4000 + ["MKDOCS_SITE_SERVER_PORT"]=4001 + ["N8N_PORT"]=5678 + ["NOCODB_PORT"]=8090 + ["HOMEPAGE_PORT"]=3010 + ["GITEA_WEB_PORT"]=3030 + ["GITEA_SSH_PORT"]=2222 + ) + + # Find available ports for each service + local -A available_ports + for service in "${!service_ports[@]}"; do + local preferred_port=${service_ports[$service]} + local available_port + + if is_port_available "$preferred_port" "$used_ports_list"; then + available_port=$preferred_port + echo "✅ $service: Using preferred port $preferred_port" + else + available_port=$(find_next_available_port "$preferred_port" "$used_ports_list") + if [ $? -eq 0 ]; then + echo "⚠️ $service: Port $preferred_port in use, using $available_port instead" + else + echo "❌ $service: Could not find available port, using $preferred_port (may conflict)" + available_port=$preferred_port + fi + fi + + available_ports[$service]=$available_port + # Add the assigned port to used_ports_list to avoid double-assignment + used_ports_list=$(printf '%s\n%s' "$used_ports_list" "$available_port" | sort -nu) + done + + # Export the available ports as global variables + for service in "${!available_ports[@]}"; do + export "$service=${available_ports[$service]}" + done + + echo "Port assignment completed successfully!" + return 0 +} + # Function to initialize the .env file with default values initialize_env_file() { - echo "Initializing new .env file with default values..." + echo "Initializing new .env file with available ports..." + + # Initialize available ports before creating the .env file + initialize_available_ports cat > "$ENV_FILE" << EOL # Never share this file publicly. It contains sensitive information. @@ -94,17 +232,17 @@ USER_NAME=coder USER_ID=1000 GROUP_ID=1000 -# Port Configuration -CODE_SERVER_PORT=8888 -LISTMONK_PORT=9000 -LISTMONK_DB_PORT=5432 -MKDOCS_PORT=4000 -MKDOCS_SITE_SERVER_PORT=4001 -N8N_PORT=5678 -NOCODB_PORT=8090 -HOMEPAGE_PORT=3010 -GITEA_WEB_PORT=3030 -GITEA_SSH_PORT=2222 +# Port Configuration (automatically assigned to avoid conflicts) +CODE_SERVER_PORT=${CODE_SERVER_PORT:-8888} +LISTMONK_PORT=${LISTMONK_PORT:-9000} +LISTMONK_DB_PORT=${LISTMONK_DB_PORT:-5432} +MKDOCS_PORT=${MKDOCS_PORT:-4000} +MKDOCS_SITE_SERVER_PORT=${MKDOCS_SITE_SERVER_PORT:-4001} +N8N_PORT=${N8N_PORT:-5678} +NOCODB_PORT=${NOCODB_PORT:-8090} +HOMEPAGE_PORT=${HOMEPAGE_PORT:-3010} +GITEA_WEB_PORT=${GITEA_WEB_PORT:-3030} +GITEA_SSH_PORT=${GITEA_SSH_PORT:-2222} # Domain Configuration BASE_DOMAIN=https://changeme.org @@ -141,7 +279,7 @@ N8N_ENCRYPTION_KEY=changeMe GENERIC_TIMEZONE=UTC # Nocodb Configuration -NOCODB_PORT=8090 +NOCODB_PORT=${NOCODB_PORT:-8090} NOCODB_JWT_SECRET=changeMe NOCODB_DB_NAME=nocodb NOCODB_DB_USER=noco @@ -149,7 +287,22 @@ NOCODB_DB_PASSWORD=changeMe HOMEPAGE_VAR_BASE_URL=https://changeme.org EOL - echo "New .env file created with default values." + echo "New .env file created with conflict-free port assignments." + + # Show port assignments summary + echo "" + echo "=== Port Assignments Summary ===" + echo "Code Server: ${CODE_SERVER_PORT:-8888}" + echo "Listmonk: ${LISTMONK_PORT:-9000}" + echo "Listmonk DB: ${LISTMONK_DB_PORT:-5432}" + echo "MkDocs: ${MKDOCS_PORT:-4000}" + echo "MkDocs Site: ${MKDOCS_SITE_SERVER_PORT:-4001}" + echo "N8N: ${N8N_PORT:-5678}" + echo "NocoDB: ${NOCODB_PORT:-8090}" + echo "Homepage: ${HOMEPAGE_PORT:-3010}" + echo "Gitea Web: ${GITEA_WEB_PORT:-3030}" + echo "Gitea SSH: ${GITEA_SSH_PORT:-2222}" + echo "================================" } # Function to update the site_url in mkdocs.yml @@ -512,6 +665,12 @@ if [ ! -f "$ENV_FILE" ]; then else echo "Found existing .env file. Will update values." backup_env_file + + # For existing .env files, also scan ports and suggest alternatives if conflicts exist + echo "" + echo "Checking existing port assignments for conflicts..." + load_env_vars + check_port_conflicts fi # Load existing environment variables diff --git a/configs/cloudflare/tunnel-config.yml b/configs/cloudflare/tunnel-config.yml index c4ea466..4f28d65 100644 --- a/configs/cloudflare/tunnel-config.yml +++ b/configs/cloudflare/tunnel-config.yml @@ -2,32 +2,32 @@ # Cloudflare Tunnel Configuration # Auto-generated by Changemaker Configuration Wizard -tunnel: 4948fed8-3fd4-4562-ace7-d3e9ebc590b0 # e.g. 1234567890abcdef -credentials-file: /home/coder/.cloudflared/4948fed8-3fd4-4562-ace7-d3e9ebc590b0.json # e.g. /home/coder/.cloudflared/[insert tunnel number].json +tunnel: ${CF_TUNNEL_ID} # e.g. 1234567890abcdef +credentials-file: /home/coder/.cloudflared/${CF_TUNNEL_ID}.json # e.g. /home/coder/.cloudflared/[insert tunnel number].json ingress: - - hostname: homepage.albertademocracytaskforce.org + - hostname: homepage.test.com service: http://localhost:3010 - - hostname: code.albertademocracytaskforce.org + - hostname: code.test.com service: http://localhost:8888 - - hostname: listmonk.albertademocracytaskforce.org - service: http://localhost:9000 + - hostname: listmonk.test.com + service: http://localhost:9001 - - hostname: docs.albertademocracytaskforce.org + - hostname: docs.test.com service: http://localhost:4000 - - hostname: albertademocracytaskforce.org - service: http://localhost:4001 + - hostname: test.com + service: http://localhost:4002 - - hostname: n8n.albertademocracytaskforce.org + - hostname: n8n.test.com service: http://localhost:5678 - - hostname: db.albertademocracytaskforce.org + - hostname: db.test.com service: http://localhost:8090 - - hostname: git.albertademocracytaskforce.org + - hostname: git.test.com service: http://localhost:3030 # Catch-all rule (required) diff --git a/configs/homepage/services.yaml.backup_20250602_184336 b/configs/homepage/services.yaml.backup_20250602_184336 new file mode 100644 index 0000000..ea833ac --- /dev/null +++ b/configs/homepage/services.yaml.backup_20250602_184336 @@ -0,0 +1,87 @@ +--- +# For public access, replace "http://localhost" with your subdomain URLs + +- Essential Tools: + - Code Server: + href: "http://localhost:8888" + # href: "https://code.albertademocracytaskforce.org" # Uncomment for public access + description: VS Code in the browser + icon: mdi-code-braces + widget: + type: docker + container: code-server-changemaker + server: my-docker + - Listmonk: + href: "http://localhost:9000" + # href: "https://listmonk.albertademocracytaskforce.org" # Uncomment for public access + description: Newsletter & mailing list manager + icon: mdi-email-newsletter + widget: + type: docker + container: listmonk_app + server: my-docker + - NocoDB: + href: "http://localhost:8090" + # href: "https://db.albertademocracytaskforce.org" # Uncomment for public access + description: No-code database platform + icon: mdi-database + widget: + type: docker + container: changemakerlite-nocodb-1 + server: my-docker + - Gitea: + href: "http://localhost:3030" + # href: "https://git.albertademocracytaskforce.org" # Uncomment for public access + description: Git repository hosting + icon: mdi-git + widget: + type: docker + container: gitea_changemaker + server: my-docker + +- Content & Documentation: + - MkDocs (Live): + href: "http://localhost:4000" + # href: "https://docs.albertademocracytaskforce.org" # Uncomment for public access + description: Live documentation server with hot reload + icon: mdi-book-open-page-variant + widget: + type: docker + container: mkdocs-changemaker + server: my-docker + - Static Site: + href: "http://localhost:4001" + # href: "https://albertademocracytaskforce.org" # Uncomment for public access + description: Built documentation hosting + icon: mdi-web + widget: + type: docker + container: mkdocs-site-server-changemaker + server: my-docker + +- Automation & Infrastructure: + - n8n: + href: "http://localhost:5678" + # href: "https://n8n.albertademocracytaskforce.org" # Uncomment for public access + description: Workflow automation platform + icon: mdi-workflow + widget: + type: docker + container: n8n-changemaker + server: my-docker + - PostgreSQL (Listmonk): + href: "#" + description: Database for Listmonk + icon: mdi-database-outline + widget: + type: docker + container: listmonk_db + server: my-docker + - PostgreSQL (NocoDB): + href: "#" + description: Database for NocoDB + icon: mdi-database-outline + widget: + type: docker + container: changemakerlite-root_db-1 + server: my-docker diff --git a/mkdocs/docs/overrides/main.html.backup_20250602_184336 b/mkdocs/docs/overrides/main.html.backup_20250602_184336 new file mode 100644 index 0000000..732197a --- /dev/null +++ b/mkdocs/docs/overrides/main.html.backup_20250602_184336 @@ -0,0 +1,9 @@ +{% extends "base.html" %} + +{% block extrahead %} +{% endblock %} + +{% block announce %} +Login +Changemaker Archive. Learn more +{% endblock %}