Created
November 24, 2022 13:03
-
-
Save wusuopu/36f145de99056942f4f5e97412f3f6ae to your computer and use it in GitHub Desktop.
clean some aws ecr docker image
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
#!/usr/bin/env python | |
# encoding: utf-8 | |
import subprocess | |
import json | |
import logging | |
import os | |
import sys | |
logger = logging.getLogger('docker') | |
handler = logging.FileHandler('/tmp/ecr-clean.log') | |
# handler = logging.StreamHandler(sys.stdout) | |
handler.setLevel(logging.INFO) | |
handler.setFormatter(logging.Formatter('%(asctime)s - %(message)s')) | |
logger.setLevel(logging.INFO) | |
logger.addHandler(handler) | |
# aws_cli = 'aws' | |
aws_cli = 'docker run --rm -tiv %s/.aws:/root/.aws mikesir87/aws-cli aws' % (os.environ.get('HOME', '')) | |
def list_images(repository): | |
cmd = '%s ecr list-images --no-cli-pager --repository-name %s' % (aws_cli, repository) | |
logger.info('list_images: %s' % (cmd)) | |
filename = '/tmp/images-%s.json' % (repository) | |
with open(filename, 'w') as f: | |
p = subprocess.Popen( | |
cmd, | |
shell=True, | |
stdout=f, | |
stderr=sys.stderr | |
) | |
p.wait() | |
if p.returncode != 0: | |
logger.error('list_images error: %s' % (p.stderr.read())) | |
return [] | |
with open(filename, 'r') as f: | |
ret = json.load(f) | |
os.unlink(filename) | |
return ret.get('imageIds', []) | |
def delete_images(repository, all_tags): | |
if not all_tags: | |
return | |
count = len(all_tags) | |
limit = 100 | |
i = 0 | |
while i < count: | |
end = i + limit | |
tags = all_tags[i:end] | |
i = end | |
ids = " ".join(map(lambda t: 'imageTag=%s' % t, tags)) | |
logger.info('delete images: %s %s' % (repository, " ".join(tags))) | |
p = subprocess.Popen( | |
'%s ecr batch-delete-image --no-cli-pager --repository-name %s --image-ids %s' % (aws_cli, repository, ids), | |
shell=True, | |
stdout=subprocess.PIPE | |
) | |
p.wait() | |
if p.returncode != 0: | |
logger.error(p.stdout.read()) | |
def group_images(images): | |
tags = list(map(lambda t: (t['imageTag']), images)) | |
tags.sort() | |
images = { 'dev': [], 'prod': [], } | |
for tag in tags: | |
if tag.startswith('develop'): | |
images['dev'].append(tag) | |
if tag.startswith('production'): | |
images['prod'].append(tag) | |
images['dev'] = images['dev'][0:-20] | |
images['prod'] = images['prod'][0:-20] | |
return images | |
def main(): | |
repository_name = ['project1'] | |
for r in repository_name: | |
images = list_images(r) | |
tags = group_images(images) | |
delete_images(r, tags['dev']) | |
delete_images(r, tags['prod']) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment