-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy_build.py
executable file
·147 lines (117 loc) · 4.27 KB
/
deploy_build.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python
import os
import sys
import time
import argparse
import boto3
import fabric
STACK_NAME = 'Frontdesk'
STACK_ASG = 'WebAutoScalingGroup'
BUILD_PARAMETER = '/Frontdesk/ApplicationBuild'
ASG_PROCESSES = [
'HealthCheck',
'ReplaceUnhealthy',
'AZRebalance',
'ScheduledActions',
'AlarmNotification']
def deploy_build(app_build):
# retrieve the stack and it's outputs
cloudformation = boto3.resource('cloudformation')
stack = cloudformation.Stack(STACK_NAME)
outputs = {i['OutputKey']: i['OutputValue'] for i in stack.outputs}
# determine the deployment bucket
s3 = boto3.resource('s3')
bucket = s3.Bucket(outputs['DeploymentBucket'])
# upload the application build
print("Uploading {0} to {1}".format(app_build, bucket.name))
application = os.path.basename(app_build)
bucket.upload_file(app_build, application)
# retrieve autoscaling group
asg = stack.Resource(STACK_ASG)
asg_name = asg.physical_resource_id
# suspend autoscaling processes
print("Suspend {0} processes".format(asg_name))
autoscaling = boto3.client('autoscaling')
autoscaling.suspend_processes(
AutoScalingGroupName=asg_name,
ScalingProcesses=ASG_PROCESSES)
# retrieve instances in autoscaling group
response = autoscaling.describe_auto_scaling_groups(
AutoScalingGroupNames=[asg_name])
ec2 = boto3.resource('ec2')
instance_data = response['AutoScalingGroups'][0]['Instances']
instances = [ec2.Instance(d['InstanceId']) for d in instance_data]
# update instances sequentially
for instance in instances:
# standby the instance
print("Moving instance {0} to standby".format(instance.id))
autoscaling.enter_standby(
AutoScalingGroupName=asg_name,
InstanceIds=[instance.id],
ShouldDecrementDesiredCapacity=True)
print("Waiting...", end='')
sys.stdout.flush()
time.sleep(3)
while True:
response = autoscaling.describe_auto_scaling_instances(
InstanceIds=[instance.id])
state = response['AutoScalingInstances'][0]['LifecycleState']
if state == 'Standby':
print("OK")
break
elif state != 'EnteringStandby':
print("FAILED\n{0}".format(state))
sys.exit(1)
print(".", end='')
sys.stdout.flush()
time.sleep(1)
# connect to the instance and run the update script
print("Updating instance...")
connection = fabric.Connection(instance.public_ip_address, user='ubuntu')
result = connection.run(' '.join([
'sudo', '-H', 'python3',
'/opt/frontdesk/deploy.py',
'--app-build', application
]))
# resume the instance
print("Moving instance {0} to service pool".format(instance.id))
autoscaling.exit_standby(
AutoScalingGroupName=asg_name,
InstanceIds=[instance.id])
print("Waiting...", end='')
sys.stdout.flush()
time.sleep(3)
while True:
response = autoscaling.describe_auto_scaling_instances(
InstanceIds=[instance.id])
state = response['AutoScalingInstances'][0]['LifecycleState']
if state == 'InService':
print("OK")
break
elif state != 'Pending':
print("FAILED\n{0}".format(state))
sys.exit(1)
print(".", end='')
sys.stdout.flush()
time.sleep(1)
# update the build parameter
print("Updating {0} to {1}".format(BUILD_PARAMETER, application))
ssm = boto3.client('ssm')
ssm.put_parameter(
Name=BUILD_PARAMETER,
Type='String',
Value=application,
Overwrite=True)
# resume autoscaling processes
print("Resume {0} processes".format(asg_name))
autoscaling.resume_processes(AutoScalingGroupName=asg_name)
if __name__ == '__main__':
# parse command line arguments
parser = argparse.ArgumentParser()
parser.add_argument(
'--app-build', '-a',
required=True,
help='application build')
args = parser.parse_args()
# deploy the build
deploy_build(args.app_build)