Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 39558
1
#!/usr/bin/python
2
3
######################################################################
4
# This is a daemon-ization script for the IPython notebook, for running
5
# it under a specific URL behind a given proxy server. It is probably
6
# only of use directly in https://cocalc.com.
7
#
8
# This is written in Python, but that can be any python2 on the system; not the
9
# Python that the ipython command runs.
10
#
11
#
12
# Copyright (C) 2016, Sagemath Inc.
13
# 2-clause BSD:
14
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
15
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
16
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
17
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
18
# The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted as representing official policies, either expressed or implied, of the SageMath Project.
19
######################################################################
20
21
22
import json, os, random, signal, sys, time
23
24
def server_setup():
25
global SMC, DATA, DAEMON_FILE, mode, INFO_FILE, info, project_id, base_url, ip
26
# start from home directory, since we want daemon to serve all files in that directory tree.
27
os.chdir(os.environ['HOME'])
28
29
SMC = os.environ['SMC']
30
31
os.environ["PYTHONUSERBASE"] = os.environ['HOME'] + '/.local'
32
33
DATA = os.path.join(SMC, 'jupyter')
34
if not os.path.exists(DATA):
35
os.makedirs(DATA)
36
37
# When run in Daemon mode, it stores info (e.g., pid, port) in this file, in addition to printing
38
# to standard out. This avoids starting a redundant copy of the daemon, if one is already running.
39
DAEMON_FILE = os.path.join(DATA, "daemon.json")
40
41
if len(sys.argv) == 1:
42
print "Usage: %s [start/stop/status] normal Jupyter notebook options..."%sys.argv[0]
43
print "If start or stop is given, then runs as a daemon; otherwise, runs in the foreground."
44
sys.exit(1)
45
46
mode = sys.argv[1]
47
del sys.argv[1]
48
49
INFO_FILE = os.path.join(SMC, 'info.json')
50
if os.path.exists(INFO_FILE):
51
info = json.loads(open(INFO_FILE).read())
52
project_id = info['project_id']
53
base_url = info['base_url']
54
ip = info['location']['host']
55
if ip == 'localhost':
56
# Listening on localhost for devel purposes -- NOTE: this is a *VERY* significant security risk!
57
ip = '127.0.0.1'
58
else:
59
project_id = ''
60
base_url = ''
61
ip = '127.0.0.1'
62
63
def random_port():
64
# get an available port; a race condition is possible, but very, very unlikely.
65
while True:
66
port = random.randint(1025,65536)
67
a = os.popen("netstat -ano|grep %s|grep LISTEN"%port).read()
68
if len(a) < 5:
69
return port
70
71
def command():
72
port = random_port() # time consuming!
73
if project_id:
74
b = "%s/%s/port/jupyter/"%(base_url, project_id)
75
base = " --NotebookApp.base_url=%s "%(b)
76
else:
77
base = ''
78
79
# 2nd argument after "start" ("start" is already eaten, see above)
80
if len(sys.argv) >= 2:
81
mathjax_url = sys.argv.pop(1)
82
else:
83
mathjax_url = "/static/mathjax/MathJax.js" # fallback
84
85
# --NotebookApp.iopub_data_rate_limit=<Float>
86
# Default: 0
87
# (bytes/sec) Maximum rate at which messages can be sent on iopub before they
88
# are limited.
89
# --NotebookApp.iopub_msg_rate_limit=<Float>
90
# (msg/sec) Maximum rate at which messages can be sent on iopub before they
91
# are limited.
92
93
cmd = "jupyter notebook --port-retries=0 --no-browser --NotebookApp.iopub_data_rate_limit=2000000 --NotebookApp.iopub_msg_rate_limit=50 --NotebookApp.mathjax_url=%s %s --ip=%s --port=%s --NotebookApp.token='' --NotebookApp.password=''"%(mathjax_url, base, ip, port)
94
cmd += " " + ' '.join(sys.argv[1:])
95
return cmd, base, port
96
97
if '--help' in ''.join(sys.argv):
98
os.system("jupyter " + ' '.join(sys.argv))
99
sys.exit(0)
100
101
def is_daemon_running():
102
if not os.path.exists(DAEMON_FILE):
103
return False
104
try:
105
s = open(DAEMON_FILE).read()
106
info = json.loads(s)
107
try:
108
os.kill(info['pid'],0)
109
# process exists
110
return info
111
except OSError:
112
# error if no process
113
return False
114
except:
115
# status file corrupted, so ignore it, and
116
# just fall through to the below....
117
return False
118
119
120
def action(mode):
121
sys.stdout.flush()
122
123
if mode == 'status':
124
info = is_daemon_running()
125
if info:
126
info['status'] = 'running'
127
s = info
128
else:
129
s = {'status':'stopped'}
130
print json.dumps(s)
131
return
132
133
elif mode == 'start':
134
if os.path.exists(DAEMON_FILE) and time.time() - os.path.getmtime(DAEMON_FILE) < 60:
135
# If we just tried to start then called again, wait a bit before checking
136
# on process. Note that this should never happen, since local_hub doesn't
137
# call this script repeatedly.
138
time.sleep(10)
139
140
info = is_daemon_running()
141
if info:
142
# already running -- nothing to do
143
print json.dumps(info)
144
return
145
146
# The below approach to finding the PID is *HIDEOUS* and could in theory break.
147
# It is the only way I could come up with to do this without modifying source code of ipython :-(
148
# See http://mail.scipy.org/pipermail/ipython-user/2012-May/010043.html
149
cmd, base, port = command()
150
151
c = '%s 2> "%s"/jupyter-notebook.err 1>"%s"/jupyter-notebook.log &'%(cmd, DATA, DATA)
152
sys.stderr.write(c+'\n'); sys.stderr.flush()
153
os.system(c)
154
155
s = json.dumps({'base':base, 'port':port})
156
open(DAEMON_FILE,'w').write(s)
157
158
tries = 0
159
pid = 0
160
#sys.stderr.write("getting pid...\n"); sys.stderr.flush()
161
wait = 1
162
while not pid:
163
tries += 1
164
#sys.stderr.write("tries... %s\n"%tries); sys.stderr.flush()
165
if tries >= 20:
166
print json.dumps({"error":"Failed to find pid of subprocess."})
167
sys.exit(1)
168
169
c = "ps -u`whoami` -o pid,cmd|grep '/usr/local/bin/jupyter-notebook'"
170
for s in os.popen(c).read().splitlines():
171
v = s.split()
172
if len(v) < 2 or not v[1].split('/')[-1].startswith('python'):
173
continue
174
p = int(v[0])
175
if "port=%s"%port not in s:
176
try:
177
os.kill(p, 9) # kill any other ipython notebook servers by this user
178
except:
179
pass
180
else:
181
pid = p
182
if not pid:
183
time.sleep(wait)
184
wait *= 1.2
185
wait = min(wait, 10)
186
187
s = json.dumps({'base':base, 'port':port, 'pid':pid})
188
print s
189
open(DAEMON_FILE,'w').write(s)
190
return
191
192
elif mode == 'stop':
193
info = is_daemon_running()
194
if not info:
195
# not running -- nothing to do
196
return
197
# IPython server seems rock solid about responding to kill signals and properly cleaning up.
198
try:
199
os.kill(info['pid'], signal.SIGTERM)
200
except OSError: # maybe already dead
201
pass
202
try:
203
os.unlink(DAEMON_FILE)
204
except:
205
pass
206
return
207
208
elif mode == 'restart':
209
action('stop')
210
action('start')
211
212
else:
213
raise RuntimeError("unknown command '%s'"%mode)
214
215
def main():
216
server_setup()
217
action(mode)
218
219
def prepare_file_for_open():
220
# This is unrelated to running the server; instead, before opening
221
# a file, we run this to make sure there is a blank JSON template in place.
222
# This is for compatibility with "new jupyter".
223
# See https://github.com/sagemathinc/cocalc/issues/1978
224
for path in sys.argv[1:]:
225
if not os.path.exists(path) or len(open(path).read().strip()) == 0:
226
open(path,'w').write('{"cells": [{"outputs": [], "source": [], "cell_type": "code", "metadata": {"collapsed": false}, "execution_count": null}], "nbformat_minor": 0, "nbformat": 4, "metadata": {"language_info": {"mimetype": "text/x-python", "version": "2.7.8", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "codemirror_mode": {"name": "ipython", "version": 2}, "file_extension": ".py", "name": "python"}, "kernelspec": {"name": "python2", "language": "python", "display_name": "Python 2"}}}')
227
228