# ============================================================================= # PMS 正式环境部署脚本(在**本地开发机**执行,不要拿到服务器上跑) # # 为什么是 PowerShell 而不是原 deploy-prod.sh: # 原脚本有 3 个致命问题(2026-09 修复)—— # 1. 它在 `target/release/` 找二进制,而实际构建产物在 # `target/x86_64-unknown-linux-gnu/release/`(zigbuild 交叉编译)→ 找不到文件 # 2. 它安装并重启 `pms-api-prod` / `pms-device-prod`,而线上真实单元名是 # `pms-api` / `pms-device` → 会**多出一套重复单元**且重启失败 # 3. 它会用本地 `.env.production` **覆盖线上 `.env`** → 一旦本地端口/主机写错, # 生产直接起不来(历史上 `.env` 还带行内注释,更是必挂) # 本脚本按"本地构建 → 校验产物 → 服务器备份 → 传 `.new` → 原子替换 → 重启 → 验收" # 的顺序执行,并在收尾打印回滚方法。 # # 用法: # pwsh deploy/deploy-prod.ps1 # 完整部署(构建 + 上传 + 重启 + 验收) # pwsh deploy/deploy-prod.ps1 -SkipBuild # 跳过构建,用现有产物重新部署 # pwsh deploy/deploy-prod.ps1 -DryRun # 只打印将要执行的步骤,不做任何改动 # pwsh deploy/deploy-prod.ps1 -SkipWeb # 只发后端,不更新前端 # ============================================================================= param( [switch]$SkipBuild, # 跳过 cargo zigbuild / npm run build [switch]$SkipWeb, # 跳过前端 [switch]$DryRun # 只演练,不执行 ) $ErrorActionPreference = 'Stop' # ---- 环境参数(正式环境)--------------------------------------------------- $Host_ = '118.31.119.206' $User = 'root' $Pwd_ = 'Hbhyg731024@' # 内网仓库明文密码(见 AGENTS.md「代码审查豁免」) $Dst = '/data/pms' $ApiPort = 9000 $TcpPort = 9001 $Domain = 'app.anzhizhichong.com' # ---- 路径 ------------------------------------------------------------------- $Repo = Split-Path -Parent $PSScriptRoot $ApiBin = Join-Path $Repo 'software/api-server/target/x86_64-unknown-linux-gnu/release/pms-api' $DevBin = Join-Path $Repo 'software/device-server/target/x86_64-unknown-linux-gnu/release/pms-device-server' $WebDist = Join-Path $Repo 'software/web/dist' function Step($msg) { Write-Host "`n=== $msg ===" -ForegroundColor Cyan } function Say($msg) { Write-Host " $msg" } function Invoke-Remote([string]$cmd) { if ($DryRun) { Say "[DryRun] ssh: $cmd"; return } & plink -batch -pw $Pwd_ "$User@$Host_" $cmd if ($LASTEXITCODE -ne 0) { throw "远端命令失败(exit $LASTEXITCODE):$cmd" } } function Send-File([string]$local, [string]$remote) { if ($DryRun) { Say "[DryRun] scp: $local -> $remote"; return } & pscp -batch -pw $Pwd_ $local "$User@$Host_`:$remote" if ($LASTEXITCODE -ne 0) { throw "上传失败(exit $LASTEXITCODE):$local" } } # ---- 1. 构建 --------------------------------------------------------------- if (-not $SkipBuild) { Step '1/7 本地交叉编译(服务器 2核/3.5G 禁止编译,必须本地构建)' Say 'api-server ...' if (-not $DryRun) { cargo zigbuild --release --target x86_64-unknown-linux-gnu --manifest-path (Join-Path $Repo 'software/api-server/Cargo.toml') } Say 'device-server ...' if (-not $DryRun) { cargo zigbuild --release --target x86_64-unknown-linux-gnu --manifest-path (Join-Path $Repo 'software/device-server/Cargo.toml') } if (-not $SkipWeb) { Say 'web ...' if (-not $DryRun) { Push-Location (Join-Path $Repo 'software/web'); npm run build; Pop-Location } } } # ---- 2. 校验产物 + 静态检查 ------------------------------------------------ Step '2/7 校验产物与静态检查' foreach ($f in @($ApiBin, $DevBin)) { if (-not (Test-Path $f)) { throw "找不到构建产物:$f`n(请先执行 cargo zigbuild,或用 -SkipBuild 前确认已构建)" } Say ("✅ {0} ({1:N1} MB)" -f (Split-Path $f -Leaf), ((Get-Item $f).Length / 1MB)) } if (-not $SkipWeb) { if (-not (Test-Path (Join-Path $WebDist 'index.html'))) { throw "找不到前端产物:$WebDist" } Say ("✅ web/dist ({0} 个文件)" -f (Get-ChildItem $WebDist -Recurse -File).Count) } # ---- 3. 服务器备份(可回滚)------------------------------------------------ Step '3/7 服务器备份(保留旧二进制/.env/nginx/单元/前端)' Invoke-Remote @" set -e TS=`$(date +%Y%m%d-%H%M%S) mkdir -p $Dst/backup-`$TS cp -a $Dst/api-server/pms-api $Dst/backup-`$TS/ 2>/dev/null || true cp -a $Dst/device-server/pms-device-server $Dst/backup-`$TS/ 2>/dev/null || true cp -a $Dst/api-server/.env $Dst/device-server/.env $Dst/backup-`$TS/ 2>/dev/null || true mkdir -p $Dst/backup-`$TS/web && cp -a $Dst/web/. $Dst/backup-`$TS/web/ 2>/dev/null || true echo `$TS > /tmp/pms_last_backup echo " 备份目录: $Dst/backup-`$TS" "@ # ---- 4. 上传(临时名,避免 ETXTBSY)---------------------------------------- Step '4/7 上传(二进制传 .new,运行中的二进制不可直接覆盖)' Send-File $ApiBin "$Dst/api-server/pms-api.new" Send-File $DevBin "$Dst/device-server/pms-device-server.new" if (-not $SkipWeb) { $tgz = Join-Path $env:TEMP 'pms-dist.tgz' if (-not $DryRun) { tar -czf $tgz -C $WebDist . } Send-File $tgz '/tmp/pms-dist.tgz' } # ---- 5. 切换 + 重启 -------------------------------------------------------- Step '5/7 原子替换并重启服务' Invoke-Remote @" set -e chmod +x $Dst/api-server/pms-api.new $Dst/device-server/pms-device-server.new systemctl stop pms-api pms-device mv -f $Dst/api-server/pms-api.new $Dst/api-server/pms-api mv -f $Dst/device-server/pms-device-server.new $Dst/device-server/pms-device-server $(if (-not $SkipWeb) { "rm -rf $Dst/web/* && tar -xzf /tmp/pms-dist.tgz -C $Dst/web/ && rm -f /tmp/pms-dist.tgz" } else { 'true' }) systemctl daemon-reload systemctl start pms-device pms-api sleep 8 nginx -t && systemctl reload nginx "@ # ---- 6. 验收 --------------------------------------------------------------- Step '6/7 验收(服务/端口/域名/日志)' Invoke-Remote @" echo '--- 服务 ---'; systemctl is-active pms-api pms-device nginx | paste -sd' ' echo '--- 端口(应为 $ApiPort / $TcpPort)---'; ss -lntp | grep -E ':$ApiPort|:$TcpPort' | awk '{print `" `" `$4}' echo '--- 域名 ---' curl -sk -m 8 -o /dev/null -w ' /api/pms/cabinets -> %{http_code}\n' https://$Domain/api/pms/cabinets curl -sk -m 8 -o /dev/null -w ' /pms/ -> %{http_code}\n' https://$Domain/pms/ echo '--- 启动日志尾部 ---'; journalctl -u pms-api --no-pager -n 4 | tail -4 echo '--- 新进程 ERROR 数(应为 0)---' MAIN=`$(systemctl show -p MainPID --value pms-api) journalctl -u pms-api --since '-3 min' --no-pager | grep -c " ERROR " | sed 's/^/ ERROR 行数: /' "@ # ---- 7. 消费链路探测(无副作用)------------------------------------------- Step '7/7 上报消费链路探测(用不存在的 IMEI,不碰真实设备数据)' if (-not $DryRun) { $probe = @' import redis, json, time r = redis.Redis(host='10.8.0.252', password='Hbhyg731024@', db=5, socket_timeout=5) r.lpush('device:reports', json.dumps({"act":"auth_str","dev_id":"899999999999999","msg_id":1})) time.sleep(3) left = r.llen('device:reports') print(f" 队列剩余={left} -> {'✅ 消费正常' if left == 0 else '❌ 消费未工作,请立即回滚'}") '@ $probe | Out-File -Encoding utf8 "$env:TEMP\pms_probe.py" python "$env:TEMP\pms_probe.py" } Write-Host "`n=== 部署完成 ===" -ForegroundColor Green Say "正式环境:https://$Domain/pms/" Say "API :$ApiPort TCP :$TcpPort" Say '' Say '回滚方法(如需):' Say " plink -batch -pw root@$Host_ `"systemctl stop pms-api pms-device && cp /data/pms/backup-/* /data/pms/... && systemctl start pms-api pms-device`"" Say ' 备份目录见 /data/pms/backup-<时间戳>/(脚本已把 .env / 前端 / 单元一并备份)'