Skip to content

Commit

Permalink
[python] Change the sudo_if function to avoid issues with Bash (#694)
Browse files Browse the repository at this point in the history
* [python] Updated `sudo_if` function

- Remove double quotes to avoid issues with string tokenization

* Add test scenario

* Revert "Add test scenario"

This reverts commit 9e62a37.

* Update `jupyterlab git` package name

* Bump feature version

* Test: Install jupyterlab under root user

* Refactor changes

* Bump patch version

* Address review points
  • Loading branch information
alexander-smolyakov authored Sep 27, 2023
1 parent e7f7d19 commit 0d2fc3a
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/python/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"id": "python",
"version": "1.1.0",
"version": "1.2.0",
"name": "Python",
"documentationURL": "https://github.com/devcontainers/features/tree/main/src/python",
"description": "Installs the provided version of Python, as well as PIPX, and other common Python utilities. JupyterLab is conditionally installed with the python feature. Note: May require source code compilation.",
Expand Down
39 changes: 29 additions & 10 deletions src/python/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -304,25 +304,31 @@ sudo_if() {
if [ "$(id -u)" -eq 0 ] && [ "$USERNAME" != "root" ]; then
su - "$USERNAME" -c "$COMMAND"
else
"$COMMAND"
$COMMAND
fi
}

install_user_package() {
PACKAGE="$1"
sudo_if "${PYTHON_SRC}" -m pip install --user --upgrade --no-cache-dir "$PACKAGE"
INSTALL_UNDER_ROOT="$1"
PACKAGE="$2"

if [ "$INSTALL_UNDER_ROOT" = true ]; then
sudo_if "${PYTHON_SRC}" -m pip install --upgrade --no-cache-dir "$PACKAGE"
else
sudo_if "${PYTHON_SRC}" -m pip install --user --upgrade --no-cache-dir "$PACKAGE"
fi
}

add_user_jupyter_config() {
CONFIG_DIR="/home/$USERNAME/.jupyter"
CONFIG_FILE="$CONFIG_DIR/jupyter_server_config.py"
CONFIG_DIR="$1"
CONFIG_FILE="$2"

# Make sure the config file exists or create it with proper permissions
test -d "$CONFIG_DIR" || sudo_if mkdir "$CONFIG_DIR"
test -f "$CONFIG_FILE" || sudo_if touch "$CONFIG_FILE"

# Don't write the same config more than once
grep -q "$1" "$CONFIG_FILE" || echo "$1" >> "$CONFIG_FILE"
grep -q "$3" "$CONFIG_FILE" || echo "$3" >> "$CONFIG_FILE"
}

install_python() {
Expand Down Expand Up @@ -461,13 +467,26 @@ if [ "${INSTALL_JUPYTERLAB}" = "true" ]; then
exit 1
fi

install_user_package jupyterlab
install_user_package jupyterlab-git
INSTALL_UNDER_ROOT=true
if [ "$(id -u)" -eq 0 ] && [ "$USERNAME" != "root" ]; then
INSTALL_UNDER_ROOT=false
fi

install_user_package $INSTALL_UNDER_ROOT jupyterlab
install_user_package $INSTALL_UNDER_ROOT jupyterlab-git

# Configure JupyterLab if needed
if [ -n "${CONFIGURE_JUPYTERLAB_ALLOW_ORIGIN}" ]; then
add_user_jupyter_config "c.ServerApp.allow_origin = '${CONFIGURE_JUPYTERLAB_ALLOW_ORIGIN}'"
add_user_jupyter_config "c.NotebookApp.allow_origin = '${CONFIGURE_JUPYTERLAB_ALLOW_ORIGIN}'"
# Resolve config directory
CONFIG_DIR="/root/.jupyter"
if [ "$INSTALL_UNDER_ROOT" = false ]; then
CONFIG_DIR="/home/$USERNAME/.jupyter"
fi

CONFIG_FILE="$CONFIG_DIR/jupyter_server_config.py"

add_user_jupyter_config $CONFIG_DIR $CONFIG_FILE "c.ServerApp.allow_origin = '${CONFIGURE_JUPYTERLAB_ALLOW_ORIGIN}'"
add_user_jupyter_config $CONFIG_DIR $CONFIG_FILE "c.NotebookApp.allow_origin = '${CONFIGURE_JUPYTERLAB_ALLOW_ORIGIN}'"
fi
fi

Expand Down
2 changes: 1 addition & 1 deletion test/python/install_additional_jupyterlab.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ packages="$(python3 -m pip list)"
check "location" grep jupyter <<< "$packages"

# Check for git extension
check "jupyterlab-git" grep jupyterlab-git <<< "$packages"
check "jupyterlab_git" grep jupyterlab_git <<< "$packages"

# Check for correct JupyterLab configuration
check "config" grep ".*.allow_origin = '*'" /home/vscode/.jupyter/jupyter_server_config.py
Expand Down
2 changes: 1 addition & 1 deletion test/python/install_jupyterlab.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ packages="$(python3 -m pip list)"
check "location" grep jupyter <<< "$packages"

# Check for git extension
check "jupyterlab-git" grep jupyterlab-git <<< "$packages"
check "jupyterlab_git" grep jupyterlab_git <<< "$packages"

# Check for correct JupyterLab configuration
check "config" grep ".*.allow_origin = '*'" /home/vscode/.jupyter/jupyter_server_config.py
Expand Down
22 changes: 22 additions & 0 deletions test/python/install_jupyterlab_debian.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

set -e

# Optional: Import test library
source dev-container-features-test-lib

# Check for an installation of JupyterLab
check "version" jupyter lab --version

# Check location of JupyterLab installation
packages="$(python3 -m pip list)"
check "location" grep jupyter <<< "$packages"

# Check for git extension
check "jupyterlab_git" grep jupyterlab_git <<< "$packages"

# Check for correct JupyterLab configuration
check "config" grep ".*.allow_origin = '*'" /root/.jupyter/jupyter_server_config.py

# Report result
reportResults
22 changes: 22 additions & 0 deletions test/python/install_jupyterlab_ubuntu.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

set -e

# Optional: Import test library
source dev-container-features-test-lib

# Check for an installation of JupyterLab
check "version" jupyter lab --version

# Check location of JupyterLab installation
packages="$(python3 -m pip list)"
check "location" grep jupyter <<< "$packages"

# Check for git extension
check "jupyterlab_git" grep jupyterlab_git <<< "$packages"

# Check for correct JupyterLab configuration
check "config" grep ".*.allow_origin = '*'" /root/.jupyter/jupyter_server_config.py

# Report result
reportResults
22 changes: 21 additions & 1 deletion test/python/scenarios.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,25 @@
"features": {
"python": "3.10"
}
},
"install_jupyterlab_debian": {
"image": "debian:bullseye-slim",
"features": {
"python": {
"version": "3.11",
"installJupyterlab": true,
"configureJupyterlabAllowOrigin": "*"
}
}
},
"install_jupyterlab_ubuntu": {
"image": "ubuntu:focal",
"features": {
"python": {
"version": "3.11",
"installJupyterlab": true,
"configureJupyterlabAllowOrigin": "*"
}
}
}
}
}

0 comments on commit 0d2fc3a

Please sign in to comment.