109 lines
3.5 KiB
Python
109 lines
3.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Run cover purge with systemd S3 env on dylive."""
|
|
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 main():
|
|
c = paramiko.SSHClient()
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
c.connect(HOST, 22, "root", PASS, timeout=30, allow_agent=False, look_for_keys=False)
|
|
cmd = r"""
|
|
set -e
|
|
# load same env as douyinweb-api
|
|
ENVFILE=$(mktemp)
|
|
systemctl show douyinweb-api -p Environment --value | tr ' ' '\n' | sed '/^$/d' > "$ENVFILE"
|
|
# also parse Environment= lines from unit
|
|
grep -E '^Environment=' /etc/systemd/system/douyinweb-api.service | sed 's/^Environment=//' | tr ' ' '\n' >> "$ENVFILE" || true
|
|
echo '---S3 env---'
|
|
grep -E '^DY_S3_' "$ENVFILE" | sort -u
|
|
cd /opt/douyinWEB/backend
|
|
set -a
|
|
# shellcheck disable=SC1090
|
|
. "$ENVFILE" || true
|
|
set +a
|
|
export $(grep -E '^DY_S3_' "$ENVFILE" | sort -u | xargs -0 2>/dev/null || true)
|
|
# safer export
|
|
while IFS= read -r line; do
|
|
case "$line" in
|
|
DY_S3_*=*) export "$line" ;;
|
|
esac
|
|
done < <(grep -E '^DY_S3_' "$ENVFILE" | sort -u)
|
|
./.venv/bin/python - <<'PY'
|
|
import json, os
|
|
from app.config import settings
|
|
print('s3_enabled_cfg', settings.s3_enabled, settings.s3_endpoint, settings.s3_bucket)
|
|
print('env', {k:os.environ.get(k) for k in sorted(os.environ) if k.startswith('DY_S3_')})
|
|
from app import s3_storage, db
|
|
print('s3_enabled()', s3_storage.s3_enabled())
|
|
# list a few objects age stats first
|
|
client=None
|
|
try:
|
|
client=s3_storage._client()
|
|
resp=client.list_objects_v2(Bucket=settings.s3_bucket, Prefix='covers/', MaxKeys=5)
|
|
print('sample', [(o.get('Key'), str(o.get('LastModified'))) for o in (resp.get('Contents') or [])])
|
|
# count roughly
|
|
n=0
|
|
token=None
|
|
while True:
|
|
kw={'Bucket':settings.s3_bucket,'Prefix':'covers/','MaxKeys':1000}
|
|
if token: kw['ContinuationToken']=token
|
|
r=client.list_objects_v2(**kw)
|
|
n += len(r.get('Contents') or [])
|
|
if not r.get('IsTruncated'): break
|
|
token=r.get('NextContinuationToken')
|
|
if n>50000: break
|
|
print('approx_objects', n)
|
|
except Exception as e:
|
|
print('list_err', e)
|
|
cfg=db.get_settings_map()
|
|
r=s3_storage.purge_covers_older_than(keep_days=float(cfg.get('covers_s3_keep_days') or 2), max_delete=50000)
|
|
print(json.dumps(r, ensure_ascii=False))
|
|
# second pass if truncated by limit
|
|
if r.get('deleted',0)>=50000:
|
|
r2=s3_storage.purge_covers_older_than(keep_days=2, max_delete=50000)
|
|
print('pass2', json.dumps(r2, ensure_ascii=False))
|
|
PY
|
|
# seaweed vacuum via filer/master - try common ports
|
|
for u in \
|
|
'http://127.0.0.1:19333/vol/vacuum?garbageThreshold=0.01' \
|
|
'http://127.0.0.1:9333/vol/vacuum?garbageThreshold=0.01'; do
|
|
echo "try $u"
|
|
curl -sS -m 15 --http1.1 "$u" | head -c 400; echo
|
|
done
|
|
ss -lntp | grep -E 'weed|8333|9333|19333|18888' | head -20
|
|
du -sh /data/seaweed
|
|
df -h /
|
|
rm -f "$ENVFILE"
|
|
"""
|
|
_i, o, e = c.exec_command(cmd, timeout=600, 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.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 > 580:
|
|
print("TIMEOUT", flush=True)
|
|
break
|
|
time.sleep(0.15)
|
|
print("\n[exit", chan.recv_exit_status(), "]", flush=True)
|
|
c.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|