Fix exit handling

os-repl
root 2022-11-29 19:53:08 +00:00
parent b884a180c8
commit b7e4e26eda
1 changed files with 9 additions and 12 deletions

View File

@ -55,8 +55,8 @@ def check_auth_publickey(self, username, key):
class Runner(threading.Thread): class Runner(threading.Thread):
def __init__(self, client, transport: paramiko.Transport): def __init__(self, client, transport: paramiko.Transport):
threading.Thread.__init__(self, name='sshim.Runner')
self.instance_name = "instance-" + str(uuid.uuid4()) self.instance_name = "instance-" + str(uuid.uuid4())
threading.Thread.__init__(self, name=f'sshim.Runner {self.instance_name}')
self.instance_password = str(uuid.uuid4()) # TODO: secure password generation self.instance_password = str(uuid.uuid4()) # TODO: secure password generation
self.daemon = True self.daemon = True
self.client = client self.client = client
@ -80,12 +80,16 @@ class Runner(threading.Thread):
if self.shell_channel in r: if self.shell_channel in r:
x = self.shell_channel.recv(1024) x = self.shell_channel.recv(1024)
if len(x) == 0: if len(x) == 0:
self.shell_channel.close()
self.shell_channel = None self.shell_channel = None
continue
client_shell_channel.send(x) client_shell_channel.send(x)
if client_shell_channel in r: if client_shell_channel in r:
x = client_shell_channel.recv(1024) x = client_shell_channel.recv(1024)
if len(x) == 0: if len(x) == 0:
self.shell_channel.close()
self.shell_channel = None self.shell_channel = None
continue
self.shell_channel.send(x) self.shell_channel.send(x)
if self.sftp_channel is not None: # TODO: move this to function if self.sftp_channel is not None: # TODO: move this to function
@ -93,28 +97,21 @@ class Runner(threading.Thread):
if self.sftp_channel in r: if self.sftp_channel in r:
x = self.sftp_channel.recv(1024) x = self.sftp_channel.recv(1024)
if len(x) == 0: if len(x) == 0:
self.sftp_channel.close()
self.sftp_channel = None self.sftp_channel = None
continue
client_sftp_channel.send(x) client_sftp_channel.send(x)
if client_sftp_channel in r: if client_sftp_channel in r:
x = client_sftp_channel.recv(1024) x = client_sftp_channel.recv(1024)
if len(x) == 0: if len(x) == 0:
self.sftp_channel.close()
self.sftp_channel = None self.sftp_channel = None
continue
self.sftp_channel.send(x) self.sftp_channel.send(x)
if self.transport.is_active() is False: if self.transport.is_active() is False:
break break
try:
client_shell_channel.close()
self.shell_channel.close()
except AttributeError as e:
logger.debug(e)
try:
client_sftp_channel.close()
self.sftp_channel.close()
except AttributeError as e:
logger.debug(e)
lxd_interface.destroy_instance(self.instance_name) lxd_interface.destroy_instance(self.instance_name)
def set_shell_channel(self, channel): def set_shell_channel(self, channel):