当新用户在 JuPyter hub 上签名 up/in 时,如何在启动文件夹中自动写入文件?

How to have file written automatically in the startup folder when a new user signs up/in on JuPyter hub?

我在 k8s 上使用 JuPyter hub。它有一个持久的卷声明。我想让我的用户每次使用 jupyter notebook 时都使用一个变量 run_id = "sample"

这样做需要在路径 /home/jovyan/.ipython/profile_default/startup 中创建一个文件 aviral.py,内容为 run_id = "sample".

我必须手动执行此操作,并且希望在第一次创建新用户的 pod 时立即执行此操作,即文件本身写入那里。

有什么方法可以自动化吗?

这里提到的都是下架的,如下所述:

https://zero-to-jupyterhub.readthedocs.io/en/latest/setup-jupyterhub.html

我认为最简单的方法是从您的 aviral.py 文件创建一个 ConfigMap

kubectl create configmap aviral-configmap --from-file=aviral.py

并将其添加到 JuPyter Hub 使用的 Deployment 中。 您可以阅读如何 Customizing your Deployment,因为这需要对您进行修改 config.yaml 并应用更改。

在您的部署中,您需要添加以下容器规范:

spec:
  containers:
    - name: <Container_Name>
      image: <Image_Name>
      volumeMounts:
        - name: my-config
          mountPath: /home/jovyan/.ipython/profile_default/startup
  volumes:
    - name: my-config
      configMap:
        name: aviral-configmap

如果我没记错的话这确实是正确的 config.yaml 对于 Jupyter Hub,storage 部分应该如下所示:

...
    extraVolumes:
        - name: home
          hostPath:
            path: /data/homes/{username}
        - name: tutorial
          hostPath:
            path: /data/homes/_tutorials
        - name: my-config
          configMap:
            name: aviral-configmap
    extraVolumeMounts:
        - name: home
          mountPath: /home/jovyan
        - name: tutorial
          mountPath: /home/jovyan/tutorials
          readOnly: True
        - name: my-config
          mountPath: /home/jovyan/.ipython/profile_default/startup
...

或者另一种方法,您可以修改 config.yaml 并更改 postStart 命令,使其看起来像这样:

...
postStart:
      exec:
        command: ["/bin/sh", "-c", "test -d $HOME/my-work || mkdir $HOME/my-work; mkdir -p /home/jovyan/.ipython/profile_default/startup; echo 'run_id = sample' > aviral.py"]
...

您可以查看有关 Define a Command and Arguments for a Container 的文档。

希望对您有所帮助。