forked from jupyter/tmpnb
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathorchestrate.py
277 lines (231 loc) · 9.24 KB
/
orchestrate.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import datetime
import json
import os
import re
import uuid
from concurrent.futures import ThreadPoolExecutor
import tornado
import tornado.options
from tornado.log import app_log
from tornado.web import RequestHandler
from tornado import gen, web
import dockworker
import spawnpool
class LoadingHandler(RequestHandler):
def get(self, path=None):
if self.allow_origin:
self.set_header("Access-Control-Allow-Origin", self.allow_origin)
self.render("loading.html", path=path)
@property
def allow_origin(self):
return self.settings['allow_origin']
class StatsHandler(RequestHandler):
def get(self):
'''Returns some statistics/metadata about the tmpnb server'''
response = {
'available': len(self.pool.available),
'capacity': self.pool.capacity,
'version': '0.0.1-dev',
}
if self.allow_origin:
self.set_header("Access-Control-Allow-Origin", self.allow_origin)
self.write(response)
@property
def pool(self):
return self.settings['pool']
@property
def allow_origin(self):
return self.settings['allow_origin']
class SpawnHandler(RequestHandler):
@gen.coroutine
def get(self, path=None):
'''Spawns a brand new server'''
if self.allow_origin:
self.set_header("Access-Control-Allow-Origin", self.allow_origin)
try:
if path is None:
# No path. Assign a prelaunched container from the pool and redirect to it.
# Append self.redirect_uri to the redirect target.
container_path = self.pool.acquire().path
app_log.info("Allocated [%s] from the pool.", container_path)
url = "/{}/{}".format(container_path, self.redirect_uri)
else:
# Split /user/{some_user}/long/url/path and acquire {some_user}
path_parts = path.lstrip('/').split('/', 2)
user = path_parts[1]
# Scrap a container from the pool and replace it with an ad-hoc replacement.
# This takes longer, but is necessary to support ad-hoc containers
yield self.pool.adhoc(user)
url = path
app_log.debug("Redirecting [%s] -> [%s].", self.request.path, url)
self.redirect(url, permanent=False)
except spawnpool.EmptyPoolError:
app_log.warning("The container pool is empty!")
self.render("full.html", cull_period=self.cull_period)
@property
def pool(self):
return self.settings['pool']
@property
def cull_period(self):
return self.settings['cull_period']
@property
def redirect_uri(self):
return self.settings['redirect_uri']
@property
def allow_origin(self):
return self.settings['allow_origin']
class APISpawnHandler(RequestHandler):
@gen.coroutine
def post(self):
'''Spawns a brand new server programatically'''
if self.allow_origin:
self.set_header("Access-Control-Allow-Origin", self.allow_origin)
try:
url = self.pool.acquire().path
app_log.info("Allocated [%s] from the pool.", url)
app_log.debug("Responding with container url [%s].", url)
self.write({'url': url})
except spawnpool.EmptyPoolError:
app_log.warning("The container pool is empty!")
self.write({'status': 'full'})
@property
def pool(self):
return self.settings['pool']
@property
def allow_origin(self):
return self.settings['allow_origin']
def main():
tornado.options.define('cull_period', default=600,
help="Interval (s) for culling idle containers."
)
tornado.options.define('cull_timeout', default=3600,
help="Timeout (s) for culling idle containers."
)
tornado.options.define('container_ip', default='127.0.0.1',
help="IP address for containers to bind to"
)
tornado.options.define('container_port', default='8888',
help="Port for containers to bind to"
)
command_default = (
'ipython3 notebook --no-browser'
' --port {port} --ip=0.0.0.0'
' --NotebookApp.base_url=/{base_path}'
)
tornado.options.define('command', default=command_default,
help="command to run when booting the image. A placeholder for base_path should be provided."
)
tornado.options.define('port', default=9999,
help="port for the main server to listen on"
)
tornado.options.define('ip', default=None,
help="ip for the main server to listen on [default: all interfaces]"
)
tornado.options.define('max_dock_workers', default=2,
help="Maximum number of docker workers"
)
tornado.options.define('mem_limit', default="512m",
help="Limit on Memory, per container"
)
tornado.options.define('cpu_shares', default=None,
help="Limit CPU shares, per container"
)
tornado.options.define('image', default="jupyter/minimal",
help="Docker container to spawn for new users. Must be on the system already"
)
tornado.options.define('docker_version', default="1.13",
help="Version of the Docker API to use"
)
tornado.options.define('redirect_uri', default="/tree",
help="URI to redirect users to upon initial notebook launch"
)
tornado.options.define('pool_size', default=10,
help="Capacity for containers on this system. Will be prelaunched at startup."
)
tornado.options.define('pool_name', default=None,
help="Container name fragment used to identity containers that belong to this instance."
)
tornado.options.define('static_files', default=None,
help="Static files to extract from the initial container launch"
)
tornado.options.define('allow_origin', default=None,
help="Set the Access-Control-Allow-Origin header. Use '*' to allow any origin to access."
)
tornado.options.parse_command_line()
opts = tornado.options.options
handlers = [
(r"/", LoadingHandler),
(r"/spawn/?(/user/\w+(?:/.*)?)?", SpawnHandler),
(r"/api/spawn/", APISpawnHandler),
(r"/(user/\w+)(?:/.*)?", LoadingHandler),
(r"/stats", StatsHandler),
]
proxy_token = os.environ['CONFIGPROXY_AUTH_TOKEN']
proxy_endpoint = os.environ.get('CONFIGPROXY_ENDPOINT', "http://127.0.0.1:8001")
docker_host = os.environ.get('DOCKER_HOST', 'unix://var/run/docker.sock')
max_age = datetime.timedelta(seconds=opts.cull_timeout)
pool_name = opts.pool_name
if pool_name is None:
# Derive a valid container name from the image name by default.
pool_name = re.sub('[^a-zA-Z0_.-]+', '', opts.image.split(':')[0])
container_config = dockworker.ContainerConfig(
image=opts.image,
command=opts.command,
mem_limit=opts.mem_limit,
cpu_shares=opts.cpu_shares,
container_ip=opts.container_ip,
container_port=opts.container_port,
)
spawner = dockworker.DockerSpawner(docker_host,
version=opts.docker_version,
timeout=30,
max_workers=opts.max_dock_workers,
)
static_path = os.path.join(os.path.dirname(__file__), "static")
pool = spawnpool.SpawnPool(proxy_endpoint=proxy_endpoint,
proxy_token=proxy_token,
spawner=spawner,
container_config=container_config,
capacity=opts.pool_size,
max_age=max_age,
static_files=opts.static_files,
static_dump_path=static_path,
pool_name=pool_name,
)
ioloop = tornado.ioloop.IOLoop().instance()
settings = dict(
static_path=static_path,
cookie_secret=uuid.uuid4(),
xsrf_cookies=False,
debug=True,
cull_period=opts.cull_period,
allow_origin=opts.allow_origin,
spawner=spawner,
pool=pool,
autoescape=None,
proxy_token=proxy_token,
template_path=os.path.join(os.path.dirname(__file__), 'templates'),
proxy_endpoint=proxy_endpoint,
redirect_uri=opts.redirect_uri.lstrip('/'),
)
# Synchronously cull any existing, inactive containers, and pre-launch a set number of
# containers, ready to serve.
ioloop.run_sync(pool.heartbeat)
if(opts.static_files):
ioloop.run_sync(pool.copy_static)
# Periodically execute a heartbeat function to cull used containers and regenerated failed
# ones, self-healing the cluster.
cull_ms = opts.cull_period * 1e3
app_log.info("Culling containers unused for %i seconds every %i seconds.",
opts.cull_timeout,
opts.cull_period)
culler = tornado.ioloop.PeriodicCallback(pool.heartbeat, cull_ms)
culler.start()
app_log.info("Listening on {}:{}".format(opts.ip or '*', opts.port))
application = tornado.web.Application(handlers, **settings)
application.listen(opts.port, opts.ip)
ioloop.start()
if __name__ == "__main__":
main()