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

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

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

<p>Is there any way to automate this?</p>

<p>Everything mentioned here is taken off the shelf, as described here:</p>

<p><a href="https://zero-to-jupyterhub.readthedocs.io/en/latest/setup-jupyterhub.html" rel="nofollow noreferrer">https://zero-to-jupyterhub.readthedocs.io/en/latest/setup-jupyterhub.html</a></p>

Aviral Srivastava
<p>I think the easiest way would be to create a <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/" rel="nofollow noreferrer"><code>ConfigMap</code></a> from your <code>aviral.py</code> file:</p>

<p><code>kubectl create configmap aviral-configmap --from-file=aviral.py</code></p>

<p>And add it to the <code>Deployment</code> used by JuPyter Hub.
You can read how to <a href="https://zero-to-jupyterhub.readthedocs.io/en/latest/extending-jupyterhub.html" rel="nofollow noreferrer">Customizing your Deployment</a> as this would require modification of you <code>config.yaml</code> and applying the changes.</p>

<p>Inside your deployment you need to add following container spec:</p>

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

<p>If I'm not mistaken and this is indeed correct <a href="https://jupyernetes.com/config-yaml/" rel="nofollow noreferrer">config.yaml</a> for Jupyter Hub, the <code>storage</code> part should look like the following:</p>

<pre><code>...
    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
...
</code></pre>

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

<pre><code>...
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' &gt; aviral.py"]
...
</code></pre>

<p>You can check the documentation about <a href="https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/" rel="nofollow noreferrer">Define a Command and Arguments for a Container</a>.</p>

<p>I hope this helps.</p>
