|  | 
|  | 1 | +#!/bin/bash | 
|  | 2 | +# Copyright (c) Jupyter Development Team. | 
|  | 3 | +# Distributed under the terms of the Modified BSD License. | 
|  | 4 | + | 
|  | 5 | +set -e | 
|  | 6 | + | 
|  | 7 | +# Exec the specified command or fall back on bash | 
|  | 8 | +if [ $# -eq 0 ]; then | 
|  | 9 | +    cmd=( "bash" ) | 
|  | 10 | +else | 
|  | 11 | +    cmd=( "$@" ) | 
|  | 12 | +fi | 
|  | 13 | + | 
|  | 14 | +run-hooks () { | 
|  | 15 | +    # Source scripts or run executable files in a directory | 
|  | 16 | +    if [[ ! -d "$1" ]] ; then | 
|  | 17 | +        return | 
|  | 18 | +    fi | 
|  | 19 | +    echo "$0: running hooks in $1" | 
|  | 20 | +    for f in "$1/"*; do | 
|  | 21 | +        case "$f" in | 
|  | 22 | +            *.sh) | 
|  | 23 | +                echo "$0: running $f" | 
|  | 24 | +                source "$f" | 
|  | 25 | +                ;; | 
|  | 26 | +            *) | 
|  | 27 | +                if [[ -x "$f" ]] ; then | 
|  | 28 | +                    echo "$0: running $f" | 
|  | 29 | +                    "$f" | 
|  | 30 | +                else | 
|  | 31 | +                    echo "$0: ignoring $f" | 
|  | 32 | +                fi | 
|  | 33 | +                ;; | 
|  | 34 | +        esac | 
|  | 35 | +    done | 
|  | 36 | +    echo "$0: done running hooks in $1" | 
|  | 37 | +} | 
|  | 38 | + | 
|  | 39 | +run-hooks /usr/local/bin/start-notebook.d | 
|  | 40 | + | 
|  | 41 | +# Handle special flags if we're root | 
|  | 42 | +if [ $(id -u) == 0 ] ; then | 
|  | 43 | + | 
|  | 44 | +    # Only attempt to change the jovyan username if it exists | 
|  | 45 | +    if id jovyan &> /dev/null ; then | 
|  | 46 | +        echo "Set username to: $NB_USER" | 
|  | 47 | +        usermod -d /home/$NB_USER -l $NB_USER jovyan | 
|  | 48 | +    fi | 
|  | 49 | + | 
|  | 50 | +    # Handle case where provisioned storage does not have the correct permissions by default | 
|  | 51 | +    # Ex: default NFS/EFS (no auto-uid/gid) | 
|  | 52 | +    if [[ "$CHOWN_HOME" == "1" || "$CHOWN_HOME" == 'yes' ]]; then | 
|  | 53 | +        echo "Changing ownership of /home/$NB_USER to $NB_UID:$NB_GID with options '${CHOWN_HOME_OPTS}'" | 
|  | 54 | +        chown $CHOWN_HOME_OPTS $NB_UID:$NB_GID /home/$NB_USER | 
|  | 55 | +    fi | 
|  | 56 | +    if [ ! -z "$CHOWN_EXTRA" ]; then | 
|  | 57 | +        for extra_dir in $(echo $CHOWN_EXTRA | tr ',' ' '); do | 
|  | 58 | +            echo "Changing ownership of ${extra_dir} to $NB_UID:$NB_GID with options '${CHOWN_EXTRA_OPTS}'" | 
|  | 59 | +            chown $CHOWN_EXTRA_OPTS $NB_UID:$NB_GID $extra_dir | 
|  | 60 | +        done | 
|  | 61 | +    fi | 
|  | 62 | + | 
|  | 63 | +    # handle home and working directory if the username changed | 
|  | 64 | +    if [[ "$NB_USER" != "jovyan" ]]; then | 
|  | 65 | +        # changing username, make sure homedir exists | 
|  | 66 | +        # (it could be mounted, and we shouldn't create it if it already exists) | 
|  | 67 | +        if [[ ! -e "/home/$NB_USER" ]]; then | 
|  | 68 | +            echo "Relocating home dir to /home/$NB_USER" | 
|  | 69 | +            mv /home/jovyan "/home/$NB_USER" || ln -s /home/jovyan "/home/$NB_USER" | 
|  | 70 | +        fi | 
|  | 71 | +        # if workdir is in /home/jovyan, cd to /home/$NB_USER | 
|  | 72 | +        if [[ "$PWD/" == "/home/jovyan/"* ]]; then | 
|  | 73 | +            newcwd="/home/$NB_USER/${PWD:13}" | 
|  | 74 | +            echo "Setting CWD to $newcwd" | 
|  | 75 | +            cd "$newcwd" | 
|  | 76 | +        fi | 
|  | 77 | +    fi | 
|  | 78 | + | 
|  | 79 | +    # Change UID:GID of NB_USER to NB_UID:NB_GID if it does not match | 
|  | 80 | +    if [ "$NB_UID" != $(id -u $NB_USER) ] || [ "$NB_GID" != $(id -g $NB_USER) ]; then | 
|  | 81 | +        echo "Set user $NB_USER UID:GID to: $NB_UID:$NB_GID" | 
|  | 82 | +        if [ "$NB_GID" != $(id -g $NB_USER) ]; then | 
|  | 83 | +            groupadd -g $NB_GID -o ${NB_GROUP:-${NB_USER}} | 
|  | 84 | +        fi | 
|  | 85 | +        userdel $NB_USER | 
|  | 86 | +        useradd --home /home/$NB_USER -u $NB_UID -g $NB_GID -G 100 -l $NB_USER | 
|  | 87 | +    fi | 
|  | 88 | + | 
|  | 89 | +    # Enable sudo if requested | 
|  | 90 | +    if [[ "$GRANT_SUDO" == "1" || "$GRANT_SUDO" == 'yes' ]]; then | 
|  | 91 | +        echo "Granting $NB_USER sudo access and appending $CONDA_DIR/bin to sudo PATH" | 
|  | 92 | +        echo "$NB_USER ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/notebook | 
|  | 93 | +    fi | 
|  | 94 | + | 
|  | 95 | +    # Add $CONDA_DIR/bin to sudo secure_path | 
|  | 96 | +    sed -r "s#Defaults\s+secure_path\s*=\s*\"?([^\"]+)\"?#Defaults secure_path=\"\1:$CONDA_DIR/bin\"#" /etc/sudoers | grep secure_path > /etc/sudoers.d/path | 
|  | 97 | + | 
|  | 98 | +    # Exec the command as NB_USER with the PATH and the rest of | 
|  | 99 | +    # the environment preserved | 
|  | 100 | +    run-hooks /usr/local/bin/before-notebook.d | 
|  | 101 | +    echo "Executing the command: ${cmd[@]}" | 
|  | 102 | +    exec sudo -E -H -u $NB_USER PATH=$PATH XDG_CACHE_HOME=/home/$NB_USER/.cache PYTHONPATH=${PYTHONPATH:-} "${cmd[@]}" | 
|  | 103 | +else | 
|  | 104 | +    if [[ "$NB_UID" == "$(id -u jovyan)" && "$NB_GID" == "$(id -g jovyan)" ]]; then | 
|  | 105 | +        # User is not attempting to override user/group via environment | 
|  | 106 | +        # variables, but they could still have overridden the uid/gid that | 
|  | 107 | +        # container runs as. Check that the user has an entry in the passwd | 
|  | 108 | +        # file and if not add an entry. | 
|  | 109 | +        STATUS=0 && whoami &> /dev/null || STATUS=$? && true | 
|  | 110 | +        if [[ "$STATUS" != "0" ]]; then | 
|  | 111 | +            if [[ -w /etc/passwd ]]; then | 
|  | 112 | +                echo "Adding passwd file entry for $(id -u)" | 
|  | 113 | +                cat /etc/passwd | sed -e "s/^jovyan:/nayvoj:/" > /tmp/passwd | 
|  | 114 | +                echo "jovyan:x:$(id -u):$(id -g):,,,:/home/jovyan:/bin/bash" >> /tmp/passwd | 
|  | 115 | +                cat /tmp/passwd > /etc/passwd | 
|  | 116 | +                rm /tmp/passwd | 
|  | 117 | +            else | 
|  | 118 | +                echo 'Container must be run with group "root" to update passwd file' | 
|  | 119 | +            fi | 
|  | 120 | +        fi | 
|  | 121 | + | 
|  | 122 | +        # Warn if the user isn't going to be able to write files to $HOME. | 
|  | 123 | +        if [[ ! -w /home/jovyan ]]; then | 
|  | 124 | +            echo 'Container must be run with group "users" to update files' | 
|  | 125 | +        fi | 
|  | 126 | +    else | 
|  | 127 | +        # Warn if looks like user want to override uid/gid but hasn't | 
|  | 128 | +        # run the container as root. | 
|  | 129 | +        if [[ ! -z "$NB_UID" && "$NB_UID" != "$(id -u)" ]]; then | 
|  | 130 | +            echo 'Container must be run as root to set $NB_UID' | 
|  | 131 | +        fi | 
|  | 132 | +        if [[ ! -z "$NB_GID" && "$NB_GID" != "$(id -g)" ]]; then | 
|  | 133 | +            echo 'Container must be run as root to set $NB_GID' | 
|  | 134 | +        fi | 
|  | 135 | +    fi | 
|  | 136 | + | 
|  | 137 | +    # Warn if looks like user want to run in sudo mode but hasn't run | 
|  | 138 | +    # the container as root. | 
|  | 139 | +    if [[ "$GRANT_SUDO" == "1" || "$GRANT_SUDO" == 'yes' ]]; then | 
|  | 140 | +        echo 'Container must be run as root to grant sudo permissions' | 
|  | 141 | +    fi | 
|  | 142 | + | 
|  | 143 | +    # Execute the command | 
|  | 144 | +    run-hooks /usr/local/bin/before-notebook.d | 
|  | 145 | +    echo "Executing the command: ${cmd[@]}" | 
|  | 146 | +    exec "${cmd[@]}" | 
|  | 147 | +fi | 
0 commit comments