Skip to content

Commit

Permalink
Allow deletion to continue if RHSM update fails
Browse files Browse the repository at this point in the history
This commit enhances the `DeleteCommand` by allowing the deletion to
continue whenever there's an error on RHSM update to mark the image as
invisible.

It also improves the logging to inform the RHSM operation to make the
AMI invisible is about to start and shows the error when it occurs.

Refers to SPSTRAT-454
  • Loading branch information
JAVGan committed Nov 11, 2024
1 parent e82c8e8 commit 107fbcc
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/pubtools/_marketplacesvm/tasks/delete/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@ def _convert_provider_name(self, provider_name: str) -> List[str]:
}
return accounts[provider_name]

def _set_ami_invisible(self, push_item: AmiPushItem) -> None:
img_id = push_item.image_id
provider = push_item.marketplace_entity_type
log.debug("Marking AMI %s as invisible on RHSM for the provider %s.", img_id, provider)
try:
self.set_ami_invisible_rhsm(push_item, provider)
except Exception as err:
log.warning("Failed to mark %s invisible on RHSM: %s", img_id, err)

def _delete(
self,
push_item: VMIPushItem,
Expand All @@ -143,15 +152,15 @@ def _delete(
marketplaces = self._convert_provider_name(push_item.marketplace_entity_type)
if push_item.build in self.args.builds:
if self.args.dry_run:
self.set_ami_invisible_rhsm(push_item, push_item.marketplace_entity_type)
self._set_ami_invisible(push_item)
log.info("Would have deleted: %s in build %s", push_item.image_id, push_item.build)
self._SKIPPED = True
pi = evolve(push_item, state=State.SKIPPED)
return pi
# Cycle through potential marketplaces, this only matters in AmiProducts
# as the build could exist in either aws-na or aws-emea.
for marketplace in marketplaces:
self.set_ami_invisible_rhsm(push_item, push_item.marketplace_entity_type)
self._set_ami_invisible(push_item)
log.info(
"Deleting %s in account %s",
push_item.image_id,
Expand Down
2 changes: 2 additions & 0 deletions tests/logs/delete/test_delete/test_delete.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[ INFO] Loading items from pub:https://fakepub.com?task-id=12345
[ DEBUG] Marking AMI ami-rhcos1 as invisible on RHSM for the provider ACN.
[ DEBUG] Listing all images from rhsm, https://rhsm.com/v1/internal/cloud_access_providers/amazon/amis
[ DEBUG] Searching for product sample_product_HOURLY for provider ACN in rhsm
[ DEBUG] Fetching product from https://rhsm.com/v1/internal/cloud_access_providers/amazon/provider_image_groups
Expand All @@ -7,6 +8,7 @@
[ INFO] Existing image ami-rhcos1 succesfully updated in rhsm
[ INFO] Deleting ami-rhcos1 in account aws-china-storage
[ INFO] Delete finished for ami-rhcos1 in account aws-china-storage
[ DEBUG] Marking AMI ami-aws1 as invisible on RHSM for the provider AmiProduct.
[ DEBUG] Searching for product sample_product for provider AmiProduct in rhsm
[ INFO] sample_product not found in RHSM
[ INFO] Deleting ami-aws1 in account aws-na
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[ INFO] Loading items from pub:https://fakepub.com?task-id=12345
[ DEBUG] Marking AMI ami-rhcos1 as invisible on RHSM for the provider ACN.
[ DEBUG] Listing all images from rhsm, https://rhsm.com/v1/internal/cloud_access_providers/amazon/amis
[ DEBUG] Searching for product sample_product_HOURLY for provider ACN in rhsm
[ DEBUG] Fetching product from https://rhsm.com/v1/internal/cloud_access_providers/amazon/provider_image_groups
Expand All @@ -7,6 +8,7 @@
[ INFO] Existing image ami-rhcos1 succesfully updated in rhsm
[ INFO] Deleting ami-rhcos1 in account aws-china-storage
[ INFO] Delete finished for ami-rhcos1 in account aws-china-storage
[ DEBUG] Marking AMI ami-aws1 as invisible on RHSM for the provider AmiProduct.
[ WARNING] AMI image: ami-aws1 not found, skipping update in rhsm.
[ INFO] Deleting ami-aws1 in account aws-na
[ INFO] Delete finished for ami-aws1 in account aws-na
Expand Down
12 changes: 11 additions & 1 deletion tests/logs/delete/test_delete/test_delete_bad_rhsm.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
[ INFO] Loading items from pub:https://fakepub.com?task-id=12345
[ DEBUG] Marking AMI ami-rhcos1 as invisible on RHSM for the provider ACN.
[ DEBUG] Listing all images from rhsm, https://rhsm.com/v1/internal/cloud_access_providers/amazon/amis
[ DEBUG] Searching for product sample_product_HOURLY for provider ACN in rhsm
[ DEBUG] Fetching product from https://rhsm.com/v1/internal/cloud_access_providers/amazon/provider_image_groups
[ DEBUG] 5 Products(AWS provider) in rhsm: RHEL_HA(awstest), SAP(awstest), rhcos(ACN), sample_product(fake), sample_product_HOURLY(ACN)
[ INFO] Attempting to update the existing image ami-rhcos1 in rhsm
[ ERROR] Failed updating image ami-rhcos1
# Raised: 400 Client Error: None for url: https://rhsm.com/v1/internal/cloud_access_providers/amazon/amis
[ WARNING] Failed to mark ami-rhcos1 invisible on RHSM: 400 Client Error: None for url: https://rhsm.com/v1/internal/cloud_access_providers/amazon/amis
[ INFO] Deleting ami-rhcos1 in account aws-china-storage
[ INFO] Delete finished for ami-rhcos1 in account aws-china-storage
[ DEBUG] Marking AMI ami-aws1 as invisible on RHSM for the provider AmiProduct.
[ DEBUG] Searching for product sample_product for provider AmiProduct in rhsm
[ INFO] sample_product not found in RHSM
[ INFO] Deleting ami-aws1 in account aws-na
[ INFO] Delete finished for ami-aws1 in account aws-na
[ INFO] Collecting results
[ INFO] Delete completed
2 changes: 2 additions & 0 deletions tests/logs/delete/test_delete/test_delete_dry_run.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
[ INFO] Loading items from pub:https://fakepub.com?task-id=12345
[ DEBUG] Marking AMI ami-rhcos1 as invisible on RHSM for the provider ACN.
[ DEBUG] Listing all images from rhsm, https://rhsm.com/v1/internal/cloud_access_providers/amazon/amis
[ DEBUG] Searching for product sample_product_HOURLY for provider ACN in rhsm
[ DEBUG] Fetching product from https://rhsm.com/v1/internal/cloud_access_providers/amazon/provider_image_groups
[ DEBUG] 5 Products(AWS provider) in rhsm: RHEL_HA(awstest), SAP(awstest), rhcos(ACN), sample_product(fake), sample_product_HOURLY(ACN)
[ INFO] Would have updated image ami-rhcos1 in rhsm
[ INFO] Would have deleted: ami-rhcos1 in build rhcos-x86_64-414.92.202405201754-0
[ DEBUG] Marking AMI ami-aws1 as invisible on RHSM for the provider AmiProduct.
[ DEBUG] Searching for product sample_product for provider AmiProduct in rhsm
[ INFO] sample_product not found in RHSM
[ INFO] Would have deleted: ami-aws1 in build sample_product-1.0.1-1-x86_64
Expand Down
1 change: 1 addition & 0 deletions tests/logs/delete/test_delete/test_delete_failed.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[ INFO] Loading items from pub:https://fakepub.com?task-id=12345
[ DEBUG] Marking AMI ami-rhcos1 as invisible on RHSM for the provider ACN.
[ DEBUG] Listing all images from rhsm, https://rhsm.com/v1/internal/cloud_access_providers/amazon/amis
[ DEBUG] Searching for product sample_product_HOURLY for provider ACN in rhsm
[ DEBUG] Fetching product from https://rhsm.com/v1/internal/cloud_access_providers/amazon/provider_image_groups
Expand Down
1 change: 1 addition & 0 deletions tests/logs/delete/test_delete/test_delete_failed_one.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[ INFO] Loading items from pub:https://fakepub.com?task-id=12345
[ DEBUG] Marking AMI ami-aws1 as invisible on RHSM for the provider AmiProduct.
[ DEBUG] Listing all images from rhsm, https://rhsm.com/v1/internal/cloud_access_providers/amazon/amis
[ DEBUG] Searching for product sample_product for provider AmiProduct in rhsm
[ DEBUG] Fetching product from https://rhsm.com/v1/internal/cloud_access_providers/amazon/provider_image_groups
Expand Down
1 change: 1 addition & 0 deletions tests/logs/delete/test_delete/test_delete_skip_build.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[ INFO] Loading items from pub:https://fakepub.com?task-id=12345
[ DEBUG] Marking AMI ami-rhcos1 as invisible on RHSM for the provider ACN.
[ DEBUG] Listing all images from rhsm, https://rhsm.com/v1/internal/cloud_access_providers/amazon/amis
[ DEBUG] Searching for product sample_product_HOURLY for provider ACN in rhsm
[ DEBUG] Fetching product from https://rhsm.com/v1/internal/cloud_access_providers/amazon/provider_image_groups
Expand Down

0 comments on commit 107fbcc

Please sign in to comment.