forked from ContainerSolutions/construct
/
launch.py
93 lines (74 loc) · 2.94 KB
/
launch.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
import random
from time import sleep
import pprint
import requests
from construct import ApiConnector, get_json
from async_framework import Framework
from Queue import Queue
pretty = pprint.PrettyPrinter(indent=2)
API_V1 = '/api/v1/scheduler'
DOCKER_JSON = "./resources/container.json"
LAUNCH_JSON = "./resources/launch.json"
TASK_RESOURCES_JSON = "./resources/task_resources.json"
class Launcher:
def __init__(self, master_url):
self.conn = None
self.background_thread = None
self.master_url = master_url
self.api_url = '{}/{}'.format(master_url, API_V1)
def connect(self):
r = requests.get("{}/state.json".format(self.master_url))
self.conn = ApiConnector()
#self.background_thread = self.conn.register_framework()
self.framework = Framework(self.conn, self.api_url)
self.framework.start()
def wait_for_offers(self):
count = 0
result = False
while not self.conn.framework_id and count < 10:
sleep(3)
print('.')
count += 1
if not self.conn.framework_id:
print("Failed to register, terminating Framework")
self.background_thread.close_channel()
else:
count = 0
while not self.conn.offers and count < 10:
print('.')
sleep(3)
count += 1
if not self.conn.offers:
print("Failed to obtain resources, terminating Framework")
self.conn.terminate_framework(self.conn.framework_id)
self.background_thread.close_channel()
else:
print("Got offers:")
pretty.pprint(self.conn.offers)
result = True
return result
def launch(self):
for i in range(0, len(self.conn.offers)):
print("Starting offer ", i)
offer = self.conn.offers.get('offers')[i]
launch_json = get_json(LAUNCH_JSON)
task_id = str(random.randint(100, 1000))
launch_json["accept"]["offer_ids"].append(offer["id"])
launch_json["framework_id"]["value"] = self.conn.framework_id
task_infos = launch_json["accept"]["operations"][0]["launch"]["task_infos"][0]
task_infos["task_id"]["value"] = task_id
task_infos["command"]["value"] = "cd /var/local/www && /usr/bin/python -m SimpleHTTPServer 9000"
task_infos["agent_id"]["value"] = self.conn.offers.get('offers')[0]["agent_id"]["value"]
task_infos["resources"] = get_json(TASK_RESOURCES_JSON)
q = Queue(1)
self.framework.request("launch", launch_json, q)
r = q.get()
print("reply: {}".format(r))
def main():
launcher = Launcher("http://192.168.33.10:5050")
launcher.connect()
if launcher.wait_for_offers():
launcher.launch()
launcher.background_thread.join()
if __name__ == '__main__':
main()