forked from openshift/os
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathami_uncopy_regions
28 lines (25 loc) · 1.27 KB
/
ami_uncopy_regions
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
#!/usr/bin/python3
import os,subprocess,json,sys
with open(sys.argv[1]) as f:
meta = json.load(f)
for ami in meta['amis']:
subprocess.check_call(['aws', 'ec2', f'--region={ami["name"]}', 'modify-image-attribute', '--image-id', ami['hvm'], '--launch-permission', 'Remove=[{Group=all}]'])
try:
output = subprocess.check_output(['aws', '--output=json', 'ec2', "--region", ami['name'], 'describe-images', '--image-id', ami['hvm']])
except subprocess.CalledProcessError as e:
print("Failed to describe image {}".format(e))
sys.exit(1)
image_description = json.loads(output)
snapshot = None
for image in image_description['Images']:
for block_device_mapping in image['BlockDeviceMappings']:
if block_device_mapping['Ebs']:
snapshot = block_device_mapping['Ebs']['SnapshotId']
break
if snapshot is None:
print("Failed to find SnapshotId associated with ami")
sys.exit(1)
subprocess.check_call(['aws', 'ec2', '--region', ami['name'], 'modify-snapshot-attribute',
'--snapshot-id', snapshot, '--attribute', 'createVolumePermission',
'--operation-type', 'remove', '--group-names', 'all'])
print(f"Done {ami} with snapshot ID {snapshot}")