3
3
# Distributed under the terms of the Modified BSD License.
4
4
5
5
set -e
6
- echo " Running: start.sh" " $@ "
7
6
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:" " $@ "
14
16
15
17
# The run-hooks function looks for .sh scripts to source and executable files to
16
18
# run within a passed directory.
17
19
run-hooks () {
18
20
if [[ ! -d " ${1} " ]] ; then
19
21
return
20
22
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) "
22
24
for f in " ${1} /" * ; do
23
25
case " ${f} " in
24
26
* .sh)
25
- echo " ${0} : running script ${f} "
27
+ _log " ${0} : running script ${f} "
26
28
# shellcheck disable=SC1090
27
29
source " ${f} "
28
30
;;
29
31
* )
30
32
if [[ -x " ${f} " ]] ; then
31
- echo " ${0} : running executable ${f} "
33
+ _log " ${0} : running executable ${f} "
32
34
" ${f} "
33
35
else
34
- echo " ${0} : ignoring non-executable ${f} "
36
+ _log " ${0} : ignoring non-executable ${f} "
35
37
fi
36
38
;;
37
39
esac
38
40
done
39
- echo " ${0} : done running hooks in ${1} "
41
+ _log " ${0} : done running hooks in ${1} "
40
42
}
41
43
42
44
# A helper function to unset env vars listed in the value of the env var
@@ -52,6 +54,13 @@ unset_explicit_env_vars () {
52
54
}
53
55
54
56
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
+
55
64
# NOTE: This hook will run as the user the container was started with!
56
65
run-hooks /usr/local/bin/start-notebook.d
57
66
@@ -74,20 +83,18 @@ if [ "$(id -u)" == 0 ] ; then
74
83
# Refit the jovyan user to the desired the user (NB_USER)
75
84
if id jovyan & > /dev/null ; then
76
85
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} "
80
89
fi
81
90
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."
85
92
exit 1
86
93
fi
87
94
# Ensure the desired user (NB_USER) gets its desired user id (NB_UID) and is
88
95
# a member of the desired group (NB_GROUP, NB_GID)
89
96
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} "
91
98
# Ensure the desired group's existence
92
99
if [ " ${NB_GID} " != " $( id -g " ${NB_USER} " ) " ]; then
93
100
groupadd --force --gid " ${NB_GID} " --non-unique " ${NB_GROUP:- ${NB_USER} } "
@@ -102,39 +109,39 @@ if [ "$(id -u)" == 0 ] ; then
102
109
# directory to the new location if needed.
103
110
if [[ " ${NB_USER} " != " jovyan" ]]; then
104
111
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} ..."
106
113
mkdir " /home/${NB_USER} "
107
114
if cp -a /home/jovyan/. " /home/${NB_USER} /" ; then
108
- echo " Success!"
115
+ _log " Success!"
109
116
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} ..."
112
119
if ln -s /home/jovyan " /home/${NB_USER} " ; then
113
- echo " Success creating symlink!"
120
+ _log " Success creating symlink!"
114
121
else
115
- echo " Failed to create symlink!"
122
+ _log " ERROR: Failed copy data from /home/jovyan to /home/ ${NB_USER} or to create symlink!"
116
123
exit 1
117
124
fi
118
125
fi
119
126
fi
120
127
# Ensure the current working directory is updated to the new path
121
128
if [[ " ${PWD} /" == " /home/jovyan/" * ]]; then
122
129
new_wd=" /home/${NB_USER} /${PWD: 13} "
123
- echo " Changing working directory to ${new_wd} "
130
+ _log " Changing working directory to ${new_wd} "
124
131
cd " ${new_wd} "
125
132
fi
126
133
fi
127
134
128
135
# Optionally ensure the desired user get filesystem ownership of it's home
129
136
# folder and/or additional folders
130
137
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} )} "
132
139
# shellcheck disable=SC2086
133
140
chown ${CHOWN_HOME_OPTS} " ${NB_UID} :${NB_GID} " " /home/${NB_USER} "
134
141
fi
135
142
if [ -n " ${CHOWN_EXTRA} " ]; then
136
143
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} )} "
138
145
# shellcheck disable=SC2086
139
146
chown ${CHOWN_EXTRA_OPTS} " ${NB_UID} :${NB_GID} " " ${extra_dir} "
140
147
done
@@ -148,15 +155,15 @@ if [ "$(id -u)" == 0 ] ; then
148
155
149
156
# Optionally grant passwordless sudo rights for the desired user
150
157
if [[ " $GRANT_SUDO " == " 1" || " $GRANT_SUDO " == " yes" ]]; then
151
- echo " Granting ${NB_USER} passwordless sudo rights!"
158
+ _log " Granting ${NB_USER} passwordless sudo rights!"
152
159
echo " ${NB_USER} ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers.d/added-by-start-script
153
160
fi
154
161
155
162
# NOTE: This hook is run as the root user!
156
163
run-hooks /usr/local/bin/before-notebook.d
157
164
158
165
unset_explicit_env_vars
159
- echo " Running as ${NB_USER} :" " ${cmd[@]} "
166
+ _log " Running as ${NB_USER} :" " ${cmd[@]} "
160
167
exec sudo --preserve-env --set-home --user " ${NB_USER} " \
161
168
PATH=" ${PATH} " \
162
169
PYTHONPATH=" ${PYTHONPATH:- } " \
@@ -167,18 +174,18 @@ if [ "$(id -u)" == 0 ] ; then
167
174
else
168
175
# Warn about misconfiguration of: desired username, user id, or group id
169
176
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!"
171
178
fi
172
179
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!"
174
181
fi
175
182
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!"
177
184
fi
178
185
179
186
# Warn about misconfiguration of: granting sudo rights
180
187
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!"
182
189
fi
183
190
184
191
# Attempt to ensure the user uid we currently run as has a named entry in
188
195
#
189
196
# ref: https://github.com/jupyter/docker-stacks/issues/552
190
197
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..."
192
199
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) )"
194
201
195
202
# We cannot use "sed --in-place" since sed tries to create a temp file in
196
203
# /etc/ and we may not have write access. Apply sed on our own temp file:
@@ -199,20 +206,20 @@ else
199
206
cat /tmp/passwd > /etc/passwd
200
207
rm /tmp/passwd
201
208
202
- echo " Added new jovyan user ($( id -u) :$( id -g) ). Fixed UID!"
209
+ _log " Added new jovyan user ($( id -u) :$( id -g) ). Fixed UID!"
203
210
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."
205
212
fi
206
213
fi
207
214
208
215
# Warn if the user isn't able to write files to ${HOME}
209
216
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)."
211
218
fi
212
219
213
220
# NOTE: This hook is run as the user we started the container as!
214
221
run-hooks /usr/local/bin/before-notebook.d
215
222
unset_explicit_env_vars
216
- echo " Executing the command:" " ${cmd[@]} "
223
+ _log " Executing the command:" " ${cmd[@]} "
217
224
exec " ${cmd[@]} "
218
225
fi
0 commit comments