param( [ValidateSet('h5', 'api', 'both')] [string]$Target = 'both', [string]$KeyFile = '', [string]$User = 'root' ) $ErrorActionPreference = 'Stop' $root = Split-Path -Parent $PSScriptRoot if (-not $KeyFile) { $candidates = @( (Join-Path $root 'deploy\id_rsa_tencent'), (Join-Path $root 'id_rsa_tencent') ) foreach ($c in $candidates) { if (Test-Path $c) { $KeyFile = $c; break } } } if (-not $KeyFile -or -not (Test-Path $KeyFile)) { Write-Error "SSH key not found. Place private key at deploy\id_rsa_tencent or pass -KeyFile" } $envFile = Join-Path $root '.env.tencent' $lines = @{} if (Test-Path $envFile) { Get-Content $envFile | ForEach-Object { if ($_ -match '^\s*#' -or $_ -notmatch '=') { return } $k, $v = $_ -split '=', 2 $lines[$k.Trim()] = $v.Trim() } } $hosts = @{ h5 = @{ Name = 'test-h5'; Ip = $lines['TENCENT_PROD_H5_IP'] ?? '119.45.34.106'; User = 'root' } api = @{ Name = 'test-api'; Ip = $lines['TENCENT_PROD_API_IP'] ?? '146.56.223.139'; User = 'ubuntu' } } $ssh = Get-Command ssh -ErrorAction SilentlyContinue if (-not $ssh) { Write-Error 'OpenSSH client not found. Enable "OpenSSH Client" in Windows Optional Features.' } function Invoke-Remote([string]$ip, [string]$label, [string]$role, [string]$sshUser) { Write-Host "`n=== $label ($ip) role=$role user=$sshUser ===" -ForegroundColor Cyan $cmd = "cd /opt/ai-fresh-delivery 2>/dev/null || cd ~/Projects/canteen-delivery; bash scripts/prod-on-server.sh $role" & ssh -i $KeyFile -o StrictHostKeyChecking=accept-new -o ConnectTimeout=15 "${sshUser}@${ip}" $cmd if ($LASTEXITCODE -ne 0) { Write-Warning "$label failed (exit $LASTEXITCODE). Check repo path on server." } } if ($Target -eq 'h5' -or $Target -eq 'both') { Invoke-Remote $hosts.h5.Ip $hosts.h5.Name 'h5' $hosts.h5.User } if ($Target -eq 'api' -or $Target -eq 'both') { Invoke-Remote $hosts.api.Ip $hosts.api.Name 'api' $hosts.api.User } Write-Host "`nDone." -ForegroundColor Green