douyinWEB/scripts/_fix_covers_now.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

105 lines
3.6 KiB
Python

# -*- coding: utf-8 -*-
"""Fix /covers/ 404 + S3 cover images on dy.nomadro.live."""
from __future__ import annotations
import sys
import time
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
HOST = "108.174.50.68"
ROOT = Path(__file__).resolve().parents[1]
APP = "/opt/douyinWEB"
# nginx: drop /covers/ → API (empty dir); let Next.js serve /covers page
NGINX_FIX = r"""
CONF=$(ls /www/server/panel/vhost/nginx/proxy/dy.nomadro.live/*.conf /etc/nginx/conf.d/dy-nomadro-live.conf 2>/dev/null | head -1)
echo "conf=$CONF"
if [ -n "$CONF" ] && grep -q 'location \^~ /covers/' "$CONF"; then
cp -a "$CONF" "${CONF}.bak.covers"
sed -i '/location \^~ \/covers\/ {/,/^ }/d' "$CONF"
echo "removed /covers/ location block"
nginx -t && nginx -s reload
else
echo "no /covers/ block or conf missing"
fi
curl -sS -o /dev/null -w "covers_slash=%{http_code}\n" -k https://127.0.0.1/covers/ -H "Host: dy.nomadro.live"
curl -sS -o /dev/null -w "covers_page=%{http_code}\n" -k https://127.0.0.1/covers -H "Host: dy.nomadro.live"
"""
S3_FIX = r"""
cd /opt/douyinWEB/backend
.venv/bin/python - <<'PY'
import json, urllib.request
import boto3
from botocore.client import Config
c = boto3.client('s3', endpoint_url='http://127.0.0.1:8333',
aws_access_key_id='douyin', aws_secret_access_key='7a9b4d3c9f12bbe404aa2e506ad8e0a4b2ac',
region_name='us-east-1', config=Config(signature_version='s3v4', s3={'addressing_style': 'path'}))
policy = json.dumps({'Version':'2012-10-17','Statement':[{'Effect':'Allow','Principal':'*','Action':'s3:GetObject','Resource':'arn:aws:s3:::douyin-covers/*'}]})
try:
c.put_bucket_policy(Bucket='douyin-covers', Policy=policy)
print('bucket_policy OK')
except Exception as e:
print('bucket_policy', e)
url='http://127.0.0.1:8333/douyin-covers/covers/5d3e14cd-8c13-4b7c-9522-e44dc1cad31e.jpg'
try:
with urllib.request.urlopen(url, timeout=8) as r:
print('anon GET', r.status, 'bytes', len(r.read()))
except Exception as e:
print('anon FAIL', e)
PY
curl -sS -o /dev/null -w "nginx_s3=%{http_code} sz=%{size_download}\n" -k \
"https://127.0.0.1/douyin-covers/covers/5d3e14cd-8c13-4b7c-9522-e44dc1cad31e.jpg" -H "Host: dy.nomadro.live"
"""
def run(c, script: str, timeout: int = 60) -> str:
sftp = c.open_sftp()
path = f"/tmp/fix_{int(time.time())}.sh"
with sftp.file(path, "w") as f:
f.write("#!/bin/bash\n" + script)
sftp.chmod(path, 0o755)
sftp.close()
_i, o, e = c.exec_command(f"bash {path}", timeout=timeout)
out = o.read().decode("utf-8", "replace")
err = e.read().decode("utf-8", "replace").strip()
if err:
out += "\nSTDERR: " + err[:800]
return out
def main() -> None:
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(HOST, 22, "root", "Xiao4669805", timeout=45, allow_agent=False, look_for_keys=False)
# upload backend fixes if present
sftp = c.open_sftp()
for rel in ("backend/app/main.py", "backend/app/db.py"):
p = ROOT / rel
if p.is_file():
print("upload", rel)
sftp.put(str(p), f"{APP}/{rel.replace(chr(92), '/')}")
sftp.close()
print("=== nginx ===")
print(run(c, NGINX_FIX, timeout=45))
print("=== s3 public read ===")
print(run(c, S3_FIX, timeout=45))
print("=== restart api ===")
c.exec_command("systemctl restart douyinweb-api", timeout=10)
time.sleep(5)
print(run(c, "systemctl is-active douyinweb-api; curl -sS -o /dev/null -w 'health=%{http_code}\n' http://127.0.0.1:8014/api/health", timeout=20))
c.close()
print("DONE")
if __name__ == "__main__":
main()