Files
SceneMarshal/scripts/deploy.ps1
T
ali 08ceff9f9d feat(scripts): media-rename 升级为 /usr/local/bin 真命令
- 新增 scripts/media-rename:解析 -p/--path(realpath + 挂载范围校验),
  未给路径时默认当前目录,有 TTY 用 docker exec -it,无 TTY 自动降级,
  便于非交互 ssh 调用
- deploy.ps1 同步 scripts/ 并在远端安装到 /usr/local/bin/media-rename
2026-11-17 12:17:53 +08:00

152 lines
5.7 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
把本机 SceneRename 源码部署到 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()) ("scenerename-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/scenerename-deploy.sh"
if ($LASTEXITCODE -ne 0) { throw 'scp 上传远端脚本失败' }
& ssh @SshOpts $Remote 'bash /tmp/scenerename-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 '=== 安装运维脚本 ==='
if [ -f "$RemoteRepo/scripts/media-rename" ]; then
sudo -n install -m 755 "$RemoteRepo/scripts/media-rename" /usr/local/bin/media-rename \
&& echo '已更新 /usr/local/bin/media-rename' \
|| echo '⚠️ 安装失败(需要免密 sudo)'
fi
"@
}
else {
$tgz = Join-Path ([IO.Path]::GetTempPath()) 'scenerename-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/scenerename-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/scenerename-backup
tar -czf "/tmp/scenerename-backup/src-$TS.tar.gz" media/src
tar -xzf /tmp/scenerename-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 "=== 安装运维脚本 ==="
if [ -f "$REPO/scripts/media-rename" ]; then
sudo -n install -m 755 "$REPO/scripts/media-rename" /usr/local/bin/media-rename \
&& echo "已更新 /usr/local/bin/media-rename" \
|| echo "⚠️ 安装失败(需要免密 sudo)"
fi
echo "备份: /tmp/scenerename-backup/src-$TS.tar.gz"
'@
}
}
finally {
Pop-Location
$stopwatch.Stop()
}
Write-Host ("✔ 部署完成,用时 {0:N1}s" -f $stopwatch.Elapsed.TotalSeconds) -ForegroundColor Green