douyinWEB/scripts/_deploy_test1_safe.py
2026-08-15 04:50:28 -05:00

121 lines
4.0 KiB
Python
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.

# -*- coding: utf-8 -*-
"""
安全部署 test1 分支 —— 只更新代码,绝不污染业务数据。
允许:
- git fetch / checkout test1 / pull(仅代码)
- systemctl restart api/web
- frontend npm run build
- 部署前备份 monitor.db 到 /tmp(只读拷贝,从不回写)
严禁(本脚本不会、也禁止手工做):
- 上传/覆盖 monitor.db、*.db-shm、*.db-wal
- 覆盖 pb_data、covers/、clips/、tiktok_cookies/
- scp/sftp 覆盖服务器业务文件
- git reset --hard / git clean -fd(会误删未跟踪数据若配置不当)
- 从本机推送任何 runtime 数据
用法:
python scripts/_deploy_test1_safe.py
"""
from __future__ import annotations
import time
import paramiko
HOST = "107.173.30.245"
USER = "root"
PASSWORD = "Xiao4669805"
REPO = "/opt/douyinWEB"
BRANCH = "test1"
# 绝对禁止被本机覆盖的路径(文档 + 运行时断言)
PROTECTED = (
f"{REPO}/backend/data/monitor.db",
f"{REPO}/backend/data/monitor.db-shm",
f"{REPO}/backend/data/monitor.db-wal",
f"{REPO}/backend/data/covers",
f"{REPO}/backend/data/clips",
f"{REPO}/backend/data/tiktok_cookies",
f"{REPO}/pb_data",
)
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(HOST, 22, USER, PASSWORD, timeout=30, allow_agent=False, look_for_keys=False)
def run(cmd: str, timeout: int = 300) -> str:
_, out, err = c.exec_command(cmd, timeout=timeout)
o = out.read().decode("utf-8", "replace").strip()
e = err.read().decode("utf-8", "replace").strip()
return (o + ("\n" + e if e else "")).strip()
print("=== SAFE DEPLOY test1 (code only, never touch business data) ===")
print("protected:", ", ".join(PROTECTED))
# 1) 备份数据库(只读拷贝到 /tmp,绝不从本机写入)
ts = int(time.time())
print("[backup] monitor.db -> /tmp")
print(
run(
f"test -f {REPO}/backend/data/monitor.db && "
f"cp -a {REPO}/backend/data/monitor.db /tmp/monitor.db.bak_test1_{ts} && "
f"ls -lh /tmp/monitor.db.bak_test1_{ts} || echo 'NO_DB_SKIP'"
)
)
# 2) 记录数据指纹(部署前后对比,确认未被覆盖)
print("[fingerprint] before")
before = run(
f"stat -c '%n %s %Y' {REPO}/backend/data/monitor.db 2>/dev/null; "
f"du -sb {REPO}/backend/data/tiktok_cookies {REPO}/backend/data/clips "
f"{REPO}/backend/data/covers 2>/dev/null | head -20"
)
print(before)
# 3) 仅拉代码:fetch + checkout test1 + pull(不用 reset --hard / clean)
print("[git] fetch + checkout test1 + pull")
print(
run(
f"cd {REPO} && "
f"git fetch origin {BRANCH} 2>&1 && "
f"git checkout {BRANCH} 2>&1 && "
f"git pull --ff-only origin {BRANCH} 2>&1 && "
f"git log --oneline -3 && git status -sb"
)
)
# 4) 重启 API / 构建前端 / 重启 Web(不碰 data)
print("[restart api]")
c.exec_command("systemctl restart douyinweb-api >/dev/null 2>&1", timeout=5)
time.sleep(8)
print("api:", run("systemctl is-active douyinweb-api"))
print("[npm build]")
print(run(f"cd {REPO}/frontend && npm run build 2>&1 | tail -10", timeout=360))
print("[restart web]")
c.exec_command("systemctl restart douyinweb-web >/dev/null 2>&1", timeout=5)
time.sleep(5)
print("web:", run("systemctl is-active douyinweb-web"))
# 5) 再次指纹,必须一致(inode/size/mtime 允许服务写入导致 mtime 变,但 size 不应被本机替换)
print("[fingerprint] after")
after = run(
f"stat -c '%n %s %Y' {REPO}/backend/data/monitor.db 2>/dev/null; "
f"du -sb {REPO}/backend/data/tiktok_cookies {REPO}/backend/data/clips "
f"{REPO}/backend/data/covers 2>/dev/null | head -20"
)
print(after)
# size 对比:monitor.db 字节数若骤降到接近本机空库,视为污染
print("[safety] compare db size line")
print("BEFORE_LINE:", before.splitlines()[0] if before else "")
print("AFTER_LINE:", after.splitlines()[0] if after else "")
c.close()
print("DONE — code updated on test1; business data paths were never uploaded from local.")