-
Notifications
You must be signed in to change notification settings - Fork 1
/
cht_jupyter.py
executable file
·468 lines (375 loc) · 16.3 KB
/
cht_jupyter.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#!/usr/bin/env python
"""
Launch Jupyter Notebook within a Docker notebook image and
automatically open up the URL in the default web browser. It also
sets up port forwarding for ssh and X11 forwarding.
"""
# Author: Xiangmin Jiao <[email protected]>
from __future__ import print_function
import sys
import subprocess
import time
import os
owner = "paralab"
proj = "cht-coupler"
image = owner + '/' + proj
projdir = "project"
workdir = "project"
volume = '_DEFAULT_'
config = proj + '_config'
def parse_args(description):
"Parse command-line arguments"
import argparse
# Process command-line arguments
parser = argparse.ArgumentParser(description=description)
tag = "latest"
parser.add_argument('-i', '--image',
help='The Docker image to use. ' +
'The default is ' + image + '.',
default=image)
parser.add_argument('-t', '--tag',
help='Tag of the image. The default is latest. ' +
'If the image already has a tag, its tag prevails.',
default=tag)
parser.add_argument('-v', '--volume',
help='A data volume to be mounted at ~/" + projdir + ". ' +
'The default is ' + proj + '_<tag>_project.',
default=volume)
parser.add_argument('-w', '--workdir',
help='The starting work directory in container. ' +
'The default is ~/' + workdir + '.',
default=workdir)
parser.add_argument('-p', '--pull',
help='Pull the latest Docker image. ' +
'The default is not to pull.',
action='store_true',
default=False)
parser.add_argument('-r', '--reset',
help='Reset configurations to default.',
action='store_true',
default=False)
parser.add_argument('-c', '--clear',
help='Clear the project data volume (please use with caution).',
action='store_true',
default=False)
parser.add_argument('-d', '--detach',
help='Run in background and print container id',
action='store_true',
default=False)
parser.add_argument('-n', '--no-browser',
help='Do not start web browser',
action='store_true',
default=False)
parser.add_argument('-N', '--nvidia',
help='Mount the Nvidia card for GPU computation. ' +
'(Linux only, experimental, sudo required).',
action='store_true',
default="")
parser.add_argument('-V', '--verbose',
help='Enable verbose mode and print debug info to stderr.',
action='store_true',
default=False)
parser.add_argument('-q', '--quiet',
help='Disable screen output (some Docker output cannot be disabled).',
action='store_true',
default=False)
parser.add_argument('-A', '--args',
help='Additional arguments for the "docker run" command. ' +
'Useful for specifying additional resources or environment variables.',
default="")
parser.add_argument('-J', '--jupyter',
help='Additional arguments for jupyter-notebook.',
default="")
parser.add_argument('notebook', nargs='?',
help='The notebook to open.', default="")
args = parser.parse_args()
# Append tag to image if the image has no tag
if args.image.find(':') < 0:
if not args.tag:
pass
else:
args.image += ':' + args.tag
tag = args.tag
else:
tag = args[args.image.find(':')+1:]
if args.volume == '_DEFAULT_':
args.volume = proj + '_' + tag + '_project'
return args
def random_ports(port, n):
"""Generate a list of n random ports near the given port.
The first 5 ports will be sequential, and the remaining n-5 will be
randomly selected in the range [port-2*n, port+2*n].
"""
import random
for i in range(min(5, n)):
yield port + i
for i in range(n - 5):
yield max(1, port + random.randint(-2 * n, 2 * n))
def id_generator(size=6):
"""Generate a container ID"""
import random
import string
chars = string.ascii_lowercase
return proj + "-" + (''.join(random.choice(chars) for _ in range(size)))
def get_local_ip():
"Get the local IP address"
import socket
# https://stackoverflow.com/questions/166506/finding-local-ip-addresses
return [l for l in ([ip for ip in
socket.gethostbyname_ex(socket.gethostname())[2]
if not ip.startswith("127.")][:1],
[[(s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close())
for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]])
if l][0][0]
def find_free_port(port, retries):
"Find a free port"
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
for prt in random_ports(port, retries + 1):
try:
sock.bind(("127.0.0.1", prt))
sock.close()
return prt
except socket.error:
continue
return ''
def handle_interrupt(container):
"""Handle keyboard interrupt"""
try:
print("Press Ctrl-C again to stop the server: ")
time.sleep(5)
print('Invalid response. Resuming...')
except KeyboardInterrupt:
print('*** Stopping the server.')
subprocess.Popen(["docker", "exec", container,
"killall", "my_init"],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
sys.exit(0)
if __name__ == "__main__":
import webbrowser
import platform
import re
import glob
args = parse_args(description=__doc__)
if args.quiet:
def print(*args, **kwargs):
"Do nothing"
pass
def stdout_write(*args, **kwargs):
"Do nothing"
pass
def stderr_write(*args, **kwargs):
"Do nothing"
pass
else:
def stdout_write(*args, **kwargs):
"Call sys.stderr.write"
sys.stdout.write(*args, **kwargs)
def stderr_write(*args, **kwargs):
"Call sys.stderr.write"
sys.stderr.write(*args, **kwargs)
pwd = os.getcwd()
homedir = os.path.expanduser('~')
if platform.system() == "Linux":
if subprocess.check_output(['groups']).find(b'docker') < 0:
print('You are not a member of the docker group. Please add')
print('yourself to the docker group using the following command:')
print(' sudo addgroup $USER docker')
print('Then, log out and log back in before you can use Docker.')
sys.exit(-1)
uid = str(os.getuid())
if uid == '0':
print('You are running as root. This is not safe. ' +
'Please run as a regular user.')
sys.exit(-1)
else:
uid = ""
try:
if args.verbose:
stdout_write("Check whether Docker is up and running.\n")
img = subprocess.check_output(['docker', 'images', '-q', args.image])
except:
stderr_write("Docker failed. Please make sure docker was properly " +
"installed and has been started.\n")
sys.exit(-1)
if args.pull or not img:
try:
if args.verbose:
stdout_write("Pulling latest docker image " +
args.image + '.\n')
err = subprocess.call(["docker", "pull", args.image])
except BaseException:
err = -1
if err:
sys.exit(err)
# Delete dangling image
if img and subprocess.check_output(['docker', 'images', '-f',
'dangling=true',
'-q']).find(img) >= 0:
subprocess.Popen(["docker", "rmi", "-f", img.decode('utf-8')[:-1]])
docker_user = "ubuntu"
docker_home = "/home/" + docker_user
if args.reset:
try:
if args.verbose:
stdout_write("Removing old docker volume " + config + ".\n")
output = subprocess.check_output(
["docker", "volume", "rm", "-f", config])
except subprocess.CalledProcessError as e:
stderr_write(e.output.decode('utf-8'))
volumes = ["-v", pwd + ":" + docker_home + "/shared",
"-v", config + ":" + docker_home + "/.config"]
if os.path.exists(homedir + "/.gnupg"):
volumes += ["-v", homedir + "/.gnupg" +
":" + docker_home + "/.gnupg"]
# Mount .gitconfig to Docker image
if os.path.isfile(homedir + "/.gitconfig"):
volumes += ["-v", homedir + "/.gitconfig" +
":" + docker_home + "/.gitconfig_host"]
if args.volume:
if args.clear:
try:
if args.verbose:
stdout_write(
"Removing old docker volume " + config + ".\n")
output = subprocess.check_output(["docker", "volume",
"rm", "-f", args.volume])
except subprocess.CalledProcessError as e:
stderr_write(e.output.decode('utf-8'))
volumes += ["-v", args.volume + ":" + docker_home + "/" + projdir]
if args.workdir[0] == '/':
volumes += ["-w", args.workdir]
else:
volumes += ["-w", docker_home + "/" + args.workdir]
stderr_write("Starting up docker image...\n")
if subprocess.check_output(["docker", "--version"]). \
find(b"Docker version 1.") >= 0:
rmflag = "-t"
else:
rmflag = "--rm"
# Generate a container ID
container = id_generator()
envs = ["--hostname", container,
"--env", "HOST_UID=" + uid]
# Find a free port for ssh tunning
port_ssh = str(find_free_port(2222, 50))
if not port_ssh:
stderr_write("Error: Could not find a free port.\n")
sys.exit(-1)
envs += ["-p", port_ssh + ":22"]
# Create directory .ssh if not exist
if not os.path.exists(homedir + "/.ssh"):
os.mkdir(homedir + "/.ssh")
volumes += ["-v", homedir + "/.ssh" + ":" + docker_home + "/.ssh"]
devices = []
if args.nvidia:
for d in glob.glob('/dev/nvidia*'):
devices += ['--device', d + ':' + d]
# set up X11 forwarding for Mac or Linux if DISPLAY is set
if platform.system() != 'Windows' and 'DISPLAY' in os.environ:
# Mac OS X by default does not support X11 forwarding
# and its DISPLAY environment variable cannot be shared
envs += ["--env", "DISPLAY=" + get_local_ip() + ":0"]
if os.path.exists('/usr/X11/bin/xhost') or os.path.exists('/usr/bin/xhost'):
subprocess.check_output(['xhost', '+' + get_local_ip()])
# Start the docker image in the background and pipe the stderr
port_http = str(find_free_port(8888, 50))
if not port_http:
stderr_write("Error: Could not find a free port.\n")
sys.exit(-1)
cmd = ["docker", "run", "-d", rmflag, "--name", container,
"--shm-size", "2g", "-p", port_http + ":" + port_http] + \
envs + volumes + args.args.split() + \
['--security-opt', 'seccomp=unconfined', args.image,
"jupyter-notebook --no-browser --ip=0.0.0.0 --port " +
port_http + " " + args.jupyter +
" >> " + docker_home + "/.log/jupyter.log 2>&1"]
if args.verbose:
stdout_write(' '.join(cmd[:-1]) + ' "' + cmd[-1] + '"\n')
subprocess.call(cmd)
wait_for_url = True
# Wait for user to press Ctrl-C
while True:
try:
if wait_for_url:
# Wait until the file is not empty
while not subprocess.check_output(["docker", "exec", container,
"cat", docker_home +
"/.log/jupyter.log"]):
time.sleep(1)
p = subprocess.Popen(["docker", "exec", container,
"tail", "-F",
docker_home + "/.log/jupyter.log"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
# Monitor the stdout to extract the URL
for stdout_line in iter(p.stdout.readline, ""):
if args.verbose:
stdout_write(stdout_line)
m = re.search('http://[^:]+:', stdout_line)
if m:
# Open browser if found URL
if not args.notebook:
url = "http://localhost:" + \
stdout_line[m.end():-1]
else:
url = "http://localhost:" + port_http + \
"/notebooks/" + args.notebook + \
stdout_line[stdout_line.find("?token="):-1]
print("Copy/paste this URL into your browser " +
"when you connect for the first time:")
print(" ", url)
stdout_write("You can also log into the container using the command\n ssh -X -p " + port_ssh + " " +
docker_user + "@localhost -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no\n" +
"with an authorized key in " +
homedir + "/.ssh/authorized_keys.\n")
if not args.no_browser:
webbrowser.open(url)
p.stdout.close()
p.terminate()
wait_for_url = False
break
if args.detach:
print('Started container ' + container + ' in background.')
print('To stop it, use "docker stop ' + container + '".')
sys.exit(0)
print("Press Ctrl-C to stop the server.")
time.sleep(1)
# Wait until the container exits or Ctlr-C is pressed
if not args.quiet:
if args.verbose:
stdout_write("Redirecting ~/.log/jupyter.log to stdout.\n")
subprocess.call(["docker", "exec", container,
"tail", "-F", "-n", "0",
docker_home + "/.log/jupyter.log"])
else:
subprocess.call(["docker", "exec", container,
"tail", "-f", "/dev/null"])
sys.exit(0)
except subprocess.CalledProcessError:
try:
# If Docker process no long exists, exit
if args.verbose:
stdout_write(
"Check whether docker container is running.\n")
if not subprocess.check_output(['docker', 'ps',
'-q', '-f',
'name=' + container]):
stdout_write('Docker container ' +
container + ' is no longer running\n')
sys.exit(-1)
else:
time.sleep(1)
continue
except subprocess.CalledProcessError:
stderr_write('Docker container ' +
container + ' is no longer running\n')
sys.exit(-1)
except KeyboardInterrupt:
handle_interrupt(container)
continue
except KeyboardInterrupt:
handle_interrupt(container)
except OSError:
sys.exit(-1)