handle domain

This commit is contained in:
chungyan5 2026-02-26 06:54:15 +00:00
parent 5a61262140
commit 42da37fd0a
4 changed files with 232 additions and 19 deletions

View File

@ -98,6 +98,43 @@ curl -s "https://www.ivantito.com/app-pub/demos/test/deploy-rvs-prxy.sh" | bash
2. Triggers interactive wizard if `.env` is missing 2. Triggers interactive wizard if `.env` is missing
3. Detects deployment mode (setup proxy or setup domain) 3. Detects deployment mode (setup proxy or setup domain)
### Re-running for Existing Domain
**What happens if you run the script again for a domain that's already configured?**
If you run the script for a domain that has already been added to the reverse proxy:
1. **Detection**: The script checks if a configuration file already exists for the domain and service combination
2. **Warning**: Displays a warning message:
```
[WARN] Configuration file app-service-domain-service.yml already exists, it will be overwritten
[INFO] This will update the existing domain configuration for my.domain.com
```
- Configuration File Naming:
- Format: app-service-{domain}-{service}.yml
- Example: app-service-my-domain-com-myapp.yml
- Unique per domain+service combination
3. **Update**: The existing configuration is overwritten with the new settings
4. **Result**: The domain configuration is updated (not duplicated)
**Key Points:**
- **Safe to re-run**: Running the script multiple times for the same domain is safe
- **Configuration is updated, not duplicated**: Each domain+service combination has a unique configuration file
- **No service interruption**: Traefik dynamically loads the updated configuration (no restart needed by default)
- **Use case**: Useful for updating application port changes or domain routing modifications
**Example Scenario:**
```bash
# First run - adds my.domain.com
curl -s "https://www.ivantito.com/app-pub/demos/test/deploy-rvs-prxy.sh" | bash
# [INFO] Creating new configuration for domain my.domain.com
# Second run - updates my.domain.com (e.g., after port change)
curl -s "https://www.ivantito.com/app-pub/demos/test/deploy-rvs-prxy.sh" | bash
# [WARN] Configuration file ... already exists, it will be overwritten
# [INFO] This will update the existing domain configuration for my.domain.com
```
### Script Behavior ### Script Behavior
### Mode Selection ### Mode Selection

View File

@ -32,20 +32,21 @@
- **No application service check** (application not needed at this stage) - **No application service check** (application not needed at this stage)
- No domain configuration at this stage - No domain configuration at this stage
2. **Second Run (`.env` exists, proxy running)**: Setup New Domain 2. **Add domain (`.env` exists, proxy running)**: Setup New Domain
- Interactive wizard asks for domain configuration - Interactive wizard asks for domain configuration
- Creates `.env-<domain>` with application settings - Creates `.env-<domain>` with application settings
- **Checks if application service is running** - **Checks if application service is running**
- Configures domain in existing reverse proxy - Configures domain in existing reverse proxy
``` ```
┌────────────┬──────────────────────────────┬────────────────────────────────────────────────────────┬────────────────┐ ┌────────────┬─────────────────────────────────────┬────────────────────────────────────────────────────────┬────────────────┐
│ Scenario │ Configuration │ Wizard Asks For │ Creates │ │ Scenario │ Configuration │ Wizard Asks For │ Creates │
├────────────┼──────────────────────────────┼────────────────────────────────────────────────────────┼────────────────┤ ├────────────┼─────────────────────────────────────┼────────────────────────────────────────────────────────┼────────────────┤
│ First run │ No files │ Proxy instance settings (Traefik version, email, etc.) │ .env │ │ First run │ No files │ Proxy instance settings (Traefik version, email, etc.) │ .env │
│ Second run │ .env exists, proxy running │ Domain settings (domain name, service, port) │ .env-<domain> │ │ First run │ .env exists │ Proxy instance settings (Traefik version, email, etc.) │ Null │
│ Add domain │ .env + .env-<domain1> exists │ Domain settings for new domain │ .env-<domain2> │ │ Add domain │ proxy running, no .env-<domain> │ Domain settings (domain name, service, port) │ .env-<domain> │
└────────────┴──────────────────────────────┴────────────────────────────────────────────────────────┴────────────────┘ │ Add domain │ proxy running, .env-<domain> exists │ Domain settings (domain name, service, port) │ Null │
└────────────┴─────────────────────────────────────┴────────────────────────────────────────────────────────┴────────────────┘
``` ```
- Detect the existing of reverse proxy instance -- this script and reverse proxy instance at same computer. - Detect the existing of reverse proxy instance -- this script and reverse proxy instance at same computer.

View File

