forked from meilisearch/meilisearch-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish_image.py
57 lines (49 loc) · 2.17 KB
/
publish_image.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import boto3
import config
import utils
AWS_REGION_AMIS = {}
UNSUCCESSFUL_AWS_REGION_AMIS = {}
# Copy AMI to different AWS regions
print('Triggering AMI propagation worldwide...')
for aws_region in config.AWS_REGIONS:
client = boto3.client('ec2', aws_region)
response = client.copy_image(
Name=config.SNAPSHOT_NAME,
Description=config.IMAGE_DESCRIPTION_NAME,
Encrypted=False,
SourceImageId=config.PUBLISH_IMAGE_ID,
SourceRegion=config.AWS_DEFAULT_REGION
)
if response['ResponseMetadata']['HTTPStatusCode'] == 200:
AWS_REGION_AMIS[aws_region] = response['ImageId']
print(' AMI copy triggered: {} - {}'.format(aws_region,
response['ImageId']))
else:
print(' Error: AMI could not be created for: {}.'.format(aws_region))
print(' {}'.format(response['ResponseMetadata']['HTTPStatusCode']))
# Wait for propagated AMIs creation
print('Waiting for each AWS region AMI creation...')
for region, propagated_ami in AWS_REGION_AMIS.items():
state_code, ami = utils.wait_for_ami_available(propagated_ami, region)
if state_code == utils.STATUS_OK:
print(' AMI created: {} - {}'.format(region, propagated_ami))
else:
print(' Error: {} - {}.'.format(region, propagated_ami))
del AWS_REGION_AMIS[region]
UNSUCCESSFUL_AWS_REGION_AMIS[region] = propagated_ami
# Make propagated AMIs public
print('Making each AMI Public...')
for region, propagated_ami in AWS_REGION_AMIS.items():
state_code, public = utils.make_ami_public(propagated_ami, region)
if state_code == utils.STATUS_OK:
print(' AMI published: {} - {}'.format(region, propagated_ami))
else:
print(' Error: {} - {}.'.format(region, propagated_ami))
del AWS_REGION_AMIS[region]
UNSUCCESSFUL_AWS_REGION_AMIS[region] = propagated_ami
print('Successfully created {} AMIs:'.format(len(AWS_REGION_AMIS)))
for region, propagated_ami in AWS_REGION_AMIS.items():
print(' {}'.format(region))
print('Error creating {} AMIs:'.format(len(UNSUCCESSFUL_AWS_REGION_AMIS)))
for region, propagated_ami in UNSUCCESSFUL_AWS_REGION_AMIS.items():
print(' {}'.format(region))