-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
70 lines (56 loc) · 2.82 KB
/
server.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
import json
import os
import sys
import time
import zrok
from waitress import serve
# this script waits for the deployment to enable a zrok environment which produces a reserved.json file describing the
# zrok reserved share. From that file, we extract the zrok frontend endpoint and the share name (token). The token is
# the name of Ziti Service we need to bind, so that's passed to the ziti SDK as a bind option.
# make global dict so we can assign values in functions then use it in a decoration
zrok_opts = {}
# the port is fictitious because we're using ziti to bind to the service, so there are no listening ports. waitress
# requires a port, so we're using the port number only to integrate the ziti SDK with waitress.
WAITRESS_PORT = 8000
def waitForEnableAndReserve(home_dir: str):
start_time = time.time()
while time.time() - start_time < 60:
if os.path.exists(f'{home_dir}/.zrok/reserved.json'):
with open(f'{home_dir}/.zrok/reserved.json') as f:
try:
data = json.load(f)
except json.decoder.JSONDecodeError:
time.sleep(1)
continue
if 'frontend_endpoints' in data and data['frontend_endpoints']:
zrok_frontend = data['frontend_endpoints'][0].replace('https://', '')
if zrok_frontend.endswith('/'):
zrok_frontend = zrok_frontend[:-1]
# set the ZROK_FRONTEND environment variable so that settings.py can use it to configure allowed
# HTTP origins
os.environ['ZROK_FRONTEND'] = zrok_frontend
print(f"INFO: ZROK_FRONTEND is https://{os.environ['ZROK_FRONTEND']}/")
else:
raise Exception('Unable to find frontend_endpoints in reserved.json')
if 'token' in data and data['token']:
return data['token']
else:
raise Exception('Unable to find share token in reserved.json')
else:
time.sleep(1)
else: # executed if the loop ended normally (no break)
raise Exception('Timeout: Unable to find reserved.json within 60 seconds')
def configureZrok(token: str, home_dir: str):
os.environ['HOME'] = home_dir
zrok_root = zrok.environment.root.Load()
return zrok.decor.Opts(root=zrok_root, shrToken=token, bindPort=WAITRESS_PORT)
@zrok.decor.zrok(opts=zrok_opts)
def runServer():
from zrok_django_radial_calendar.wsgi import application
serve(application, port=WAITRESS_PORT)
if __name__ == '__main__':
# the filesystem path to zrok enable's HOME directory
zrok_home = sys.argv[1] # /mnt
share_token = waitForEnableAndReserve(zrok_home)
zrok_opts['cfg'] = configureZrok(token=share_token, home_dir=zrok_home)
runServer()