@ -258,9 +258,162 @@ The `deploy` command syncs files from local folder to target server:
## AI Prompt habit as a reminder ## AI Prompt habit as a reminder
- Refer to `Design_Doc/instructions.md` - Refer to `Design_Doc/instructions.md`
## General Todos
- `.env` for
- INSTANCE_NAME, not fixing the traefik
- not fixing of docker network name as traefik_public
## Workflow ## Workflow
- in scenario of proxy running, no matter have or not have `.env-<domain>`, it will execute the "Add domain" task. However, one of domain, eg. my.domain.com, has already added into reverse proxy instance, this command `curl -s "https://www.ivantito.com/app-pub/demos/test/deploy-rvs-prxy.sh" | bash` is executed again, does it re-insert the same command into reverse proxy instance? Let me the current logic, and update into the `Design_Doc/Operation.md` if does not has this description before.
- Result -- Design_Doc/Operations.md -> section: "Re-running for Existing Domain"
- i am in Scenario 1: `.env` exists, proxy running, no `.env-<domain>`, the logic should be: Action: Starts interactive wizard for domain setup. However, the current script code running as:
```
curl -s "https://www.ivantito.com/app-pub/demos/test/deploy-rvs-prxy.sh" | bash
[INFO] Loading reverse proxy config from .env
[INFO] Deployment mode: Setup New Domain (proxy running, no domain config)
[ERROR] DOMAIN_NAME is required for domain setup mode
[ERROR] Please set DOMAIN_NAME in your .env-<domain> file
```
- adjust the code to right logic.
- Result
1. Code Fix (deploy-rvs-prxy.sh)
Problem:
- When .env exists, proxy is running, but no .env-<domain> exists
- Script detected DEPLOY_MODE="domain" and tried to validate domain settings
- Validation failed because domain settings don't exist yet (need wizard)
Solution: Added new `domain_wizard` mode
New Logic:
1 if domain_env_loaded → domain mode (validate & execute)
2 elif proxy_env_loaded:
3 if proxy running → domain_wizard mode (start wizard, no validation)
4 else → proxy mode
5 else → wizard
Changes Made:
1. `load_env()` function:
- Changed DEPLOY_MODE="domain" to DEPLOY_MODE="domain_wizard" when proxy exists but no domain config
- Added conditional validation: skip validation for domain_wizard mode
- Added comment explaining wizard will collect required values
2. `main()` function:
- Added elif [ "$DEPLOY_MODE" = "domain_wizard" ] branch
- Calls interactive_setup_wizard() to collect domain settings
- Sources the newly created domain config file
- Then calls setup_new_domain() to configure it
2. Workflow Now
Scenario 1: `.env` exists, proxy running, no `.env-<domain>`
1 [INFO] Loading reverse proxy config from .env
2 [INFO] Deployment mode: Setup New Domain (proxy running, no domain config)
3 [INFO] Starting interactive wizard for domain setup...
4 [Wizard runs and creates .env-<domain>])
5 [Domain configured successfully]
Scenario 2: `.env-<domain>` exists
1 [INFO] Loading domain config from .env-<domain>
2 [INFO] Deployment mode: Setup New Domain (found domain config)
3 [INFO] Setting up new domain in existing reverse proxy...
4 [Domain configured successfully]
Scenario 3: No config files
1 [INFO] No configuration file found.
2 [Wizard runs for proxy setup]
3 [Proxy deployed]
- For "Second Run", the Reverse Proxy Instance should be running.
- Currently, if `.env` exists, it shows this err.:
```
curl -s "https://www.ivantito.com/app-pub/demos/test/deploy-rvs-prxy.sh" | bash
[INFO] Loading reverse proxy config from .env
[INFO] Deployment mode: Setup New Domain (proxy running, no domain config)
[ERROR] DOMAIN_NAME is required for domain setup mode
[ERROR] Please set DOMAIN_NAME in your .env-<domain> file
```
- i find the script will read `.env` as the `.env-<domain>`, so adjust the code as recognizing `.env-*` only, not `.env`, `.env` usually be kept there for configuration of Reverse Proxy Instance only.
- Currently, if `.env` missing or should be right as `.env-<domain>` missing, it shows this err.:
```
curl -s "https://www.ivantito.com/app-pub/demos/test/deploy-rvs-prxy.sh" | bash
[INFO] No configuration file found.
=============================================
Welcome to Deploy Reverse Proxy Setup Wizard
=============================================
Existing reverse proxy (Traefik) detected.
--- Setup New Domain ---
Let's configure a new domain for your existing reverse proxy.
Enter the domain name for this service:
Domain (e.g., app.example.com): event.cgu.org.hk
Enter your Docker application container name:
Service name (e.g., my-app): cgu-react-client
Enter your application's internal port:
Service port (default: 3000):
[INFO] Saving domain configuration to .env-event-cgu-org-hk
[INFO] Domain configuration saved to .env-event-cgu-org-hk
=============================================
Setup Complete!
Domain configuration saved to .env-event-cgu-org-hk
=============================================
You can edit these settings later in the configuration file.
main: line 130: .env: No such file or directory
```
- Improve the code to resolve this issue.
- Result
1. Code Fixes (deploy-rvs-prxy.sh)
Issue 1: `.env` being recognized as domain config
- Root cause: The domain detection logic didn't properly distinguish between .env and .env-* files
- Fix: Changed the logic to check for domain_env_loaded first, which only gets set when a .env-* file is found (not .env)
Issue 2: "No such file or directory" error when sourcing `.env`
- Root cause: When proxy exists but .env is missing, the wizard creates only .env-<domain>, then tries to source .env which doesn't exist
- Fix:
- In interactive_setup_wizard(): Only source .env if it exists, otherwise set default FRP=false
- In load_env(): Source the correct file based on deployment mode (.env for proxy, DOMAIN_ENV_FILE for domain)
2. Updated Logic Flow
Before:
1 if proxy_env_loaded AND domain_env_loaded → domain mode
2 elif proxy_env_loaded → check proxy → domain/proxy mode
3 else → wizard → source .env → proxy mode
After:
1 if domain_env_loaded → domain mode
2 elif proxy_env_loaded → check proxy → domain/proxy mode
3 else → wizard → source correct file (.env or .env-<domain>) → appropriate mode
3. Behavior Now
Scenario 1: `.env` exists, proxy running, no `.env-<domain>`
- Detects: DEPLOY_MODE="domain"
- Message: "Setup New Domain (proxy running, no domain config)"
- Action: Starts interactive wizard for domain setup
- Creates: .env-<domain> file
- No error about missing DOMAIN_NAME
Scenario 2: Proxy running, no `.env`, no `.env-<domain>`
- Detects: No config files
- Action: Starts interactive wizard for domain setup
- Creates: .env-<domain> file only
- Sources: .env-<domain> (not .env)
- No "No such file or directory" error
- After "First Run" to Setup Reverse Proxy Instance with Interactive wizard, it show the following issue: - After "First Run" to Setup Reverse Proxy Instance with Interactive wizard, it show the following issue:
``` ```
--- Step 3: Setup Complete --- --- Step 3: Setup Complete ---
@ -364,6 +517,9 @@ The `deploy` command syncs files from local folder to target server:
## .env ## .env
- in scenario of proxy running, no matter have or not have `.env-<domain>`, it will execute the "Add domain" task. However, one of domain, eg. my.domain.com, has already added into reverse proxy instance, this command `curl -s "https://www.ivantito.com/app-pub/demos/test/deploy-rvs-prxy.sh" | bash` is executed again, does it re-insert the same command into reverse proxy instance? Let me the current logic, and update into the `Design_Doc/Operation.md` if does not has this description before.
- I am in the situation as 1) No reverse proxy exists, 2) no `.env`, 3) no `.env-<domain-with-dashes>`, it is right that go into Interactive wizard. In this situation, it should be "Setup Reverse Proxy", so the Interactive wizard is only handle the Reverse Proxy instance Configuration, and finally save those configuration into `.env`. No any Domain related Configuration. However, currently the script handle the Interactive wizard to show the following which is Domain configuation related: - I am in the situation as 1) No reverse proxy exists, 2) no `.env`, 3) no `.env-<domain-with-dashes>`, it is right that go into Interactive wizard. In this situation, it should be "Setup Reverse Proxy", so the Interactive wizard is only handle the Reverse Proxy instance Configuration, and finally save those configuration into `.env`. No any Domain related Configuration. However, currently the script handle the Interactive wizard to show the following which is Domain configuation related:
``` ```
--- Step 2: Traefik Configuration --- --- Step 2: Traefik Configuration ---

