jupyterhub / systemdspawner

Spawn JupyterHub single-user notebook servers with systemd
BSD 3-Clause "New" or "Revised" License
92 stars 45 forks source link

add post_start_hook #60

Closed jeff-sternberg closed 1 year ago

jeff-sternberg commented 5 years ago

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