Skip to content

Commit 396024a

Browse files
authored
Merge pull request #1335 from mathbunnyru/asalikhov/unify_bash_variables
Unify bash variables usage and add quotes where needed
2 parents 04fe694 + 7642ba8 commit 396024a

File tree

26 files changed

+185
-185
lines changed

26 files changed

+185
-185
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ JupyterLab, where `hostname` is the name of the computer running docker and `tok
4949
token printed in the console. Docker destroys the container after notebook server exit, but any
5050
files written to `~/work` in the container remain intact on the host.
5151

52-
docker run --rm -p 10000:8888 -e JUPYTER_ENABLE_LAB=yes -v "$PWD":/home/jovyan/work jupyter/datascience-notebook:33add21fab64
52+
docker run --rm -p 10000:8888 -e JUPYTER_ENABLE_LAB=yes -v "${PWD}":/home/jovyan/work jupyter/datascience-notebook:33add21fab64
5353

5454
## Contributing
5555

all-spark-notebook/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ RUN apt-get update --yes && \
1919
gcc && \
2020
apt-get clean && rm -rf /var/lib/apt/lists/*
2121

22-
USER $NB_UID
22+
USER ${NB_UID}
2323

2424
# R packages including IRKernel which gets installed globally.
2525
RUN conda install --quiet --yes \

base-notebook/Dockerfile

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ RUN apt-get update --yes && \
6060
# Configure environment
6161
ENV CONDA_DIR=/opt/conda \
6262
SHELL=/bin/bash \
63-
NB_USER=$NB_USER \
64-
NB_UID=$NB_UID \
65-
NB_GID=$NB_GID \
63+
NB_USER="${NB_USER}" \
64+
NB_UID=${NB_UID} \
65+
NB_GID=${NB_GID} \
6666
LC_ALL=en_US.UTF-8 \
6767
LANG=en_US.UTF-8 \
6868
LANGUAGE=en_US.UTF-8
69-
ENV PATH=$CONDA_DIR/bin:$PATH \
70-
HOME=/home/$NB_USER \
69+
ENV PATH="${CONDA_DIR}/bin:${PATH}" \
70+
HOME="/home/${NB_USER}" \
7171
CONDA_VERSION="${conda_version}" \
7272
MINIFORGE_VERSION="${miniforge_version}"
7373

@@ -86,18 +86,18 @@ RUN sed -i 's/^#force_color_prompt=yes/force_color_prompt=yes/' /etc/skel/.bashr
8686
RUN echo "auth requisite pam_deny.so" >> /etc/pam.d/su && \
8787
sed -i.bak -e 's/^%admin/#%admin/' /etc/sudoers && \
8888
sed -i.bak -e 's/^%sudo/#%sudo/' /etc/sudoers && \
89-
useradd -l -m -s /bin/bash -N -u $NB_UID $NB_USER && \
90-
mkdir -p $CONDA_DIR && \
91-
chown $NB_USER:$NB_GID $CONDA_DIR && \
89+
useradd -l -m -s /bin/bash -N -u "${NB_UID}" "${NB_USER}" && \
90+
mkdir -p "${CONDA_DIR}" && \
91+
chown "${NB_USER}:${NB_GID}" "${CONDA_DIR}" && \
9292
chmod g+w /etc/passwd && \
9393
fix-permissions "${HOME}" && \
9494
fix-permissions "${CONDA_DIR}"
9595

96-
USER $NB_UID
96+
USER ${NB_UID}
9797
ARG PYTHON_VERSION=default
9898

9999
# Setup work directory for backward-compatibility
100-
RUN mkdir "/home/$NB_USER/work" && \
100+
RUN mkdir "/home/${NB_USER}/work" && \
101101
fix-permissions "/home/${NB_USER}"
102102

103103
# Install conda as jovyan and check the sha256 sum provided on the download site
@@ -106,20 +106,20 @@ WORKDIR /tmp
106106
# Prerequisites installation: conda, mamba, pip, tini
107107
RUN wget --quiet "https://github.com/conda-forge/miniforge/releases/download/${miniforge_version}/${miniforge_installer}" && \
108108
echo "${miniforge_checksum} *${miniforge_installer}" | sha256sum --check && \
109-
/bin/bash "${miniforge_installer}" -f -b -p $CONDA_DIR && \
109+
/bin/bash "${miniforge_installer}" -f -b -p "${CONDA_DIR}" && \
110110
rm "${miniforge_installer}" && \
111111
# Conda configuration see https://conda.io/projects/conda/en/latest/configuration.html
112-
echo "conda ${CONDA_VERSION}" >> $CONDA_DIR/conda-meta/pinned && \
112+
echo "conda ${CONDA_VERSION}" >> "${CONDA_DIR}/conda-meta/pinned" && \
113113
conda config --system --set auto_update_conda false && \
114114
conda config --system --set show_channel_urls true && \
115-
if [ ! $PYTHON_VERSION = 'default' ]; then conda install --yes python=$PYTHON_VERSION; fi && \
116-
conda list python | grep '^python ' | tr -s ' ' | cut -d '.' -f 1,2 | sed 's/$/.*/' >> $CONDA_DIR/conda-meta/pinned && \
115+
if [[ "${PYTHON_VERSION}" != "default" ]]; then conda install --yes python="${PYTHON_VERSION}"; fi && \
116+
conda list python | grep '^python ' | tr -s ' ' | cut -d '.' -f 1,2 | sed 's/$/.*/' >> "${CONDA_DIR}/conda-meta/pinned" && \
117117
conda install --quiet --yes \
118118
"conda=${CONDA_VERSION}" \
119119
'pip' && \
120120
conda update --all --quiet --yes && \
121121
conda clean --all -f -y && \
122-
rm -rf /home/$NB_USER/.cache/yarn && \
122+
rm -rf "/home/${NB_USER}/.cache/yarn" && \
123123
fix-permissions "${CONDA_DIR}" && \
124124
fix-permissions "/home/${NB_USER}"
125125

@@ -137,7 +137,7 @@ RUN conda install --quiet --yes \
137137
npm cache clean --force && \
138138
jupyter notebook --generate-config && \
139139
jupyter lab clean && \
140-
rm -rf /home/$NB_USER/.cache/yarn && \
140+
rm -rf "/home/${NB_USER}/.cache/yarn" && \
141141
fix-permissions "${CONDA_DIR}" && \
142142
fix-permissions "/home/${NB_USER}"
143143

@@ -161,6 +161,6 @@ RUN sed -re "s/c.NotebookApp/c.ServerApp/g" \
161161
fix-permissions /etc/jupyter/
162162

163163
# Switch back to jovyan to avoid accidental container runs as root
164-
USER $NB_UID
164+
USER ${NB_UID}
165165

166-
WORKDIR $HOME
166+
WORKDIR "${HOME}"

base-notebook/fix-permissions

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# set permissions on a directory
33
# after any installation, if a directory needs to be (human) user-writable,
44
# run this script on it.
5-
# It will make everything in the directory owned by the group $NB_GID
5+
# It will make everything in the directory owned by the group ${NB_GID}
66
# and writable by that group.
77
# Deployments that want to set a specific user id can preserve permissions
88
# by adding the `--group-add users` line to `docker run`.
@@ -11,22 +11,22 @@
1111
# which would cause massive image explosion
1212

1313
# right permissions are:
14-
# group=$NB_GID
14+
# group=${NB_GID}
1515
# AND permissions include group rwX (directory-execute)
1616
# AND directories have setuid,setgid bits set
1717

1818
set -e
1919

2020
for d in "$@"; do
21-
find "$d" \
21+
find "${d}" \
2222
! \( \
2323
-group "${NB_GID}" \
2424
-a -perm -g+rwX \
2525
\) \
2626
-exec chgrp "${NB_GID}" {} \; \
2727
-exec chmod g+rwX {} \;
2828
# setuid, setgid *on directories only*
29-
find "$d" \
29+
find "${d}" \
3030
\( \
3131
-type d \
3232
-a ! -perm -6000 \

base-notebook/start-notebook.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ if [[ -n "${JUPYTERHUB_API_TOKEN}" ]]; then
1414
exec /usr/local/bin/start-singleuser.sh "$@"
1515
elif [[ -n "${JUPYTER_ENABLE_LAB}" ]]; then
1616
# shellcheck disable=SC1091
17-
. /usr/local/bin/start.sh $wrapper jupyter lab "$@"
17+
. /usr/local/bin/start.sh ${wrapper} jupyter lab "$@"
1818
else
1919
echo "WARN: Jupyter Notebook deprecation notice https://github.com/jupyter/docker-stacks#jupyter-notebook-deprecation-notice."
2020
# shellcheck disable=SC1091
21-
. /usr/local/bin/start.sh $wrapper jupyter notebook "$@"
21+
. /usr/local/bin/start.sh ${wrapper} jupyter notebook "$@"
2222
fi

base-notebook/start-singleuser.sh

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,37 @@
55
set -e
66

77
# set default ip to 0.0.0.0
8-
if [[ "$NOTEBOOK_ARGS $*" != *"--ip="* ]]; then
9-
NOTEBOOK_ARGS="--ip=0.0.0.0 $NOTEBOOK_ARGS"
8+
if [[ "${NOTEBOOK_ARGS} $*" != *"--ip="* ]]; then
9+
NOTEBOOK_ARGS="--ip=0.0.0.0 ${NOTEBOOK_ARGS}"
1010
fi
1111

1212
# handle some deprecated environment variables
1313
# from DockerSpawner < 0.8.
1414
# These won't be passed from DockerSpawner 0.9,
1515
# so avoid specifying --arg=empty-string
16-
if [ -n "$NOTEBOOK_DIR" ]; then
16+
if [ -n "${NOTEBOOK_DIR}" ]; then
1717
# shellcheck disable=SC2089
18-
NOTEBOOK_ARGS="--notebook-dir='$NOTEBOOK_DIR' $NOTEBOOK_ARGS"
18+
NOTEBOOK_ARGS="--notebook-dir='${NOTEBOOK_DIR}' ${NOTEBOOK_ARGS}"
1919
fi
20-
if [ -n "$JPY_PORT" ]; then
21-
NOTEBOOK_ARGS="--port=$JPY_PORT $NOTEBOOK_ARGS"
20+
if [ -n "${JPY_PORT}" ]; then
21+
NOTEBOOK_ARGS="--port=${JPY_PORT} ${NOTEBOOK_ARGS}"
2222
fi
23-
if [ -n "$JPY_USER" ]; then
24-
NOTEBOOK_ARGS="--user=$JPY_USER $NOTEBOOK_ARGS"
23+
if [ -n "${JPY_USER}" ]; then
24+
NOTEBOOK_ARGS="--user=${JPY_USER} ${NOTEBOOK_ARGS}"
2525
fi
26-
if [ -n "$JPY_COOKIE_NAME" ]; then
27-
NOTEBOOK_ARGS="--cookie-name=$JPY_COOKIE_NAME $NOTEBOOK_ARGS"
26+
if [ -n "${JPY_COOKIE_NAME}" ]; then
27+
NOTEBOOK_ARGS="--cookie-name=${JPY_COOKIE_NAME} ${NOTEBOOK_ARGS}"
2828
fi
29-
if [ -n "$JPY_BASE_URL" ]; then
30-
NOTEBOOK_ARGS="--base-url=$JPY_BASE_URL $NOTEBOOK_ARGS"
29+
if [ -n "${JPY_BASE_URL}" ]; then
30+
NOTEBOOK_ARGS="--base-url=${JPY_BASE_URL} ${NOTEBOOK_ARGS}"
3131
fi
32-
if [ -n "$JPY_HUB_PREFIX" ]; then
33-
NOTEBOOK_ARGS="--hub-prefix=$JPY_HUB_PREFIX $NOTEBOOK_ARGS"
32+
if [ -n "${JPY_HUB_PREFIX}" ]; then
33+
NOTEBOOK_ARGS="--hub-prefix=${JPY_HUB_PREFIX} ${NOTEBOOK_ARGS}"
3434
fi
35-
if [ -n "$JPY_HUB_API_URL" ]; then
36-
NOTEBOOK_ARGS="--hub-api-url=$JPY_HUB_API_URL $NOTEBOOK_ARGS"
35+
if [ -n "${JPY_HUB_API_URL}" ]; then
36+
NOTEBOOK_ARGS="--hub-api-url=${JPY_HUB_API_URL} ${NOTEBOOK_ARGS}"
3737
fi
3838
NOTEBOOK_BIN="jupyterhub-singleuser"
3939

4040
# shellcheck disable=SC1091,SC2086,SC2090
41-
. /usr/local/bin/start.sh "$NOTEBOOK_BIN" $NOTEBOOK_ARGS "$@"
41+
. /usr/local/bin/start.sh "${NOTEBOOK_BIN}" ${NOTEBOOK_ARGS} "$@"

base-notebook/start.sh

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,28 @@ fi
1313

1414
run-hooks () {
1515
# Source scripts or run executable files in a directory
16-
if [[ ! -d "$1" ]] ; then
16+
if [[ ! -d "${1}" ]] ; then
1717
return
1818
fi
19-
echo "$0: running hooks in $1"
20-
for f in "$1/"*; do
21-
case "$f" in
19+
echo "${0}: running hooks in ${1}"
20+
for f in "${1}/"*; do
21+
case "${f}" in
2222
*.sh)
23-
echo "$0: running $f"
23+
echo "${0}: running ${f}"
2424
# shellcheck disable=SC1090
25-
source "$f"
25+
source "${f}"
2626
;;
2727
*)
28-
if [[ -x "$f" ]] ; then
29-
echo "$0: running $f"
30-
"$f"
28+
if [[ -x "${f}" ]] ; then
29+
echo "${0}: running ${f}"
30+
"${f}"
3131
else
32-
echo "$0: ignoring $f"
32+
echo "${0}: ignoring ${f}"
3333
fi
3434
;;
3535
esac
3636
done
37-
echo "$0: done running hooks in $1"
37+
echo "${0}: done running hooks in ${1}"
3838
}
3939

4040
run-hooks /usr/local/bin/start-notebook.d
@@ -44,73 +44,73 @@ if [ "$(id -u)" == 0 ] ; then
4444

4545
# Only attempt to change the jovyan username if it exists
4646
if id jovyan &> /dev/null ; then
47-
echo "Set username to: $NB_USER"
48-
usermod -d "/home/$NB_USER" -l "$NB_USER" jovyan
47+
echo "Set username to: ${NB_USER}"
48+
usermod -d "/home/${NB_USER}" -l "${NB_USER}" jovyan
4949
fi
5050

5151
# handle home and working directory if the username changed
52-
if [[ "$NB_USER" != "jovyan" ]]; then
52+
if [[ "${NB_USER}" != "jovyan" ]]; then
5353
# changing username, make sure homedir exists
5454
# (it could be mounted, and we shouldn't create it if it already exists)
55-
if [[ ! -e "/home/$NB_USER" ]]; then
56-
echo "Relocating home dir to /home/$NB_USER"
57-
mv /home/jovyan "/home/$NB_USER" || ln -s /home/jovyan "/home/$NB_USER"
55+
if [[ ! -e "/home/${NB_USER}" ]]; then
56+
echo "Relocating home dir to /home/${NB_USER}"
57+
mv /home/jovyan "/home/${NB_USER}" || ln -s /home/jovyan "/home/${NB_USER}"
5858
fi
59-
# if workdir is in /home/jovyan, cd to /home/$NB_USER
60-
if [[ "$PWD/" == "/home/jovyan/"* ]]; then
61-
newcwd="/home/$NB_USER/${PWD:13}"
62-
echo "Setting CWD to $newcwd"
63-
cd "$newcwd"
59+
# if workdir is in /home/jovyan, cd to /home/${NB_USER}
60+
if [[ "${PWD}/" == "/home/jovyan/"* ]]; then
61+
newcwd="/home/${NB_USER}/${PWD:13}"
62+
echo "Setting CWD to ${newcwd}"
63+
cd "${newcwd}"
6464
fi
6565
fi
6666

6767
# Handle case where provisioned storage does not have the correct permissions by default
6868
# Ex: default NFS/EFS (no auto-uid/gid)
69-
if [[ "$CHOWN_HOME" == "1" || "$CHOWN_HOME" == 'yes' ]]; then
70-
echo "Changing ownership of /home/$NB_USER to $NB_UID:$NB_GID with options '${CHOWN_HOME_OPTS}'"
69+
if [[ "${CHOWN_HOME}" == "1" || "${CHOWN_HOME}" == 'yes' ]]; then
70+
echo "Changing ownership of /home/${NB_USER} to ${NB_UID}:${NB_GID} with options '${CHOWN_HOME_OPTS}'"
7171
# shellcheck disable=SC2086
72-
chown $CHOWN_HOME_OPTS "$NB_UID:$NB_GID" "/home/$NB_USER"
72+
chown ${CHOWN_HOME_OPTS} "${NB_UID}:${NB_GID}" "/home/${NB_USER}"
7373
fi
74-
if [ -n "$CHOWN_EXTRA" ]; then
75-
for extra_dir in $(echo "$CHOWN_EXTRA" | tr ',' ' '); do
76-
echo "Changing ownership of ${extra_dir} to $NB_UID:$NB_GID with options '${CHOWN_EXTRA_OPTS}'"
74+
if [ -n "${CHOWN_EXTRA}" ]; then
75+
for extra_dir in $(echo "${CHOWN_EXTRA}" | tr ',' ' '); do
76+
echo "Changing ownership of ${extra_dir} to ${NB_UID}:${NB_GID} with options '${CHOWN_EXTRA_OPTS}'"
7777
# shellcheck disable=SC2086
78-
chown $CHOWN_EXTRA_OPTS "$NB_UID:$NB_GID" "$extra_dir"
78+
chown ${CHOWN_EXTRA_OPTS} "${NB_UID}:${NB_GID}" "${extra_dir}"
7979
done
8080
fi
8181

8282
# Change UID:GID of NB_USER to NB_UID:NB_GID if it does not match
83-
if [ "$NB_UID" != "$(id -u "$NB_USER")" ] || [ "$NB_GID" != "$(id -g "$NB_USER")" ]; then
84-
echo "Set user $NB_USER UID:GID to: $NB_UID:$NB_GID"
85-
if [ "$NB_GID" != "$(id -g "$NB_USER")" ]; then
86-
groupadd -f -g "$NB_GID" -o "${NB_GROUP:-${NB_USER}}"
83+
if [ "${NB_UID}" != "$(id -u "${NB_USER}")" ] || [ "${NB_GID}" != "$(id -g "${NB_USER}")" ]; then
84+
echo "Set user ${NB_USER} UID:GID to: ${NB_UID}:${NB_GID}"
85+
if [ "${NB_GID}" != "$(id -g "${NB_USER}")" ]; then
86+
groupadd -f -g "${NB_GID}" -o "${NB_GROUP:-${NB_USER}}"
8787
fi
88-
userdel "$NB_USER"
89-
useradd --home "/home/$NB_USER" -u "$NB_UID" -g "$NB_GID" -G 100 -l "$NB_USER"
88+
userdel "${NB_USER}"
89+
useradd --home "/home/${NB_USER}" -u "${NB_UID}" -g "${NB_GID}" -G 100 -l "${NB_USER}"
9090
fi
9191

9292
# Enable sudo if requested
93-
if [[ "$GRANT_SUDO" == "1" || "$GRANT_SUDO" == 'yes' ]]; then
94-
echo "Granting $NB_USER sudo access and appending $CONDA_DIR/bin to sudo PATH"
95-
echo "$NB_USER ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/notebook
93+
if [[ "${GRANT_SUDO}" == "1" || "${GRANT_SUDO}" == 'yes' ]]; then
94+
echo "Granting ${NB_USER} sudo access and appending ${CONDA_DIR}/bin to sudo PATH"
95+
echo "${NB_USER} ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/notebook
9696
fi
9797

98-
# Add $CONDA_DIR/bin to sudo secure_path
99-
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
98+
# Add ${CONDA_DIR}/bin to sudo secure_path
99+
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
100100

101101
# Exec the command as NB_USER with the PATH and the rest of
102102
# the environment preserved
103103
run-hooks /usr/local/bin/before-notebook.d
104104
echo "Executing the command:" "${cmd[@]}"
105-
exec sudo -E -H -u "$NB_USER" PATH="$PATH" XDG_CACHE_HOME="/home/$NB_USER/.cache" PYTHONPATH="${PYTHONPATH:-}" "${cmd[@]}"
105+
exec sudo -E -H -u "${NB_USER}" PATH="${PATH}" XDG_CACHE_HOME="/home/${NB_USER}/.cache" PYTHONPATH="${PYTHONPATH:-}" "${cmd[@]}"
106106
else
107-
if [[ "$NB_UID" == "$(id -u jovyan 2>/dev/null)" && "$NB_GID" == "$(id -g jovyan 2>/dev/null)" ]]; then
107+
if [[ "${NB_UID}" == "$(id -u jovyan 2>/dev/null)" && "${NB_GID}" == "$(id -g jovyan 2>/dev/null)" ]]; then
108108
# User is not attempting to override user/group via environment
109109
# variables, but they could still have overridden the uid/gid that
110110
# container runs as. Check that the user has an entry in the passwd
111111
# file and if not add an entry.
112112
STATUS=0 && whoami &> /dev/null || STATUS=$? && true
113-
if [[ "$STATUS" != "0" ]]; then
113+
if [[ "${STATUS}" != "0" ]]; then
114114
if [[ -w /etc/passwd ]]; then
115115
echo "Adding passwd file entry for $(id -u)"
116116
sed -e "s/^jovyan:/nayvoj:/" /etc/passwd > /tmp/passwd
@@ -122,24 +122,24 @@ else
122122
fi
123123
fi
124124

125-
# Warn if the user isn't going to be able to write files to $HOME.
125+
# Warn if the user isn't going to be able to write files to ${HOME}.
126126
if [[ ! -w /home/jovyan ]]; then
127127
echo 'Container must be run with group "users" to update files'
128128
fi
129129
else
130130
# Warn if looks like user want to override uid/gid but hasn't
131131
# run the container as root.
132-
if [[ -n "$NB_UID" && "$NB_UID" != "$(id -u)" ]]; then
133-
echo "Container must be run as root to set NB_UID to $NB_UID"
132+
if [[ -n "${NB_UID}" && "${NB_UID}" != "$(id -u)" ]]; then
133+
echo "Container must be run as root to set NB_UID to ${NB_UID}"
134134
fi
135-
if [[ -n "$NB_GID" && "$NB_GID" != "$(id -g)" ]]; then
136-
echo "Container must be run as root to set NB_GID to $NB_GID"
135+
if [[ -n "${NB_GID}" && "${NB_GID}" != "$(id -g)" ]]; then
136+
echo "Container must be run as root to set NB_GID to ${NB_GID}"
137137
fi
138138
fi
139139

140140
# Warn if looks like user want to run in sudo mode but hasn't run
141141
# the container as root.
142-
if [[ "$GRANT_SUDO" == "1" || "$GRANT_SUDO" == 'yes' ]]; then
142+
if [[ "${GRANT_SUDO}" == "1" || "${GRANT_SUDO}" == 'yes' ]]; then
143143
echo 'Container must be run as root to grant sudo permissions'
144144
fi
145145

0 commit comments

Comments
 (0)