Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to create user home if it is not present on login? #38

Open
Jamesits opened this issue Sep 4, 2018 · 5 comments
Open

Option to create user home if it is not present on login? #38

Jamesits opened this issue Sep 4, 2018 · 5 comments

Comments

@Jamesits
Copy link

Jamesits commented Sep 4, 2018

There are some cases when a user can login, have a user home directory path, but the directory itself is not present in the system. This is very common if the system is set up to use centralized auth (i.e. kerberos). Is it possible for systemdspawner to create the user home directory on login, so we don't need an admin to provision every new user manually?

@minrk
Copy link
Member

minrk commented Oct 16, 2018

This might be more appropriately a job for the authenticator instead of the spawner. Maybe both, though.

@JanBobolz
Copy link

This already works (also with systemdspawner) as described here: https://github.com/jupyterhub/jupyterhub/tree/master/examples/bootstrap-script

In short: add c.Spawner.pre_spawn_hook = ANY_PYTHON_FUNCTION to the jupyterhub config file (define ANY_PYTHON_FUNCTION in the same file). This function is run before spawning a new instance. This allows you to create a home directory if it's missing.

@whositwhatnow
Copy link

@JanBobolz Would you possibly have a working example? Im running into issues with home directories not getting created (using SSSD Auth wit hPAM)

@JanBobolz
Copy link

Our example is pretty much the same as in the link provided:

jupyterhub_config.py:

#see https://github.com/jupyterhub/jupyterhub/tree/master/examples/bootstrap-script for reference
from subprocess import check_call
import os
def init_user(spawner):
    username = spawner.user.name.strip().lower() # get the username
    script = "/home/jhubrunner/init_user.sh"
    check_call(["sudo", script, username])

# attach the hook function to the spawner
c.Spawner.pre_spawn_hook = init_user #called before any singleuser instance is instantiated (will run repeatedly)

init_user.sh:

#!/bin/bash

USER=$1
if [ "$USER" == "" ]; then
    exit 1
fi
# ----------------------------------------------------------------------------


echo "initializing $USER ..."

# User Directory: That's the private directory for the user to be created, if none exists
USER_DIRECTORY=/home/$USER


if [ -d "$USER_DIRECTORY/jupyter-notebooks" ]; then
    echo "...directory for user already exists. done."
    exit 0 # all good. nothing to do.
else
    echo "...creating home dir"
    install -d -m 0700 -o $USER $USER_DIRECTORY $USER_DIRECTORY/jupyter-notebooks
fi

exit 0

sudoers file:

jhubrunner      ALL=(ALL:ALL) NOPASSWD:/home/jhubrunner/init_user.sh

where jhubrunner is the linux user running jupyterhub. If jhubrunner is not supposed to have sudo access for other stuff, make sure to make the init_user.sh non-writable to jhubrunner.

Debugging

If anything goes wrong, you should see something helpful in the journal (sudo journalctl -r should show some sort of error towards the top if there's a problem running the script)

Disclaimer

There may also be a solution where you don't have to run the init_user.sh script as sudo, it's just that we have a bunch of other stuff we're doing in that script (that I've cut out for this example) that needs sudo.

@whositwhatnow
Copy link

whositwhatnow commented Jul 6, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants