Skip to content

Commit

Permalink
Merge pull request #108 from kbase/dev_jupyterhub
Browse files Browse the repository at this point in the history
create bashrc file
  • Loading branch information
Tianhao-Gu authored Oct 16, 2024
2 parents 5e4b06f + 2b6a957 commit 87ba49d
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/jupyterhub_config/custom_docker_spawner.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import shutil
import venv
from pathlib import Path

Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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']
Expand Down

0 comments on commit 87ba49d

Please sign in to comment.