# -*- coding: utf-8 -*- """Force-stop all Neko except one on 3.3GiB physical box.""" from __future__ import annotations import sys import time import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") PASS = "4669805" KEEP = 5 # user was using neko/5 REMOTE = f''' import sqlite3, subprocess, time KEEP={KEEP} con=sqlite3.connect("/opt/douyinWEB/backend/data/monitor.db") now=time.time() # disable all neko except KEEP con.execute("UPDATE yt_assign_groups SET neko_enabled=0, updated_at=? WHERE neko_instance_id!=?", (now, KEEP)) con.execute("UPDATE yt_assign_groups SET neko_enabled=1, updated_at=? WHERE neko_instance_id=?", (now, KEEP)) con.commit() print("db updated keep", KEEP) ps=subprocess.check_output("docker ps -a --format '{{{{.Names}}}}'", shell=True, text=True) for name in ps.splitlines(): name=name.strip() if not name.startswith("douyinweb-neko-"): continue n=int(name.rsplit("-",1)[-1]) if n==KEEP: print("KEEP running", name) # ensure started subprocess.run(["docker","start",name], check=False) continue print("STOP", name) subprocess.run(["docker","stop","-t","5",name], check=False) print(subprocess.check_output("docker ps --format '{{{{.Names}}}}\\t{{{{.Status}}}}' | grep neko | sort || echo none", shell=True, text=True)) print(subprocess.check_output("free -h | head -2; uptime", shell=True, text=True)) print("DONE") ''' def main(): c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) for i in range(8): try: c.connect( "107.173.30.245", 4014, "eric", PASS, timeout=50, banner_timeout=100, allow_agent=False, look_for_keys=False, ) break except Exception as ex: print("ssh", i, type(ex).__name__, flush=True) time.sleep(2) else: raise SystemExit("ssh fail") sftp = c.open_sftp() with sftp.file("/tmp/_neko_force1.py", "w") as f: f.write(REMOTE) sftp.close() _, o, e = c.exec_command( f"echo '{PASS}' | sudo -S -p '' python3 /tmp/_neko_force1.py", timeout=180, ) print((o.read() + e.read()).decode("utf-8", "replace"), flush=True) c.close() if __name__ == "__main__": main()