-
Notifications
You must be signed in to change notification settings - Fork 0
/
replacer.py
69 lines (60 loc) · 2.12 KB
/
replacer.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
from marathon import MarathonClient
import time
#
# import launching and updating scripts
#
import launcher, namespacer
import yaml
import ast
import uuid
import os
marathon_host = os.environ['MARATHON_HOST']
marathon_port = os.environ['MARATHON_PORT']
etcd_host = os.environ['ETCD_HOST_ADDRESS']
# data = yaml.load(open('mesos.yaml', 'r'))
# etcd_host = data['etcd']['host']
# marathon_host = data['marathon']['host']
# marathon_port = data['marathon']['port']
#
# replaces app with another app. many may be required for fixed host constraint
#
def rolling_replace_app(service_name, app1_id, app2_id, app2_config, labels):
print ' replacing '+app1_id+' with '+app2_id
marathon_client = MarathonClient('http://' + str(marathon_host) + ':' + str(marathon_port))
app1 = marathon_client.get_app(app1_id)
old_tasks = app1.tasks
# launcher.launch(group2.service.name, group2.encode_marathon_id, group2.config, instances = 0)
launcher.launch_app(service_name, app2_id, app2_config, labels, instances = 0 )
new_app = marathon_client.get_app(app2_id)
for old_task in old_tasks:
#
# replace each old task with a new task of the new app
#
num_started = num_started_tasks(app2_id)
new_instances = num_started+1
# add 1 instance of new task
launcher.update_app(app2_id, app2_config, new_instances)
while num_started < new_instances:
time.sleep(1)
print 'waiting for app to start '+str(num_started)
num_started = num_started_tasks(app2_id)
#
# take down old task
#
marathon_client.kill_task(app1_id, old_task.id, scale=True)
marathon_client.delete_app(app1_id)
def rolling_replace_group(service_name, labels, config, existing_apps):
for old_app_id in existing_apps:
# make new version
version = uuid.uuid4()
new_app_id = namespacer.encode_marathon_id(service_name, labels, version)
rolling_replace_app(service_name, old_app_id, new_app_id, config, labels)
def num_started_tasks(app_id):
count = 0
marathon_client = MarathonClient('http://' + str(marathon_host) + ':' + str(marathon_port))
app = marathon_client.get_app(app_id)
tasks = app.tasks
for task in tasks:
if task.started_at:
count += 1
return count