From: Matthew Watkins Date: Mon, 7 Nov 2022 16:55:51 +0000 (+0000) Subject: Fix: Improve parameter handling in os/image cleanup X-Git-Tag: v0.37.1^0 X-Git-Url: https://gerrit.linuxfoundation.org/infra/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F70%2F70970%2F10;p=releng%2Flftools.git Fix: Improve parameter handling in os/image cleanup Issue: RELENG-4467 Signed-off-by: Matthew Watkins Change-Id: If91463f83c4be698415e695a83f10f416e379dac --- diff --git a/lftools/openstack/image.py b/lftools/openstack/image.py index 630f594e..b5a221fa 100644 --- a/lftools/openstack/image.py +++ b/lftools/openstack/image.py @@ -18,11 +18,11 @@ import subprocess import sys import tempfile from datetime import datetime, timedelta +from six.moves import urllib import openstack import openstack.config from openstack.cloud.exc import OpenStackCloudException -from six.moves import urllib log = logging.getLogger(__name__) @@ -35,19 +35,31 @@ def _filter_images(images, days=0, hide_public=False, ci_managed=True): :arg bool hide_public: Whether or not to include public images. """ filtered = [] + bad_attribute = "" for image in images: if hide_public and image.is_public: continue if ci_managed and image.metadata.get("ci_managed", None) != "yes": continue - if image.protected: - continue + # Safely handle potentially problematic attributes + try: + if image.is_protected: + continue + except AttributeError: + bad_attribute = "image.is_protected" + try: + if image.protected: + continue + except AttributeError: + bad_attribute = "image.protected" if days and ( datetime.strptime(image.created_at, "%Y-%m-%dT%H:%M:%SZ") >= datetime.now() - timedelta(days=days) ): continue filtered.append(image) + if bad_attribute: + log.warning("Use of " + bad_attribute + " resulted in an exception") return filtered @@ -73,13 +85,24 @@ def cleanup(os_cloud, days=0, hide_public=False, ci_managed=True, clouds=None): from. Otherwise os_cloud will be used. """ + bad_attribute = "" def _remove_images_from_cloud(images, cloud): log.info("Removing {} images from {}.".format(len(images), cloud.config._name)) project_info = cloud._get_project_info() for image in images: - if image.is_protected: - log.warning("Image {} is protected. Cannot remove...".format(image.name)) - continue + # Safely handle potentially problematic attributes + try: + if image.is_protected: + log.warning("Image {} is protected. Cannot remove...".format(image.name)) + continue + except AttributeError: + bad_attribute = "image.is_protected" + try: + if image.protected: + log.warning("Image {} is protected. Cannot remove...".format(image.name)) + continue + except AttributeError: + bad_attribute = "image.protected" if image.visibility == "shared": log.warning("Image {} is shared. Cannot remove...".format(image.name)) @@ -121,7 +144,8 @@ def cleanup(os_cloud, days=0, hide_public=False, ci_managed=True, clouds=None): filtered_images = _filter_images(images, days, hide_public, ci_managed) if filtered_images: _remove_images_from_cloud(filtered_images, cloud) - + if bad_attribute: + log.warning("Use of " + bad_attribute + " resulted in an exception") def share(os_cloud, image, clouds): """Share image with another tenant.""" diff --git a/releasenotes/notes/fix-os-image-cleanup-94c98f53e6c13edb.yaml b/releasenotes/notes/fix-os-image-cleanup-94c98f53e6c13edb.yaml new file mode 100644 index 00000000..909d0478 --- /dev/null +++ b/releasenotes/notes/fix-os-image-cleanup-94c98f53e6c13edb.yaml @@ -0,0 +1,4 @@ +--- +fixes: + - | + Correct parameter name in os image cleanup code