feat: 新增 XYZ 图层使用人数统计服务
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
# 用宝塔自己的接口新建网站,使其出现在面板的网站列表里。
|
||||
# 用法:/www/server/panel/pyenv/bin/python3 bt_addsite.py <域名> <网站根目录>
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
|
||||
os.chdir("/www/server/panel")
|
||||
sys.path.insert(0, "/www/server/panel/class")
|
||||
|
||||
import public # noqa: E402
|
||||
import panelSite # noqa: E402
|
||||
|
||||
domain, root = sys.argv[1], sys.argv[2]
|
||||
|
||||
get = public.dict_obj()
|
||||
get.webname = json.dumps({"domain": domain, "domainlist": [], "count": 0})
|
||||
get.path = root
|
||||
get.type_id = "0"
|
||||
get.type = "PHP"
|
||||
get.version = "00"
|
||||
get.port = "80"
|
||||
get.ps = "XYZ 图层使用人数统计(xyzserver)"
|
||||
get.ftp = "false"
|
||||
get.sql = "false"
|
||||
get.codeing = "utf8mb4"
|
||||
|
||||
res = panelSite.panelSite().AddSite(get)
|
||||
print(json.dumps(res, ensure_ascii=False, default=str))
|
||||
ok = isinstance(res, dict) and (res.get("siteStatus") is True or res.get("status") is True)
|
||||
sys.exit(0 if ok else 1)
|
||||
@@ -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 }
|
||||
@@ -0,0 +1,149 @@
|
||||
#!/usr/bin/env bash
|
||||
# 在服务器上以 root 执行,由 deploy.ps1 上传后调用。可重复执行。
|
||||
set -euo pipefail
|
||||
|
||||
DOMAIN="${XYZ_DOMAIN:?缺少 XYZ_DOMAIN}"
|
||||
PORT="${XYZ_PORT:?缺少 XYZ_PORT}"
|
||||
DEBUG="${XYZ_DEBUG:-0}"
|
||||
|
||||
STAGE=/tmp/xyzserver-deploy
|
||||
APP=/opt/xyzserver
|
||||
PANEL=/www/server/panel
|
||||
NGINX=/www/server/nginx/sbin/nginx
|
||||
VHOST=$PANEL/vhost/nginx
|
||||
SITE_CONF=$VHOST/$DOMAIN.conf
|
||||
REWRITE=$PANEL/vhost/rewrite/$DOMAIN.conf
|
||||
UPSTREAM=$VHOST/0.xyzserver.upstream.conf
|
||||
SITE_ROOT=/www/wwwroot/$DOMAIN
|
||||
|
||||
log() { echo "[deploy] $*"; }
|
||||
fail() { echo "[deploy] 失败:$*" >&2; exit 1; }
|
||||
|
||||
[ -x "$NGINX" ] || fail "没有找到宝塔的 Nginx:$NGINX"
|
||||
[ -d "$PANEL" ] || fail "没有找到宝塔面板:$PANEL"
|
||||
[ -f "$STAGE/xyzserver" ] || fail "没有找到上传的程序:$STAGE/xyzserver"
|
||||
|
||||
log "1/5 运行账号和目录"
|
||||
if ! id xyzserver >/dev/null 2>&1; then
|
||||
useradd --system --no-create-home --shell "$(command -v nologin || echo /sbin/nologin)" xyzserver
|
||||
fi
|
||||
mkdir -p "$APP/data"
|
||||
chown xyzserver:xyzserver "$APP/data"
|
||||
chmod 750 "$APP/data"
|
||||
|
||||
log "2/5 安装程序"
|
||||
install -m 755 "$STAGE/xyzserver" "$APP/xyzserver.new"
|
||||
if [ -f "$APP/xyzserver" ]; then
|
||||
cp -f "$APP/xyzserver" "$APP/xyzserver.prev"
|
||||
fi
|
||||
mv -f "$APP/xyzserver.new" "$APP/xyzserver"
|
||||
|
||||
log "3/5 systemd 服务(端口 127.0.0.1:$PORT,调试日志=$DEBUG)"
|
||||
cat > /etc/systemd/system/xyzserver.service <<EOF
|
||||
[Unit]
|
||||
Description=XYZ layer usage counter (xyzserver)
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
User=xyzserver
|
||||
Group=xyzserver
|
||||
ExecStart=$APP/xyzserver
|
||||
Environment=XYZ_LISTEN=127.0.0.1:$PORT
|
||||
Environment=XYZ_DATA_DIR=$APP/data
|
||||
Environment=XYZ_TRUST_PROXY=true
|
||||
Environment=XYZ_COUNT_HTTPS_ONLY=true
|
||||
Environment=XYZ_DEBUG_LOG=$DEBUG
|
||||
Restart=on-failure
|
||||
RestartSec=3
|
||||
LimitNOFILE=65535
|
||||
NoNewPrivileges=true
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
EOF
|
||||
systemctl daemon-reload
|
||||
systemctl enable xyzserver >/dev/null 2>&1
|
||||
systemctl restart xyzserver
|
||||
|
||||
healthy() {
|
||||
for _ in $(seq 1 20); do
|
||||
if curl -fsS "http://127.0.0.1:$PORT/healthz" >/dev/null 2>&1; then
|
||||
return 0
|
||||
fi
|
||||
sleep 0.5
|
||||
done
|
||||
return 1
|
||||
}
|
||||
if ! healthy; then
|
||||
journalctl -u xyzserver -n 40 --no-pager || true
|
||||
if [ -f "$APP/xyzserver.prev" ]; then
|
||||
log "新版本启动失败,回滚到上一版本"
|
||||
cp -f "$APP/xyzserver.prev" "$APP/xyzserver"
|
||||
systemctl restart xyzserver
|
||||
healthy && log "已回滚,旧版本正常运行"
|
||||
fi
|
||||
fail "服务没有通过健康检查"
|
||||
fi
|
||||
|
||||
log "4/5 宝塔网站 $DOMAIN"
|
||||
if [ ! -f "$SITE_CONF" ]; then
|
||||
(cd "$PANEL" && "$PANEL/pyenv/bin/python3" "$STAGE/bt_addsite.py" "$DOMAIN" "$SITE_ROOT") \
|
||||
|| fail "宝塔建站失败"
|
||||
fi
|
||||
[ -f "$SITE_CONF" ] || fail "宝塔建站后没有生成 $SITE_CONF"
|
||||
grep -q "include $REWRITE;" "$SITE_CONF" || fail "$SITE_CONF 中没有引用 $REWRITE,请检查网站配置"
|
||||
|
||||
log "5/5 Nginx 转发规则"
|
||||
TS=$(date +%Y%m%d%H%M%S)
|
||||
[ -f "$REWRITE" ] && cp -f "$REWRITE" "$REWRITE.bak.$TS"
|
||||
[ -f "$UPSTREAM" ] && cp -f "$UPSTREAM" "$UPSTREAM.bak.$TS"
|
||||
|
||||
cat > "$UPSTREAM" <<EOF
|
||||
# xyzserver 部署脚本生成,请勿手工修改
|
||||
upstream xyzserver_backend {
|
||||
server 127.0.0.1:$PORT;
|
||||
keepalive 32;
|
||||
}
|
||||
EOF
|
||||
|
||||
cat > "$REWRITE" <<EOF
|
||||
# xyzserver 部署脚本生成:把整个网站转发给 xyzserver。
|
||||
# 不要在宝塔「伪静态」里改这里,也不要再给本站添加「反向代理」:
|
||||
# 宝塔的反向代理会给 .png 加 expires,覆盖瓦片的 no-store,统计会失真。
|
||||
location ^~ /.well-known/ {
|
||||
root $SITE_ROOT;
|
||||
default_type text/plain;
|
||||
}
|
||||
|
||||
location ^~ /tiles/ {
|
||||
proxy_pass http://xyzserver_backend;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Connection "";
|
||||
proxy_set_header Host \$host;
|
||||
proxy_set_header X-Real-IP \$remote_addr;
|
||||
proxy_set_header X-Forwarded-Proto \$scheme;
|
||||
proxy_cache off;
|
||||
access_log off;
|
||||
}
|
||||
|
||||
location ^~ / {
|
||||
proxy_pass http://xyzserver_backend;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Connection "";
|
||||
proxy_set_header Host \$host;
|
||||
proxy_set_header X-Real-IP \$remote_addr;
|
||||
proxy_set_header X-Forwarded-Proto \$scheme;
|
||||
proxy_cache off;
|
||||
}
|
||||
EOF
|
||||
|
||||
if ! "$NGINX" -t >/tmp/xyzserver-nginx-test.log 2>&1; then
|
||||
cat /tmp/xyzserver-nginx-test.log
|
||||
[ -f "$REWRITE.bak.$TS" ] && mv -f "$REWRITE.bak.$TS" "$REWRITE" || rm -f "$REWRITE"
|
||||
[ -f "$UPSTREAM.bak.$TS" ] && mv -f "$UPSTREAM.bak.$TS" "$UPSTREAM" || rm -f "$UPSTREAM"
|
||||
fail "Nginx 配置检查不通过,已恢复原配置"
|
||||
fi
|
||||
"$NGINX" -s reload
|
||||
rm -f "$REWRITE".bak.* "$UPSTREAM".bak.*
|
||||
|
||||
log "完成:$(systemctl is-active xyzserver),程序 $APP/xyzserver,数据 $APP/data"
|
||||
Reference in New Issue
Block a user