From 6597057d016941510769a5e070e067a3f93c3a72 Mon Sep 17 00:00:00 2001 From: Oliver Schmidt Date: Wed, 26 Feb 2025 14:49:21 +0100 Subject: [PATCH] devhost: fix fc-devhost-vm-cleanup.service timezone handling Earlier changes regarding the deprecation of `datetime.utcnow()` had the consequence, that persisted isoformat timestamps now contained a timezone. Parsing these stamps again and comparing them with a naive time resulted in an error. From now on, always compare timezoned datetime objects. Includes a migration path for naive isoformat timestamps created in earlier releases. PL-133467 --- ...402_PL-133467-fix-devhost-cleanup_scriv.md | 17 ++++++++++++++ nixos/roles/devhost/fc-devhost.py | 22 ++++++++++++------- 2 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 changelog.d/20250226_145402_PL-133467-fix-devhost-cleanup_scriv.md diff --git a/changelog.d/20250226_145402_PL-133467-fix-devhost-cleanup_scriv.md b/changelog.d/20250226_145402_PL-133467-fix-devhost-cleanup_scriv.md new file mode 100644 index 000000000..a2f3d52e4 --- /dev/null +++ b/changelog.d/20250226_145402_PL-133467-fix-devhost-cleanup_scriv.md @@ -0,0 +1,17 @@ + + + +### NixOS XX.XX platform + +- devhost: fix cleanup of old development VMs (PL-133467) diff --git a/nixos/roles/devhost/fc-devhost.py b/nixos/roles/devhost/fc-devhost.py index 4af89e849..0b1a01698 100644 --- a/nixos/roles/devhost/fc-devhost.py +++ b/nixos/roles/devhost/fc-devhost.py @@ -428,23 +428,29 @@ def cleanup(self, location=None): vm_shut_down = False for vm_cfg in list_all_vm_configs(): if "last_deploy_date" not in vm_cfg: - vm_cfg["last_deploy_date"] = ( - datetime.datetime.utcnow().isoformat() - ) + vm_cfg["last_deploy_date"] = datetime.datetime.now( + datetime.UTC + ).isoformat() with open( CONFIG_DIR / f"{vm_cfg['name']}.json", mode="w" ) as f: f.write(json.dumps(vm_cfg)) - if datetime.datetime.fromisoformat(vm_cfg["last_deploy_date"]) < ( - datetime.datetime.utcnow() - datetime.timedelta(days=31) + # existing VMs might have persisted a timezone-naive timestamp + last_deploy_date_parsed = datetime.datetime.fromisoformat( + vm_cfg["last_deploy_date"] + ).astimezone(datetime.UTC) + if last_deploy_date_parsed < ( + datetime.datetime.now(datetime.UTC) + - datetime.timedelta(days=31) ): print(f"Deleting VM {vm_cfg['name']}.") Manager(name=vm_cfg["name"]).destroy() - elif datetime.datetime.fromisoformat( - vm_cfg["last_deploy_date"] - ) < (datetime.datetime.utcnow() - datetime.timedelta(days=14)): + elif last_deploy_date_parsed < ( + datetime.datetime.now(datetime.UTC) + - datetime.timedelta(days=14) + ): if vm_cfg["online"] == False: continue print(f"Shutting down VM {vm_cfg['name']}.")