This adds a post_start_hook that is similar to spawner.pre_spawn_hook, but runs just after we successfully start the systemd service.
This is helpful when running code in the hook that depends on dynamic users being allocated, which happens after the service is started. For example, referencing the dynamic user name in chown.
With this logic we can do something like this in jupyterhub_config.py:
import subprocess
c.JupyterHub.spawner_class = 'systemdspawner.SystemdSpawner'
c.SystemdSpawner.dynamic_users = True
c.SystemdSpawner.unit_name_template = 'jupyter-{USERID}'
def get_examples(spawner):
unit_name = f"jupyter-{spawner.user.id}"
examples_dir = f"/var/lib/private/{spawner.user.name}/examples"
# copy examples from an external loc to examples_dir
# these files will be owned by `root`, so make them owned by the dynamic user with:
subprocess.call(['chown', '-R', f"{unit_name}:{unit_name}", examples_dir])
c.SystemdSpawner.post_start_hook = get_examples
This adds a
post_start_hook
that is similar tospawner.pre_spawn_hook
, but runs just after we successfully start the systemd service.This is helpful when running code in the hook that depends on dynamic users being allocated, which happens after the service is started. For example, referencing the dynamic user name in
chown
.With this logic we can do something like this in
jupyterhub_config.py
: