diff --git a/src/jupyterhub_config/custom_docker_spawner.py b/src/jupyterhub_config/custom_docker_spawner.py index 2031b3e..33dc946 100644 --- a/src/jupyterhub_config/custom_docker_spawner.py +++ b/src/jupyterhub_config/custom_docker_spawner.py @@ -1,4 +1,5 @@ import os +import shutil import venv from pathlib import Path @@ -17,6 +18,7 @@ def start(self): # Ensure the user directory exists self._ensure_user_directory(user_dir, username) + self._ensure_bashrc(user_dir) # Ensure the user's Jupyter directory exists self._ensure_user_jupyter_directory(user_dir) @@ -49,6 +51,31 @@ def _ensure_user_directory(self, user_dir: Path, username: str): else: self.log.info(f'Reusing user directory for {username}') + def _ensure_bashrc(self, user_dir: Path): + """ + Ensure the user's .bashrc and .bash_profile files exist, copying them from .tmpl templates if needed. + """ + + config_dir = Path(os.environ['CONFIG_DIR']) + bashrc_tmpl = config_dir / '.bashrc.tmpl' + bash_profile_tmpl = config_dir / '.bash_profile.tmpl' + + # Keep a copy of the template files in the user's home directory in case they are needed later + # for recovery or debugging. They are not used by the user's shell. + shutil.copy2(bashrc_tmpl, user_dir/'.bashrc.tmpl') + shutil.copy2(bash_profile_tmpl, user_dir/'.bash_profile.tmpl') + + bashrc_dest = user_dir / '.bashrc' + bash_profile_dest = user_dir / '.bash_profile' + + if not bashrc_dest.exists(): + self.log.info(f'Creating .bashrc file for {user_dir}') + shutil.copy2(bashrc_tmpl, bashrc_dest) + + if not bash_profile_dest.exists(): + self.log.info(f'Creating .bash_profile file for {user_dir}') + shutil.copy2(bash_profile_tmpl, bash_profile_dest) + def _ensure_user_jupyter_directory(self, user_dir: Path): """ Create the user's Jupyter directory and subdirectories if they do not exist. And set the @@ -111,6 +138,8 @@ def _configure_environment(self, user_dir: Path, user_env_dir: Path, username: s self.environment['PYTHONSTARTUP'] = os.path.join(os.environ['JUPYTERHUB_CONFIG_DIR'], 'startup.py') self.environment['JUPYTERHUB_USER'] = username + self.environment['SHELL'] = '/usr/bin/bash' + if self._is_rw_minio_user(): self.log.info(f'MinIO read/write user detected: {self.user.name}. Setting up minio_rw credentials.') self.environment['MINIO_ACCESS_KEY'] = self.environment['MINIO_RW_ACCESS_KEY']