From 092cbf14df48c396d2268ea78e84e67a138fcff1 Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Mon, 25 Mar 2024 17:25:01 +0100 Subject: [PATCH 1/2] install: remove zzzz-temp-installer-unattended-upgrade after running UU On recent installations of Ubuntu, the zzzz-installer-unattended-upgrade file ended up in the etc/apt/apt.conf.d directory of the target system. It is supposed to be a temporary file intended for one time invocation of unattended-upgrade at the end of the installation. Let's remove the file after unattended-upgrades finishes using a try ... finally construct. The previous implementation was doing the call to UU inside the with open(...) block, after a manual call to .close(). This is unnecessary, the file is automatically closed at the end of the with block. Signed-off-by: Olivier Gayot --- subiquity/server/controllers/install.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/subiquity/server/controllers/install.py b/subiquity/server/controllers/install.py index e1959d2a..db4d0d9d 100644 --- a/subiquity/server/controllers/install.py +++ b/subiquity/server/controllers/install.py @@ -751,10 +751,10 @@ class InstallController(SubiquityController): apt_conf_contents += uu_apt_conf_update_all else: apt_conf_contents += uu_apt_conf_update_security - fname = "zzzz-temp-installer-unattended-upgrade" - with open(os.path.join(aptdir, fname), "wb") as apt_conf: + apt_conf_path = Path(aptdir) / "zzzz-temp-installer-unattended-upgrade" + with open(apt_conf_path, "wb") as apt_conf: apt_conf.write(apt_conf_contents) - apt_conf.close() + try: self.unattended_upgrades_ctx = context self.unattended_upgrades_cmd = await start_curtin_command( self.app, @@ -772,8 +772,10 @@ class InstallController(SubiquityController): except subprocess.CalledProcessError as cpe: log_process_streams(logging.ERROR, cpe, "Unattended upgrades") context.description = f"FAILED to apply {policy} updates" - self.unattended_upgrades_cmd = None - self.unattended_upgrades_ctx = None + finally: + apt_conf_path.unlink() + self.unattended_upgrades_cmd = None + self.unattended_upgrades_ctx = None async def stop_unattended_upgrades(self): with self.unattended_upgrades_ctx.parent.child( From 2d8a1ea9b3ca7e6185e1f2b7d5b31d3d30fd30fa Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Mon, 25 Mar 2024 17:41:07 +0100 Subject: [PATCH 2/2] install: use pathlib.Path.write_bytes construct instead of open + write Signed-off-by: Olivier Gayot --- subiquity/server/controllers/install.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/subiquity/server/controllers/install.py b/subiquity/server/controllers/install.py index db4d0d9d..1f9cd759 100644 --- a/subiquity/server/controllers/install.py +++ b/subiquity/server/controllers/install.py @@ -752,8 +752,7 @@ class InstallController(SubiquityController): else: apt_conf_contents += uu_apt_conf_update_security apt_conf_path = Path(aptdir) / "zzzz-temp-installer-unattended-upgrade" - with open(apt_conf_path, "wb") as apt_conf: - apt_conf.write(apt_conf_contents) + apt_conf_path.write_bytes(apt_conf_contents) try: self.unattended_upgrades_ctx = context self.unattended_upgrades_cmd = await start_curtin_command(