Skip to content

Commit

Permalink
Merge "Fix: Refactor image clean"
Browse files Browse the repository at this point in the history
  • Loading branch information
tykeal authored and Gerrit Code Review committed Aug 16, 2021
2 parents efa6c4b + 41d6c1f commit d263044
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
38 changes: 17 additions & 21 deletions lftools/openstack/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,9 @@ def cleanup(os_cloud, days=0, hide_public=False, ci_managed=True, clouds=None):
"""

def _remove_images_from_cloud(images, cloud):
log.info("Removing {} images from {}.".format(len(images), cloud.cloud_config.name))
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
Expand All @@ -87,9 +86,7 @@ def _remove_images_from_cloud(images, cloud):
continue

if project_info["id"] != image.owner:
log.warning(
"Image {} not owned by project {}. Cannot remove...".format(image.name, cloud.cloud_config.name)
)
log.warning("Image {} not owned by project {}. Cannot remove...".format(image.name, cloud.config._name))
continue

try:
Expand All @@ -104,27 +101,26 @@ def _remove_images_from_cloud(images, cloud):

if not result:
log.warning(
'Failed to remove "{}" from {}. Possibly already deleted.'.format(
image.name, cloud.cloud_config.name
)
'Failed to remove "{}" from {}. Possibly already deleted.'.format(image.name, cloud.config._name)
)
else:
log.info('Removed "{}" from {}.'.format(image.name, cloud.cloud_config.name))
log.info('Removed "{}" from {}.'.format(image.name, cloud.config._name))

cloud = openstack.connection.from_config(cloud=os_cloud)
cloud_list = []
if clouds:
cloud_list = []
for c in clouds.split(","):
cloud_list.append(openstack.connection.from_config(cloud=c))

images = cloud.list_images()
filtered_images = _filter_images(images, days, hide_public, ci_managed)

if clouds:
for c in cloud_list:
_remove_images_from_cloud(filtered_images, c)
else:
_remove_images_from_cloud(filtered_images, cloud)
cloud_list.append(c)

if os_cloud not in cloud_list:
cloud_list.append(os_cloud)

for c in cloud_list:
cloud = openstack.connection.from_config(cloud=c)
images = cloud.list_images()
if images:
filtered_images = _filter_images(images, days, hide_public, ci_managed)
if filtered_images:
_remove_images_from_cloud(filtered_images, cloud)


def share(os_cloud, image, clouds):
Expand Down
8 changes: 8 additions & 0 deletions releasenotes/notes/fix-image-cleanup-4266161f3068cf96.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
fixes:
- |
Refactor image cleanup code to handle attribute types
and multi-cloud config correctly. (Issue-ID: RELENG-3869)
Error:
AttributeError: 'Connection' object has no attribute 'cloud_config'

0 comments on commit d263044

Please sign in to comment.