2018-06-15 16:54:40 -07:00
|
|
|
"""
|
|
|
|
|
JupyterHub config for the littlest jupyterhub.
|
|
|
|
|
"""
|
2019-02-11 13:27:08 +01:00
|
|
|
|
2018-07-30 22:35:55 -07:00
|
|
|
from glob import glob
|
2019-02-11 13:27:08 +01:00
|
|
|
import os
|
2018-06-15 16:17:07 -07:00
|
|
|
|
2018-07-31 12:02:47 +02:00
|
|
|
from systemdspawner import SystemdSpawner
|
2019-10-28 08:48:52 +01:00
|
|
|
from tljh import configurer, user
|
2018-08-28 14:00:43 +02:00
|
|
|
from tljh.config import INSTALL_PREFIX, USER_ENV_PREFIX, CONFIG_DIR
|
2018-09-12 16:46:59 -07:00
|
|
|
from tljh.normalize import generate_system_username
|
2019-10-28 08:48:52 +01:00
|
|
|
from tljh.utils import get_plugin_manager
|
2019-01-22 16:24:38 +02:00
|
|
|
from jupyterhub_traefik_proxy import TraefikTomlProxy
|
2018-07-12 13:33:24 -07:00
|
|
|
|
2019-07-01 17:25:37 +03:00
|
|
|
from traitlets import Dict, Unicode, List
|
2019-06-20 21:54:51 +03:00
|
|
|
|
2018-09-12 16:46:59 -07:00
|
|
|
class UserCreatingSpawner(SystemdSpawner):
|
|
|
|
|
"""
|
|
|
|
|
SystemdSpawner with user creation on spawn.
|
|
|
|
|
|
|
|
|
|
FIXME: Remove this somehow?
|
|
|
|
|
"""
|
2019-07-01 17:59:10 +03:00
|
|
|
user_groups = Dict(key_trait=Unicode(), value_trait=List(Unicode()), config=True)
|
2019-06-20 21:54:51 +03:00
|
|
|
|
2018-06-26 23:30:06 -07:00
|
|
|
def start(self):
|
|
|
|
|
"""
|
|
|
|
|
Perform system user activities before starting server
|
|
|
|
|
"""
|
|
|
|
|
# FIXME: Move this elsewhere? Into the Authenticator?
|
2018-09-12 16:46:59 -07:00
|
|
|
system_username = generate_system_username('jupyter-' + self.user.name)
|
2018-09-14 12:38:13 -07:00
|
|
|
|
|
|
|
|
# FIXME: This is a hack. Allow setting username directly instead
|
|
|
|
|
self.username_template = system_username
|
2018-07-12 13:33:24 -07:00
|
|
|
user.ensure_user(system_username)
|
|
|
|
|
user.ensure_user_group(system_username, 'jupyterhub-users')
|
2018-06-26 23:30:06 -07:00
|
|
|
if self.user.admin:
|
2018-07-12 13:33:24 -07:00
|
|
|
user.ensure_user_group(system_username, 'jupyterhub-admins')
|
2018-06-27 00:33:41 -07:00
|
|
|
else:
|
2018-07-12 13:33:24 -07:00
|
|
|
user.remove_user_group(system_username, 'jupyterhub-admins')
|
2019-06-20 21:54:51 +03:00
|
|
|
if self.user_groups:
|
|
|
|
|
for group, users in self.user_groups.items():
|
|
|
|
|
if self.user.name in users:
|
|
|
|
|
user.ensure_user_group(system_username, group)
|
2018-06-26 23:30:06 -07:00
|
|
|
return super().start()
|
|
|
|
|
|
2018-09-12 16:46:59 -07:00
|
|
|
c.JupyterHub.spawner_class = UserCreatingSpawner
|
2018-06-15 16:17:07 -07:00
|
|
|
|
2018-08-29 16:46:04 +02:00
|
|
|
# leave users running when the Hub restarts
|
|
|
|
|
c.JupyterHub.cleanup_servers = False
|
|
|
|
|
|
2018-06-27 17:38:42 -07:00
|
|
|
# Use a high port so users can try this on machines with a JupyterHub already present
|
|
|
|
|
c.JupyterHub.hub_port = 15001
|
|
|
|
|
|
2019-01-22 16:24:38 +02:00
|
|
|
c.TraefikTomlProxy.should_start = False
|
2019-02-11 09:24:16 +02:00
|
|
|
|
2020-06-18 17:29:50 +03:00
|
|
|
dynamic_conf_file_path = os.path.join(INSTALL_PREFIX, 'state', 'rules', 'rules.toml')
|
2019-02-11 09:24:16 +02:00
|
|
|
c.TraefikTomlProxy.toml_dynamic_config_file = dynamic_conf_file_path
|
2019-01-22 16:24:38 +02:00
|
|
|
c.JupyterHub.proxy_class = TraefikTomlProxy
|
2018-06-27 17:38:42 -07:00
|
|
|
|
2018-06-26 18:36:42 -07:00
|
|
|
c.SystemdSpawner.extra_paths = [os.path.join(USER_ENV_PREFIX, 'bin')]
|
2018-06-28 01:33:28 -07:00
|
|
|
c.SystemdSpawner.default_shell = '/bin/bash'
|
2018-06-29 01:24:30 -07:00
|
|
|
# Drop the '-singleuser' suffix present in the default template
|
|
|
|
|
c.SystemdSpawner.unit_name_template = 'jupyter-{USERNAME}'
|
2018-06-27 01:18:07 -07:00
|
|
|
|
2019-02-22 11:17:59 +01:00
|
|
|
tljh_config = configurer.load_config()
|
|
|
|
|
configurer.apply_config(tljh_config, c)
|
2018-07-30 22:35:55 -07:00
|
|
|
|
2019-05-31 23:35:08 -07:00
|
|
|
# Let TLJH hooks modify `c` if they want
|
2019-06-01 00:32:23 -07:00
|
|
|
|
|
|
|
|
# Call our custom configuration plugin
|
2019-10-28 08:48:52 +01:00
|
|
|
pm = get_plugin_manager()
|
2019-06-01 00:32:23 -07:00
|
|
|
pm.hook.tljh_custom_jupyterhub_config(c=c)
|
2019-05-31 23:35:08 -07:00
|
|
|
|
2018-07-30 22:35:55 -07:00
|
|
|
# Load arbitrary .py config files if they exist.
|
|
|
|
|
# This is our escape hatch
|
2018-08-28 14:00:43 +02:00
|
|
|
extra_configs = sorted(glob(os.path.join(CONFIG_DIR, 'jupyterhub_config.d', '*.py')))
|
2018-07-30 22:35:55 -07:00
|
|
|
for ec in extra_configs:
|
|
|
|
|
load_subconfig(ec)
|