|
4 | 4 |
|
5 | 5 | # Configuration file for JupyterHub
|
6 | 6 | import os
|
7 |
| -import shutil |
8 | 7 |
|
9 | 8 | from customauthenticator.custom import CustomTokenAuthenticator
|
10 | 9 |
|
|
14 | 13 | # avoid having to rebuild the JupyterHub container every time we change a
|
15 | 14 | # configuration parameter.
|
16 | 15 |
|
| 16 | + |
| 17 | +def create_dir_hook(spawner): |
| 18 | + username = spawner.user.name # get the username |
| 19 | + |
| 20 | + volume_path = os.path.join("/volumes/jupyterhub/", username) |
| 21 | + print(f"Checking if {volume_path} exists…") |
| 22 | + |
| 23 | + if not os.path.exists(volume_path): |
| 24 | + print(f"{volume_path} does not exist. Creating directory...") |
| 25 | + # create a directory with umask 0755 |
| 26 | + # hub and container user must have the same UID to be writeable |
| 27 | + # still readable by other users on the system |
| 28 | + try: |
| 29 | + # Attempt to create the directory |
| 30 | + os.mkdir(volume_path) |
| 31 | + print(f"Directory {volume_path} created.") |
| 32 | + # Now, do whatever you think your user needs |
| 33 | + # ... |
| 34 | + except OSError as e: |
| 35 | + print(f"Error creating directory {volume_path}: {e}") |
| 36 | + |
| 37 | + try: |
| 38 | + os.chown(volume_path, 1003, 1003) |
| 39 | + print("Ownership of changed successfully.") |
| 40 | + except OSError as e: |
| 41 | + print(f"Error changing ownership of: {e}") |
| 42 | + # now do whatever you think your user needs |
| 43 | + |
| 44 | + |
| 45 | +# attach the hook function to the spawner |
| 46 | +c.Spawner.pre_spawn_hook = create_dir_hook |
| 47 | + |
17 | 48 | # Spawn single-user servers as Docker containers
|
18 | 49 | c.JupyterHub.spawner_class = "dockerspawner.DockerSpawner"
|
19 | 50 |
|
|
37 | 68 | # notebook directory in the container
|
38 | 69 | c.DockerSpawner.volumes = {"jupyterhub-user-{username}": notebook_dir}
|
39 | 70 |
|
40 |
| - |
41 | 71 | # Remove containers once they are stopped
|
42 | 72 | c.DockerSpawner.remove = True
|
43 | 73 |
|
|
103 | 133 | admin = os.environ.get("JUPYTERHUB_ADMIN")
|
104 | 134 | if admin:
|
105 | 135 | c.Authenticator.admin_users = [admin]
|
106 |
| - |
107 |
| - |
108 |
| -# Pre spawn hook |
109 |
| -def post_spawn_hook(spawner, auth_state): |
110 |
| - username = spawner.user.name |
111 |
| - spawner.environment["GREETING"] = f"Hello Master {username}" |
112 |
| - |
113 |
| - target_file_path = f"/home/jovyan/work/Clowder_APIs.ipynb" |
114 |
| - |
115 |
| - if not os.path.exists(target_file_path): |
116 |
| - shutil.copy2("/etc/jupyter/Clowder_APIs.ipynb", target_file_path) |
117 |
| - |
118 |
| - |
119 |
| -# c.Spawner.auth_state_hook = post_spawn_hook |
|
0 commit comments