douyinWEB/scripts/_deploy_covers_keep_2d_dylive.py
2026-09-13 22:10:49 -05:00

124 lines
3.5 KiB
Python

#!/usr/bin/env python3
"""Deploy cover 2-day retention + run purge on dylive; verify disk_ok."""
from __future__ import annotations
import hashlib
import sys
import time
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
ROOT = Path(r"c:\Users\admin\Desktop\douyinWEB")
PASS = "Xiao4669805"
HOST = "107.173.30.245"
FILES = [
"backend/app/s3_storage.py",
"backend/app/clip_retention.py",
"backend/app/db.py",
]
def connect(host):
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
for i in range(8):
try:
print(f"ssh {host} try {i}", flush=True)
c.connect(host, 22, "root", PASS, timeout=30, allow_agent=False, look_for_keys=False)
print("ok", flush=True)
return c
except Exception as e:
print(e, flush=True)
time.sleep(1 + i)
raise SystemExit(f"ssh_fail {host}")
def put(c, rel):
local = ROOT / rel.replace("/", "\\")
remote = f"/opt/douyinWEB/{rel}"
h = hashlib.sha256(local.read_bytes()).hexdigest()[:12]
print(f"put {rel} {h}", flush=True)
sftp = c.open_sftp()
with sftp.file(remote, "wb") as f:
f.write(local.read_bytes())
sftp.close()
return h
def run(c, cmd, timeout=300):
print(f"$ {cmd[:100]}...", flush=True)
_i, o, e = c.exec_command(cmd, timeout=timeout, get_pty=True)
chan = o.channel
start = time.time()
while True:
if chan.recv_ready():
sys.stdout.write(chan.recv(8192).decode("utf-8", "replace"))
sys.stdout.flush()
if chan.recv_stderr_ready():
sys.stdout.write(chan.recv_stderr(8192).decode("utf-8", "replace"))
sys.stdout.flush()
if chan.exit_status_ready():
while chan.recv_ready():
sys.stdout.write(chan.recv(8192).decode("utf-8", "replace"))
sys.stdout.flush()
break
if time.time() - start > timeout:
print("TIMEOUT", flush=True)
break
time.sleep(0.12)
code = chan.recv_exit_status()
print(f"\n[exit {code}]", flush=True)
return code
def main():
c = connect(HOST)
for rel in FILES:
put(c, rel)
code = run(
c,
r"""
set -e
cd /opt/douyinWEB/backend
# persist setting
./.venv/bin/python - <<'PY'
from app import db
db.update_settings_map({"covers_s3_keep_days": 2})
print("covers_s3_keep_days", db.get_settings_map().get("covers_s3_keep_days"))
PY
systemctl restart douyinweb-api
sleep 2
echo api=$(systemctl is-active douyinweb-api)
# one-shot purge (may take a while if many objects)
./.venv/bin/python - <<'PY'
import json
from app import s3_storage, db
from app.clip_retention import disk_ok_for_record, disk_free_gb
cfg=db.get_settings_map()
print("free_gb", round(disk_free_gb(),2), "disk_ok", disk_ok_for_record(cfg))
r=s3_storage.purge_covers_older_than(keep_days=float(cfg.get("covers_s3_keep_days") or 2), max_delete=20000)
print(json.dumps(r, ensure_ascii=False))
print("free_after", round(disk_free_gb(),2))
PY
# try seaweed vacuum if weed binary present
which weed || true
weed version 2>/dev/null || true
# volume vacuum via master HTTP if available
curl -sS -m 10 'http://127.0.0.1:19333/vol/vacuum?garbageThreshold=0.01' || true
echo
df -h /
du -sh /data/seaweed 2>/dev/null
""",
timeout=400,
)
c.close()
if code != 0:
raise SystemExit(code)
print("COVER_POLICY_DONE", flush=True)
if __name__ == "__main__":
main()