Skip to content

Commit d616811

Browse files
authored
Merge pull request #1526 from maresb/quiet
Refactor logging in start.sh and add JUPYTER_DOCKER_STACKS_QUIET
2 parents 4df8548 + 23e3284 commit d616811

File tree

1 file changed

+47
-40
lines changed

1 file changed

+47
-40
lines changed

base-notebook/start.sh

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,42 @@
33
# Distributed under the terms of the Modified BSD License.
44

55
set -e
6-
echo "Running: start.sh" "$@"
76

8-
# Exec the specified command or fall back on bash
9-
if [ $# -eq 0 ]; then
10-
cmd=( "bash" )
11-
else
12-
cmd=( "$@" )
13-
fi
7+
# The _log function is used for everything this script wants to log. It will
8+
# always log errors and warnings, but can be silenced for other messages
9+
# by setting JUPYTER_DOCKER_STACKS_QUIET environment variable.
10+
_log () {
11+
if [[ "$*" == "ERROR:"* ]] || [[ "$*" == "WARNING:"* ]] || [[ "${JUPYTER_DOCKER_STACKS_QUIET}" == "" ]]; then
12+
echo "$@"
13+
fi
14+
}
15+
_log "Entered start.sh with args:" "$@"
1416

1517
# The run-hooks function looks for .sh scripts to source and executable files to
1618
# run within a passed directory.
1719
run-hooks () {
1820
if [[ ! -d "${1}" ]] ; then
1921
return
2022
fi
21-
echo "${0}: running hooks in ${1} as uid / gid: $(id -u) / $(id -g)"
23+
_log "${0}: running hooks in ${1} as uid / gid: $(id -u) / $(id -g)"
2224
for f in "${1}/"*; do
2325
case "${f}" in
2426
*.sh)
25-
echo "${0}: running script ${f}"
27+
_log "${0}: running script ${f}"
2628
# shellcheck disable=SC1090
2729
source "${f}"
2830
;;
2931
*)
3032
if [[ -x "${f}" ]] ; then
31-
echo "${0}: running executable ${f}"
33+
_log "${0}: running executable ${f}"
3234
"${f}"
3335
else
34-
echo "${0}: ignoring non-executable ${f}"
36+
_log "${0}: ignoring non-executable ${f}"
3537
fi
3638
;;
3739
esac
3840
done
39-
echo "${0}: done running hooks in ${1}"
41+
_log "${0}: done running hooks in ${1}"
4042
}
4143

4244
# A helper function to unset env vars listed in the value of the env var
@@ -52,6 +54,13 @@ unset_explicit_env_vars () {
5254
}
5355

5456

