0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

JupyterHub でカスタムアクションを定義する(hook 機能)

Last updated at Posted at 2020-07-29

JupyterHub のhook機能を使うと、ログイン時にユーザ毎のディレクトリを自動作成できたりする。

設定ファイル(jupyterhub_config.py)に関数として定義する。
hook には以下の種類がある。

  • c.JupyterHub.user_redirect_hook
  • c.Spawner.auth_state_hook
  • c.Spawner.post_stop_hook
  • c.Spawner.pre_spawn_hook
  • c.Authenticator.post_auth_hook

※それぞれの詳細な説明は割愛(未調査)

公式リポジトリにサンプルスクリプトが紹介されている。

jupyterhub/examples/bootstrap-script at master · jupyterhub/jupyterhub

例えばユーザフォルダを自動作成するhookアクションは以下。

# in jupyterhub_config.py  
import os
def create_dir_hook(spawner):
    username = spawner.user.name # get the username
    volume_path = os.path.join('/volumes/jupyterhub', username)
    if not os.path.exists(volume_path):
        # create a directory with umask 0755 
        # hub and container user must have the same UID to be writeable
        # still readable by other users on the system
        os.mkdir(volume_path, 0o755)
        # now do whatever you think your user needs
        # ...
        pass

# attach the hook function to the spawner
c.Spawner.pre_spawn_hook = create_dir_hook

※起動ユーザの所有権限になるため、合わせて chown / chmod もした方が良さそう。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?