forked from d2iq-archive/marathon-lb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluegreen_deploy.py
executable file
·470 lines (405 loc) · 17.5 KB
/
bluegreen_deploy.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#!/usr/bin/env python3
from common import *
from datetime import datetime
from io import StringIO
import argparse
import json
import requests
import csv
import time
import re
import math
import socket
import urllib
logger = logging.getLogger('bluegreen_deploy')
def query_yes_no(question, default="yes"):
# Thanks stackoverflow:
# https://stackoverflow.com/questions/3041986/python-command-line-yes-no-input
"""Ask a yes/no question via input() and return their answer.
"question" is a string that is presented to the user.
"default" is the presumed answer if the user just hits <Enter>.
It must be "yes" (the default), "no" or None (meaning
an answer is required of the user).
The "answer" return value is True for "yes" or False for "no".
"""
valid = {"yes": True, "y": True, "ye": True,
"no": False, "n": False}
if default is None:
prompt = " [y/n] "
elif default == "yes":
prompt = " [Y/n] "
elif default == "no":
prompt = " [y/N] "
else:
raise ValueError("invalid default answer: '%s'" % default)
while True:
sys.stdout.write(question + prompt)
choice = input().lower()
if default is not None and choice == '':
return valid[default]
elif choice in valid:
return valid[choice]
else:
sys.stdout.write("Please respond with 'yes' or 'no' "
"(or 'y' or 'n').\n")
def get_app_info(args, deployment_group, alt_port):
url = args.marathon + "/v2/apps"
response = requests.get(url, auth=get_marathon_auth_params(args))
response.raise_for_status()
apps = response.json()
existing_app = None
colour = 'blue'
next_port = alt_port
resuming = False
for app in apps['apps']:
if 'labels' in app and \
'HAPROXY_DEPLOYMENT_GROUP' in app['labels'] and \
'HAPROXY_DEPLOYMENT_COLOUR' in app['labels'] and \
app['labels']['HAPROXY_DEPLOYMENT_GROUP'] == deployment_group:
if existing_app is not None:
if args.resume:
logger.info("Found previous deployment, resuming")
resuming = True
if existing_app['labels']['HAPROXY_DEPLOYMENT_STARTED_AT']\
< app['labels']['HAPROXY_DEPLOYMENT_STARTED_AT']:
# stop here
break
else:
raise Exception("There appears to be an existing"
" deployment in progress")
prev_colour = app['labels']['HAPROXY_DEPLOYMENT_COLOUR']
prev_port = app['ports'][0]
existing_app = app
if prev_port == int(alt_port):
next_port = app['labels']['HAPROXY_0_PORT']
else:
next_port = alt_port
if prev_colour == 'blue':
colour = 'green'
else:
colour = 'blue'
return (colour, next_port, existing_app, resuming)
def get_hostports_from_backends(hmap, backends, haproxy_instance_count):
hostports = {}
regex = re.compile(r"^(\d+)_(\d+)_(\d+)_(\d+)_(\d+)$", re.IGNORECASE)
counts = {}
for backend in backends:
svname = backend[hmap['svname']]
if svname in counts:
counts[svname] += 1
else:
counts[svname] = 1
# Are all backends across all instances draining?
if counts[svname] == haproxy_instance_count:
m = regex.match(svname)
host = '.'.join(m.group(1, 2, 3, 4))
port = m.group(5)
if host in hostports:
hostports[host].append(int(port))
else:
hostports[host] = [int(port)]
return hostports
def find_tasks_to_kill(tasks, hostports):
tasks_to_kill = set()
for task in tasks:
if task['host'] in hostports:
for port in hostports[task['host']]:
if port in task['ports']:
tasks_to_kill.add(task['id'])
return list(tasks_to_kill)
def check_if_tasks_drained(args, app, existing_app, step_started_at):
time.sleep(args.step_delay)
url = args.marathon + "/v2/apps" + existing_app['id']
response = requests.get(url, auth=get_marathon_auth_params(args))
response.raise_for_status()
existing_app = response.json()['app']
url = args.marathon + "/v2/apps" + app['id']
response = requests.get(url, auth=get_marathon_auth_params(args))
response.raise_for_status()
app = response.json()['app']
target_instances = \
int(app['labels']['HAPROXY_DEPLOYMENT_TARGET_INSTANCES'])
logger.info("Existing app running {} instances, "
"new app running {} instances"
.format(existing_app['instances'], app['instances']))
url = args.marathon_lb
url = urllib.parse.urlparse(url)
# Have to find _all_ haproxy stats backends
addrs = socket.gethostbyname_ex(url.hostname)[2]
csv_data = ''
for addr in addrs:
try:
nexturl = \
urllib.parse.urlunparse((url[0],
addr + ":" + str(url.port),
url[2],
url[3],
url[4],
url[5]))
response = requests.get(nexturl + "/haproxy?stats;csv")
response.raise_for_status()
csv_data = csv_data + response.text
response = requests.get(nexturl + "/_haproxy_getpids")
response.raise_for_status()
pids = response.text.split()
if len(pids) > 1 and time.time() - step_started_at < args.max_wait:
# HAProxy has not finished reloading
logger.info("Waiting for {} pids on {}"
.format(len(pids), nexturl))
return check_if_tasks_drained(args,
app,
existing_app,
step_started_at)
except requests.exceptions.RequestException as e:
logger.exception("Caught exception when retrieving HAProxy"
" stats from " + nexturl)
return check_if_tasks_drained(args,
app,
existing_app,
step_started_at)
backends = []
f = StringIO(csv_data)
header = None
haproxy_instance_count = 0
for row in csv.reader(f, delimiter=',', quotechar="'"):
if row[0][0] == '#':
header = row
haproxy_instance_count += 1
continue
if row[0] == app['labels']['HAPROXY_DEPLOYMENT_GROUP'] + "_" + \
app['labels']['HAPROXY_0_PORT'] and \
row[1] != "BACKEND" and \
row[1] != "FRONTEND":
backends.append(row)
logger.info("Found {} app backends across {} HAProxy instances"
.format(len(backends), haproxy_instance_count))
# Create map of column names to idx
hmap = {}
for i in range(0, len(header)):
hmap[header[i]] = i
if (len(backends) / haproxy_instance_count) != \
app['instances'] + existing_app['instances']:
# HAProxy hasn't updated yet, try again
return check_if_tasks_drained(args,
app,
existing_app,
step_started_at)
up_backends = \
[b for b in backends if b[hmap['status']] == 'UP']
if (len(up_backends) / haproxy_instance_count) < target_instances:
# Wait until we're in a healthy state
return check_if_tasks_drained(args,
app,
existing_app,
step_started_at)
# Double check that current draining backends are finished serving requests
draining_backends = \
[b for b in backends if b[hmap['status']] == 'MAINT']
if (len(draining_backends) / haproxy_instance_count) < 1:
# No backends have started draining yet
return check_if_tasks_drained(args,
app,
existing_app,
step_started_at)
for backend in draining_backends:
# Verify that the backends have no sessions or pending connections.
# This is likely overkill, but we'll do it anyway to be safe.
if int(backend[hmap['qcur']]) > 0 or int(backend[hmap['scur']]) > 0:
# Backends are not yet drained.
return check_if_tasks_drained(args,
app,
existing_app,
step_started_at)
# If we made it here, all the backends are drained and we can start
# slaughtering tasks, with prejudice
hostports = get_hostports_from_backends(hmap,
draining_backends,
haproxy_instance_count)
tasks_to_kill = find_tasks_to_kill(existing_app['tasks'], hostports)
logger.info("There are {} drained backends, "
"about to kill & scale for these tasks:\n{}"
.format(len(tasks_to_kill), "\n".join(tasks_to_kill)))
if app['instances'] == target_instances and \
len(tasks_to_kill) == existing_app['instances']:
logger.info("About to delete old app {}".format(existing_app['id']))
if args.force or query_yes_no("Continue?"):
url = args.marathon + "/v2/apps" + existing_app['id']
response = requests.delete(url,
auth=get_marathon_auth_params(args))
response.raise_for_status()
return True
else:
return False
if args.force or query_yes_no("Continue?"):
# Scale new app up
instances = math.floor(app['instances'] + (app['instances'] + 1) / 2)
if instances >= existing_app['instances']:
instances = target_instances
logger.info("Scaling new app up to {} instances".format(instances))
url = args.marathon + "/v2/apps" + app['id']
data = json.dumps({'instances': instances})
headers = {'Content-Type': 'application/json'}
response = requests.put(url, headers=headers, data=data,
auth=get_marathon_auth_params(args))
response.raise_for_status()
# Scale old app down
logger.info("Scaling down old app by {} instances"
.format(len(tasks_to_kill)))
data = json.dumps({'ids': tasks_to_kill})
url = args.marathon + "/v2/tasks/delete?scale=true"
response = requests.post(url, headers=headers, data=data,
auth=get_marathon_auth_params(args))
response.raise_for_status()
return check_if_tasks_drained(args,
app,
existing_app,
time.time())
return False
def start_deployment(args, app, existing_app, resuming):
if not resuming:
url = args.marathon + "/v2/apps"
data = json.dumps(app)
headers = {'Content-Type': 'application/json'}
response = requests.post(url, headers=headers, data=data,
auth=get_marathon_auth_params(args))
response.raise_for_status()
if existing_app is not None:
return check_if_tasks_drained(args,
app,
existing_app,
time.time())
return False
def get_service_port(app):
if 'container' in app and \
'docker' in app['container'] and \
'portMappings' in app['container']['docker']:
portMappings = app['container']['docker']['portMappings']
# Just take the first servicePort
return portMappings[0]['servicePort']
return app['ports'][0]
def set_service_port(app, port):
if 'container' in app and \
'docker' in app['container'] and \
'portMappings' in app['container']['docker']:
app['container']['docker']['portMappings'][0]['servicePort'] = \
int(port)
return app
app['ports'][0] = int(servicePort)
return app
def process_json(args, out=sys.stdout):
with open(args.json, 'r') as content_file:
content = content_file.read()
app = json.loads(content)
app_id = app['id']
if app_id is None:
raise Exception("App doesn't contain a valid App ID")
if 'labels' not in app:
raise Exception("No labels found. Please define the"
"HAPROXY_DEPLOYMENT_GROUP label"
)
if 'HAPROXY_DEPLOYMENT_GROUP' not in app['labels']:
raise Exception("Please define the "
"HAPROXY_DEPLOYMENT_GROUP label"
)
if 'HAPROXY_DEPLOYMENT_ALT_PORT' not in app['labels']:
raise Exception("Please define the "
"HAPROXY_DEPLOYMENT_ALT_PORT label"
)
deployment_group = app['labels']['HAPROXY_DEPLOYMENT_GROUP']
alt_port = app['labels']['HAPROXY_DEPLOYMENT_ALT_PORT']
app['labels']['HAPROXY_APP_ID'] = app_id
service_port = get_service_port(app)
(colour, port, existing_app, resuming) = \
get_app_info(args, deployment_group, alt_port)
app = set_service_port(app, port)
app['id'] = app_id + "-" + colour
if app['id'][0] != '/':
app['id'] = '/' + app['id']
if existing_app is not None:
app['instances'] = args.initial_instances
app['labels']['HAPROXY_DEPLOYMENT_TARGET_INSTANCES'] = \
str(existing_app['instances'])
else:
app['labels']['HAPROXY_DEPLOYMENT_TARGET_INSTANCES'] = \
str(app['instances'])
app['labels']['HAPROXY_DEPLOYMENT_COLOUR'] = colour
app['labels']['HAPROXY_DEPLOYMENT_STARTED_AT'] = datetime.now().isoformat()
app['labels']['HAPROXY_0_PORT'] = str(service_port)
logger.info('Final app definition:')
out.write(json.dumps(app, sort_keys=True, indent=2))
out.write("\n")
if args.dry_run:
return
if args.force or query_yes_no("Continue with deployment?"):
start_deployment(args, app, existing_app, resuming)
def get_arg_parser():
parser = argparse.ArgumentParser(
description="Marathon HAProxy Load Balancer",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--longhelp",
help="Print out configuration details",
action="store_true"
)
parser.add_argument("--marathon", "-m",
help="[required] Marathon endpoint, eg. -m " +
"http://marathon1:8080"
)
parser.add_argument("--marathon-lb", "-l",
help="[required] Marathon-lb stats endpoint, eg. -l " +
"http://marathon-lb.marathon.mesos:9090"
)
parser.add_argument("--json", "-j",
help="[required] App JSON"
)
parser.add_argument("--dry-run", "-d",
help="Perform a dry run",
action="store_true"
)
parser.add_argument("--force", "-f",
help="Perform deployment un-prompted",
action="store_true"
)
parser.add_argument("--step-delay", "-s",
help="Delay (in seconds) between each successive"
" deployment step",
type=int, default=5
)
parser.add_argument("--initial-instances", "-i",
help="Initial number of app instances to launch",
type=int, default=1
)
parser.add_argument("--resume", "-r",
help="Resume from a previous deployment",
action="store_true"
)
parser.add_argument("--max-wait", "-w",
help="Maximum amount of time (in seconds) to wait"
" for HAProxy to drain connections",
type=int, default=300
)
parser = set_logging_args(parser)
parser = set_marathon_auth_args(parser)
return parser
if __name__ == '__main__':
# Process arguments
arg_parser = get_arg_parser()
args = arg_parser.parse_args()
# Print the long help text if flag is set
if args.longhelp:
print(__doc__)
sys.exit()
# otherwise make sure that a Marathon URL was specified
else:
if args.marathon is None:
arg_parser.error('argument --marathon/-m is required')
if args.marathon_lb is None:
arg_parser.error('argument --marathon-lb/-l is required')
if args.json is None:
arg_parser.error('argument --json/-j is required')
# Set request retries
s = requests.Session()
a = requests.adapters.HTTPAdapter(max_retries=3)
s.mount('http://', a)
# Setup logging
setup_logging(logger, args.syslog_socket, args.log_format)
process_json(args)