From 96deb9f4d2bdba670b7d7e67cc9ca35e0a1cfcb6 Mon Sep 17 00:00:00 2001 From: romainx Date: Tue, 29 Dec 2020 20:54:30 +0100 Subject: [PATCH] #1205: Manage both config files --- base-notebook/Dockerfile | 3 +- base-notebook/jupyter_notebook_config.py | 55 ++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 base-notebook/jupyter_notebook_config.py diff --git a/base-notebook/Dockerfile b/base-notebook/Dockerfile index 49275988..b140db19 100644 --- a/base-notebook/Dockerfile +++ b/base-notebook/Dockerfile @@ -150,7 +150,8 @@ CMD ["start-notebook.sh"] # Copy local files as late as possible to avoid cache busting COPY start.sh start-notebook.sh start-singleuser.sh /usr/local/bin/ -COPY jupyter_server_config.py /etc/jupyter/ +# Need to have both files to be able to use classic and lab +COPY jupyter_notebook_config.py jupyter_server_config.py /etc/jupyter/ # Fix permissions on /etc/jupyter as root USER root diff --git a/base-notebook/jupyter_notebook_config.py b/base-notebook/jupyter_notebook_config.py new file mode 100644 index 00000000..19b5e8b4 --- /dev/null +++ b/base-notebook/jupyter_notebook_config.py @@ -0,0 +1,55 @@ +# Copyright (c) Jupyter Development Team. +# Distributed under the terms of the Modified BSD License. + +from jupyter_core.paths import jupyter_data_dir +import subprocess +import os +import errno +import stat + +c = get_config() # noqa: F821 +c.NotebookApp.ip = '0.0.0.0' +c.NotebookApp.port = 8888 +c.NotebookApp.open_browser = False + +# https://github.com/jupyter/notebook/issues/3130 +c.FileContentsManager.delete_to_trash = False + +# 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 an openssl.cnf file to set the distinguished name + cnf_file = os.path.join(os.getenv('CONDA_DIR', '/usr/lib'), 'ssl', 'openssl.cnf') + if not os.path.isfile(cnf_file): + with open(cnf_file, 'w') as fh: + fh.write('''\ +[req] +distinguished_name = req_distinguished_name +[req_distinguished_name] +''') + + # 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))