57+
# Default to starting bash if no command was specified
58+
if [ $# -eq 0 ]; then
59+
cmd=( "bash" )
60+
else
61+
cmd=( "$@" )
62+
fi
63+
5564
# NOTE: This hook will run as the user the container was started with!
5665
run-hooks /usr/local/bin/start-notebook.d
5766

@@ -74,20 +83,18 @@ if [ "$(id -u)" == 0 ] ; then
7483
# Refit the jovyan user to the desired the user (NB_USER)
7584
if id jovyan &> /dev/null ; then
7685
if ! usermod --home "/home/${NB_USER}" --login "${NB_USER}" jovyan 2>&1 | grep "no changes" > /dev/null; then
77-
echo "Updated the jovyan user:"
78-
echo "- username: jovyan -> ${NB_USER}"
79-
echo "- home dir: /home/jovyan -> /home/${NB_USER}"
86+
_log "Updated the jovyan user:"
87+
_log "- username: jovyan -> ${NB_USER}"
88+
_log "- home dir: /home/jovyan -> /home/${NB_USER}"
8089
fi
8190
elif ! id -u "${NB_USER}" &> /dev/null; then
82-
echo "ERROR: Neither the jovyan user or '${NB_USER}' exists."
83-
echo " This could be the result of stopping and starting, the"
84-
echo " container with a different NB_USER environment variable."
91+
_log "ERROR: Neither the jovyan user or '${NB_USER}' exists. This could be the result of stopping and starting, the container with a different NB_USER environment variable."
8592
exit 1
8693
fi
8794
# Ensure the desired user (NB_USER) gets its desired user id (NB_UID) and is
8895
# a member of the desired group (NB_GROUP, NB_GID)
8996
if [ "${NB_UID}" != "$(id -u "${NB_USER}")" ] || [ "${NB_GID}" != "$(id -g "${NB_USER}")" ]; then
90-
echo "Update ${NB_USER}'s UID:GID to ${NB_UID}:${NB_GID}"
97+
_log "Update ${NB_USER}'s UID:GID to ${NB_UID}:${NB_GID}"
9198
# Ensure the desired group's existence
9299
if [ "${NB_GID}" != "$(id -g "${NB_USER}")" ]; then
93100
groupadd --force --gid "${NB_GID}" --non-unique "${NB_GROUP:-${NB_USER}}"
@@ -102,39 +109,39 @@ if [ "$(id -u)" == 0 ] ; then
102109
# directory to the new location if needed.
103110
if [[ "${NB_USER}" != "jovyan" ]]; then
104111
if [[ ! -e "/home/${NB_USER}" ]]; then
105-
echo "Attempting to copy /home/jovyan to /home/${NB_USER}..."
112+
_log "Attempting to copy /home/jovyan to /home/${NB_USER}..."
106113
mkdir "/home/${NB_USER}"
107114
if cp -a /home/jovyan/. "/home/${NB_USER}/"; then
108-
echo "Success!"
115+
_log "Success!"
109116
else
110-
echo "Failed to copy data from /home/jovyan to /home/${NB_USER}!"
111-
echo "Attempting to symlink /home/jovyan to /home/${NB_USER}..."
117+
_log "Failed to copy data from /home/jovyan to /home/${NB_USER}!"
118+
_log "Attempting to symlink /home/jovyan to /home/${NB_USER}..."
112119
if ln -s /home/jovyan "/home/${NB_USER}"; then
113-
echo "Success creating symlink!"
120+
_log "Success creating symlink!"
114121
else
115-
echo "Failed to create symlink!"
122+
_log "ERROR: Failed copy data from /home/jovyan to /home/${NB_USER} or to create symlink!"
116123
exit 1
117124
fi
118125
fi
119126
fi
120127
# Ensure the current working directory is updated to the new path
121128
if [[ "${PWD}/" == "/home/jovyan/"* ]]; then
122129
new_wd="/home/${NB_USER}/${PWD:13}"
123-
echo "Changing working directory to ${new_wd}"
130+
_log "Changing working directory to ${new_wd}"
124131
cd "${new_wd}"
125132
fi
126133
fi
127134

128135
# Optionally ensure the desired user get filesystem ownership of it's home
129136
# folder and/or additional folders
130137
if [[ "${CHOWN_HOME}" == "1" || "${CHOWN_HOME}" == "yes" ]]; then
131-
echo "Ensuring /home/${NB_USER} is owned by ${NB_UID}:${NB_GID} ${CHOWN_HOME_OPTS:+(chown options: ${CHOWN_HOME_OPTS})}"
138+
_log "Ensuring /home/${NB_USER} is owned by ${NB_UID}:${NB_GID} ${CHOWN_HOME_OPTS:+(chown options: ${CHOWN_HOME_OPTS})}"
132139
# shellcheck disable=SC2086
133140
chown ${CHOWN_HOME_OPTS} "${NB_UID}:${NB_GID}" "/home/${NB_USER}"
134141
fi
135142
if [ -n "${CHOWN_EXTRA}" ]; then
136143
for extra_dir in $(echo "${CHOWN_EXTRA}" | tr ',' ' '); do
137-
echo "Ensuring ${extra_dir} is owned by ${NB_UID}:${NB_GID} ${CHOWN_HOME_OPTS:+(chown options: ${CHOWN_HOME_OPTS})}"
144+
_log "Ensuring ${extra_dir} is owned by ${NB_UID}:${NB_GID} ${CHOWN_HOME_OPTS:+(chown options: ${CHOWN_HOME_OPTS})}"
138145
# shellcheck disable=SC2086
139146
chown ${CHOWN_EXTRA_OPTS} "${NB_UID}:${NB_GID}" "${extra_dir}"
140147
done
@@ -148,15 +155,15 @@ if [ "$(id -u)" == 0 ] ; then
148155

149156
# Optionally grant passwordless sudo rights for the desired user
150157
if [[ "$GRANT_SUDO" == "1" || "$GRANT_SUDO" == "yes" ]]; then
151-
echo "Granting ${NB_USER} passwordless sudo rights!"
158+
_log "Granting ${NB_USER} passwordless sudo rights!"
152159
echo "${NB_USER} ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers.d/added-by-start-script
153160
fi
154161

155162
# NOTE: This hook is run as the root user!
156163
run-hooks /usr/local/bin/before-notebook.d
157164

158165
unset_explicit_env_vars
159-
echo "Running as ${NB_USER}:" "${cmd[@]}"
166+
_log "Running as ${NB_USER}:" "${cmd[@]}"
160167
exec sudo --preserve-env --set-home --user "${NB_USER}" \
161168
PATH="${PATH}" \
162169
PYTHONPATH="${PYTHONPATH:-}" \
@@ -167,18 +174,18 @@ if [ "$(id -u)" == 0 ] ; then
167174
else
168175
# Warn about misconfiguration of: desired username, user id, or group id
169176
if [[ -n "${NB_USER}" && "${NB_USER}" != "$(id -un)" ]]; then
170-
echo "WARNING: container must be started as root to change the desired user's name with NB_USER!"
177+
_log "WARNING: container must be started as root to change the desired user's name with NB_USER!"
171178
fi
172179
if [[ -n "${NB_UID}" && "${NB_UID}" != "$(id -u)" ]]; then
173-
echo "WARNING: container must be started as root to change the desired user's id with NB_UID!"
180+
_log "WARNING: container must be started as root to change the desired user's id with NB_UID!"
174181
fi
175182
if [[ -n "${NB_GID}" && "${NB_GID}" != "$(id -g)" ]]; then
176-
echo "WARNING: container must be started as root to change the desired user's group id with NB_GID!"
183+
_log "WARNING: container must be started as root to change the desired user's group id with NB_GID!"
177184
fi
178185

179186
# Warn about misconfiguration of: granting sudo rights
180187
if [[ "${GRANT_SUDO}" == "1" || "${GRANT_SUDO}" == "yes" ]]; then
181-
echo "WARNING: container must be started as root to grant sudo permissions!"
188+
_log "WARNING: container must be started as root to grant sudo permissions!"
182189
fi
183190

184191
# Attempt to ensure the user uid we currently run as has a named entry in
@@ -188,9 +195,9 @@ else
188195
#
189196
# ref: https://github.com/jupyter/docker-stacks/issues/552
190197
if ! whoami &> /dev/null; then
191-
echo "There is no entry in /etc/passwd for our UID. Attempting to fix..."
198+
_log "There is no entry in /etc/passwd for our UID. Attempting to fix..."
192199
if [[ -w /etc/passwd ]]; then
193-
echo "Renaming old jovyan user to nayvoj ($(id -u jovyan):$(id -g jovyan))"
200+
_log "Renaming old jovyan user to nayvoj ($(id -u jovyan):$(id -g jovyan))"
194201

195202
# We cannot use "sed --in-place" since sed tries to create a temp file in
196203
# /etc/ and we may not have write access. Apply sed on our own temp file:
@@ -199,20 +206,20 @@ else
199206
cat /tmp/passwd > /etc/passwd
200207
rm /tmp/passwd
201208

202-
echo "Added new jovyan user ($(id -u):$(id -g)). Fixed UID!"
209+
_log "Added new jovyan user ($(id -u):$(id -g)). Fixed UID!"
203210
else
204-
echo "WARNING: unable to fix missing /etc/passwd entry because we don't have write permission."
211+
_log "WARNING: unable to fix missing /etc/passwd entry because we don't have write permission."
205212
fi
206213
fi
207214

208215
# Warn if the user isn't able to write files to ${HOME}
209216
if [[ ! -w /home/jovyan ]]; then
210-
echo "WARNING: no write access to /home/jovyan. Try starting the container with group 'users' (100)."
217+
_log "WARNING: no write access to /home/jovyan. Try starting the container with group 'users' (100)."
211218
fi
212219

213220
# NOTE: This hook is run as the user we started the container as!
214221
run-hooks /usr/local/bin/before-notebook.d
215222
unset_explicit_env_vars
216-
echo "Executing the command:" "${cmd[@]}"
223+
_log "Executing the command:" "${cmd[@]}"
217224
exec "${cmd[@]}"
218225
fi

0 commit comments

Comments
 (0)