feat: 新增 XYZ 图层使用人数统计服务
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
<#
|
||||
一键部署 xyzserver:本地测试 → 编译 Linux 版 → 上传 → 服务器上安装并重启 → 外网检查。
|
||||
可重复执行,首次会顺带在宝塔里新建网站。
|
||||
|
||||
用法(在 xyzserver 目录或任意目录):
|
||||
powershell -ExecutionPolicy Bypass -File deploy\deploy.ps1
|
||||
powershell -ExecutionPolicy Bypass -File deploy\deploy.ps1 -DebugLog # 打开逐条瓦片日志,用于客户端实测
|
||||
powershell -ExecutionPolicy Bypass -File deploy\deploy.ps1 -SetupKey # 新电脑上首次使用:生成密钥并输入一次服务器密码
|
||||
#>
|
||||
param(
|
||||
[string]$Server = "asio.asia",
|
||||
[string]$User = "root",
|
||||
[int]$SshPort = 22,
|
||||
[string]$Domain = "xyz.asio.asia",
|
||||
[int]$AppPort = 18480,
|
||||
[string]$KeyFile = "$HOME\.ssh\xyzserver_deploy",
|
||||
[switch]$DebugLog,
|
||||
[switch]$SkipTests,
|
||||
[switch]$SetupKey
|
||||
)
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
$ProjectDir = Split-Path -Parent $PSScriptRoot
|
||||
$Target = "$User@$Server"
|
||||
$SshArgs = @("-i", $KeyFile, "-p", "$SshPort", "-o", "BatchMode=yes", "-o", "StrictHostKeyChecking=accept-new", "-o", "ConnectTimeout=15")
|
||||
$ScpArgs = @("-i", $KeyFile, "-P", "$SshPort", "-o", "BatchMode=yes", "-o", "StrictHostKeyChecking=accept-new", "-o", "ConnectTimeout=15")
|
||||
|
||||
function Step($msg) { Write-Host "==> $msg" -ForegroundColor Cyan }
|
||||
|
||||
function Invoke-Checked {
|
||||
param([string]$Exe, [string[]]$Arguments)
|
||||
& $Exe @Arguments
|
||||
if ($LASTEXITCODE -ne 0) { throw "$Exe 执行失败(退出码 $LASTEXITCODE)" }
|
||||
}
|
||||
|
||||
if ($SetupKey) {
|
||||
Step "配置 SSH 密钥登录"
|
||||
if (-not (Test-Path $KeyFile)) {
|
||||
New-Item -ItemType Directory -Force (Split-Path $KeyFile) | Out-Null
|
||||
& ssh-keygen -q -t ed25519 -f $KeyFile -N '""' -C "xyzserver-deploy@$env:COMPUTERNAME"
|
||||
if ($LASTEXITCODE -ne 0) { throw "生成密钥失败" }
|
||||
}
|
||||
$pub = (Get-Content "$KeyFile.pub" -Raw).Trim()
|
||||
Write-Host "接下来会提示输入一次 $Target 的密码。"
|
||||
& ssh -p $SshPort -o StrictHostKeyChecking=accept-new $Target "umask 077; mkdir -p ~/.ssh; touch ~/.ssh/authorized_keys; grep -qxF '$pub' ~/.ssh/authorized_keys || echo '$pub' >> ~/.ssh/authorized_keys"
|
||||
if ($LASTEXITCODE -ne 0) { throw "安装公钥失败" }
|
||||
}
|
||||
|
||||
if (-not (Test-Path $KeyFile)) {
|
||||
throw "没有找到部署密钥 $KeyFile。请先加 -SetupKey 运行一次。"
|
||||
}
|
||||
& ssh @SshArgs $Target "true"
|
||||
if ($LASTEXITCODE -ne 0) { throw "无法用密钥登录 $Target。请加 -SetupKey 重新配置。" }
|
||||
|
||||
Push-Location $ProjectDir
|
||||
$saved = @{ GOOS = $env:GOOS; GOARCH = $env:GOARCH; CGO_ENABLED = $env:CGO_ENABLED; GOPROXY = $env:GOPROXY }
|
||||
try {
|
||||
if (-not $env:GOPROXY) { $env:GOPROXY = "https://goproxy.cn,direct" }
|
||||
$env:CGO_ENABLED = "0"
|
||||
|
||||
if (-not $SkipTests) {
|
||||
Step "本地测试"
|
||||
Remove-Item Env:GOOS, Env:GOARCH -ErrorAction SilentlyContinue
|
||||
Invoke-Checked go @("test", "./...")
|
||||
}
|
||||
|
||||
Step "编译 Linux 版本"
|
||||
$arch = (& ssh @SshArgs $Target "uname -m").Trim()
|
||||
$goarch = switch ($arch) { "x86_64" { "amd64" } "aarch64" { "arm64" } default { throw "不支持的服务器架构:$arch" } }
|
||||
$buildDir = Join-Path $PSScriptRoot ".build"
|
||||
New-Item -ItemType Directory -Force $buildDir | Out-Null
|
||||
$env:GOOS = "linux"; $env:GOARCH = $goarch
|
||||
Invoke-Checked go @("build", "-trimpath", "-ldflags", "-s -w", "-o", (Join-Path $buildDir "xyzserver"), ".")
|
||||
}
|
||||
finally {
|
||||
foreach ($k in $saved.Keys) {
|
||||
if ($null -eq $saved[$k]) { Remove-Item "Env:$k" -ErrorAction SilentlyContinue } else { Set-Item "Env:$k" $saved[$k] }
|
||||
}
|
||||
Pop-Location
|
||||
}
|
||||
|
||||
Step "上传到 $Target"
|
||||
Invoke-Checked ssh (@($SshArgs) + @($Target, "rm -rf /tmp/xyzserver-deploy && mkdir -p /tmp/xyzserver-deploy"))
|
||||
Invoke-Checked scp (@($ScpArgs) + @(
|
||||
(Join-Path $buildDir "xyzserver"),
|
||||
(Join-Path $PSScriptRoot "remote.sh"),
|
||||
(Join-Path $PSScriptRoot "bt_addsite.py"),
|
||||
"${Target}:/tmp/xyzserver-deploy/"))
|
||||
|
||||
Step "服务器上安装并重启"
|
||||
$debug = if ($DebugLog) { "1" } else { "0" }
|
||||
$remoteCmd = "sed -i 's/\r$//' /tmp/xyzserver-deploy/remote.sh /tmp/xyzserver-deploy/bt_addsite.py && " +
|
||||
"XYZ_DOMAIN='$Domain' XYZ_PORT='$AppPort' XYZ_DEBUG='$debug' bash /tmp/xyzserver-deploy/remote.sh"
|
||||
Invoke-Checked ssh (@($SshArgs) + @($Target, $remoteCmd))
|
||||
|
||||
Step "外网检查 http://$Domain"
|
||||
$health = ""
|
||||
for ($i = 0; $i -lt 10 -and $health -ne "ok"; $i++) {
|
||||
Start-Sleep -Seconds 1
|
||||
$health = & curl.exe -s -m 15 "http://$Domain/healthz"
|
||||
}
|
||||
if ($health -ne "ok") { throw "外网 /healthz 返回:$health" }
|
||||
$headers = & curl.exe -s -m 15 -I "http://$Domain/tiles/deploycheck/0/0/0.png"
|
||||
$headers | Select-String -Pattern "^HTTP/|Cache-Control|Content-Type|Content-Length|Set-Cookie" | ForEach-Object { Write-Host " $_" }
|
||||
if (-not ($headers -match "Cache-Control: no-store")) { throw "瓦片响应缺少 Cache-Control: no-store,请检查 Nginx 配置" }
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "部署完成。统计页:http://$Domain/" -ForegroundColor Green
|
||||
Write-Host "图层地址:http://$Domain/tiles/{table}/{z}/{x}/{y}.png"
|
||||
if ($DebugLog) { Write-Host "调试日志已打开,查看:ssh $Target journalctl -u xyzserver -f。测完请不带 -DebugLog 重新部署一次关闭它。" -ForegroundColor Yellow }
|
||||
Reference in New Issue
Block a user