0

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

Aviral Srivastava
  • 4,058
  • 8
  • 29
  • 81

1 Answers1

2

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.

Crou
  • 10,232
  • 2
  • 26
  • 31
  • @AviralSrivastava, command `test` check if file exists and parameter `-d` checks if this file is a directory. I took this line from [https://jupyernetes.com/config-yaml/](https://jupyernetes.com/config-yaml/) – Crou May 16 '19 at 11:07
  • Also, as per your last approach, is this(https://pastebin.com/fYZQn34J) what you are proposing? – Aviral Srivastava May 16 '19 at 11:07
  • why would I need to check the file if I have to create a new one? And if I need to check, why is this checking done on `my-work` than the actual directory: `/home/jovyan/.ipython/profile_default/startup`? – Aviral Srivastava May 16 '19 at 11:08
  • @AviralSrivastava, unfortunately I won't be able to help you regarding Jupyter Hub because I don't know it. I've posted answer from Kubernetes perspective. – Crou May 16 '19 at 11:10
  • hey, I am sorry but your last approach didn't work. My YAML is: https://pastebin.com/fYZQn34J I checked and there was no new file creation. – Aviral Srivastava May 16 '19 at 11:17
  • @AviralSrivastava, the command should be in one line. Are you using helm to install Jupyter? – Crou May 16 '19 at 11:21
  • yes. I am using helm to install jupyter. It is successful without any error but when I check the file creation, it does not exist. I have modified the command to `command: ["/bin/sh", "-c", "cd /home/jovyan/.ipython/profile_default/startup; echo run_id = \"sample\" ' > aviral.py"]`. Let's see. – Aviral Srivastava May 16 '19 at 11:31