forked from meilisearch/meilisearch-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_image.py
92 lines (74 loc) · 2.64 KB
/
build_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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import sys
import boto3
import utils
import config
ec2 = boto3.resource('ec2', config.AWS_DEFAULT_REGION)
# Create EC2 instance to setup MeiliSearch
print('Creating AWS EC2 instance')
instances = ec2.create_instances(
ImageId=config.DEBIAN_BASE_IMAGE_ID,
MinCount=1,
MaxCount=1,
InstanceType=config.INSTANCE_TYPE,
SecurityGroups=[
config.SECURITY_GROUP,
],
UserData=config.USER_DATA
)
print(' Instance created. ID: {}'.format(instances[0].id))
# Wait for EC2 instance to be 'running'
print('Waiting for AWS EC2 instance state to be "running"')
instance = ec2.Instance(instances[0].id)
state_code, state = utils.wait_for_instance_running(
instance, config.AWS_DEFAULT_REGION, timeout_seconds=600)
print(' Instance state: {}'.format(instance.state['Name']))
if state_code == utils.STATUS_OK:
print(' Instance IP: {}'.format(instance.public_ip_address))
else:
print(' Error: {}. State: {}.'.format(state_code, state))
utils.terminate_instance_and_exit(instance)
# Wait for Health check after configuration is finished
print('Waiting for MeiliSearch health check (may take a few minutes: config and reboot)')
HEALTH = utils.wait_for_health_check(instance, timeout_seconds=600)
if HEALTH == utils.STATUS_OK:
print(' Instance is healthy')
else:
print(' Timeout waiting for health check')
utils.terminate_instance_and_exit(instance)
# Check version
print('Waiting for Version check')
try:
utils.check_meilisearch_version(
instance, config.MEILI_CLOUD_SCRIPTS_VERSION_TAG)
except Exception as err:
print(" Exception: {}".format(err))
utils.terminate_instance_and_exit(instance)
print(' Version of meilisearch match!')
# Create AMI Image
if len(sys.argv) > 1:
AMI_BUILD_NAME = sys.argv[1]
else:
AMI_BUILD_NAME = config.AMI_BUILD_NAME
print('Triggering AMI Image creation...')
image = boto3.client('ec2', config.AWS_DEFAULT_REGION).create_image(
InstanceId=instance.id,
Name=AMI_BUILD_NAME,
Description='Meilisearch {} running on {}.'.format(
config.MEILI_CLOUD_SCRIPTS_VERSION_TAG,
config.BASE_OS_NAME
)
)
print(' AMI creation triggered: {}'.format(image['ImageId']))
# Wait for AMI creation
print('Waiting for AMI creation...')
state_code, ami = utils.wait_for_ami_available(
image['ImageId'], config.AWS_DEFAULT_REGION)
if state_code == utils.STATUS_OK:
print(' AMI created: {}'.format(image['ImageId']))
else:
print(' Error: {}. State: {}.'.format(state_code, ami.state))
utils.terminate_instance_and_exit(instance)
# Terminate EC2 Instance
print('Terminating instance...')
instance.terminate()
print(' Instance terminated')