douyinWEB/scripts/_fix_physical_s3_anon.py
eric d409e416da Ship physical dual-edge stack, Studio CDP live metrics, and ops tooling.
Capture Neko/WG/network hubs, Studio viewer charts, physical deploy docs, and one-shot scripts so VPS vs physical can be toggled later without losing WIP.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-04 22:42:40 -05:00

103 lines
3.1 KiB
Python

# -*- coding: utf-8 -*-
"""Enable SeaweedFS anonymous Read on douyin-covers (physical guest)."""
from __future__ import annotations
import json
import sys
import time
import httpx
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
VPS = "107.173.30.245"
GUEST_PW = "4669805"
REMOTE = r"""#!/bin/bash
set -euo pipefail
# Locate s3 config
for p in /etc/seaweedfs/douyinweb-s3.json /etc/seaweedfs/s3.json /opt/douyinWEB/backend/data/seaweedfs/s3.json; do
if [ -f "$p" ]; then CFG="$p"; break; fi
done
echo "CFG=${CFG:-MISSING}"
if [ -n "${CFG:-}" ]; then
python3 - <<PY
import json
path = "$CFG"
with open(path) as f:
cfg = json.load(f)
ids = [i for i in cfg.get("identities", []) if i.get("name") != "anonymous"]
ids.append({"name": "anonymous", "actions": ["Read:douyin-covers", "List:douyin-covers"]})
cfg["identities"] = ids
with open(path, "w") as f:
json.dump(cfg, f, indent=2)
print("identities", [i.get("name") for i in cfg["identities"]])
PY
fi
# Also try weed shell (SeaweedFS 3.x)
if command -v weed >/dev/null 2>&1; then
WEED=weed
elif [ -x /opt/seaweedfs/weed ]; then
WEED=/opt/seaweedfs/weed
else
WEED=
fi
if [ -n "$WEED" ]; then
timeout 20 bash -c "printf 's3.bucket.access -name douyin-covers -user anonymous -access Read,List\nexit\n' | $WEED shell -master=127.0.0.1:9333" 2>&1 | tail -20 || true
fi
systemctl restart douyinweb-s3 || systemctl restart seaweedfs-s3 || true
sleep 2
systemctl is-active douyinweb-s3 2>/dev/null || systemctl is-active seaweedfs 2>/dev/null || true
KEY=youtube/avatars/4b8dae9c-18bf-4c1e-8b53-71d7a7f8558e.jpg
curl -sS -o /dev/null -w "local_s3=%{http_code} sz=%{size_download}\n" "http://127.0.0.1:8333/douyin-covers/$KEY" || true
# show unit for path
systemctl cat douyinweb-s3 2>/dev/null | head -50 || true
ls -la /etc/seaweedfs 2>/dev/null || true
"""
def ssh_guest():
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(
VPS,
4014,
"eric",
GUEST_PW,
timeout=40,
banner_timeout=80,
allow_agent=False,
look_for_keys=False,
)
return c
def run(c, cmd: str, timeout: int = 120) -> str:
_, o, e = c.exec_command(cmd, timeout=timeout)
return (o.read() + e.read()).decode("utf-8", "replace")
def main() -> int:
g = ssh_guest()
sftp = g.open_sftp()
with sftp.file("/tmp/_fix_s3_anon.sh", "wb") as f:
f.write(REMOTE.replace("\r\n", "\n").encode())
sftp.close()
print(run(g, f"echo '{GUEST_PW}' | sudo -S -p '' bash /tmp/_fix_s3_anon.sh", 120), flush=True)
g.close()
url = "https://dy.nomadro.com/douyin-covers/youtube/avatars/4b8dae9c-18bf-4c1e-8b53-71d7a7f8558e.jpg"
with httpx.Client(timeout=30, follow_redirects=True) as client:
for i in range(6):
r = client.get(url)
print(f"pub[{i}]", r.status_code, r.headers.get("content-type"), len(r.content), flush=True)
if r.status_code == 200 and len(r.content) > 64:
break
time.sleep(2)
return 0
if __name__ == "__main__":
raise SystemExit(main())