-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexpbox
More file actions
executable file
·288 lines (247 loc) · 6.96 KB
/
expbox
File metadata and controls
executable file
·288 lines (247 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/bin/bash
# Usage info
show_help() {
cat << EOF
Usage: ${0##*/} [-hltv] [-d <directory>] [-i <image>] [-n <name>] [-p <port>]
Start an EXP-enabled Jupyter Notebook or alternatively a Jupyter Lab with
an optional initial working directory.
-h display this help and exit
-l launch Jupyter Lab (default: Jupyter Notebook)
-t start a user terminal
-T Start in the tutorial directory
-d wdir position Jupyter in this directory to start (default: home)
-i image Docker image to use (default: 'the9cat/exp:24')
-n name Docker container name prefix (default: 'expbox')
-p port Jupyter port number (default: first unused port>=8888)
-P port Web server port number
-v verbose mode
Start a Jupyter Notebook in your home directory:
$ expbox
Start a Jupyter Notebook to run the pyEXP Tutorials
$ expbox -T
Start a Jupyter Lab in a specific directory under your home directory:
$ expbox -l -d /home/user/datadir
Start a Jupyter Notebook in a specific external directory. Mounted as
/work in the container:
$ expbox -d /data/nbody
Start a bash terminal in your home directory:
$ expbox -t
Start a bash terminal in a specific directory:
$ expbox -t -d /directory
EOF
}
# Flag message
flag_once=0
flag_message() {
cat << EOF
---- We use 'sudo' to check for unused ports. If you do not have or
---- can not use 'sudo', please choose Jupyter and Web ports using
---- the '-p' and '-P' flags.
----
EOF
flag_once=1
}
root_message() {
cat << EOF
---- Please run this script as a normal user, not as sudo or root.
----
EOF
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
cat << EOF
---- If you are running as root for privilege to execute docker,
---- please see https://docs.docker.com/engine/install/linux-postinstall
---- for details on configuring Docker Engine for normal users in Linux.
----
---- Alternatively, you may prefer the user-oriented Docker Desktop.
---- See https://docs.docker.com/install for instructions on
---- installing Docker Desktop for various Linux flavors.
EOF
else
cat << EOF
---- This should be the default for Mac OS or Windows OS. See
---- https://docs.docker.com/engine/install for OS-specific hints.
EOF
fi
exit
}
# Initialize variables
working_dir=""
start_page=""
verbose=0
terminal=0
jupyter_lab=0
port=0
webp=0
image="the9cat/exp:24" # Default image
name="expbox"
OPTIND=1
while getopts hvltTd:i:n:p:P: opt; do
case $opt in
h)
show_help
exit 0
;;
v) verbose=$((verbose+1))
;;
l) jupyter_lab=1
;;
t) terminal=1
;;
T) start_page="/usr/local/pyEXP-examples/Tutorials/StartHere.ipynb"
;;
d) working_dir=$OPTARG
;;
i) image=$OPTARG
;;
n) name=$OPTARG
;;
p) port=$OPTARG
;;
P) webp=$OPTARG
;;
*)
show_help >&2
exit 1
;;
esac
done
shift "$((OPTIND-1))" # Discard the options and sentinel --
# Everything that's left in "$@" is a non-option. In our case, a FILE to process.
# printf 'verbose=<%d>\noutput_file=<%s>\nLeftovers:\n' "$verbose" "$output_file"
# printf '<%s>\n' "$@"
if [ "$EUID" -eq 0 ]
then
root_message
fi
# Get an unused port in a range
WEBP=8080
WLST=8800
PORT=8888
LAST=12000
if (( webp > 0 )); then
WEBP=$webp
else
if (( flag_once == 0 )); then
flag_message
fi
until (( WEBP > WLST ))
do
if [[ -z `sudo lsof -i ":$WEBP"` ]]; then
if ((verbose > 0)); then
printf 'Found available port=<%d>\n' $WEBP;
fi
break;
elif (( WEBP == WLST )); then
echo "No ports available";
exit 1;
fi
let WEBP+=1;
done
fi
if (( port > 0 )); then
PORT=$port
else
if (( flag_once == 0 )); then
flag_message
fi
until (( PORT > LAST ))
do
if [[ -z `sudo lsof -i ":$PORT"` ]] && [[ -z `docker ps -a -q -f name="expbox_$PORT"` ]]; then
if ((verbose > 0)); then
printf 'Found available port=<%d>\n' $PORT;
fi
break;
elif (( PORT == LAST )); then
echo "No ports available";
exit 1;
fi
let PORT+=1;
done
fi
# Get the home directory prefix
if (( verbose > 0 )); then
printf '**** PORT=%d\n' $PORT;
fi
homedir="$(dirname $HOME)"
if (( verbose > 0 )); then
printf '**** Home base is %s\n' $homedir
fi
change_home=0
if [ $homedir != "/home" ]; then
if (( verbose > 0 )); then
printf 'Found home=<%s>\n' $homedir;
fi
change_home=1
fi
set -eu
USER=$(id -un)
# UID=$(id -u)
GID=$(id -g)
GROUP=$(id -gn)
NAME="${name}_$PORT"
MOUNT=""
WDIR=""
JTYPE="notebook"
if (( verbose > 0 )); then
printf '**** Container name is %s\n' $NAME
fi
if [[ -z "$working_dir" ]]; then
working_dir="$homedir/$USER";
WDIR="$working_dir";
if (( verbose > 0 )); then
printf '**** Working directory is %s and was null\n' $WDIR;
fi
else
if [[ $working_dir != "$homedir"* ]]; then
WDIR="/work"
MOUNT="-v $working_dir:/work";
else
WDIR="$working_dir";
fi
fi
if (( jupyter_lab > 0 )); then
JTYPE="lab";
fi
if ((verbose > 0)); then
printf '**** Jupyter flag is %d\n' $jupyter_lab
printf '**** Jupyter type is %s\n' $JTYPE
printf '**** Working directory is %s\n' $WDIR
printf '**** Mount command is %s\n' $MOUNT
printf '**** Home directory is %s\n' $homedir/$USER
fi
# Starts the container in an interactive, detached state in the data directory
echo "---- The container name is: $NAME"
echo -n "---- The container id is: "
docker run -dit -p $PORT:$PORT -p $WEBP:80 -w $WDIR $MOUNT -v $HOME:$homedir/$USER --name "$NAME" "$image" /bin/bash
echo "----"
echo "---- Stop the running container using: docker stop $NAME"
echo "---- Remove the stopped container using: docker rm $NAME"
echo "---- Or do both at the same time using: docker rm -f $NAME"
echo "----"
echo "---- The 'readthedocs' manual is included in this Docker container!"
echo "---- Point at http://localhost:$WEBP or http://127.0.0.1:$WEBP to see it."
echo "----"
echo "---- Adding user $USER."
# Add a new home base
if (( change_home > 0 )); then
docker exec "$NAME" /bin/bash -c "mkdir -p $homedir"
fi
# Add the group (if the group exists this will exit, no problem)
docker exec "$NAME" /bin/bash -c "groupadd -f -g $GID $GROUP"
# Attach to the container and add the host user
docker exec "$NAME" /bin/bash -c "useradd -M -b $homedir -s /bin/bash -g $GID -u $UID $USER"
echo "----"
echo "---- Starting the documentation web server"
# Start the web server
docker exec -d $NAME /bin/bash --norc -c "busybox httpd -h /var/www/html"
if ((terminal > 0)); then
echo "----"
echo "---- Beginning an interactive terminal session"
echo "----"
docker exec -it --user $USER "$NAME" /bin/bash --norc
else
echo "----"
echo "---- Starting Jupyter for user $USER"
echo "----"
docker exec --user $USER "$NAME" /bin/bash --norc -c "jupyter $JTYPE --ip 0.0.0.0 --port=$PORT --no-browser $start_page"
fi