From e744a8f5ab3096a6d7f81ace4558a1e5478bb65c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Cabrita?= Date: Thu, 15 Jul 2021 15:15:06 +0100 Subject: [PATCH] fix: Skip empty hashes when cleaning up OpenStack servers created by CI Related tickets: * [BB-4378 (OpenCraft Internal)](https://tasks.opencraft.com/browse/BB-4378) --- cleanup_utils/openstack_cleanup.py | 33 +++++++++++++++++++----------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/cleanup_utils/openstack_cleanup.py b/cleanup_utils/openstack_cleanup.py index 2c31ef7d9..bdb8b3ce5 100644 --- a/cleanup_utils/openstack_cleanup.py +++ b/cleanup_utils/openstack_cleanup.py @@ -127,9 +127,12 @@ def run_cleanup(self): # If instance is older than age limit if instance_age and instance_age < self.age_limit: # Get instance name in format edxapp-HASHinteg-1 - self.cleaned_up_hashes.append( - instance.name.split('-')[1][:-5] - ) + clean_hash = instance.name.split('-')[1][:-5] + # some instance names might not actually + # match what we expect, so we ignore them + if not clean_hash: + logger.warning("Unexpected instance name %s, skipping...", instance.name) + continue # Terminate the servers logger.info( @@ -137,18 +140,24 @@ def run_cleanup(self): instance_age, self.age_limit ) - if not self.dry_run: - try: - instance.delete() - except Exception as e: # pylint: disable=broad-except - logger.warning( - " * WARNING: Unable to delete instance. Error: %s.", - e, - ) - + self.cleaned_up_hashes.append(clean_hash) + self._delete_instance(instance) else: logger.info( " * SKIPPING: Instance was created at %s but the cut-off age threshold is %s.", instance_age, self.age_limit ) + + def _delete_instance(self, instance): + """ + Delete the instance. + """ + if not self.dry_run: + try: + instance.delete() + except Exception as e: # pylint: disable=broad-except + logger.warning( + " * WARNING: Unable to delete instance. Error: %s.", + e, + )