From 846ef2ba5387158d0db9b7348f00c38984c33375 Mon Sep 17 00:00:00 2001 From: Lorenzo Volpi Date: Wed, 22 Nov 2023 19:27:09 +0100 Subject: [PATCH] execution modules updated --- remote.py | 43 ++++++++++++++++++++++++++++++++++++++----- run.py | 15 ++++----------- 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/remote.py b/remote.py index e9afbc7..a3c0d32 100644 --- a/remote.py +++ b/remote.py @@ -15,7 +15,7 @@ username = "volpi" __exec_main = "cd tesi; /home/volpi/.local/bin/poetry run main" __exec_log = "/usr/bin/tail -f -n 0 tesi/quacc.log" -__log_file = "quacc.log" +__log_file = "remote.log" __target_dir = Path("/home/volpi/tesi") __to_sync_up = { "dir": [ @@ -161,8 +161,9 @@ def _echo_log(ssh: paramiko.SSHClient, q_: queue.Queue): _, rout, _ = ssh.exec_command(__exec_log, timeout=5.0) while True: try: + _line = rout.readline() with open(__log_file, "a") as f: - f.write(rout.readline()) + f.write(_line) except TimeoutError: pass @@ -173,15 +174,23 @@ def _echo_log(ssh: paramiko.SSHClient, q_: queue.Queue): pass -def remote(): +def remote(detatch=False): ssh = paramiko.SSHClient() ssh.load_host_keys(known_hosts) ssh.connect(hostname=hostname, username=username) sync_code(ssh=ssh) - _, rout, rerr = ssh.exec_command(__exec_main) - q = queue.Queue() + __to_exec = __exec_main + if detatch: + __to_exec += " &> out & disown" + _, rout, rerr = ssh.exec_command(__to_exec) + + if detatch: + ssh.close() + return + + q = queue.Queue() _tlog = threading.Thread(target=_echo_log, args=[ssh, q]) _tlog.start() @@ -196,6 +205,30 @@ def remote(): q.put(None) sync_output(ssh=ssh) + _tlog.join() + + ssh.close() + + +def log_remote(): + ssh = paramiko.SSHClient() + ssh.load_host_keys(known_hosts) + ssh.connect(hostname=hostname, username=username) + + sftp = ssh.open_sftp() + sftp.get(str(__target_dir / "quacc.log"), str(Path(__log_file).absolute())) + sftp.close() + + q = queue.Queue() + _tlog = threading.Thread(target=_echo_log, args=[ssh, q]) + _tlog.start() + + try: + input() + except KeyboardInterrupt: + pass + finally: + q.put(None) _tlog.join() ssh.close() diff --git a/run.py b/run.py index 73ab0c1..eddab99 100644 --- a/run.py +++ b/run.py @@ -1,24 +1,17 @@ import argparse -from quacc.main import main as _main -from remote import remote as _remote - - -def run_local(): - _main() - - -def run_remote(): - _remote() +from quacc.main import main as run_local +from remote import remote as run_remote def run(): parser = argparse.ArgumentParser() parser.add_argument("-l", "--local", action="store_true", dest="local") parser.add_argument("-r", "--remote", action="store_true", dest="remote") + parser.add_argument("-d", "--detatch", action="store_true", dest="detatch") args = parser.parse_args() if args.local: run_local() elif args.remote: - run_remote() + run_remote(detatch=args.detatch)