-
Notifications
You must be signed in to change notification settings - Fork 119
ci: Handle concurrent deletions in CI #696
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: ubuntu-jammy
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,28 @@ if [ -n "${ami_role_arn:-}" ]; then | |
| export AWS_PROFILE=resource_account | ||
| fi | ||
|
|
||
| # Every stemcell pipeline runs the published sweep against the same account, so two | ||
| # pipelines can select the same AMI before either has deleted it. This handles | ||
| # expected potential errors that could occur on a collision. | ||
| delete_tolerating_missing() { | ||
| local output | ||
| if output="$("$@" 2>&1)"; then | ||
| [ -n "${output}" ] && echo "${output}" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: echo should probably be printf |
||
| return 0 | ||
| fi | ||
|
|
||
| case "${output}" in | ||
| *InvalidAMIID.Unavailable*|*InvalidAMIID.NotFound*|*InvalidSnapshot.NotFound*) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. gemini seems to think there is 1 more condition which may be worth a looksie at:
|
||
| echo " already deleted by another run, skipping" | ||
| return 0 | ||
| ;; | ||
| *) | ||
| echo "${output}" >&2 | ||
| return 1 | ||
| ;; | ||
| esac | ||
| } | ||
|
|
||
| past_due=$(date --date="${ami_older_than_days} days ago" +"%Y-%m-%d") | ||
| # shellcheck disable=SC2016 | ||
| past_due_query='sort_by(Images,&CreationDate)[?CreationDate<`'"${past_due}"'`].{ImageId: ImageId, date:CreationDate, SnapshotId: BlockDeviceMappings[0].Ebs.SnapshotId,Version: Tags[?Key==`name`]|[0].Value}' | ||
|
|
@@ -59,16 +81,6 @@ for region in ${ami_destinations}; do | |
| ami_list=$(jq -s '.[0] + .[1]' <(echo "${ami_list}") <(echo "${results}")) | ||
| fi | ||
|
|
||
| if [ -n "${snapshot_id:-}" ]; then | ||
|
ystros marked this conversation as resolved.
|
||
| results=$(aws ec2 describe-images \ | ||
| --owners self \ | ||
| --output json \ | ||
| --region "${region}" \ | ||
| --filters "Name=block-device-mapping.snapshot-id,Values=${snapshot_id}" \ | ||
| --query "${past_due_query}" | jq 'reverse | del(.[range(env.ami_keep_latest|tonumber)])') | ||
| ami_list=$(jq -s '.[0] + .[1]' <(echo "${ami_list}") <(echo "${results}")) | ||
| fi | ||
|
|
||
| # 'ami_list' is a json array of objects, each object is an ami and its snapshot | ||
| for row in $(echo "${ami_list}" | jq -r '.[] | @base64'); do | ||
| _jq() { | ||
|
|
@@ -83,14 +95,12 @@ for region in ${ami_destinations}; do | |
| Snapshot id: $(_jq '.SnapshotId') | ||
| " | ||
|
|
||
| aws ec2 deregister-image \ | ||
| delete_tolerating_missing aws ec2 deregister-image \ | ||
| --image-id "$(_jq '.ImageId')" \ | ||
| --region "${region}" | ||
|
|
||
| if [ "${snapshot_id:-}" != "$(_jq '.SnapshotId')" ]; then | ||
| aws ec2 delete-snapshot \ | ||
| --snapshot-id "$(_jq '.SnapshotId')" \ | ||
| --region "${region}" | ||
| fi | ||
| delete_tolerating_missing aws ec2 delete-snapshot \ | ||
| --snapshot-id "$(_jq '.SnapshotId')" \ | ||
| --region "${region}" | ||
| done | ||
| done | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: should this be named something like:
run_tolerate_missing_errorsor something that doesn't specifically mentiondeletesince its also used for deregister.