#Requires -Version 7.0 <# .SYNOPSIS 把本机 SceneMarshal 源码部署到 N100 的运行副本。 .DESCRIPTION 默认(源码同步): 打包 src 与 scripts → scp 到 N100 → 就地解包 (不动 config.ini / 日志 / 缓存)→ 远端 git status 显示差异 → 容器内 compileall。 -Commit:本地 commit + push → 远端 fetch + reset --hard origin/main → 语法检查。 适合「阶段性成果」:远端 HEAD 跟上,git 历史干净。 .PARAMETER Commit 走 git 通道部署(本地提交并推送,远端硬重置到 origin/main)。 .PARAMETER Message 配合 -Commit 使用的提交说明。 .EXAMPLE deploy .EXAMPLE deploy -Commit "fix(parser): 修正日期类节目集号解析" #> [CmdletBinding()] param( [switch]$Commit, [string]$Message ) $ErrorActionPreference = 'Stop' $OutputEncoding = [Console]::OutputEncoding = [Text.Encoding]::UTF8 $RepoRoot = Split-Path -Parent $PSScriptRoot $Remote = 'ali@10.10.1.202' $RemoteRepo = '/home/ali/docker/tools/python/SceneMarshal' $SshOpts = @('-o', 'BatchMode=yes', '-o', 'ConnectTimeout=10') function Assert-Agent { if (-not $env:SSH_AUTH_SOCK) { $pipe = '\\.\pipe\openssh-ssh-agent' if (Test-Path -LiteralPath $pipe) { $env:SSH_AUTH_SOCK = $pipe } } if (-not $env:SSH_AUTH_SOCK) { throw 'SSH agent 未就绪:请启动 Bitwarden 并启用 SSH agent。' } $out = & ssh-add -l 2>&1 if ($LASTEXITCODE -ne 0) { $hint = if ($LASTEXITCODE -eq 1) { 'agent 里没有可用密钥' } else { '连不上 agent 管道' } throw "SSH agent 不可用($hint):$out`n请先解锁 Bitwarden。" } } function Invoke-RemoteScript([string]$Body) { $local = Join-Path ([IO.Path]::GetTempPath()) ("SceneMarshal-remote-{0}.sh" -f [guid]::NewGuid().ToString('N').Substring(0, 8)) [IO.File]::WriteAllText($local, $Body, [Text.UTF8Encoding]::new($false)) try { & scp @SshOpts -q $local "${Remote}:/tmp/SceneMarshal-deploy.sh" if ($LASTEXITCODE -ne 0) { throw 'scp 上传远端脚本失败' } & ssh @SshOpts $Remote 'bash /tmp/SceneMarshal-deploy.sh' if ($LASTEXITCODE -ne 0) { throw "远端脚本执行失败(exit $LASTEXITCODE)" } } finally { Remove-Item -LiteralPath $local -ErrorAction SilentlyContinue } } $stopwatch = [Diagnostics.Stopwatch]::StartNew() Assert-Agent Push-Location $RepoRoot try { if ($Commit) { if (-not $Message) { throw '-Commit 需要同时提供 -Message "提交说明"' } Write-Host '▶ 本地提交并推送 ...' -ForegroundColor Cyan & git add -A & git diff --cached --quiet if ($LASTEXITCODE -eq 0) { Write-Host ' 没有待提交改动,跳过 commit' -ForegroundColor DarkGray } else { & git commit -m $Message if ($LASTEXITCODE -ne 0) { throw 'git commit 失败' } } & git push origin main if ($LASTEXITCODE -ne 0) { throw 'git push 失败' } # 单引号 here-string:保留 shell 的 $1/$src/$dst,避免被 PowerShell 提前展开 Invoke-RemoteScript @' set -euo pipefail REPO=/home/ali/docker/tools/python/SceneMarshal cd "$REPO" git fetch --prune origin git reset --hard origin/main echo '=== 远端 HEAD ===' git log --oneline -1 echo '=== 容器内语法检查 ===' docker exec -u 0 python find /python/SceneMarshal/src -name __pycache__ -type d -exec chown -R 1000:1000 {} + 2>/dev/null || true docker exec -u 1000:1000 -w /python/SceneMarshal python sh -c 'if [ -x .venv/bin/python ]; then exec .venv/bin/python -m compileall -q src scripts; else exec python3 -m compileall -q src scripts; fi' echo COMPILE_OK echo '=== 安装运维脚本 ===' install_tool() { local src="$1" dst="$2" [ -f "$src" ] || return 0 if sudo -n install -m 755 "$src" "$dst"; then echo "已更新 $dst" else echo "⚠️ 安装失败: $dst(需要免密 sudo)" fi } install_tool "$REPO/scripts/media-rename" /usr/local/bin/media-rename install_tool "$REPO/scripts/media_archive.py" /usr/local/bin/media-archive '@ } else { $tgz = Join-Path ([IO.Path]::GetTempPath()) 'SceneMarshal-sync.tgz' Write-Host '▶ 打包 src / scripts ...' -ForegroundColor Cyan if (Test-Path -LiteralPath $tgz) { Remove-Item -LiteralPath $tgz -Force } & tar -czf $tgz -C $RepoRoot --exclude=__pycache__ --exclude='*.pyc' ` --exclude='config.ini' --exclude='*.log' --exclude='tmdb_data.json' ` --exclude='preflight_cache.json' --exclude='*.lock' src scripts if ($LASTEXITCODE -ne 0) { throw '本地打包失败' } Write-Host (" {0} KB" -f [math]::Round((Get-Item -LiteralPath $tgz).Length / 1KB, 1)) -ForegroundColor DarkGray Write-Host '▶ 上传到 N100 ...' -ForegroundColor Cyan & scp @SshOpts -q $tgz "${Remote}:/tmp/SceneMarshal-sync.tgz" if ($LASTEXITCODE -ne 0) { throw 'scp 上传失败' } Invoke-RemoteScript @' set -euo pipefail REPO=/home/ali/docker/tools/python/SceneMarshal cd "$REPO" TS=$(date +%Y%m%d-%H%M%S) mkdir -p /tmp/SceneMarshal-backup tar -czf "/tmp/SceneMarshal-backup/src-$TS.tar.gz" src scripts tar -xzf /tmp/SceneMarshal-sync.tgz -C "$REPO" find src scripts -name '*.py' -exec chmod 644 {} + echo "=== 远端相对仓库 HEAD 的变更 ===" CHANGED=$(git status --porcelain | head -40 || true) if [ -z "$CHANGED" ]; then echo "(无差异,远端已是最新)"; else echo "$CHANGED"; fi echo "=== 容器内语法检查 ===" docker exec -u 0 python find /python/SceneMarshal/src -name __pycache__ -type d -exec chown -R 1000:1000 {} + 2>/dev/null || true docker exec -u 1000:1000 -w /python/SceneMarshal python sh -c 'if [ -x .venv/bin/python ]; then exec .venv/bin/python -m compileall -q src scripts; else exec python3 -m compileall -q src scripts; fi' echo COMPILE_OK echo "=== 安装运维脚本 ===" install_tool() { local src="$1" dst="$2" [ -f "$src" ] || return 0 if sudo -n install -m 755 "$src" "$dst"; then echo "已更新 $dst" else echo "⚠️ 安装失败: $dst(需要免密 sudo)" fi } install_tool "$REPO/scripts/media-rename" /usr/local/bin/media-rename install_tool "$REPO/scripts/media_archive.py" /usr/local/bin/media-archive echo "备份: /tmp/SceneMarshal-backup/src-$TS.tar.gz" '@ } } finally { Pop-Location $stopwatch.Stop() } Write-Host ("✔ 部署完成,用时 {0:N1}s" -f $stopwatch.Elapsed.TotalSeconds) -ForegroundColor Green