Skip to content

Commit

Permalink
Fix: Improve parameter handling in os/image cleanup
Browse files Browse the repository at this point in the history
Issue: RELENG-4467
Signed-off-by: Matthew Watkins <[email protected]>
Change-Id: If91463f83c4be698415e695a83f10f416e379dac
  • Loading branch information
ModeSevenIndustrialSolutions committed Nov 25, 2022
1 parent d84948c commit 32fac03
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
38 changes: 31 additions & 7 deletions lftools/openstack/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
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__)

Expand All @@ -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


Expand All @@ -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))
Expand Down Expand Up @@ -121,7 +144,8 @@ def _remove_images_from_cloud(images, cloud):
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."""
Expand Down
4 changes: 4 additions & 0 deletions releasenotes/notes/fix-os-image-cleanup-94c98f53e6c13edb.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
Correct parameter name in os image cleanup code

0 comments on commit 32fac03

Please sign in to comment.