From 32fac03d7ed38c98dce96dba13a7d9aa9913cb93 Mon Sep 17 00:00:00 2001 From: Matthew Watkins Date: Mon, 7 Nov 2022 16:55:51 +0000 Subject: [PATCH] Fix: Improve parameter handling in os/image cleanup Issue: RELENG-4467 Signed-off-by: Matthew Watkins Change-Id: If91463f83c4be698415e695a83f10f416e379dac --- lftools/openstack/image.py | 38 ++++++++++++++++++---- .../fix-os-image-cleanup-94c98f53e6c13edb.yaml | 4 +++ 2 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 releasenotes/notes/fix-os-image-cleanup-94c98f53e6c13edb.yaml 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 -- 2.16.6