Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create bashrc file #108

Merged
merged 3 commits into from
Oct 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

# Ensure the user directory exists
self._ensure_user_directory(user_dir, username)
self._ensure_bashrc(user_dir)

Check warning on line 21 in src/jupyterhub_config/custom_docker_spawner.py

View check run for this annotation

Codecov / codecov/patch

src/jupyterhub_config/custom_docker_spawner.py#L21

Added line #L21 was not covered by tests

# Ensure the user's Jupyter directory exists
self._ensure_user_jupyter_directory(user_dir)
Expand Down Expand Up @@ -49,6 +51,31 @@
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'

Check warning on line 61 in src/jupyterhub_config/custom_docker_spawner.py

View check run for this annotation

Codecov / codecov/patch

src/jupyterhub_config/custom_docker_spawner.py#L59-L61

Added lines #L59 - L61 were not covered by tests

# 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')

Check warning on line 66 in src/jupyterhub_config/custom_docker_spawner.py

View check run for this annotation

Codecov / codecov/patch

src/jupyterhub_config/custom_docker_spawner.py#L65-L66

Added lines #L65 - L66 were not covered by tests

bashrc_dest = user_dir / '.bashrc'
bash_profile_dest = user_dir / '.bash_profile'

Check warning on line 69 in src/jupyterhub_config/custom_docker_spawner.py

View check run for this annotation

Codecov / codecov/patch

src/jupyterhub_config/custom_docker_spawner.py#L68-L69

Added lines #L68 - L69 were not covered by tests

if not bashrc_dest.exists():
self.log.info(f'Creating .bashrc file for {user_dir}')
shutil.copy2(bashrc_tmpl, bashrc_dest)

Check warning on line 73 in src/jupyterhub_config/custom_docker_spawner.py

View check run for this annotation

Codecov / codecov/patch

src/jupyterhub_config/custom_docker_spawner.py#L71-L73

Added lines #L71 - L73 were not covered by tests

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)

Check warning on line 77 in src/jupyterhub_config/custom_docker_spawner.py

View check run for this annotation

Codecov / codecov/patch

src/jupyterhub_config/custom_docker_spawner.py#L75-L77

Added lines #L75 - L77 were not covered by tests

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 @@
self.environment['PYTHONSTARTUP'] = os.path.join(os.environ['JUPYTERHUB_CONFIG_DIR'], 'startup.py')
self.environment['JUPYTERHUB_USER'] = username

self.environment['SHELL'] = '/usr/bin/bash'

Check warning on line 141 in src/jupyterhub_config/custom_docker_spawner.py

View check run for this annotation

Codecov / codecov/patch

src/jupyterhub_config/custom_docker_spawner.py#L141

Added line #L141 was not covered by tests

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
Loading