douyinWEB/scripts/configure_s3_nomadro.py
eric 47d17aee3c Document YouTube groups and Shorts side-path; sync dual-platform pipeline.
Add isolated Shorts OAuth/clip/upload worker (default off), youtube-svc + group poller, Beijing time UX, and SHORTS.md.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-10 00:04:06 -05:00

191 lines
5.6 KiB
Python

# -*- coding: utf-8 -*-
"""Configure s3.nomadro.com as SeaweedFS public base and verify covers."""
import time
from pathlib import Path
import paramiko
HOST = "107.173.30.245"
USER = "root"
PASSWORD = "Xiao4669805"
REMOTE = "/opt/douyinWEB"
LOCAL = Path(__file__).resolve().parents[1]
# Also ship cover-speed fix if not fully applied
LOCAL_FILES = [
"frontend/src/lib/api.js",
"backend/app/main.py",
]
REMOTE_SCRIPT = r'''
set -e
echo "=== current caddy sites ==="
cat /etc/caddy/Caddyfile
echo "-----"
cat /etc/caddy/sites/s3.nomadro.com.caddy
echo "-----"
cat /etc/caddy/sites/feiniu.nomadro.com.caddy
# Ensure s3.nomadro.com site is correct (path-style S3)
cat >/etc/caddy/sites/s3.nomadro.com.caddy <<'EOF'
# SeaweedFS S3 API
# https://s3.nomadro.com
s3.nomadro.com {
encode gzip
request_body {
max_size 200MB
}
header {
Access-Control-Allow-Origin *
Access-Control-Allow-Methods "GET, HEAD, PUT, POST, DELETE, OPTIONS"
Access-Control-Allow-Headers *
Access-Control-Expose-Headers ETag,x-amz-request-id
}
@options {
method OPTIONS
}
handle @options {
respond 204
}
reverse_proxy 127.0.0.1:8333
}
EOF
# Keep feiniu as alias too
cat >/etc/caddy/sites/feiniu.nomadro.com.caddy <<'EOF'
# SeaweedFS S3 API (alias / 飞牛)
# https://feiniu.nomadro.com
feiniu.nomadro.com {
encode gzip
request_body {
max_size 200MB
}
header {
Access-Control-Allow-Origin *
Access-Control-Allow-Methods "GET, HEAD, PUT, POST, DELETE, OPTIONS"
Access-Control-Allow-Headers *
Access-Control-Expose-Headers ETag,x-amz-request-id
}
@options {
method OPTIONS
}
handle @options {
respond 204
}
reverse_proxy 127.0.0.1:8333
}
EOF
caddy validate --config /etc/caddy/Caddyfile
systemctl reload caddy
sleep 2
systemctl is-active caddy
# Point API public base to s3.nomadro.com
sed -i 's|Environment=DY_S3_PUBLIC_BASE=.*|Environment=DY_S3_PUBLIC_BASE=https://s3.nomadro.com|' /etc/systemd/system/douyinweb-api.service
grep DY_S3 /etc/systemd/system/douyinweb-api.service
systemctl daemon-reload
systemctl restart douyinweb-api
sleep 4
systemctl is-active douyinweb-api douyinweb-web || systemctl start douyinweb-web
sleep 2
systemctl is-active douyinweb-api douyinweb-web
# Rewrite stored cover_path/cover URLs feiniu -> s3 (sqlite)
python3 - <<'PY'
import sqlite3
db="/opt/douyinWEB/backend/data/monitor.db"
conn=sqlite3.connect(db)
cur=conn.cursor()
old="https://feiniu.nomadro.com"
new="https://s3.nomadro.com"
n=cur.execute("UPDATE sessions SET cover_path=REPLACE(cover_path, ?, ?) WHERE cover_path LIKE ?", (old,new,old+"%")).rowcount
conn.commit()
print("rewrote_cover_path", n)
row=cur.execute("SELECT id, cover_path FROM sessions WHERE cover_path LIKE 'https://s3.nomadro.com/%' ORDER BY started_at DESC LIMIT 1").fetchone()
print("sample", row)
conn.close()
PY
echo "=== HTTPS checks ==="
curl -sI --max-time 15 https://s3.nomadro.com/ | head -12
echo ----
curl -s -o /dev/null -w 's3_root:%{http_code}\n' --max-time 15 https://s3.nomadro.com/
curl -s --max-time 15 https://dylive.nomadro.com/api/health | head -c 400
echo
SID=$(python3 - <<'PY'
import sqlite3
c=sqlite3.connect('/opt/douyinWEB/backend/data/monitor.db')
r=c.execute("SELECT id, cover_path FROM sessions WHERE cover_path LIKE 'https://s3.nomadro.com/%' ORDER BY started_at DESC LIMIT 1").fetchone()
print(r[0] if r else '')
print('PATH', r[1] if r else '', flush=True)
PY
)
echo "SID_LINE=$SID"
COVER=$(python3 - <<'PY'
import sqlite3
c=sqlite3.connect('/opt/douyinWEB/backend/data/monitor.db')
r=c.execute("SELECT cover_path FROM sessions WHERE cover_path LIKE 'https://s3.nomadro.com/%' ORDER BY started_at DESC LIMIT 1").fetchone()
print(r[0] if r else '')
PY
)
echo "COVER=$COVER"
if [ -n "$COVER" ]; then
curl -sI --max-time 15 "$COVER" | head -15
curl -s -o /dev/null -w 'object:%{http_code} size:%{size_download} time:%{time_total}\n' --max-time 20 "$COVER"
fi
# API health s3 public_base
curl -s https://dylive.nomadro.com/api/health | python3 -c "import sys,json; d=json.load(sys.stdin); print(d.get('s3'))"
'''
def run(c, cmd, timeout=300):
print("$", cmd[:140], flush=True)
chan = c.get_transport().open_session()
chan.settimeout(timeout)
chan.get_pty()
chan.exec_command(cmd)
buf = []
while True:
if chan.recv_ready():
buf.append(chan.recv(65535).decode("utf-8", "replace"))
if chan.exit_status_ready() and not chan.recv_ready():
break
time.sleep(0.2)
while chan.recv_ready():
buf.append(chan.recv(65535).decode("utf-8", "replace"))
code = chan.recv_exit_status()
print("".join(buf)[-6000:], flush=True)
return code
def main():
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(HOST, 22, USER, PASSWORD, timeout=60, banner_timeout=60, allow_agent=False, look_for_keys=False)
sftp = c.open_sftp()
for rel in LOCAL_FILES:
print("upload", rel, flush=True)
sftp.put(str(LOCAL / rel), f"{REMOTE}/{rel}")
with sftp.file("/tmp/configure_s3_nomadro.sh", "w") as f:
f.write("#!/bin/bash\n" + REMOTE_SCRIPT)
sftp.close()
run(c, "bash /tmp/configure_s3_nomadro.sh", timeout=180)
# rebuild frontend for api.js cover preference
if run(c, "cd /opt/douyinWEB/frontend && npm run build", timeout=420) != 0:
raise SystemExit(1)
run(c, "systemctl restart douyinweb-web --no-block; sleep 4; systemctl start douyinweb-api douyinweb-web; sleep 2; systemctl is-active douyinweb-api douyinweb-web")
c.close()
print("DONE")
print("https://s3.nomadro.com/")
print("DY_S3_PUBLIC_BASE=https://s3.nomadro.com")
if __name__ == "__main__":
main()