From 3d023047b4e5e4707603d27ce692c2fbbb0a908f Mon Sep 17 00:00:00 2001 From: Siarhei Siniak Date: Thu, 20 Mar 2025 19:07:34 +0300 Subject: [PATCH] [+] update cpanel 1. fix logging; 2. refactor into a class; --- d1/cpanel.py | 210 +++++++++++++++++++++++++++------------------------ 1 file changed, 110 insertions(+), 100 deletions(-) diff --git a/d1/cpanel.py b/d1/cpanel.py index c072775..55fce35 100644 --- a/d1/cpanel.py +++ b/d1/cpanel.py @@ -11,121 +11,131 @@ import logging import json import time -with io.open( - 'tmp/d1/cpanel.json', 'r' -) as f: - t3 = json.load(f) +logger = logging.getLogger(__name__) -t2 = copy.deepcopy(t3) -ssh_known_hosts : list[str] = [] +class Launcher: + def run(self): + logging.basicConfig(level=logging.INFO) -for k, v in t2.items(): - if 'ssh_known_hosts' in v: - ssh_known_hosts.append(v['ssh_known_hosts']) + with io.open( + 'tmp/d1/cpanel.json', 'r' + ) as f: + t3 = json.load(f) -if len(ssh_known_hosts) > 0: - subprocess.check_call( - r''' - mkdir -p ~/.ssh && \ - cat $SSH_KNOWN_HOSTS > ~/.ssh/known_hosts - ''', env=dict(list(os.environ.items())) | dict( - SSH_KNOWN_HOSTS=' '.join(ssh_known_hosts), - ), - shell=True - ) + t2 = copy.deepcopy(t3) -for k in t2: - v = t2[k] - v['task'] = lambda : subprocess.Popen( - v['task_cmd'], - stdin=subprocess.DEVNULL, - ) + ssh_known_hosts : list[str] = [] -def stop_task(task: subprocess.Popen[bytes]) -> None: - task.terminate() - try: - task.wait(1) - except: - task.kill() - -t1 = dict() - -shutdown = False - -while True: - try: for k, v in t2.items(): - if not k in t1: - logging.info(json.dumps(dict( - task=k, - status='starting', - ))) - t1[k] = v['task']() - logging.info(json.dumps(dict( - task=k, - status='started', - ))) - continue + if 'ssh_known_hosts' in v: + ssh_known_hosts.append(v['ssh_known_hosts']) - o = t1[k] + if len(ssh_known_hosts) > 0: + subprocess.check_call( + r''' + mkdir -p ~/.ssh && \ + cat $SSH_KNOWN_HOSTS > ~/.ssh/known_hosts + ''', env=dict(list(os.environ.items())) | dict( + SSH_KNOWN_HOSTS=' '.join(ssh_known_hosts), + ), + shell=True + ) - not_alive = None + for k in t2: + v = t2[k] + v['task'] = lambda : subprocess.Popen( + v['task_cmd'], + stdin=subprocess.DEVNULL, + ) + def stop_task(task: subprocess.Popen[bytes]) -> None: + task.terminate() try: - not_alive = not ( - requests.get(v['url'], timeout=0.5).status_code - == 200 - ) + task.wait(1) except: - logging.error(json.dumps(dict( - error=traceback.format_exc(), - time_iso=datetime.datetime.now().isoformat(), - ))) - not_alive = True + task.kill() - if not_alive: - logging.error(json.dumps( - dict( - args=o.args, - k=k, - #o=pprint.pformat(o.__dict__), - status='not_alive', - time_iso=datetime.datetime.now().isoformat(), - ) - )) + t1 = dict() - #stop_task(o) - #del t1[k] - continue + shutdown = False - if not o.poll() is None: - logging.error(json.dumps( - dict( - #o=pprint.pformat(o.__dict__), - args=o.args, - k=k, - return_code=o.poll(), - status='crashed', - time_iso=datetime.datetime.now().isoformat(), - ) - )) - del t1[k] - continue + while True: + try: + for k, v in t2.items(): + if not k in t1: + logging.info(json.dumps(dict( + task=k, + status='starting', + ))) + t1[k] = v['task']() + logging.info(json.dumps(dict( + task=k, + status='started', + ))) + continue - if shutdown: - break + o = t1[k] - print('\r%s tasks %d' % ( - datetime.datetime.now().isoformat(), - len(t1), - ), end='') - sys.stdout.flush() - except KeyboardInterrupt: - print('\nshutting down') - break - finally: - time.sleep(5 * 60) + not_alive = None -for o in t1: - stop_task(o) + try: + not_alive = not ( + requests.get(v['url'], timeout=0.5).status_code + == 200 + ) + except: + logging.error(json.dumps(dict( + error=traceback.format_exc(), + time_iso=datetime.datetime.now().isoformat(), + ))) + not_alive = True + + if not_alive: + logging.error(json.dumps( + dict( + args=o.args, + k=k, + #o=pprint.pformat(o.__dict__), + status='not_alive', + time_iso=datetime.datetime.now().isoformat(), + ) + )) + + #stop_task(o) + #del t1[k] + continue + + if not o.poll() is None: + logging.error(json.dumps( + dict( + #o=pprint.pformat(o.__dict__), + args=o.args, + k=k, + return_code=o.poll(), + status='crashed', + time_iso=datetime.datetime.now().isoformat(), + ) + )) + del t1[k] + continue + + if shutdown: + break + + print('\r%s tasks %d' % ( + datetime.datetime.now().isoformat(), + len(t1), + ), end='') + sys.stdout.flush() + except KeyboardInterrupt: + print('\nshutting down') + break + finally: + time.sleep(5 * 60) + + for o in t1: + stop_task(o) + +if __name__ == '__main__': + Launcher().run()