From db14acfe2d5bda4012fbbb363a361fc5cb2fdc85 Mon Sep 17 00:00:00 2001 From: John Lancaster <32917998+jsl12@users.noreply.github.com> Date: Sun, 16 Jun 2024 00:25:23 -0500 Subject: [PATCH] updates --- requirements.txt | 1 + run_service.py | 47 +++++++++++++++++++++++++---------------------- test_socket.py | 20 +++++++------------- 3 files changed, 33 insertions(+), 35 deletions(-) create mode 100644 requirements.txt diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..c94be38 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +rich \ No newline at end of file diff --git a/run_service.py b/run_service.py index 62d8e5f..4c2e2c2 100755 --- a/run_service.py +++ b/run_service.py @@ -1,27 +1,30 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python -import os import subprocess -from rich import print -os.chdir('/root') -cmd = ['code', 'tunnel'] -# cmd = ['echo', '"Hello world"'] - - -print('PATH:\n' + '\n'.join(f' - {p}' for p in os.environ['PATH'].split(':'))) -try: - result = subprocess.run( - cmd, - shell=True, - env=os.environ, - text=True, - stdout=subprocess.PIPE, - stderr=subprocess.STDOUT, +def run_tunnel(): + process = subprocess.Popen( + ['/root/code', 'tunnel'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True ) - result.check_returncode() -except subprocess.CalledProcessError as e: - print(e) -finally: - print(result.stdout) + + with process.stdout: + try: + for line in iter(process.stdout.readline, ''): + print(line, end='') + except KeyboardInterrupt: + print('KeyboardInterrupt') + finally: + process.terminate() + try: + # Give some time to gracefully shutdown + returncode = process.wait(timeout=5) + except subprocess.TimeoutExpired: + print('Timeout expired, sending SIGKILL') + process.kill() + returncode = process.wait() + print(f'Process finished with {returncode}') + + +if __name__ == '__main__': + run_tunnel() diff --git a/test_socket.py b/test_socket.py index 17e38c8..4f90fa8 100755 --- a/test_socket.py +++ b/test_socket.py @@ -9,16 +9,10 @@ console = Console() print = console.print with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as sock: - try: - sock.connect('/run/example.sock') - except Exception: - raise - else: - with console.status('Connected...'): - while True: - try: - sleep(0.1) - except KeyboardInterrupt: - break - finally: - print('Exiting') + sock.connect('/run/example.sock') + with console.status('Connected...'): + while True: + try: + sleep(0.1) + except KeyboardInterrupt: + break