-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DOSCT-100 Add ECR Image Clean Up (#205)
# Task Branch Pull Request ## Description of Changes This PR adds a python script for cleaning up ECR images. These images include untagged and images which count as old development images.
- Loading branch information
1 parent
9822381
commit 3830a1e
Showing
8 changed files
with
112 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
96 changes: 96 additions & 0 deletions
96
scripts/clean_up_ecr_repositories/clean_up_ecr_repositories.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
from datetime import datetime, timedelta | ||
from os import getenv | ||
|
||
from boto3 import client | ||
from pytz import timezone | ||
|
||
ecr_client = client("ecr") | ||
repository_prefix = getenv("ECR_REPOSITORY_PREFIX") | ||
mgmt_account_id = getenv("AWS_ACCOUNT_ID_MGMT") | ||
NEXT_TOKEN_DEFAULT_VALUE = "NextToken" # noqa: S105 | ||
|
||
|
||
def delete_ecr_images(repository_name: str, image_digests: list) -> None: | ||
"""Delete images from ECR repositories.""" | ||
ecr_client.batch_delete_image(repositoryName=repository_name, registryId=mgmt_account_id, imageIds=image_digests) | ||
|
||
|
||
def delete_untagged_images(repository: str) -> None: | ||
"""Delete untagged images from ECR repositories.""" | ||
next_token = NEXT_TOKEN_DEFAULT_VALUE | ||
while next_token: | ||
if next_token == NEXT_TOKEN_DEFAULT_VALUE: | ||
response = ecr_client.describe_images( | ||
repositoryName=repository, | ||
registryId=mgmt_account_id, | ||
maxResults=100, | ||
filter={ | ||
"tagStatus": "UNTAGGED", | ||
}, | ||
) | ||
else: | ||
response = ecr_client.describe_images( | ||
repositoryName=repository, | ||
registryId=mgmt_account_id, | ||
maxResults=100, | ||
nextToken=next_token, | ||
filter={ | ||
"tagStatus": "UNTAGGED", | ||
}, | ||
) | ||
images = response["imageDetails"] | ||
next_token = response.get("nextToken") | ||
image_digests = [{"imageDigest": image["imageDigest"]} for image in images] | ||
|
||
if not image_digests: | ||
return | ||
|
||
delete_ecr_images(repository, image_digests) | ||
print(f"Deleting {len(image_digests)} untagged images") | ||
|
||
|
||
def delete_old_task_images(repository: str) -> None: | ||
"""Delete old images from ECR repositories.""" | ||
date = datetime.now(timezone("Europe/London")) - timedelta(days=30) | ||
next_token = NEXT_TOKEN_DEFAULT_VALUE | ||
while next_token: | ||
if next_token == NEXT_TOKEN_DEFAULT_VALUE: | ||
response = ecr_client.describe_images( | ||
repositoryName=repository, | ||
registryId=mgmt_account_id, | ||
maxResults=100, | ||
filter={ | ||
"tagStatus": "TAGGED", | ||
}, | ||
) | ||
else: | ||
response = ecr_client.describe_images( | ||
repositoryName=repository, | ||
registryId=mgmt_account_id, | ||
maxResults=100, | ||
nextToken=next_token, | ||
filter={ | ||
"tagStatus": "TAGGED", | ||
}, | ||
) | ||
images = response["imageDetails"] | ||
response.get("nextToken") | ||
image_digests = [ | ||
{"imageDigest": image["imageDigest"]} | ||
for image in images | ||
if image["imagePushedAt"] < date and "2023" in image["imageTags"][0] | ||
] | ||
|
||
if not image_digests: | ||
return | ||
|
||
delete_ecr_images(repository, image_digests) | ||
print(f"Deleting {len(image_digests)} old development images") | ||
|
||
|
||
if __name__ == "__main__": | ||
for repository in getenv("ECR_REPOSITORIES").split(","): | ||
print(f"Cleaning up {repository}") | ||
repository_name = f"{repository_prefix}/{repository}" | ||
delete_old_task_images(repository=repository_name) | ||
delete_untagged_images(repository=repository_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
boto3 | ||
pytz |