So turns out, I just copy the files from a centralized folder whenever a user is being generated, using subprocess
.
Demo
I have a notebook containing tutorials for students, locate at /etc/jupyter
. Whenever a user creates, I just copy all files into /home/{username}
.
My code before modifying:
import os, sys, pwd, subprocess
def pre_spawn_hook(spawner):
username = spawner.user.name
try:
pwd.getpwnam(username)
except KeyError:
subprocess.check_call(['useradd', '-ms', '/bin/bash', username])
c = get_config()
c.Spawner.pre_spawn_hook = pre_spawn_hook
Using subprocess.check_call
to copy files and folders to user’s home folder. Note that the user name is now the variable username
:
subprocess.check_call(['cp', '-TRv', '/etc/jupyter', '/home/' + username])
However, just copy has never enough. It’s because of the misconfiguration in the folder. Just using the chmod
to give the read, write, execute permission to user (Permission CHMOD 777):
subprocess.check_call(['chmod', '777', '/home/' + username + '/*'])
Final Jupyter configuration, at all:
import os, sys, pwd, subprocess
def pre_spawn_hook(spawner):
username = spawner.user.name
try:
pwd.getpwnam(username)
except KeyError:
subprocess.check_call(['useradd', '-ms', '/bin/bash', username])
subprocess.check_call(['cp', '-TRv', '/etc/jupyter', '/home/' + username])
subprocess.check_call(['chmod', '777', '/home/' + username + '/*'])
c = get_config()
c.Spawner.pre_spawn_hook = pre_spawn_hook