-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
168 lines (126 loc) · 4.76 KB
/
utils.py
File metadata and controls
168 lines (126 loc) · 4.76 KB
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
main
from flask import Request
import asyncio
import httpx
import random
import string
errInvalidRequest = "Invalid request"
errHostnameListInconsistent = "Hostname List Inconsistent"
def validateRequest(request: Request):
req = request.get_json()
if req['n'] is None or req['n'] <= 0:
return None, None, errInvalidRequest
if req['n'] > 0 and len(req['hostnames']) > req['n']:
return None, None, errHostnameListInconsistent
if req['hostnames'] is None:
return req['n'], [], None
return req['n'], req['hostnames'], None
async def fetch_url(url) -> bool:
async with httpx.AsyncClient() as client:
try:
response = await client.get(url, timeout=5)
response.raise_for_status()
return True
except:
return False
async def fetch_all_urls(urls) -> list[bool]:
tasks = [asyncio.create_task(fetch_url(url)) for url in urls]
responses = await asyncio.gather(*tasks)
return responses
async def get_server_health(servers: list[str]) -> list[str]:
urls = [f"http://{server}:5000/heartbeat" for server in servers]
responses = await fetch_all_urls(urls)
output: list[str] = []
for server, result in zip(servers, responses):
if result == True:
output.append(server)
return output
async def get_unhealty_servers(servers: list[str]) -> set[str]:
urls = [f"http://{server}:5000/heartbeat" for server in servers]
responses = await fetch_all_urls(urls)
output: set[str] = set()
for server, result in zip(servers, responses):
if result == False:
output.add(server)
return output
def get_container_run_command(hostname: str, network_name: str) -> list[str]:
output = ["sudo", "docker", "run",
"--name", hostname,
"--network", network_name,
"--network-alias", hostname,
"-e", f"SERVER_ID={hostname}",
"-d", "server"
]
return output
def get_container_rm_command(hostname: str) -> list[str]:
output = ["sudo", "docker", "rm", "-f", hostname]
return output
def get_random_name(length: int) -> str:
output = ''.join(random.choices(string.ascii_lowercase + string.digits, k=length))
return output
def get_random_number(length: int) -> int:
output = ''.join(random.choices(string.digits, k=length))
=======
from flask import Request
import asyncio
import httpx
import random
import string
errInvalidRequest = "Invalid request"
errHostnameListInconsistent = "Hostname List Inconsistent"
def validateRequest(request: Request):
req = request.get_json()
if req['n'] is None or req['n'] <= 0:
return None, None, errInvalidRequest
if req['n'] > 0 and len(req['hostnames']) > req['n']:
return None, None, errHostnameListInconsistent
if req['hostnames'] is None:
return req['n'], [], None
return req['n'], req['hostnames'], None
async def fetch_url(url) -> bool:
async with httpx.AsyncClient() as client:
try:
response = await client.get(url, timeout=5)
response.raise_for_status()
return True
except:
return False
async def fetch_all_urls(urls) -> list[bool]:
tasks = [asyncio.create_task(fetch_url(url)) for url in urls]
responses = await asyncio.gather(*tasks)
return responses
async def get_server_health(servers: list[str]) -> list[str]:
urls = [f"http://{server}:5000/heartbeat" for server in servers]
responses = await fetch_all_urls(urls)
output: list[str] = []
for server, result in zip(servers, responses):
if result == True:
output.append(server)
return output
async def get_unhealty_servers(servers: list[str]) -> set[str]:
urls = [f"http://{server}:5000/heartbeat" for server in servers]
responses = await fetch_all_urls(urls)
output: set[str] = set()
for server, result in zip(servers, responses):
if result == False:
output.add(server)
return output
def get_container_run_command(hostname: str, network_name: str) -> list[str]:
output = ["sudo", "docker", "run",
"--name", hostname,
"--network", network_name,
"--network-alias", hostname,
"-e", f"SERVER_ID={hostname}",
"-d", "server"
]
return output
def get_container_rm_command(hostname: str) -> list[str]:
output = ["sudo", "docker", "rm", "-f", hostname]
return output
def get_random_name(length: int) -> str:
output = ''.join(random.choices(string.ascii_lowercase + string.digits, k=length))
return output
def get_random_number(length: int) -> int:
output = ''.join(random.choices(string.digits, k=length))
main
return int(output)