-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathquarantine.py
41 lines (39 loc) · 1.32 KB
/
quarantine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import boto3
import json
import sys
import subprocess
# date_handler = lambda obj: (
# obj.isoformat()
# if isinstance(obj, (datetime.datetime, datetime.date))
# else None
# )
ecr = boto3.client('ecr')
s3 = boto3.resource('s3')
to_quarantine = []
with open(sys.argv[1]) as json_file:
to_quarantine = json.load(json_file)
for q in to_quarantine:
image_name = q['registryId'] + '.dkr.ecr.us-east-1.amazonaws.com/' + q['repositoryName'] + '@' + q['imageId']
subprocess.run(['docker', 'pull', image_name])
imageId = q['imageId'].split('sha256:')[1]
archive_name = imageId + '.tar'
with open(imageId + '.json', 'w') as outfile:
json.dump(q, outfile)
subprocess.run(['docker', 'save', image_name, '-o', archive_name])
s3.Bucket('10011-ecr-quarantine').upload_file(
Filename=imageId + '.json',
Key=q['registryId'] + '/' + q['repositoryName'] + '/' + imageId + '/' + imageId + '.json'
)
s3.Bucket('10011-ecr-quarantine').upload_file(
Filename=archive_name,
Key=q['registryId'] + '/' + q['repositoryName'] + '/' + imageId + '/' + archive_name
)
ecr.batch_delete_image(
registryId=q['registryId'],
repositoryName=q['repositoryName'],
imageIds=[
{
'imageDigest': q['imageId']
}
]
)