This repository has been archived by the owner on Jan 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwait-for-agency.py
executable file
·71 lines (57 loc) · 1.9 KB
/
wait-for-agency.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
#!/usr/bin/env python3
import psutil
import sys
import time
import requests
def get_cmdline_value(cmdline, key):
try:
index = cmdline.index(key)
return cmdline[index+1]
except ValueError:
return None
def wait_for_agency_endpoint():
# get a list of all current processes
known = set()
while True:
time.sleep(0.5)
current = set(psutil.pids())
new = current.difference(known)
for pid in new:
p = psutil.Process(pid)
if p.name() == "arangod" or p.name() == "arangod.exe":
cmdline = p.cmdline()
if get_cmdline_value(cmdline, "--agency.activate") == "true":
return get_cmdline_value(cmdline, "--agency.my-address")
known = current
def wait_for_leader(endpoint):
while True:
try:
response = requests.get(f"{endpoint}/_api/agency/config")
if response.status_code == 200:
config = response.json()
if config.get('leaderId'):
return config['configuration']['pool'][config['leaderId']]
else:
print("leader not yet available", file=sys.stderr)
else:
print(f"status code = {response.status_code}", file=sys.stderr)
except Exception as e:
print(f"exception: {e}", file=sys.stderr)
time.sleep(0.5)
def fix_endpoint_url(url):
if url.startswith("tcp://"):
return f"http://{url[6:]}"
elif url.startswith("ssl://"):
return f"https://{url[6:]}"
else:
return url
def main():
# wait for the next agency process
endpoint = wait_for_agency_endpoint()
endpoint = fix_endpoint_url(endpoint)
# wait for the agency to have formed
endpoint = wait_for_leader(endpoint)
endpoint = fix_endpoint_url(endpoint)
print(endpoint)
if __name__ == "__main__":
main()