2020-11-16 10:44:49 +01:00
|
|
|
from tljh.normalize import generate_system_username
|
|
|
|
|
from tljh import user
|
2021-04-05 21:42:01 +03:00
|
|
|
from tljh import configurer
|
2020-11-16 10:44:49 +01:00
|
|
|
from systemdspawner import SystemdSpawner
|
|
|
|
|
from traitlets import Dict, Unicode, List
|
2021-04-06 13:44:29 +03:00
|
|
|
from jupyterhub_configurator.mixins import ConfiguratorSpawnerMixin
|
2020-11-16 10:44:49 +01:00
|
|
|
|
2021-11-01 09:42:45 +01:00
|
|
|
|
2021-04-05 21:42:01 +03:00
|
|
|
class CustomSpawner(SystemdSpawner):
|
2020-11-16 10:44:49 +01:00
|
|
|
"""
|
|
|
|
|
SystemdSpawner with user creation on spawn.
|
|
|
|
|
|
|
|
|
|
FIXME: Remove this somehow?
|
|
|
|
|
"""
|
2021-11-01 09:42:45 +01:00
|
|
|
|
2020-11-16 10:44:49 +01:00
|
|
|
user_groups = Dict(key_trait=Unicode(), value_trait=List(Unicode()), config=True)
|
|
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
|
"""
|
|
|
|
|
Perform system user activities before starting server
|
|
|
|
|
"""
|
|
|
|
|
# FIXME: Move this elsewhere? Into the Authenticator?
|
2021-11-03 23:55:34 +01:00
|
|
|
system_username = generate_system_username("jupyter-" + self.user.name)
|
2020-11-16 10:44:49 +01:00
|
|
|
|
|
|
|
|
# FIXME: This is a hack. Allow setting username directly instead
|
|
|
|
|
self.username_template = system_username
|
|
|
|
|
user.ensure_user(system_username)
|
2021-11-03 23:55:34 +01:00
|
|
|
user.ensure_user_group(system_username, "jupyterhub-users")
|
2020-11-16 10:44:49 +01:00
|
|
|
if self.user.admin:
|
2021-11-03 23:55:34 +01:00
|
|
|
user.ensure_user_group(system_username, "jupyterhub-admins")
|
2020-11-16 10:44:49 +01:00
|
|
|
else:
|
2021-11-03 23:55:34 +01:00
|
|
|
user.remove_user_group(system_username, "jupyterhub-admins")
|
2020-11-16 10:44:49 +01: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)
|
|
|
|
|
return super().start()
|
|
|
|
|
|
2021-11-01 09:42:45 +01:00
|
|
|
|
2021-04-05 21:42:01 +03:00
|
|
|
cfg = configurer.load_config()
|
2021-04-06 14:00:28 +03:00
|
|
|
# Use the jupyterhub-configurator mixin only if configurator is enabled
|
|
|
|
|
# otherwise, any bugs in the configurator backend will stop new user spawns!
|
2021-11-03 23:55:34 +01:00
|
|
|
if cfg["services"]["configurator"]["enabled"]:
|
2021-04-06 14:00:28 +03:00
|
|
|
# Dynamically create the Spawner class using `type`(https://docs.python.org/3/library/functions.html?#type),
|
|
|
|
|
# based on whether or not it should inherit from ConfiguratorSpawnerMixin
|
2021-11-01 09:42:45 +01:00
|
|
|
UserCreatingSpawner = type(
|
2021-11-03 23:55:34 +01:00
|
|
|
"UserCreatingSpawner", (ConfiguratorSpawnerMixin, CustomSpawner), {}
|
2021-11-01 09:42:45 +01:00
|
|
|
)
|
2021-04-05 21:42:01 +03:00
|
|
|
else:
|
2021-11-03 23:55:34 +01:00
|
|
|
UserCreatingSpawner = type("UserCreatingSpawner", (CustomSpawner,), {})
|