nomadro/apps/ebook/dymini/deploy/deploy_ntfy_hook.py
eric d93a355fd5 Migrate navmini/ebook/nomadweb into apps/ as same-origin portal subpaths.
Shield external portal jumps (keep EXTERNAL_* for later), serve /nav from static build, and proxy /book + /community to vertical Next apps with Caddy notes.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-28 03:30:34 -05:00

81 lines
2.4 KiB
Python

"""部署 ntfy 推送集成到腾讯云 API。"""
from __future__ import annotations
import io
import tarfile
import tempfile
from pathlib import Path
import paramiko
from scp import SCPClient
ROOT = Path(__file__).resolve().parents[1]
HOST = "82.157.112.245"
USER = "ubuntu"
PASSWORD = "Xiao4669805"
NTFY_TOKEN = "tk_xt9nxxtbwo24w9bm7qpzcqu7urgkw"
tmp = tempfile.NamedTemporaryFile(suffix=".tar.gz", delete=False)
tmp.close()
tar_path = Path(tmp.name)
with tarfile.open(tar_path, "w:gz") as tar:
for rel in [
"app/routers/pay.py",
"app/services/ntfy.py",
"app/config.py",
]:
p = ROOT / "backend" / rel
data = p.read_bytes().replace(b"\r\n", b"\n")
info = tarfile.TarInfo(name=rel)
info.size = len(data)
tar.addfile(info, fileobj=io.BytesIO(data))
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(HOST, username=USER, password=PASSWORD, timeout=30)
with SCPClient(c.get_transport()) as scp:
scp.put(str(tar_path), "/tmp/ntfy-hook.tar.gz")
cmd = rf"""
set -e
cd /tmp
rm -rf ntfy-hook && mkdir ntfy-hook
tar -xzf ntfy-hook.tar.gz -C ntfy-hook
cp ntfy-hook/app/routers/pay.py /opt/nomadmini/backend/app/routers/pay.py
cp ntfy-hook/app/services/ntfy.py /opt/nomadmini/backend/app/services/ntfy.py
cp ntfy-hook/app/config.py /opt/nomadmini/backend/app/config.py
ENV=/opt/nomadmini/backend/.env
upsert() {{
key="$1"; val="$2"
if grep -q "^${{key}}=" "$ENV"; then
sed -i "s|^${{key}}=.*|${{key}}=${{val}}|" "$ENV"
else
echo "${{key}}=${{val}}" >> "$ENV"
fi
}}
upsert NTFY_ENABLED true
upsert NTFY_BASE_URL http://127.0.0.1:2586
upsert NTFY_TOPIC nomadmini
upsert NTFY_TOKEN {NTFY_TOKEN}
sudo systemctl restart nomadmini-api
sleep 2
curl -s http://127.0.0.1:8010/health
echo
cd /opt/nomadmini/backend
/opt/nomadmini/backend/.venv/bin/python - <<'PY'
import asyncio
from app.services.ntfy import notify
ok = asyncio.run(notify('联调:支付/消息将推送到本 topic', title='Nomadmini 已接通', tags='rocket', priority=4))
print('ntfy_ok', ok)
PY
rm -rf /tmp/ntfy-hook /tmp/ntfy-hook.tar.gz
"""
_, o, e = c.exec_command(cmd)
out = o.read().decode("utf-8", "replace")
err = e.read().decode("utf-8", "replace")
Path("deploy/ntfy-hook-deploy.txt").write_text(out + "\nERR\n" + err, encoding="utf-8")
c.close()
tar_path.unlink(missing_ok=True)
print(out.encode("ascii", "replace").decode())
print(err.encode("ascii", "replace").decode())