Shared writable folder for each group

Hi minrk,

Thanks so much for your detailed explanation of these two methods. Actually I’ve just figured it out and was planning to post my workaround here, though it is a little bit tedious. I’ll try to modify it according to your hints :grinning: Thank you again!

import sqlite3
team_map = {} # prepare an empty dict
def pre_spawn_hook(spawner):
    username = spawner.user.name # get the username
    team_map[str(username)] = []
    conn = sqlite3.connect("./app/app.db") # connect to our app's database
    cur = conn.cursor()
    projects = cur.execute("SELECT name FROM Project LEFT JOIN user_projects ON user_projects.project_id = Project.id WHERE user_projects.user_id = %s;"%(str(username)))
    for p in projects:
        team_map[str(username)].append(p[0]) # retrieve user and project information and put into team_map
    conn.close()

c.Spawner.pre_spawn_hook = pre_spawn_hook

class MyDockerSpawner(DockerSpawner):
    def start(self):
        teams = team_map[self.user.name]
        # add team volume to volumes
        for team in teams:
            self.volumes['jupyterhub-team-{}'.format(team)] = {
                'bind': '/home/jovyan/{}'.format(team),
                'mode': 'rw'
            }
            # resolve 'permission denied' issue
            self.environment = {
                "CHOWN_HOME": "yes",
                "CHOWN_EXTRA": "/home/jovyan",
                "CHOWN_HOME_OPTS": "-R",
                "NB_UID": 1000,
                "NB_GID": 1000,
            }
            self.extra_create_kwargs = {'user': 'root'}
        return super().start()

c.JupyterHub.spawner_class = MyDockerSpawner
c.DockerSpawner.remove = True
2 Likes