#!/usr/bin/env python3 """P0+P1 cleanup on dylive (authorized). Seaweed inspect only in this pass.""" from __future__ import annotations import sys import time import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") HOST = "107.173.30.245" PASS = "Xiao4669805" def run(c, cmd, timeout=300): print(f"\n===== {cmd[:120]} =====", 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.15) code = chan.recv_exit_status() print(f"\n[exit {code}]", flush=True) return code def main(): c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) for i in range(8): try: print(f"ssh try {i}", flush=True) c.connect(HOST, 22, "root", PASS, timeout=30, allow_agent=False, look_for_keys=False) break except Exception as ex: print(ex, flush=True) time.sleep(1 + i) else: raise SystemExit("ssh_fail") # --- P0 --- print("\n######## P0 ########", flush=True) run( c, r""" set -e echo BEFORE=$(df -h / | awk 'NR==2{print $3,$4,$5}') # docker build cache docker builder prune -af # journal journalctl --vacuum-size=300M # .next.bak* keep .next cd /opt/douyinWEB/frontend ls -d .next.bak* 2>/dev/null || true rm -rf .next.bak* # /tmp deploy tgz rm -f /tmp/next-*.tgz /tmp/dy_next_*.tgz /tmp/dy-next-*.tgz /tmp/next-fe-*.tgz /tmp/src-*.tgz /tmp/pb.zip 2>/dev/null || true ls -lh /tmp/*.tgz /tmp/*.zip 2>/dev/null | head -20 || echo tmp_tgz_cleared # apt apt-get clean || true echo AFTER_P0=$(df -h / | awk 'NR==2{print $3,$4,$5}') """, timeout=400, ) # --- P1 --- print("\n######## P1 ########", flush=True) run( c, r""" set -e BK=/opt/douyinWEB/backend/data/backups # keep newest 3 monitor backups cd "$BK" ls -1t monitor_*.db 2>/dev/null | tail -n +4 | while read f; do rm -f -- "$f"; echo del_bak "$f"; done echo bak_left=$(ls -1 monitor_*.db 2>/dev/null | wc -l) bak_size=$(du -sh "$BK" | awk '{print $1}') # npm / cache rm -rf /root/.npm /root/.cache # .local: only clear obvious caches if present du -sh /root/.local/* 2>/dev/null | sort -h | tail -20 || true rm -rf /root/.local/share/Trash /root/.local/share/pip /root/.local/lib 2>/dev/null || true # docker exited containers + dangling images docker container prune -f docker image prune -f echo AFTER_P1=$(df -h / | awk 'NR==2{print $3,$4,$5}') docker system df """, timeout=300, ) # seaweed layout inspect for 2-day retention print("\n######## SEAWEED INSPECT ########", flush=True) run( c, r""" df -h / /data du -sh /data/seaweed /data/seaweed/* 2>/dev/null find /data/seaweed -maxdepth 3 -type d 2>/dev/null | head -40 # sample object keys via weed / s3 if possible ls -la /data/seaweed/volume 2>/dev/null | head -20 # env for s3 grep -E 'S3|SEAWEED|AWS|covers' /opt/douyinWEB/backend/.env /etc/systemd/system/douyinweb-*.service 2>/dev/null | head -40 """, timeout=60, ) c.close() print("CLEANUP_DONE", flush=True) if __name__ == "__main__": main()