Replicating ssh behavior with jupyternotebook spawn

I have a remote machine running ubuntu where i am configuring a jupyterhub notebooks server. The server is already up and running, however, i noticed that it only works well with users that have previously logged in the machine via ssh. For users that have never logged in the machine via ssh before, the server spawns a login screen but after the login comes the following image:


It displayed a different directory path before (i mean different than /user/john.snow), but i configured the jupyterhub spawner class to make the directory by adding the lines:

if os.path.exists('mkdir /home/FOLDER/' + env['JUPYTERHUB_USER'])!=True:
    os.system('mkdir /home/FOLDER/' + env['JUPYTERHUB_USER'])

(i append the complete spawner code at the end of the question, if thats useful)

Since i dont intend to need to test every single directory that jupyter notebook looks for, my desire is to find the ssh configuration files in the computer and mimic what ssh does for that particular user with the spawner. Is it possible? I tried looking at /etc/ssh/ssh_config and similar but almost all of the file is commented and the syntax is mysterious.

Thanks for any suggestions.

OBS1: full spawner code:

import os, getpass
import yaml
from jupyterhub.spawner import Spawner, LocalProcessSpawner

class spawner(LocalProcessSpawner):

    def start(self):
        # get environment variables,
        # several of which are required for configuring the single-user server
        env = self.get_env()
        ret = super(spawner, self).start()
        if os.path.exists('mkdir /home/FOLDER/' + env['JUPYTERHUB_USER'])!=True:
            os.system('mkdir /home/FOLDER/' + env['JUPYTERHUB_USER'])
        os.system('mkdir /home/FOLDER/' + env['JUPYTERHUB_USER'] + '/notebooks')
        os.system('cp -r /usr/local/scripts/notebooks/* /home/FOLDER/' + env['JUPYTERHUB_USER'] + '/notebooks/')
        os.system('chmod -R 777 /home/FOLDER/' + env['JUPYTERHUB_USER'] + '/notebooks/')



        return ret

OBS2: this question is duplicated in python - Replicating ssh behavior with jupyternotebook spawn - Stack Overflow but still with no answer.

Hi! How was your server setup? Did you install and configure it yourself, or is it provided by a different department? How are your users managed and created?

Can you show us the full debug logs from JupyterHub?

Hi! The server was setup by myself via a set of scripts that i got from my department documentation. I only partially understand what the scripts do. I think the most accurate and interesting details i can give now are:

Jupyter hub is now initialized via

sudo systemctl start jupyterhub.service

jupyterhub.service calls for jupyterhub.sh which is just a file that defines environment variables and then calls for jupyterhub_config.py. jupyterhub_config.py uses the spawner mentioned above in the question. I append all of these files below. About the logs, there is a jupyter.conf file in the same directory as jupyter_config.py that doesnt seem to be called by any of these files but defines

stdout_logfile=/var/log/jupyterhub.log

however, there is no /var/log/jupyterhub.log file for some reason.

jupyterhub.service:

[Unit]
Description=Jupyter Hub

[Service]
PIDFile=/etc/jupyterhub/jupyterhub.pid
ExecStart=/etc/systemd/system/jupyterhub.sh
ExecReload=/etc/systemd/system/jupyterhub.sh
Restart=on-failure
RestartSec=15s

[Install]
WantedBy=multi-user.target

jupyterhub.sh:

#!/usr/bin/env bash
source /etc/profile
source /etc/profile.d/mypaths.sh
source /etc/profile.d/bash_completion.sh
source /etc/profile.d/soft.sh
source /etc/profile.d/scanner.sh
source /etc/profile.d/designer-path.sh
source /etc/profile.d/gui-path.sh
source /etc/bash.bashrc
export PATH=$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/sbin:/usr/local/epics/base/bin/linux-x86_64:/usr/local/epics/extensions/bin/linux-x8
export PYTHONPATH=$PYTHONPATH::/usr/local/me/scanner/:/usr/local/me/GUI:/usr/local/me/GUI/my-widgets/:/usr/local/me/GUI/scanner/:
jupyterhub -f /etc/jupyterhub/jupyterhub_config.py

jupyterhub_config.py:

c.JupyterHub.bind_url = ‘http://:8000/jupyter’
c.JupyterHub.pid_file = ‘/etc/jupyterhub/jupyterhub.pid’
c.JupyterHub.shutdown_on_logout = True
c.JupyterHub.spawner_class = ‘MySpawner.spawner.spawner’
c.Spawner.env_keep = [‘PATH’, ‘PYTHONPATH’, ‘CONDA_ROOT’, ‘CONDA_DEFAULT_ENV’, ‘VIRTUAL_ENV’, ‘LANG’, ‘LC_ALL’, ‘PYDM_DEFAULT_PROTOCOL’]
c.Spawner.notebook_dir = ‘~/’ #Added for testing. Should be removed?

finally, MySpawner.spawner.spawner is defined as:

import os, getpass
import yaml
from jupyterhub.spawner import Spawner, LocalProcessSpawner

class spawner(LocalProcessSpawner):

def start(self):
    # get environment variables,
    # several of which are required for configuring the single-user server
    env = self.get_env()
    ret = super(spawner, self).start()
    os.system('source /etc/skel/.bashrc')
    os.system('source /etc/skel/.profile')
    #if os.path.exists('mkdir /home/ABTLUS/' + env['JUPYTERHUB_USER'])!=True:
        #os.system('mkdir /home/ABTLUS/' + env['JUPYTERHUB_USER'])
    os.system('mkdir /home/ABTLUS/' + env['JUPYTERHUB_USER'] + '/notebooks')
    os.system('cp -r /usr/local/scripts/notebooks/* /home/ABTLUS/' + env['JUPYTERHUB_USER'] + '/notebooks/')
    os.system('chmod -R 777 /home/ABTLUS/' + env['JUPYTERHUB_USER'] + '/notebooks/')

    #try: 
    #    with open('/etc/jupyterhub/users.yml') as yaml_file:
    #        data = yaml.safe_load(yaml_file)
    #        path = data[env['JUPYTERHUB_USER']]['root_dir']
    #        self.notebook_dir = path
    #except Exception:
    #    self.notebook_dir = ''

    return ret

jupyterhub.conf:

user=root
command=jupyterhub -f /etc/jupyterhub/jupyterhub_config.py
environment=PYTHONPATH=“/etc/jupyterhub/lnlsSpawner/:/etc/jupyterhub:%(ENV_PYTHONPATH)s”, LC_ALL=“%(ENV_LC_ALL)s,PATH=”%(ENV_PATH)s",
directory=/etc/jupyterhub/
autostart=true
autorestart=true
startretries=1
exitcodes=0,2
stopsignal=TERM
redirect_stderr=true
stdout_logfile=/var/log/jupyterhub.log
stdout_logfile_maxbytes=1MB
stdout_logfile_backups=10
stdout_capture_maxbytes=1MB

Also, you can see i tried something new: i found some ssh configuration files in /etc/skel and tried sourcing from them in the spawner (the lines

os.system('source /etc/skel/.bashrc')
os.system('source /etc/skel/.profile')

are the ones that do this).

But it also didnt work.

Aparently the spawner class is being executed as root so adding

os.system('su '+env['JUPYTERHUB_USER'])

before

os.system('source /etc/skel/.bashrc')
os.system('source /etc/skel/.profile')

and

os.system('exit')

after those two lines solved for new users to log in. However, the user that displayed the red loading bar before were still not being able to spawn the server after login. Deleting these users home folder and browser cache seemed to resolve everything.