From 2ee5f7fc9b63a1bef36c8f26ba874e5f8c6ce67d Mon Sep 17 00:00:00 2001 From: Riaz Arbi Date: Thu, 19 Sep 2024 22:04:10 +0200 Subject: [PATCH] Add r and rstudio images (#34) * Remove rsession Dockerfile * Add r image files * don't copy $PWD to $HOME * Add rstudio image * Update README * Fix dependency tree Co-authored-by: Gordon Inggs * remove VSCode --------- Co-authored-by: Gordon Inggs --- README.md | 25 +-- .../python/jupyter-k8s/rsession/Dockerfile | 47 ------ base/drivers/python_minimal/r/Dockerfile | 89 ++++++++++ base/drivers/python_minimal/r/apt.txt | 3 + base/drivers/python_minimal/r/install.R | 33 ++++ .../python_minimal/r/rstudio/Dockerfile | 96 +++++++++++ .../python_minimal/r/rstudio/fix-permissions | 36 +++++ .../r/rstudio/jupyter_notebook_config.py | 56 +++++++ .../r/rstudio/start-notebook.sh | 14 ++ .../r/rstudio/start-singleuser.sh | 44 +++++ .../drivers/python_minimal/r/rstudio/start.sh | 152 ++++++++++++++++++ 11 files changed, 538 insertions(+), 57 deletions(-) delete mode 100644 base/drivers/python_minimal/python/jupyter-k8s/rsession/Dockerfile create mode 100644 base/drivers/python_minimal/r/Dockerfile create mode 100644 base/drivers/python_minimal/r/apt.txt create mode 100644 base/drivers/python_minimal/r/install.R create mode 100644 base/drivers/python_minimal/r/rstudio/Dockerfile create mode 100644 base/drivers/python_minimal/r/rstudio/fix-permissions create mode 100644 base/drivers/python_minimal/r/rstudio/jupyter_notebook_config.py create mode 100644 base/drivers/python_minimal/r/rstudio/start-notebook.sh create mode 100644 base/drivers/python_minimal/r/rstudio/start-singleuser.sh create mode 100644 base/drivers/python_minimal/r/rstudio/start.sh diff --git a/README.md b/README.md index 242d351..a0d1f66 100644 --- a/README.md +++ b/README.md @@ -8,13 +8,13 @@ The current structure of these image tags (and the repo): ``` └── base └── drivers - ├── python_minimal - ├── jupyter-ide - ├── python - │ └── jupyter-k8s - └── r_minimal + └── python_minimal + | ├── jupyter-ide + | └── python + | └── jupyter-k8s └── r └── rstudio + ``` Overview of the tags: @@ -24,13 +24,12 @@ Overview of the tags: [here](./base/drivers/python_minimal/python_additions.sh)). * `python` - Installs many Python packages (see [here](./base/drivers/python_minimal/python/python_additions.sh)). * `jupyter-k8s` - Installs and runs Jupyterlab on top of `python` -* `r_minimal` - (coming soon) Installs R and a limited section of packages. On top of `python_minimal`. -* `r` - (coming soon) Installs many R packages -* `rstudio` - (coming soon) Install and runs RStudio via rsession proxy on top of `r`. +* `r` - Installs R base, ubuntu system packages for common R packages, `renv`, `pak`, `rspm` and `remotes`. +* `rstudio` - Installs Jupyterlab, Rstudio, and VSCode on top of `r`. Automatically launches to jupyterlab. Use `renv` to manage R packages on a per-project basis. * `jupyter-ide` - Makes various IDEs (PyCharm, VSCode) available on top of `python_minimal`. ## Usage -These images are intented to be used either as part of a Jupyterhub setup, or as the base for airflow Kubernetes +These images are intended to be used either as part of a Jupyterhub setup, or as the base for airflow Kubernetes operator jobs. To enter an image and poke around (useful for checking whether a dependency is statisfied), use the `docker exec` @@ -38,4 +37,10 @@ or `kubectl exec` command to start a bash shell, e.g. ```bash docker exec -it -rm cityofcapetown/datascience:python bash -``` \ No newline at end of file +``` + +To launch a jupyter-based web server from `jupyter-ide`, `jupyter-k8s` or `rstudio` enter something like the following: + +```bash +docker run -it --rm -p 8888:8888 cityofcapetown/datascience:{jupyter-ide,jupyter-k8s,rstudio} +``` diff --git a/base/drivers/python_minimal/python/jupyter-k8s/rsession/Dockerfile b/base/drivers/python_minimal/python/jupyter-k8s/rsession/Dockerfile deleted file mode 100644 index 94a08f3..0000000 --- a/base/drivers/python_minimal/python/jupyter-k8s/rsession/Dockerfile +++ /dev/null @@ -1,47 +0,0 @@ -FROM cityofcapetown/datascience:jupyter-k8s - -USER root - -RUN apt-get update && \ - apt-get install -y --no-install-recommends \ - libapparmor1 \ - libedit2 \ - lsb-release \ - psmisc \ - libssl1.0.0 - -# R pre-requisites -RUN apt-get update && \ - apt-get install -y --no-install-recommends \ - fonts-dejavu \ - unixodbc \ - unixodbc-dev \ - gfortran gnupg \ - gcc libclang-dev psmisc libapparmor1 lsb-release && \ - rm -rf /var/lib/apt/lists/* - -RUN gpg --keyserver keyserver.ubuntu.com --recv-key E298A3A825C0D65DFD57CBB651716619E084DAB9 -RUN gpg -a --export E298A3A825C0D65DFD57CBB651716619E084DAB9 | sudo apt-key add - - -#RUN add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran35/' -RUN echo deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran35/ >> /etc/apt/sources.list - -RUN apt-get update && \ - apt-get install -y --no-install-recommends \ - r-base-dev gdebi-core && \ - rm -rf /var/lib/apt/lists/* - - -# ENV RSTUDIO_VERSION 1.1.456 -ENV RSTUDIO_VERSION 1.2.1335 - -# RUN wget --quiet https://download2.rstudio.org/rstudio-server-${RSTUDIO_VERSION}-amd64.deb -RUN wget --quiet https://download2.rstudio.org/server/bionic/amd64/rstudio-server-${RSTUDIO_VERSION}-amd64.deb -RUN gdebi -n rstudio-server-${RSTUDIO_VERSION}-amd64.deb && rm rstudio-server-${RSTUDIO_VERSION}-amd64.deb -RUN apt-get clean && rm -rf /var/lib/apt/lists/* - -RUN pip install git+https://github.com/jupyterhub/jupyter-server-proxy -RUN pip install git+https://github.com/jupyterhub/jupyter-rsession-proxy - -USER $NB_USER -ENV PATH="${PATH}:/usr/lib/rstudio-server/bin" diff --git a/base/drivers/python_minimal/r/Dockerfile b/base/drivers/python_minimal/r/Dockerfile new file mode 100644 index 0000000..b590088 --- /dev/null +++ b/base/drivers/python_minimal/r/Dockerfile @@ -0,0 +1,89 @@ +FROM cityofcapetown/datascience:python_minimal + +LABEL authors="Riaz Arbi" +ARG DEBIAN_FRONTEND=noninteractive + + +# BASE ========================================== +# Set locales and install make +RUN apt-get clean && \ + apt-get update && \ + apt-get install -y \ + locales locales-all tzdata + +ENV TZ="Africa/Johannesburg" +ENV LC_ALL en_US.UTF-8 +ENV LANG en_US.UTF-8 +ENV LANGUAGE en_US.UTF-8 +ENV TERM xterm + +# Copy in needed files +COPY apt.txt . +COPY install.R . + +# Do the build in one layer to keep the image as small as possible. +# temporary symlink to fix broken apt_inst and apt_pkg for gdebi and add-apt-repository +# remove when the python 3.9 dependencies are removed from python_minimal +RUN ln -s /usr/lib/python3/dist-packages/apt_pkg.cpython-310-x86_64-linux-gnu.so /usr/lib/python3/dist-packages/apt_pkg.so \ + && ln -s /usr/lib/python3/dist-packages/apt_inst.cpython-310-x86_64-linux-gnu.so /usr/lib/python3/dist-packages/apt_inst.so \ +# Install common utils + && apt-get install -y --no-install-recommends \ + make \ + git \ +# Install R +&& apt-get install -y --no-install-recommends \ + software-properties-common \ + dirmngr \ + curl \ + wget \ + gpg \ + gcc \ + build-essential \ + && wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc \ + && sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/" \ + && apt-get update \ + && apt-get install -y --no-install-recommends r-base \ +# Install system dependencies for common R packages +# First install known minimal system dependencies + && echo "Checking for 'apt.txt'..." \ + ; if test -f "apt.txt" ; then \ + apt-get update --fix-missing > /dev/null\ + && xargs -a apt.txt apt-get install --yes \ + && apt-get clean > /dev/null \ + && rm -rf /var/lib/apt/lists/* \ + && rm -rf /tmp/* \ + ; fi \ +# Makes use of pak to install system dependencies of CRAN top 100 packages as well +# Use install.R to determine what apt packages are needed... + && if [ -f install.R ]; then R --quiet -f install.R; fi \ +# ...install.R will create a bash install file called R_apt_deps.txt... +# ...and then use apt to install those packages + && echo "Checking for 'R_apt_deps.'..." \ + ; if test -f "R_apt_deps.sh" ; then \ + /bin/bash R_apt_deps.sh \ + && apt-get clean > /dev/null \ + && rm -rf /var/lib/apt/lists/* \ + && rm -rf /tmp/* \ + ; fi + + +# Enable prompt color in the skeleton .bashrc before creating the default NB_USER +RUN sed -i 's/^#force_color_prompt=yes/force_color_prompt=yes/' /etc/skel/.bashrc + +# Create $NB_USER +ENV NB_USER=rovyan +ENV NB_UID=1000 +ENV USER ${NB_USER} +ENV NB_UID ${NB_UID} +ENV HOME /home/${NB_USER} + +RUN adduser --disabled-password \ + --gecos "Default user" \ + --uid ${NB_UID} \ + ${NB_USER} + +# Make sure the contents of our repo are in ${HOME} +USER root +RUN chown -R ${NB_UID} ${HOME} +USER ${NB_USER} +WORKDIR ${HOME} diff --git a/base/drivers/python_minimal/r/apt.txt b/base/drivers/python_minimal/r/apt.txt new file mode 100644 index 0000000..d323e02 --- /dev/null +++ b/base/drivers/python_minimal/r/apt.txt @@ -0,0 +1,3 @@ +libcurl4-openssl-dev +apt-file +libx11-dev \ No newline at end of file diff --git a/base/drivers/python_minimal/r/install.R b/base/drivers/python_minimal/r/install.R new file mode 100644 index 0000000..aaf9201 --- /dev/null +++ b/base/drivers/python_minimal/r/install.R @@ -0,0 +1,33 @@ +# Base packages +install.packages(c("pak", "pillar")) + +# Base packages +pak::pak_install_extra() + +pak::pkg_install(c("rspm", "renv", "cranlogs", "remotes")) + +rspm::enable() + +required_packages <- c( + "sf", + "imager", + "tidymodels", + "tidyverse", + "arrow", + "duckdb", + "plotly", + "quarto", + "rmarkdown", + "aws.s3", + "reticulate") + +top_packages <- cranlogs::cran_top_downloads(when = "last-month", count = 100) + +packages <- c(required_packages, top_packages$package) + +sysreqs <- pak::pkg_sysreqs(packages) + +fileConn <- "R_apt_deps.sh" +cat(sysreqs$pre_install, "\n", file = fileConn, append = FALSE) +cat(sysreqs$install_scripts, "\n", file = fileConn, append = TRUE) +cat(sysreqs$post_install, "\n", file = fileConn, append = TRUE) \ No newline at end of file diff --git a/base/drivers/python_minimal/r/rstudio/Dockerfile b/base/drivers/python_minimal/r/rstudio/Dockerfile new file mode 100644 index 0000000..3345a4e --- /dev/null +++ b/base/drivers/python_minimal/r/rstudio/Dockerfile @@ -0,0 +1,96 @@ +FROM cityofcapetown/datascience:r + +USER root + +# Install system dependencies ================================================== +# BOTH JUPYTER AND RSTUDIO DEPS ================================================ +RUN apt-get update --fix-missing > /dev/null \ + && apt-get install --yes --no-install-recommends \ + # for jupyter + git \ + sudo \ + libzmq3-dev \ + wget \ + bzip2 \ + ca-certificates \ + sudo \ + locales \ + fonts-liberation \ + nodejs \ + npm \ + # for Rstudio + pkg-config \ + file \ + gdebi-core \ + psmisc \ + libclang-dev \ + libpq5 \ + libssl-dev \ + && apt-get clean > /dev/null \ + && rm -rf /var/lib/apt/lists/* \ + && rm -rf /tmp/* \ + # Install Jupyter ============================================================== + && python3 -m pip install "charset-normalizer" \ + && python3 -m pip install jsonschema[format-nongpl] \ + && python3 -m pip install --no-cache-dir notebook jupyterlab \ + && python3 -m pip install --no-cache-dir jupyter-rsession-proxy jupyterhub \ + && Rscript -e 'remotes::install_github("IRkernel/IRkernel@*release")' \ + && Rscript -e 'IRkernel::installspec(user = FALSE)' \ + # Install Rstudio + && wget --quiet https://download2.rstudio.org/server/jammy/amd64/rstudio-server-2024.04.2-764-amd64.deb \ + && gdebi -n rstudio-server-2024.04.2-764-amd64.deb \ +# Clean up + && rm -rf /var/lib/apt/lists/* \ + && rm -rf /tmp/downloaded_packages \ + && rm -rf rstudio-server-2024.04.2-764-amd64.deb + +# Back to non privileged user +# These env vars are in FROM image +ENV NB_USER=rovyan +ENV NB_UID=1000 +ENV NB_GID=100 +ENV USER ${NB_USER} +ENV HOME /home/${NB_USER} +RUN chown -R ${NB_USER} ${HOME} + + +# USER SETTINGS ============================================================ + +USER ${NB_USER} +WORKDIR ${HOME} + + +# Set NB_USER ENV vars +ENV PATH="${PATH}:/usr/lib/rstudio-server/bin" +ENV TZ="Africa/Johannesburg" + +# Clean npm cache, create a new jupyter notebook config +RUN npm cache clean --force \ + && jupyter notebook --generate-config \ + && rm -rf /home/$NB_USER/.cache/yarn + +# Configure container startup +CMD ["/bin/bash", "start-notebook.sh"] + +# Image launch scripts +COPY start.sh /usr/local/bin/ +COPY start-notebook.sh /usr/local/bin/ +COPY start-singleuser.sh /usr/local/bin/ +COPY jupyter_notebook_config.py /etc/jupyter/ + +# ROOT USER CLEANUP ========================================================= + +# Fix permissions on /etc/jupyter as root +USER root + +# Add a script that we will use to correct permissions after running certain commands +ADD fix-permissions /usr/local/bin/fix-permissions +RUN chmod +x /usr/local/bin/fix-permissions + +RUN /usr/local/bin/fix-permissions /etc/jupyter/ +RUN /usr/local/bin/fix-permissions $HOME + +# Run as NB_USER ============================================================ + +USER $NB_USER + diff --git a/base/drivers/python_minimal/r/rstudio/fix-permissions b/base/drivers/python_minimal/r/rstudio/fix-permissions new file mode 100644 index 0000000..72795a7 --- /dev/null +++ b/base/drivers/python_minimal/r/rstudio/fix-permissions @@ -0,0 +1,36 @@ +#!/bin/bash +# set permissions on a directory +# after any installation, if a directory needs to be (human) user-writable, +# run this script on it. +# It will make everything in the directory owned by the group $NB_GID +# and writable by that group. +# Deployments that want to set a specific user id can preserve permissions +# by adding the `--group-add users` line to `docker run`. + +# uses find to avoid touching files that already have the right permissions, +# which would cause massive image explosion + +# right permissions are: +# group=$NB_GID +# AND permissions include group rwX (directory-execute) +# AND directories have setuid,setgid bits set + +set -e + +for d in "$@"; do + find "$d" \ + ! \( \ + -group $NB_GID \ + -a -perm -g+rwX \ + \) \ + -exec chgrp $NB_GID {} \; \ + -exec chmod g+rwX {} \; + # setuid,setgid *on directories only* + find "$d" \ + \( \ + -type d \ + -a ! -perm -6000 \ + \) \ + -exec chmod +6000 {} \; +done + diff --git a/base/drivers/python_minimal/r/rstudio/jupyter_notebook_config.py b/base/drivers/python_minimal/r/rstudio/jupyter_notebook_config.py new file mode 100644 index 0000000..a8ce591 --- /dev/null +++ b/base/drivers/python_minimal/r/rstudio/jupyter_notebook_config.py @@ -0,0 +1,56 @@ +# Copyright (c) Jupyter Development Team. +# Distributed under the terms of the Modified BSD License. + +from jupyter_core.paths import jupyter_data_dir +import subprocess +import os +import errno +import stat + +c = get_config() +c.NotebookApp.ip = '0.0.0.0' +c.NotebookApp.port = 8888 +c.NotebookApp.open_browser = False + +# https://github.com/jupyter/notebook/issues/3130 +c.FileContentsManager.delete_to_trash = False + +# Generate a self-signed certificate +if 'GEN_CERT' in os.environ: + dir_name = jupyter_data_dir() + pem_file = os.path.join(dir_name, 'notebook.pem') + try: + os.makedirs(dir_name) + except OSError as exc: # Python >2.5 + if exc.errno == errno.EEXIST and os.path.isdir(dir_name): + pass + else: + raise + + # Generate an openssl.cnf file to set the distinguished name + cnf_file = os.path.join(os.getenv('CONDA_DIR', '/usr/lib'), 'ssl', 'openssl.cnf') + if not os.path.isfile(cnf_file): + with open(cnf_file, 'w') as fh: + fh.write('''\ +[req] +distinguished_name = req_distinguished_name +[req_distinguished_name] +''') + + # Generate a certificate if one doesn't exist on disk + subprocess.check_call(['openssl', 'req', '-new', + '-newkey', 'rsa:2048', + '-days', '365', + '-nodes', '-x509', + '-subj', '/C=XX/ST=XX/L=XX/O=generated/CN=generated', + '-keyout', pem_file, + '-out', pem_file]) + # Restrict access to the file + os.chmod(pem_file, stat.S_IRUSR | stat.S_IWUSR) + c.NotebookApp.certfile = pem_file + +# Change default umask for all subprocesses of the notebook server if set in +# the environment +if 'NB_UMASK' in os.environ: + os.umask(int(os.environ['NB_UMASK'], 8)) + diff --git a/base/drivers/python_minimal/r/rstudio/start-notebook.sh b/base/drivers/python_minimal/r/rstudio/start-notebook.sh new file mode 100644 index 0000000..b38c738 --- /dev/null +++ b/base/drivers/python_minimal/r/rstudio/start-notebook.sh @@ -0,0 +1,14 @@ +#!/bin/bash +# Copyright (c) Jupyter Development Team. +# Distributed under the terms of the Modified BSD License. + +set -e + +if [[ ! -z "${JUPYTERHUB_API_TOKEN}" ]]; then + # launched by JupyterHub, use single-user entrypoint + exec /usr/local/bin/start-singleuser.sh "$@" +elif [[ ! -z "${JUPYTER_ENABLE_LAB}" ]]; then + . /usr/local/bin/start.sh jupyter lab "$@" +else + . /usr/local/bin/start.sh jupyter notebook "$@" +fi diff --git a/base/drivers/python_minimal/r/rstudio/start-singleuser.sh b/base/drivers/python_minimal/r/rstudio/start-singleuser.sh new file mode 100644 index 0000000..f5ec3b0 --- /dev/null +++ b/base/drivers/python_minimal/r/rstudio/start-singleuser.sh @@ -0,0 +1,44 @@ +#!/bin/bash +# Copyright (c) Jupyter Development Team. +# Distributed under the terms of the Modified BSD License. + +set -e + +# set default ip to 0.0.0.0 +if [[ "$NOTEBOOK_ARGS $@" != *"--ip="* ]]; then + NOTEBOOK_ARGS="--ip=0.0.0.0 $NOTEBOOK_ARGS" +fi + +# handle some deprecated environment variables +# from DockerSpawner < 0.8. +# These won't be passed from DockerSpawner 0.9, +# so avoid specifying --arg=empty-string +if [ ! -z "$NOTEBOOK_DIR" ]; then + NOTEBOOK_ARGS="--notebook-dir='$NOTEBOOK_DIR' $NOTEBOOK_ARGS" +fi +if [ ! -z "$JPY_PORT" ]; then + NOTEBOOK_ARGS="--port=$JPY_PORT $NOTEBOOK_ARGS" +fi +if [ ! -z "$JPY_USER" ]; then + NOTEBOOK_ARGS="--user=$JPY_USER $NOTEBOOK_ARGS" +fi +if [ ! -z "$JPY_COOKIE_NAME" ]; then + NOTEBOOK_ARGS="--cookie-name=$JPY_COOKIE_NAME $NOTEBOOK_ARGS" +fi +if [ ! -z "$JPY_BASE_URL" ]; then + NOTEBOOK_ARGS="--base-url=$JPY_BASE_URL $NOTEBOOK_ARGS" +fi +if [ ! -z "$JPY_HUB_PREFIX" ]; then + NOTEBOOK_ARGS="--hub-prefix=$JPY_HUB_PREFIX $NOTEBOOK_ARGS" +fi +if [ ! -z "$JPY_HUB_API_URL" ]; then + NOTEBOOK_ARGS="--hub-api-url=$JPY_HUB_API_URL $NOTEBOOK_ARGS" +fi +if [ ! -z "$JUPYTER_ENABLE_LAB" ]; then + NOTEBOOK_BIN="jupyter labhub" +else + NOTEBOOK_BIN="jupyterhub-singleuser" +fi + +. /usr/local/bin/start.sh $NOTEBOOK_BIN $NOTEBOOK_ARGS "$@" + diff --git a/base/drivers/python_minimal/r/rstudio/start.sh b/base/drivers/python_minimal/r/rstudio/start.sh new file mode 100644 index 0000000..ad39498 --- /dev/null +++ b/base/drivers/python_minimal/r/rstudio/start.sh @@ -0,0 +1,152 @@ +#!/bin/bash +# Copyright (c) Jupyter Development Team. +# Distributed under the terms of the Modified BSD License. + +set -e + +# Exec the specified command or fall back on bash +if [ $# -eq 0 ]; then + cmd=( "bash" ) +else + cmd=( "$@" ) +fi + +run-hooks () { + # Source scripts or run executable files in a directory + if [[ ! -d "$1" ]] ; then + return + fi + echo "$0: running hooks in $1" + for f in "$1/"*; do + case "$f" in + *.sh) + echo "$0: running $f" + source "$f" + ;; + *) + if [[ -x "$f" ]] ; then + echo "$0: running $f" + "$f" + else + echo "$0: ignoring $f" + fi + ;; + esac + done + echo "$0: done running hooks in $1" +} + +run-hooks /usr/local/bin/start-notebook.d + +# Handle special flags if we're root +if [ $(id -u) == 0 ] ; then + + # Only attempt to change the jovyan username if it exists + if id jovyan &> /dev/null ; then + echo "Set username to: $NB_USER" + usermod -d /home/$NB_USER -l $NB_USER jovyan + fi + + # Handle case where provisioned storage does not have the correct permissions by default + # Ex: default NFS/EFS (no auto-uid/gid) + if [[ "$CHOWN_HOME" == "1" || "$CHOWN_HOME" == 'yes' ]]; then + echo "Changing ownership of /home/$NB_USER to $NB_UID:$NB_GID with options '${CHOWN_HOME_OPTS}'" + chown $CHOWN_HOME_OPTS $NB_UID:$NB_GID /home/$NB_USER + fi + if [ ! -z "$CHOWN_EXTRA" ]; then + for extra_dir in $(echo $CHOWN_EXTRA | tr ',' ' '); do + echo "Changing ownership of ${extra_dir} to $NB_UID:$NB_GID with options '${CHOWN_EXTRA_OPTS}'" + chown $CHOWN_EXTRA_OPTS $NB_UID:$NB_GID $extra_dir + done + fi + + # handle home and working directory if the username changed + if [[ "$NB_USER" != "jovyan" ]]; then + # changing username, make sure homedir exists + # (it could be mounted, and we shouldn't create it if it already exists) + if [[ ! -e "/home/$NB_USER" ]]; then + echo "Relocating home dir to /home/$NB_USER" + mv /home/jovyan "/home/$NB_USER" + fi + # if workdir is in /home/jovyan, cd to /home/$NB_USER + if [[ "$PWD/" == "/home/jovyan/"* ]]; then + newcwd="/home/$NB_USER/${PWD:13}" + echo "Setting CWD to $newcwd" + cd "$newcwd" + fi + fi + + # Change UID of NB_USER to NB_UID if it does not match + if [ "$NB_UID" != $(id -u $NB_USER) ] ; then + echo "Set $NB_USER UID to: $NB_UID" + usermod -u $NB_UID $NB_USER + fi + + # Set NB_USER primary gid to NB_GID (after making the group). Set + # supplementary gids to NB_GID and 100. + if [ "$NB_GID" != $(id -g $NB_USER) ] ; then + echo "Add $NB_USER to group: $NB_GID" + groupadd -g $NB_GID -o ${NB_GROUP:-${NB_USER}} + usermod -g $NB_GID -aG 100 $NB_USER + fi + + # Enable sudo if requested + if [[ "$GRANT_SUDO" == "1" || "$GRANT_SUDO" == 'yes' ]]; then + echo "Granting $NB_USER sudo access and appending $CONDA_DIR/bin to sudo PATH" + echo "$NB_USER ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/notebook + fi + + # Add $CONDA_DIR/bin to sudo secure_path + sed -r "s#Defaults\s+secure_path=\"([^\"]+)\"#Defaults secure_path=\"\1:$CONDA_DIR/bin\"#" /etc/sudoers | grep secure_path > /etc/sudoers.d/path + + # Exec the command as NB_USER with the PATH and the rest of + # the environment preserved + run-hooks /usr/local/bin/before-notebook.d + echo "Executing the command: ${cmd[@]}" + exec sudo -E -H -u $NB_USER PATH=$PATH XDG_CACHE_HOME=/home/$NB_USER/.cache PYTHONPATH=${PYTHONPATH:-} "${cmd[@]}" +else + if [[ "$NB_UID" == "$(id -u jovyan)" && "$NB_GID" == "$(id -g jovyan)" ]]; then + # User is not attempting to override user/group via environment + # variables, but they could still have overridden the uid/gid that + # container runs as. Check that the user has an entry in the passwd + # file and if not add an entry. + STATUS=0 && whoami &> /dev/null || STATUS=$? && true + if [[ "$STATUS" != "0" ]]; then + if [[ -w /etc/passwd ]]; then + echo "Adding passwd file entry for $(id -u)" + cat /etc/passwd | sed -e "s/^jovyan:/nayvoj:/" > /tmp/passwd + echo "jovyan:x:$(id -u):$(id -g):,,,:/home/jovyan:/bin/bash" >> /tmp/passwd + cat /tmp/passwd > /etc/passwd + rm /tmp/passwd + else + echo 'Container must be run with group "root" to update passwd file' + fi + fi + + # Warn if the user isn't going to be able to write files to $HOME. + if [[ ! -w /home/jovyan ]]; then + echo 'Container must be run with group "users" to update files' + fi + else + # Warn if looks like user want to override uid/gid but hasn't + # run the container as root. + if [[ ! -z "$NB_UID" && "$NB_UID" != "$(id -u)" ]]; then + echo 'Container must be run as root to set $NB_UID' + fi + if [[ ! -z "$NB_GID" && "$NB_GID" != "$(id -g)" ]]; then + echo 'Container must be run as root to set $NB_GID' + fi + fi + + # Warn if looks like user want to run in sudo mode but hasn't run + # the container as root. + if [[ "$GRANT_SUDO" == "1" || "$GRANT_SUDO" == 'yes' ]]; then + echo 'Container must be run as root to grant sudo permissions' + fi + + # Execute the command + run-hooks /usr/local/bin/before-notebook.d + echo "Executing the command: ${cmd[@]}" + exec "${cmd[@]}" +fi +