aisop/tools/Deploy-D3Nano.ps1

72 lines
4.5 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

param(
[string]$Target = 'Administrator@192.168.0.184',
[string]$InstallRoot = 'D:\D3Nano',
[string]$ModelSource = 'E:\D3Nano',
[string]$WheelsSource = 'E:\00-Inbox\aisop\scratch\wheels\d3nano',
[string]$WheelsRemote = 'D:\d3-wheels',
[string]$ServiceScript = 'C:\sophelper\current\services\start_d3_lite_local.py',
[int]$Port = 8766,
[switch]$SkipWheels,
[switch]$SkipStart
)
# 把 d3-nano(模型 + 离线 wheel + Python 环境)部署到远端 Windows 工位机,并实机验证服务。
# 远端访问 pytorch 官方源很慢,wheel 由本机 pip download 后打包传入。
$ErrorActionPreference = 'Stop'
if ($Target -notmatch '^[a-zA-Z0-9_.-]+@[a-zA-Z0-9_.-]+$') { throw 'Target must be user@host' }
if ($InstallRoot -notmatch '^[A-Za-z]:\\[A-Za-z0-9_.\\-]+$') { throw 'InstallRoot must be an absolute Windows path without spaces' }
if ($WheelsRemote -notmatch '^[A-Za-z]:\\[A-Za-z0-9_.\\-]+$') { throw 'WheelsRemote must be an absolute Windows path without spaces' }
$repo = Split-Path $PSScriptRoot -Parent
$remoteFwd = $InstallRoot.Replace('\', '/')
# 1. 模型包:model/ 与 local-model-manifest.json,跳过 __pycache__ 与下载缓存。
$modelTar = Join-Path $repo 'artifacts\d3-nano-model.tar'
Push-Location $ModelSource
try {
$items = Get-ChildItem -LiteralPath $ModelSource -Force | Where-Object { $_.Name -in @('model', 'local-model-manifest.json', 'local-serving-settings.json') } | Select-Object -ExpandProperty Name
if (!$items) { throw "模型源目录缺少 model/:$ModelSource" }
if (Test-Path -LiteralPath $modelTar) { Remove-Item -LiteralPath $modelTar -Force }
& tar -cf $modelTar $items
if ($LASTEXITCODE) { throw '打包模型失败' }
} finally { Pop-Location }
Write-Host ('模型包: ' + $modelTar + ' (' + [math]::Round((Get-Item $modelTar).Length/1GB,2) + ' GB)')
# 2. wheel 包:本机 pip download 得到的 cp313 / win_amd64 / cu128 依赖。
$wheelsTar = ''
if (!$SkipWheels) {
if (!(Test-Path -LiteralPath $WheelsSource)) { throw "缺少 wheel 目录: $WheelsSource" }
$wheelCount = @(Get-ChildItem -LiteralPath $WheelsSource -Filter *.whl).Count
if (!$wheelCount) { throw "目录中没有 wheel: $WheelsSource" }
$wheelsTar = Join-Path $repo 'artifacts\d3-nano-wheels.tar'
if (Test-Path -LiteralPath $wheelsTar) { Remove-Item -LiteralPath $wheelsTar -Force }
& tar -cf $wheelsTar -C $WheelsSource .
if ($LASTEXITCODE) { throw '打包 wheel 失败' }
Write-Host ('wheel 包: ' + $wheelsTar + ' (' + [math]::Round((Get-Item $wheelsTar).Length/1MB,1) + ' MB, ' + $wheelCount + ' 个)')
}
# 3. 传入远端并安装。
$payload = @($modelTar, (Join-Path $PSScriptRoot 'windows\install-d3nano.ps1'), (Join-Path $PSScriptRoot 'windows\register-d3nano-task.ps1'), (Join-Path $PSScriptRoot 'windows\d3nano-service.py'), (Join-Path $PSScriptRoot 'windows\probe-d3nano.py'))
if ($wheelsTar) { $payload += $wheelsTar }
& ssh -o BatchMode=yes $Target "New-Item -ItemType Directory -Force -Path $InstallRoot, $WheelsRemote, $InstallRoot\benchmark | Out-Null"
if ($LASTEXITCODE) { throw 'SSH failed' }
& scp -o BatchMode=yes $payload "${Target}:$remoteFwd/"
if ($LASTEXITCODE) { throw 'Transfer failed' }
$images = Get-ChildItem -LiteralPath (Join-Path $repo 'artifacts\imajev-benchmark') -Filter *-input.jpg
& scp -o BatchMode=yes $images.FullName "${Target}:$remoteFwd/benchmark/"
if ($LASTEXITCODE) { throw 'Transfer failed' }
if ($wheelsTar) {
& ssh -o BatchMode=yes $Target "tar -xf $InstallRoot\d3-nano-wheels.tar -C $WheelsRemote"
if ($LASTEXITCODE) { throw '远端解包 wheel 失败' }
}
& ssh -o BatchMode=yes $Target "powershell -NoProfile -ExecutionPolicy Bypass -File $InstallRoot\install-d3nano.ps1 -InstallRoot $InstallRoot -WheelsDir $WheelsRemote"
if ($LASTEXITCODE) { throw '安装 d3-nano 失败' }
# 4. 注册开机自启的计划任务承载服务,并实机验证(真实相机 JPEG + noul 请求)。
$venvPython = Join-Path $InstallRoot '.venv\Scripts\python.exe'
if (!$SkipStart) {
& ssh -o BatchMode=yes $Target "powershell -NoProfile -ExecutionPolicy Bypass -File $InstallRoot\register-d3nano-task.ps1 -InstallRoot $InstallRoot -Port $Port -Script $ServiceScript"
if ($LASTEXITCODE) { throw 'd3-nano 计划任务注册或启动失败' }
& ssh -o BatchMode=yes $Target "$venvPython $InstallRoot\d3nano-service.py probe --root $InstallRoot --port $Port --image-root $InstallRoot\benchmark"
if ($LASTEXITCODE) { throw 'd3-nano 真实图片请求验证失败' }
}
Write-Host "已部署 d3-nano 到 $Target:$InstallRoot;SOPHELPER_D3_ROOT 已写入系统环境变量。"