来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
337 lines
14 KiB
PowerShell
337 lines
14 KiB
PowerShell
param(
|
|
[string]$KeyFile = '',
|
|
[switch]$SkipBuild,
|
|
[switch]$SkipMigrate,
|
|
[switch]$ApiOnly,
|
|
[switch]$H5Only
|
|
)
|
|
|
|
# HISTORICAL: zero-downtime rolling via CLB dual backends.
|
|
# Current prod is single H5 + single API — prefer .\scripts\deploy-prod-all.ps1
|
|
# Design (current): docs/design/20260909-prod-single-h5-api.md
|
|
# Design (rolling): docs/design/20260805-prod-rolling-zero-downtime.md
|
|
# Frontend fingerprint: docs/design/20260810-prod-rolling-fe-fingerprint.md
|
|
# Requires CLB dual first: node server/scripts/tencent-clb-rolling.mjs ensure-dual
|
|
$ErrorActionPreference = 'Stop'
|
|
$root = Split-Path -Parent $PSScriptRoot
|
|
Set-Location $root
|
|
. (Join-Path $PSScriptRoot 'tencent-prod-env.ps1') | Out-Null
|
|
|
|
if (-not $KeyFile) { $KeyFile = Join-Path $root 'deploy\id_rsa_tencent' }
|
|
if (-not (Test-Path $KeyFile)) { Write-Error "SSH key not found: $KeyFile" }
|
|
|
|
$sshOpts = @('-i', $KeyFile, '-o', 'StrictHostKeyChecking=accept-new', '-o', 'ConnectTimeout=25', '-o', 'ServerAliveInterval=15', '-o', 'ServerAliveCountMax=4', '-o', 'BatchMode=yes', '-T')
|
|
$FrontendDir = '/opt/canteen-frontend'
|
|
$ClbRolling = Join-Path $root 'server\scripts\tencent-clb-rolling.mjs'
|
|
|
|
$H5 = @{ Host = 'root@119.45.34.106'; Name = 'test-h5'; Key = 'h5'; Health = 'http://119.45.34.106/' }
|
|
$H51 = @{ Host = 'ubuntu@175.27.253.206'; Name = 'test-h51'; Key = 'h51'; Health = 'http://175.27.253.206/' }
|
|
$Api = @{ Host = 'ubuntu@146.56.223.139'; Name = 'test-api'; Key = 'api'; Health = 'http://146.56.223.139:3001/api/health'; Migrate = $true }
|
|
$Api1 = @{ Host = 'ubuntu@118.195.186.82'; Name = 'test-api1'; Key = 'api1'; Health = 'http://118.195.186.82:3001/api/health'; Migrate = $false }
|
|
|
|
function Get-AppBuildFromHtmlText {
|
|
param([string]$Html)
|
|
$m = [regex]::Match($Html, 'app-build"\s+content="([^"]+)"')
|
|
if (-not $m.Success) { return '' }
|
|
return $m.Groups[1].Value.Trim()
|
|
}
|
|
|
|
function Get-LocalFrontendAppBuild {
|
|
$indexPath = Join-Path $root 'dist\index.html'
|
|
if (-not (Test-Path $indexPath)) { throw "Missing $indexPath" }
|
|
$build = Get-AppBuildFromHtmlText -Html (Get-Content -Raw -Path $indexPath)
|
|
if (-not $build) { throw "Local dist/index.html missing app-build meta" }
|
|
return $build
|
|
}
|
|
|
|
function Get-HttpAppBuild {
|
|
param([string]$Url)
|
|
$tmp = Join-Path $env:TEMP ("canteen-app-build-{0}.html" -f [guid]::NewGuid().ToString('N'))
|
|
try {
|
|
& curl.exe -sS --connect-timeout 12 -o $tmp $Url
|
|
if ($LASTEXITCODE -ne 0) { return '' }
|
|
return Get-AppBuildFromHtmlText -Html (Get-Content -Raw -Path $tmp)
|
|
} finally {
|
|
Remove-Item $tmp -Force -ErrorAction SilentlyContinue
|
|
}
|
|
}
|
|
|
|
function Assert-AppBuildMatch {
|
|
param([string]$Label, [string]$Actual, [string]$Expected)
|
|
if ($Actual -ne $Expected) {
|
|
throw "FE fingerprint mismatch ($Label): remote='$Actual' expected='$Expected'"
|
|
}
|
|
Write-Host "OK $Label app-build=$Actual" -ForegroundColor Green
|
|
}
|
|
|
|
function Invoke-Ssh {
|
|
param([string]$Target, [string]$Cmd)
|
|
$attempt = 0
|
|
while ($true) {
|
|
$attempt++
|
|
# Windows OpenSSH:远程命令必须是 -- 后的单一参数(整串加引号),否则会按空格拆碎
|
|
& ssh @sshOpts $Target -- "$Cmd"
|
|
if ($LASTEXITCODE -eq 0) { return }
|
|
if ($attempt -ge 3) { throw "SSH failed after $attempt tries: $Target" }
|
|
Write-Host "SSH retry $attempt -> $Target" -ForegroundColor DarkYellow
|
|
Start-Sleep -Seconds (2 * $attempt)
|
|
}
|
|
}
|
|
|
|
function Invoke-Scp {
|
|
param([string]$Local, [string]$Remote)
|
|
$attempt = 0
|
|
while ($true) {
|
|
$attempt++
|
|
& scp @sshOpts $Local $Remote
|
|
if ($LASTEXITCODE -eq 0) { return }
|
|
if ($attempt -ge 3) { throw "SCP failed after $attempt tries: $Local -> $Remote" }
|
|
Write-Host "SCP retry $attempt -> $Remote" -ForegroundColor DarkYellow
|
|
Start-Sleep -Seconds (2 * $attempt)
|
|
}
|
|
}
|
|
|
|
function Wait-HttpOk {
|
|
param([string]$Url, [string]$Label, [int]$Tries = 20)
|
|
for ($i = 1; $i -le $Tries; $i++) {
|
|
try {
|
|
$code = & curl.exe -sS -o NUL -w '%{http_code}' --connect-timeout 10 $Url
|
|
if ($code -eq '200') {
|
|
Write-Host "OK $Label -> 200" -ForegroundColor Green
|
|
return
|
|
}
|
|
Write-Host "wait $Label ($i/$Tries) code=$code" -ForegroundColor DarkYellow
|
|
} catch {
|
|
Write-Host "wait $Label ($i/$Tries) err" -ForegroundColor DarkYellow
|
|
}
|
|
Start-Sleep -Seconds 3
|
|
}
|
|
throw "Health check failed: $Label $Url"
|
|
}
|
|
|
|
function Set-ClbWeight {
|
|
param([string]$Group, [string]$Instance, [int]$Weight)
|
|
Write-Host "CLB weight $Group/$Instance = $Weight" -ForegroundColor Cyan
|
|
node $ClbRolling weight --group $Group --instance $Instance --weight $Weight
|
|
if ($LASTEXITCODE -ne 0) { throw "CLB weight failed" }
|
|
}
|
|
|
|
function Invoke-ClbStatus {
|
|
node $ClbRolling status
|
|
if ($LASTEXITCODE -ne 0) { throw "CLB status failed" }
|
|
}
|
|
|
|
function Deploy-FrontendHost {
|
|
param(
|
|
$HostInfo,
|
|
[string]$TarPath,
|
|
[string]$ExpectedAppBuild
|
|
)
|
|
Write-Host "--> FE $($HostInfo.Name) (force sync + fingerprint)" -ForegroundColor Yellow
|
|
Invoke-Scp $TarPath "$($HostInfo.Host):/tmp/dist-deploy.tar.gz"
|
|
|
|
# h51 等非 root 账号需 sudo 才能可靠覆盖 /opt/canteen-frontend
|
|
$sudo = if ($HostInfo.Host -like 'root@*') { '' } else { 'sudo ' }
|
|
# Windows OpenSSH 对「多行远程命令」常只执行首行;改为上传脚本再 bash
|
|
$extractCmd = @"
|
|
set -euo pipefail
|
|
FE='$FrontendDir'
|
|
STAGING='/tmp/canteen-frontend-next'
|
|
EXPECTED='$ExpectedAppBuild'
|
|
$($sudo)rm -rf "`$STAGING"
|
|
$($sudo)mkdir -p "`$FE" "`$STAGING"
|
|
$($sudo)tar -xzf /tmp/dist-deploy.tar.gz -C "`$STAGING"
|
|
$($sudo)test -s "`$STAGING/index.html"
|
|
$($sudo)find "`$FE" -maxdepth 1 \( -name '*.js' -o -name '*.css' -o -name 'index.html' \) ! -name 'config.js' -delete
|
|
$($sudo)cp -a "`$STAGING"/. "`$FE"/
|
|
$($sudo)rm -rf "`$STAGING" /tmp/dist-deploy.tar.gz
|
|
REMOTE=`$(sed -n 's/.*app-build" content="\([^"]*\)".*/\1/p' "`$FE/index.html" | head -n 1)
|
|
test -n "`$REMOTE"
|
|
test "`$REMOTE" = "`$EXPECTED"
|
|
echo "FE_APP_BUILD_OK `$REMOTE"
|
|
"@ -replace "`r", ''
|
|
$localSh = Join-Path $env:TEMP ("fe-sync-{0}.sh" -f $HostInfo.Key)
|
|
$utf8NoBom = New-Object System.Text.UTF8Encoding $false
|
|
[System.IO.File]::WriteAllText($localSh, $extractCmd, $utf8NoBom)
|
|
Invoke-Scp $localSh "$($HostInfo.Host):/tmp/fe-sync-deploy.sh"
|
|
Remove-Item $localSh -Force -ErrorAction SilentlyContinue
|
|
Invoke-Ssh $HostInfo.Host "$($sudo)bash /tmp/fe-sync-deploy.sh && rm -f /tmp/fe-sync-deploy.sh"
|
|
|
|
$clbNginx = Join-Path $root 'deploy\nginx.canteen-clb-http.conf'
|
|
$nginxScript = Join-Path $root 'scripts\setup-prod-frontend-nginx.sh'
|
|
Invoke-Scp $clbNginx "$($HostInfo.Host):${FrontendDir}/nginx.canteen.conf"
|
|
Invoke-Scp $nginxScript "$($HostInfo.Host):/tmp/setup-prod-frontend-nginx.sh"
|
|
if ($HostInfo.Host -like 'root@*') {
|
|
Invoke-Ssh $HostInfo.Host "chmod +x /tmp/setup-prod-frontend-nginx.sh && bash /tmp/setup-prod-frontend-nginx.sh ${FrontendDir}/nginx.canteen.conf"
|
|
} else {
|
|
Invoke-Ssh $HostInfo.Host "chmod +x /tmp/setup-prod-frontend-nginx.sh && sudo bash /tmp/setup-prod-frontend-nginx.sh ${FrontendDir}/nginx.canteen.conf"
|
|
}
|
|
|
|
Wait-HttpOk -Url $HostInfo.Health -Label $HostInfo.Name
|
|
$httpBuild = Get-HttpAppBuild -Url $HostInfo.Health
|
|
Assert-AppBuildMatch -Label $HostInfo.Name -Actual $httpBuild -Expected $ExpectedAppBuild
|
|
}
|
|
|
|
function Deploy-BackendHost {
|
|
param($HostInfo, [string]$TarPath, [bool]$DoMigrate)
|
|
Write-Host "--> BE $($HostInfo.Name) migrate=$DoMigrate" -ForegroundColor Yellow
|
|
Invoke-Ssh $HostInfo.Host 'mkdir -p ~/deploy-staging/deploy ~/deploy-staging/scripts'
|
|
$nginxConf = Join-Path $root 'deploy\nginx.api-proxy.conf'
|
|
$nginxScript = Join-Path $root 'scripts\setup-prod-api-nginx.sh'
|
|
if (Test-Path $nginxConf) { Invoke-Scp $nginxConf "$($HostInfo.Host):~/deploy-staging/deploy/" }
|
|
if (Test-Path $nginxScript) { Invoke-Scp $nginxScript "$($HostInfo.Host):~/deploy-staging/scripts/" }
|
|
$skip = if ($DoMigrate) { '0' } else { '1' }
|
|
# 注意:成功前不要删 /tmp/backend-deploy.tar.gz;失败重试需重新 scp
|
|
$applyCmd = "set -euo pipefail; rm -rf ~/deploy-staging/dist ~/deploy-staging/migrations; mkdir -p ~/deploy-staging/dist ~/deploy-staging/migrations ~/deploy-staging/deploy ~/deploy-staging/scripts; tar -xzf /tmp/backend-deploy.tar.gz -C ~/deploy-staging; test -f ~/deploy-staging/dist/index.js; mv ~/deploy-staging/prod-apply-backend.sh ~/deploy-staging/scripts/ 2>/dev/null || true; mv ~/deploy-staging/*.sql ~/deploy-staging/migrations/ 2>/dev/null || true; chmod +x ~/deploy-staging/scripts/prod-apply-backend.sh; SKIP_DB_MIGRATE=$skip bash ~/deploy-staging/scripts/prod-apply-backend.sh; rm -f /tmp/backend-deploy.tar.gz"
|
|
$attempt = 0
|
|
while ($true) {
|
|
$attempt++
|
|
Invoke-Scp $TarPath "$($HostInfo.Host):/tmp/backend-deploy.tar.gz"
|
|
& ssh @sshOpts $HostInfo.Host -- $applyCmd
|
|
if ($LASTEXITCODE -eq 0) { break }
|
|
if ($attempt -ge 3) { throw "Backend deploy failed after $attempt tries: $($HostInfo.Host)" }
|
|
Write-Host "Backend deploy retry $attempt -> $($HostInfo.Host)" -ForegroundColor DarkYellow
|
|
Start-Sleep -Seconds (2 * $attempt)
|
|
}
|
|
Wait-HttpOk -Url $HostInfo.Health -Label $HostInfo.Name
|
|
}
|
|
|
|
Write-Host "`n=== CLB status (before) ===" -ForegroundColor Cyan
|
|
Invoke-ClbStatus
|
|
|
|
if ($SkipBuild) {
|
|
Write-Host "`n=== Skip build ===" -ForegroundColor Cyan
|
|
if (-not (Test-Path (Join-Path $root 'dist\index.html'))) { Write-Error 'Missing dist/' }
|
|
if (-not (Test-Path (Join-Path $root 'server\dist'))) { Write-Error 'Missing server/dist/' }
|
|
} else {
|
|
Write-Host "`n=== Build frontend ===" -ForegroundColor Cyan
|
|
npm run build
|
|
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
|
Write-Host "`n=== Build backend ===" -ForegroundColor Cyan
|
|
npm --prefix server run build
|
|
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
|
}
|
|
|
|
$feTar = $null
|
|
$beTar = $null
|
|
|
|
if (-not $ApiOnly) {
|
|
$feTar = Join-Path $root 'dist-deploy.tar.gz'
|
|
if (Test-Path $feTar) { Remove-Item $feTar -Force }
|
|
tar -czf $feTar -C (Join-Path $root 'dist') .
|
|
if ($LASTEXITCODE -ne 0) { Write-Error 'Failed FE tar' }
|
|
$expectedFeBuild = Get-LocalFrontendAppBuild
|
|
Write-Host "Local FE app-build=$expectedFeBuild" -ForegroundColor Cyan
|
|
} else {
|
|
$expectedFeBuild = $null
|
|
}
|
|
|
|
if (-not $H5Only) {
|
|
$beTar = Join-Path $root 'backend-deploy.tar.gz'
|
|
New-BackendDeployTar -Root $root -OutFile $beTar
|
|
}
|
|
|
|
# ---------- API rolling ----------
|
|
if (-not $H5Only) {
|
|
Write-Host "`n=== API rolling ===" -ForegroundColor Cyan
|
|
|
|
# 1) New code on spare first (skip migrate). Additive migrations: run on primary while still old code.
|
|
if (-not $SkipMigrate) {
|
|
Write-Host "--> Migrate on primary (old process still serving)" -ForegroundColor Yellow
|
|
# Upload migrations only + run migrate.js without replacing dist
|
|
$migTar = Join-Path $root 'migrations-only.tar.gz'
|
|
if (Test-Path $migTar) { Remove-Item $migTar -Force }
|
|
tar -czf $migTar -C (Join-Path $root 'migrations') .
|
|
Invoke-Scp $migTar "$($Api.Host):/tmp/migrations-only.tar.gz"
|
|
Remove-Item $migTar -Force -ErrorAction SilentlyContinue
|
|
$migCmd = @'
|
|
set -euo pipefail
|
|
APP=/opt/ai-fresh-delivery
|
|
mkdir -p "$APP/migrations"
|
|
tar -xzf /tmp/migrations-only.tar.gz -C "$APP/migrations"
|
|
rm -f /tmp/migrations-only.tar.gz
|
|
cd "$APP/server"
|
|
export MIGRATIONS_DIR="$APP/migrations"
|
|
export SKIP_DEMO_SEED=1
|
|
export SKIP_E2E_SEED=1
|
|
node dist/migrate.js
|
|
'@ -replace "`r", ''
|
|
Invoke-Ssh $Api.Host $migCmd
|
|
}
|
|
|
|
Deploy-BackendHost -HostInfo $Api1 -TarPath $beTar -DoMigrate:$false
|
|
|
|
Write-Host "--> Ensure api1 on CLB (API only)" -ForegroundColor Yellow
|
|
node $ClbRolling ensure-dual-api
|
|
if ($LASTEXITCODE -ne 0) { throw 'ensure-dual-api failed' }
|
|
|
|
Set-ClbWeight -Group api -Instance api -Weight 0
|
|
Write-Host "Drain primary API 25s..." -ForegroundColor DarkGray
|
|
Start-Sleep -Seconds 25
|
|
|
|
Deploy-BackendHost -HostInfo $Api -TarPath $beTar -DoMigrate:$false
|
|
Set-ClbWeight -Group api -Instance api -Weight 10
|
|
Set-ClbWeight -Group api -Instance api1 -Weight 10
|
|
}
|
|
|
|
# ---------- H5 rolling ----------
|
|
if (-not $ApiOnly) {
|
|
Write-Host "`n=== H5 rolling ===" -ForegroundColor Cyan
|
|
|
|
Deploy-FrontendHost -HostInfo $H5 -TarPath $feTar -ExpectedAppBuild $expectedFeBuild
|
|
|
|
Write-Host "--> Ensure h5 on CLB (H5 only, clb-safe nginx already applied)" -ForegroundColor Yellow
|
|
node $ClbRolling ensure-dual-h5
|
|
if ($LASTEXITCODE -ne 0) { throw 'ensure-dual-h5 failed' }
|
|
|
|
Set-ClbWeight -Group h5 -Instance h51 -Weight 0
|
|
Write-Host "Drain h51 15s..." -ForegroundColor DarkGray
|
|
Start-Sleep -Seconds 15
|
|
|
|
Deploy-FrontendHost -HostInfo $H51 -TarPath $feTar -ExpectedAppBuild $expectedFeBuild
|
|
Set-ClbWeight -Group h5 -Instance h5 -Weight 10
|
|
Set-ClbWeight -Group h5 -Instance h51 -Weight 10
|
|
}
|
|
|
|
if ($feTar -and (Test-Path $feTar)) { Remove-Item $feTar -Force -ErrorAction SilentlyContinue }
|
|
if ($beTar -and (Test-Path $beTar)) { Remove-Item $beTar -Force -ErrorAction SilentlyContinue }
|
|
|
|
Write-Host "`n=== CLB status (after) ===" -ForegroundColor Cyan
|
|
Invoke-ClbStatus
|
|
|
|
Write-Host "`n=== Final health ===" -ForegroundColor Cyan
|
|
$checks = @(
|
|
@{ Name = 'domain-web'; Url = 'https://www.xdjchuanhui.com/' },
|
|
@{ Name = 'domain-api'; Url = 'https://www.xdjchuanhui.com/api/health' },
|
|
@{ Name = 'h5'; Url = $H5.Health },
|
|
@{ Name = 'h51'; Url = $H51.Health },
|
|
@{ Name = 'api'; Url = $Api.Health },
|
|
@{ Name = 'api1'; Url = $Api1.Health }
|
|
)
|
|
foreach ($c in $checks) {
|
|
try {
|
|
$code = & curl.exe -sS -o NUL -w '%{http_code}' --connect-timeout 12 $c.Url
|
|
Write-Host ("{0,-12} {1} -> {2}" -f $c.Name, $c.Url, $code)
|
|
} catch {
|
|
Write-Host ("{0,-12} FAIL" -f $c.Name) -ForegroundColor Red
|
|
}
|
|
}
|
|
|
|
if (-not $ApiOnly -and $expectedFeBuild) {
|
|
Write-Host "`n=== FE fingerprint (final) ===" -ForegroundColor Cyan
|
|
foreach ($c in @(
|
|
@{ Name = 'domain-web'; Url = 'https://www.xdjchuanhui.com/' },
|
|
@{ Name = 'h5'; Url = $H5.Health },
|
|
@{ Name = 'h51'; Url = $H51.Health }
|
|
)) {
|
|
$build = Get-HttpAppBuild -Url $c.Url
|
|
Assert-AppBuildMatch -Label $c.Name -Actual $build -Expected $expectedFeBuild
|
|
}
|
|
}
|
|
|
|
Write-Host "`n=== Rolling deploy complete ===" -ForegroundColor Green
|
|
Write-Host 'Web: https://www.xdjchuanhui.com'
|
|
if ($expectedFeBuild) {
|
|
Write-Host "FE app-build: $expectedFeBuild"
|
|
}
|