-->

How to have file written automatically in the star

2019-08-17 13:13发布

问题:

I am using JuPyter hub on k8s. It has a persistent volume claim. I want to have my users use a variable run_id = "sample"every time they use jupyter notebook.

Doing so requires making a file aviral.py in the path /home/jovyan/.ipython/profile_default/startup with the content run_id = "sample".

I have to do this manually and would want this to be done as soon as the new user's pod is created for the first time i.e. the file gets written there itself.

Is there any way to automate this?

Everything mentioned here is taken off the shelf, as described here:

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

回答1:

I think the easiest way would be to create a ConfigMap from your aviral.py file:

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

And add it to the Deployment used by JuPyter Hub. You can read how to Customizing your Deployment as this would require modification of you config.yaml and applying the changes.

Inside your deployment you need to add following container spec:

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

If I'm not mistaken and this is indeed correct config.yaml for Jupyter Hub, the storage part should look like the following:

...
    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
...

Or another approach, you could modify your config.yaml and change postStart command so it might look like this:

...
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"]
...

You can check the documentation about Define a Command and Arguments for a Container.

I hope this helps.