Files
SceneMarshal/scripts/deploy.ps1
T
ali e66d082f31 chore: 项目更名为 SceneMarshal
- README/脚本注释/LICENSE/图标文件名统一改名
- 右键菜单注册项改为 SceneMarshal / SceneMarshalArchive
- media_archive 支持 SCENEMARSHAL_* 环境变量(保留旧变量兼容)
- README 补充 preflight/字幕/字体/归档等实际能力与 check 用法
2026-11-19 12:21:42 +08:00

164 lines
5.9 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.
#Requires -Version 7.0
<#
.SYNOPSIS
把本机 SceneMarshal 源码部署到 N100 的运行副本。
.DESCRIPTION
默认(源码同步):
打包 media/src → 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'
$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 失败' }
Invoke-RemoteScript @"
set -euo pipefail
cd $RemoteRepo
git fetch --prune origin
git reset --hard origin/main
echo '=== 远端 HEAD ==='
git log --oneline -1
echo '=== 容器内语法检查 ==='
docker exec -u 0 python find /python/media/src -name __pycache__ -type d -exec chown -R 1000:1000 {} + 2>/dev/null || true
docker exec -u 1000:1000 -w /python/media/src python /python/media/bin/python -m compileall -q .
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 "$RemoteRepo/scripts/media-rename" /usr/local/bin/media-rename
install_tool "$RemoteRepo/scripts/media_archive.py" /usr/local/bin/media-archive
"@
}
else {
$tgz = Join-Path ([IO.Path]::GetTempPath()) 'SceneMarshal-sync.tgz'
Write-Host '▶ 打包 media/src ...' -ForegroundColor Cyan
if (Test-Path -LiteralPath $tgz) { Remove-Item -LiteralPath $tgz -Force }
& tar -czf $tgz -C $RepoRoot --exclude=__pycache__ --exclude='*.pyc' media/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
cd "$REPO"
TS=$(date +%Y%m%d-%H%M%S)
mkdir -p /tmp/SceneMarshal-backup
tar -czf "/tmp/SceneMarshal-backup/src-$TS.tar.gz" media/src
tar -xzf /tmp/SceneMarshal-sync.tgz -C "$REPO"
find media/src -name '*.py' -exec chmod 644 {} +
echo "=== 远端相对仓库 HEAD 的变更 ==="
CHANGED=$(git status --porcelain -- media/src | head -40 || true)
if [ -z "$CHANGED" ]; then echo "(无差异,远端已是最新)"; else echo "$CHANGED"; fi
echo "=== 容器内语法检查 ==="
docker exec -u 0 python find /python/media/src -name __pycache__ -type d -exec chown -R 1000:1000 {} + 2>/dev/null || true
docker exec -u 1000:1000 -w /python/media/src python /python/media/bin/python -m compileall -q .
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