From df3f9dcd30a13232447b3be67c7845c51cb527a0 Mon Sep 17 00:00:00 2001 From: Ian Wienand Date: Thu, 21 Jul 2022 14:41:39 +1000 Subject: zuul-stream: automatically remove streaming files When using protocol version 1, send a finalise message when streaming is complete so that the zuul_console daemon can delete the temporary file. We test this by inspecting the Ansible console output, which logs a message with the UUID of the streaming job. We dump the temporary files on the remote side and make sure a console file for that job isn't present. Change-Id: I823156dc2bcae91bd6d9770bd1520aa55ad875b4 --- zuul/ansible/base/callback/zuul_stream.py | 10 ++++++++++ zuul/ansible/base/library/zuul_console.py | 22 ++++++++++++++++------ 2 files changed, 26 insertions(+), 6 deletions(-) (limited to 'zuul') diff --git a/zuul/ansible/base/callback/zuul_stream.py b/zuul/ansible/base/callback/zuul_stream.py index d33b4dda0..f31983ed6 100644 --- a/zuul/ansible/base/callback/zuul_stream.py +++ b/zuul/ansible/base/callback/zuul_stream.py @@ -211,6 +211,16 @@ class CallbackModule(default.CallbackModule): done = self._log_streamline( host, line.decode("utf-8", "backslashreplace")) if done: + if self._zuul_console_version > 0: + try: + # reestablish connection and tell console to + # clean up + s = self._read_log_connect(host, ip, port) + s.send(f'f:{log_id}\n'.encode('utf-8')) + s.close() + except Exception: + # Don't worry if this fails + pass return else: more = s.recv(4096) diff --git a/zuul/ansible/base/library/zuul_console.py b/zuul/ansible/base/library/zuul_console.py index bd23d5f49..f8e4e1e6a 100755 --- a/zuul/ansible/base/library/zuul_console.py +++ b/zuul/ansible/base/library/zuul_console.py @@ -174,11 +174,17 @@ class Server(object): except UnicodeDecodeError: pass + def _clean_uuid(self, log_uuid): + # use path split to make use the input isn't trying to be clever + # and construct some path like /tmp/console-/../../something + return os.path.split(log_uuid)[-1] + def handleOneConnection(self, conn): # V1 protocol # ----------- # v: get version number, is remote version # s: send logs for + # f: finalise/cleanup while True: command = self.get_command(conn) if command.startswith('v:'): @@ -187,8 +193,16 @@ class Server(object): # for now. conn.send(f'{ZUUL_CONSOLE_PROTO_VERSION}\n'.encode('utf-8')) continue + elif command.startswith('f:'): + log_uuid = self._clean_uuid(command[2:]) + try: + os.unlink(self.path.format(log_uuid=log_uuid)) + except Exception: + # something might have cleaned /tmp + pass + continue elif command.startswith('s:'): - log_uuid = command[2:] + log_uuid = self._clean_uuid(command[2:]) break else: # NOTE(ianw): 2022-07-21 In releases < 6.3.0 the streaming @@ -196,13 +210,9 @@ class Server(object): # default assume that is what is coming in here. We can # remove this fallback when we decide it is no longer # necessary. - log_uuid = command + log_uuid = self._clean_uuid(command) break - # use path split to make use the input isn't trying to be clever - # and construct some path like /tmp/console-/../../something - log_uuid = os.path.split(log_uuid)[-1] - # FIXME: this won't notice disconnects until it tries to send console = None try: -- cgit v1.2.1