-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathjupyter_notebook_config.py
300 lines (239 loc) · 11.6 KB
/
jupyter_notebook_config.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
import errno
import os
import re
import requests
import stat
import subprocess
from shutil import copyfile
from jupyter_core.paths import jupyter_data_dir
from notebook.auth import passwd
c = get_config()
# Setup the Notebook to listen on 127.0.0.1:8888 by default
c.NotebookApp.ip = '127.0.0.1'
c.NotebookApp.port = 8888
c.NotebookApp.open_browser = False
# https://github.com/jupyter/notebook/issues/3130
c.FileContentsManager.delete_to_trash = False
# Only (pre)set a password if we're *not* using OpenID Connect
if not (os.getenv('OIDC_DISCOVERY_URI') and
os.getenv('OIDC_CLIENT_ID')):
# Set Jupyter Notebook Server password to 'jupyter-<Marathon-App-Prefix>'
# e.g., Marathon App ID '/foo/bar/app' maps to password: 'jupyter-foo-bar'
if os.getenv('MARATHON_APP_ID'):
MARATHON_APP_PREFIX = \
'-'.join(os.getenv('MARATHON_APP_ID').split('/')[:-1])
c.NotebookApp.password = passwd('jupyter{}'.format(MARATHON_APP_PREFIX))
# Set a password if JUPYTER_PASSWORD (override) is set
if os.getenv('JUPYTER_PASSWORD'):
c.NotebookApp.password = passwd(os.getenv('JUPYTER_PASSWORD'))
del(os.environ['JUPYTER_PASSWORD'])
else:
# Disable Notebook authentication since we're authenticating using OpenID Connect
c.NotebookApp.password = u''
c.NotebookApp.token = u''
# Don't leak OpenID Connect configuration to the end-user
for env in ['OIDC_DISCOVERY_URI',
'OIDC_REDIRECT_URI',
'OIDC_CLIENT_ID',
'OIDC_CLIENT_SECRET',
'OIDC_EMAIL']:
try:
del(os.environ[env])
except KeyError:
pass
# Allow CORS and TLS from behind Nginx/Marathon-LB/HAProxy
# Trust X-Scheme/X-Forwarded-Proto and X-Real-Ip/X-Forwarded-For
# Necessary if the proxy handles SSL
c.NotebookApp.trust_xheaders = True
# Set the Access-Control-Allow-Origin header
c.NotebookApp.allow_origin = '*'
# Allow requests where the Host header doesn't point to a local server
c.NotebookApp.allow_remote_access = True
# If running under Apache Mesos:
if (os.getenv('MESOS_SANDBOX')):
if os.getenv('MARATHON_APP_LABEL_HAPROXY_0_VHOST'):
c.NotebookApp.allow_origin = \
'http://{}'.format(os.getenv('MARATHON_APP_LABEL_HAPROXY_0_VHOST'))
if os.getenv('MARATHON_APP_LABEL_HAPROXY_0_REDIRECT_TO_HTTPS'):
c.NotebookApp.allow_origin = \
'https://{}'.format(os.getenv('MARATHON_APP_LABEL_HAPROXY_0_VHOST'))
# Set the Jupyter Notebook server base URL to the HAPROXY_PATH specified
if os.getenv('MARATHON_APP_LABEL_HAPROXY_0_PATH'):
c.NotebookApp.base_url = \
os.getenv('MARATHON_APP_LABEL_HAPROXY_0_PATH')
# Tidy up Mesos Env Vars that will interfere with Spark Drivers
mesos_sandbox = os.getenv('MESOS_SANDBOX')
os.environ['MESOS_DIRECTORY'] = mesos_sandbox
try:
for env in ['MESOS_EXECUTOR_ID',
'MESOS_FRAMEWORK_ID',
'MESOS_SLAVE_ID',
'MESOS_SLAVE_PID'
'MESOS_TASK_ID']:
del os.environ[env]
except KeyError:
pass
# Set the current working directory to ${MESOS_SANDBOX}
os.chdir(mesos_sandbox)
# Build up ${SPARK_OPTS} for Apache Toree and to conveniently reuse with spark-submit:
# eval spark-submit ${SPARK_OPTS} <...>
spark_opts = []
if os.getenv('SPARK_MASTER_URL'):
spark_opts.append('--master={}'.format(os.getenv('SPARK_MASTER_URL')))
if os.getenv('SPARK_DEPLOY_MODE'):
spark_opts.append('--deploy-mode={}'.format(os.getenv('SPARK_DEPLOY_MODE')))
if os.getenv('SPARK_CLASS'):
spark_opts.append('--class={}'.format(os.getenv('SPARK_CLASS')))
if os.getenv('SPARK_APP_NAME'):
spark_opts.append('--name={}'.format(os.getenv('SPARK_APP_NAME')))
if os.getenv('SPARK_JARS'):
spark_opts.append('--jars={}'.format(os.getenv('SPARK_JARS')))
if os.getenv('SPARK_PACKAGES'):
spark_opts.append('--packages={}'.format(os.getenv('SPARK_PACKAGES')))
if os.getenv('SPARK_EXCLUDE_PACKAGES'):
spark_opts.append('--exclude-packages={}'.format(os.getenv('SPARK_EXCLUDE_PACKAGES')))
if os.getenv('SPARK_REPOSITORIES'):
spark_opts.append('--repositories={}'.format(os.getenv('SPARK_REPOSITORIES')))
if os.getenv('SPARK_PY_FILES'):
spark_opts.append('--py-files={}'.format(os.getenv('SPARK_PY_FILES')))
# Forward Kerberos Credentials Cache (File) onto Spark Executors (for TensorFlowOnSpark)?
if os.getenv('ENABLE_SPARK_KERBEROS_TICKET_FORWARDING'):
if os.getenv('SPARK_FILES'):
spark_opts.append('--files={},{}'.format(
os.getenv('KRB5CCNAME'), os.getenv('SPARK_FILES')))
else:
spark_opts.append('--files={}'.format(os.getenv('KRB5CCNAME')))
spark_opts.append('--conf spark.executorEnv.KRB5CCNAME={}/krb5cc_{}'.format(
os.getenv('MESOS_SANDBOX', '/home/jovyan'), os.getuid()))
if os.getenv('SPARK_PROPERTIES_FILE'):
spark_opts.append('--properties-file={}'.format(os.getenv('SPARK_PROPERTIES_FILE')))
if os.getenv('SPARK_DRIVER_CORES'):
spark_opts.append('--driver-cores={}'.format(os.getenv('SPARK_DRIVER_CORES')))
if os.getenv('SPARK_DRIVER_MEMORY'):
spark_opts.append('--driver-memory={}'.format(os.getenv('SPARK_DRIVER_MEMORY')))
if os.getenv('SPARK_DRIVER_JAVA_OPTIONS'):
spark_opts.append('--driver-java-options={}'.format(os.getenv('SPARK_DRIVER_JAVA_OPTIONS')))
if os.getenv('SPARK_DRIVER_LIBRARY_PATH'):
spark_opts.append('--driver-library-path={}'.format(os.getenv('SPARK_DRIVER_LIBRARY_PATH')))
if os.getenv('SPARK_DRIVER_CLASS_PATH'):
spark_opts.append('--driver-class-path={}'.format(os.getenv('SPARK_DRIVER_CLASS_PATH')))
if os.getenv('SPARK_TOTAL_EXECUTOR_CORES'):
spark_opts.append('--total-executor-cores={}'.format(os.getenv('SPARK_TOTAL_EXECUTOR_CORES')))
if os.getenv('SPARK_EXECUTOR_CORES'):
spark_opts.append('--executor-cores={}'.format(os.getenv('SPARK_EXECUTOR_CORES')))
if os.getenv('SPARK_EXECUTOR_MEMORY'):
spark_opts.append('--executor-memory={}'.format(os.getenv('SPARK_EXECUTOR_MEMORY')))
if os.getenv('SPARK_PROXY_USER'):
spark_opts.append('--proxy-user={}'.format(os.getenv('SPARK_PROXY_USER')))
if os.getenv('SPARK_SUPERVISE'):
spark_opts.append('--supervise={}'.format(os.getenv('SPARK_SUPERVISE')))
if os.getenv('SPARK_YARN_QUEUE'):
spark_opts.append('--queue={}'.format(os.getenv('SPARK_YARN_QUEUE')))
if os.getenv('SPARK_YARN_NUM_EXECUTORS'):
spark_opts.append('--num-executors={}'.format(os.getenv('SPARK_YARN_NUM_EXECUTORS')))
if os.getenv('SPARK_YARN_PRINCIPAL'):
spark_opts.append('--principal={}'.format(os.getenv('SPARK_YARN_PRINCIPAL')))
if os.getenv('SPARK_YARN_KEYTAB'):
spark_opts.append('--keytab={}'.format(os.getenv('SPARK_YARN_KEYTAB')))
if os.getenv('PORT_SPARKDRIVER'):
spark_opts.append('--conf spark.driver.port={}'.format(os.getenv('PORT_SPARKDRIVER')))
if os.getenv('PORT_SPARKBLOCKMANAGER'):
spark_opts.append('--conf spark.driver.blockManager.port={}'.format(os.getenv('PORT_SPARKBLOCKMANAGER')))
if os.getenv('PORT_SPARKUI'):
spark_opts.append('--conf spark.ui.port={}'.format(os.getenv('PORT_SPARKUI')))
# Set Spark Executor Environment Variables for TensorFlowOnSpark
spark_opts.append('--conf spark.executorEnv.CLASSPATH={}'.format(os.getenv('CLASSPATH')))
spark_opts.append('--conf spark.executorEnv.LD_LIBRARY_PATH={}'.format(os.getenv('LD_LIBRARY_PATH')))
# Download configuration files
JUPYTER_CONF_FILES = ['core-site.xml',
'hdfs-site.xml',
'hive-site.xml',
'yarn-site.xml',
'krb5.conf',
'jaas.conf']
spark_mesos_uris = []
jupyter_conf_urls = os.getenv('JUPYTER_CONF_URLS')
if (jupyter_conf_urls) and (jupyter_conf_urls != ''):
for url in jupyter_conf_urls.split(','):
for file in JUPYTER_CONF_FILES:
r = requests.get('{}/{}'.format(url, file), stream=True)
if r.status_code != requests.status_codes.codes.ok:
continue
spark_mesos_uris.append('{}/{}'.format(url, file))
with open(file, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
spark_conf_spark_mesos_uris = os.getenv('SPARK_CONF_SPARK_MESOS_URIS')
if (spark_conf_spark_mesos_uris) and (spark_conf_spark_mesos_uris != ''):
spark_mesos_uris.extend(spark_conf_spark_mesos_uris.split('=')[1].split(','))
# Deduplicate URIs with set() and return a list()
spark_mesos_uris = list(set(spark_mesos_uris))
# Remove from the environment so it's not picked up (redundantly) later on
del(os.environ['SPARK_CONF_SPARK_MESOS_URIS'])
if spark_mesos_uris:
spark_opts.append('--conf spark.mesos.uris={}'.format(
','.join(spark_mesos_uris)))
# Copy ${MESOS_SANDBOX}/krb5.conf if it exists to /etc/krb5.conf
if os.path.exists('krb5.conf'):
copyfile('krb5.conf', '/etc/krb5.conf')
# Set a sane base path for .ivy2
spark_opts.append('--conf spark.jars.ivy={}/.ivy2'.format(
os.getenv('MESOS_SANDBOX', '/home/jovyan')))
# Accumulate Spark --conf properties specified in SPARK_CONF_<> env vars
spark_conf_env_pattern = re.compile(r'^SPARK_CONF')
spark_conf_envs = [env for env in os.environ if spark_conf_env_pattern.match(env)]
for env in spark_conf_envs:
spark_opts.append('--conf {}'.format(os.getenv(env)))
os.environ['SPARK_OPTS'] = ' '.join(spark_opts)
os.environ['PYSPARK_SUBMIT_ARGS'] = ' '.join(spark_opts + ['pyspark-shell'])
os.environ['SPARKR_SUBMIT_ARGS'] = ' '.join(spark_opts + ['sparkr-shell'])
if os.getenv('ENABLE_SPARK_MONITOR'):
from conda.cli.main_info import get_info_dict
conda_site_packages = os.path.dirname(get_info_dict()['conda_location'])
spark_driver_extraclasspath = os.path.join(conda_site_packages, 'sparkmonitor/listener.jar')
spark_monitor_conf = [
'--conf spark.driver.extraClassPath={}'.format(spark_driver_extraclasspath),
'--conf spark.extraListeners=sparkmonitor.listener.JupyterSparkMonitorListener'
]
os.environ['PYSPARK_SUBMIT_ARGS'] = ' '.join(
spark_opts + spark_monitor_conf + ['pyspark-shell']
)
# Ray Redis (Head Node) Address
os.environ['RAY_REDIS_ADDRESS'] = '{}:{}'.format(
os.getenv('MESOS_CONTAINER_IP'),
os.getenv('PORT_RAYREDIS', '6379'))
# Dask Scheduler Address
os.environ['DASK_SCHEDULER_ADDRESS'] = '{}:{}'.format(
os.getenv('MESOS_CONTAINER_IP'),
os.getenv('PORT_DASKSCHEDULER', '8786'))
# Generate a self-signed certificate
if 'GEN_CERT' in os.environ:
dir_name = jupyter_data_dir()
pem_file = os.path.join(dir_name, 'notebook.pem')
try:
os.makedirs(dir_name)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(dir_name):
pass
else:
raise
# Generate a certificate if one doesn't exist on disk
subprocess.check_call(['openssl', 'req', '-new',
'-newkey', 'rsa:2048',
'-days', '365',
'-nodes', '-x509',
'-subj', '/C=XX/ST=XX/L=XX/O=generated/CN=generated',
'-keyout', pem_file,
'-out', pem_file])
# Restrict access to the file
os.chmod(pem_file, stat.S_IRUSR | stat.S_IWUSR)
c.NotebookApp.certfile = pem_file
# Change default umask for all subprocesses of the notebook server if set in
# the environment
if 'NB_UMASK' in os.environ:
os.umask(int(os.environ['NB_UMASK'], 8))
# Create empty file to signal configuration is complete
SIGNAL_FILE = os.path.join(os.getenv('MESOS_SANDBOX', '/home/jovyan'), 'JUPYTER_NOTEBOOK_CONFIG_COMPLETE')
with open(SIGNAL_FILE, "w") as f:
f.write("")