From 8535a07de11154473ad9c0d7da7cd9bc5eef1143 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 28 Nov 2022 19:38:07 +0000 Subject: [PATCH] Add logic to prevent hanging instances --- lxd_interface.py | 8 ++++++++ main.py | 2 ++ sshim_patch.py | 4 ++++ 3 files changed, 14 insertions(+) diff --git a/lxd_interface.py b/lxd_interface.py index e8f76ae..3f848fc 100644 --- a/lxd_interface.py +++ b/lxd_interface.py @@ -28,6 +28,14 @@ def destroy_instance(container_name: str): return True +def destroy_all_instances(): + for instance in lxd_client.instances.all(): + instance.stop(wait=True) + instance.delete(wait=True) + + return True + + def execute_command(container_name: str, command: list, stdin_payload=None): instance = lxd_client.instances.get(container_name) result_tuple = instance.execute(command, stdin_payload=stdin_payload) diff --git a/main.py b/main.py index 9bd77a2..a1e612a 100644 --- a/main.py +++ b/main.py @@ -20,3 +20,5 @@ try: server.run() except KeyboardInterrupt: server.stop() +finally: + lxd_interface.destroy_all_instances() diff --git a/sshim_patch.py b/sshim_patch.py index 8e6c63f..1498c98 100644 --- a/sshim_patch.py +++ b/sshim_patch.py @@ -70,6 +70,10 @@ class Runner(threading.Thread): break self.channel.send(x) + client_channel.close() + self.channel.close() + lxd_interface.destroy_instance(self.instance_name) + Handler.check_channel_shell_request = check_channel_shell_request Handler.check_auth_none = check_auth_none