View File

@ -91,10 +91,10 @@ load_env() {
done done
# Determine deployment mode based on what configuration files exist # Determine deployment mode based on what configuration files exist
if [ "$proxy_env_loaded" = true ] && [ "$domain_env_loaded" = true ]; then if [ "$domain_env_loaded" = true ]; then
# Both proxy and domain config exist - setup new domain # Domain config exists - setup new domain
DEPLOY_MODE="domain" DEPLOY_MODE="domain"
log_info "Deployment mode: Setup New Domain (found .env and domain config)" log_info "Deployment mode: Setup New Domain (found domain config: ${DOMAIN_ENV_FILE})"
elif [ "$proxy_env_loaded" = true ]; then elif [ "$proxy_env_loaded" = true ]; then
# Only proxy config exists - check if proxy is already running # Only proxy config exists - check if proxy is already running
local proxy_exists=false local proxy_exists=false
@ -114,8 +114,8 @@ load_env() {
fi fi
if [ "$proxy_exists" = true ]; then if [ "$proxy_exists" = true ]; then
# Proxy exists but no domain config - need domain setup # Proxy exists but no domain config - need domain setup via wizard
DEPLOY_MODE="domain" DEPLOY_MODE="domain_wizard"
log_info "Deployment mode: Setup New Domain (proxy running, no domain config)" log_info "Deployment mode: Setup New Domain (proxy running, no domain config)"
else else
# Proxy config exists but proxy not running - setup proxy # Proxy config exists but proxy not running - setup proxy
@ -126,14 +126,17 @@ load_env() {
# No configuration files found - run interactive wizard # No configuration files found - run interactive wizard
echo -e "${YELLOW}[INFO]${NC} No configuration file found." >&2 echo -e "${YELLOW}[INFO]${NC} No configuration file found." >&2
interactive_setup_wizard interactive_setup_wizard
# Source the newly created .env file # Source the newly created files
source .env if [ "$DEPLOY_MODE" = "domain" ] && [ -n "$DOMAIN_ENV_FILE" ]; then
DEPLOY_MODE="proxy" source "$DOMAIN_ENV_FILE"
log_info "Deployment mode: Setup Reverse Proxy (interactive setup)" else
source .env
fi
fi fi
# Validate required parameters based on mode # Validate required parameters based on mode (skip validation for domain_wizard - will be validated after wizard)
if [ "$DEPLOY_MODE" = "domain" ]; then if [ "$DEPLOY_MODE" = "domain" ]; then
# Domain config file exists - validate it
if [ -z "$DOMAIN_NAME" ]; then if [ -z "$DOMAIN_NAME" ]; then
log_error "DOMAIN_NAME is required for domain setup mode" log_error "DOMAIN_NAME is required for domain setup mode"
log_error "Please set DOMAIN_NAME in your .env-<domain> file" log_error "Please set DOMAIN_NAME in your .env-<domain> file"
@ -167,6 +170,7 @@ load_env() {
fi fi
fi fi
fi fi
# Note: domain_wizard mode skips validation here - wizard will collect the required values
} }
# Logging functions # Logging functions
@ -299,12 +303,16 @@ interactive_setup_wizard() {
# Save to domain-specific .env file # Save to domain-specific .env file
save_domain_env "$DOMAIN_NAME" "$LOCAL_SERVICE_NAME" "$LOCAL_SERVICE_PORT" save_domain_env "$DOMAIN_NAME" "$LOCAL_SERVICE_NAME" "$LOCAL_SERVICE_PORT"
# Load FRP setting from existing .env # Load FRP setting from existing .env if it exists
if [ -f ".env" ]; then if [ -f ".env" ]; then
source .env source .env
else
# Set default FRP mode if .env doesn't exist
FRP=false
fi fi
DEPLOY_MODE="domain" DEPLOY_MODE="domain"
log_info "Deployment mode: Setup New Domain (interactive setup)"
else else
# No reverse proxy exists, setup new one # No reverse proxy exists, setup new one
@ -1488,6 +1496,9 @@ EOF
if [ -n "$config_exists" ]; then if [ -n "$config_exists" ]; then
log_warn "Configuration file $config_file_name already exists, it will be overwritten" log_warn "Configuration file $config_file_name already exists, it will be overwritten"
log_info "This will update the existing domain configuration for ${DOMAIN_NAME}"
else
log_info "Creating new configuration for domain ${DOMAIN_NAME}"
fi fi
# Copy the configuration file into the volume using a temporary container # Copy the configuration file into the volume using a temporary container
@ -2322,6 +2333,14 @@ main() {
if [ "$DEPLOY_MODE" = "domain" ]; then if [ "$DEPLOY_MODE" = "domain" ]; then
log_info "Setting up new domain in existing reverse proxy..." log_info "Setting up new domain in existing reverse proxy..."
setup_new_domain setup_new_domain
elif [ "$DEPLOY_MODE" = "domain_wizard" ]; then
log_info "Starting interactive wizard for domain setup..."
interactive_setup_wizard
# Source the newly created domain config
if [ -n "$DOMAIN_ENV_FILE" ]; then
source "$DOMAIN_ENV_FILE"
fi
setup_new_domain
else else
log_info "Setting up reverse proxy..." log_info "Setting up reverse proxy..."
setup_reverse_proxy setup_reverse_proxy