-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_jupyter_notebooks
executable file
·158 lines (142 loc) · 4.36 KB
/
run_jupyter_notebooks
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
#!/usr/bin/env bash
USAGE="Usage: run_jupyter_notebooks -i <image> -u <user list> -p <admin password>\n
\n
This command generates docker containers based on a jupyter notebook container for each user\n
\n
-i image name. If not present yet it will be downloaded from docker hub. Should be based on a jupyter stacks image. Default: jupyter/base-notebook\n
-u user list. Tab delimited list of users with three columns: port, user name, password\n
-p admin password. Password used for the admin container.\n
-m maximum memory for the user container. Default: 16g.\n
-c maximum number of cpu for the user container. Default: 4.\n
-n container name prefix. Default: jupyter \n
-a add admin container[yes/no]. Default: yes \n
-d add an additional port at port-2000 [yes/no]. Default: no"
while getopts ":i:u:p:m:c:n:a:d:" opt
do
case $opt in
i)
IMAGE=$OPTARG
;;
u)
USERLIST=$OPTARG
;;
p)
MASTERPW=$OPTARG
;;
m)
MEMORY=$OPTARG
;;
c)
CPU=$OPTARG
;;
n)
CONTAINER_NAME=$OPTARG
;;
a)
ADD_ADMIN=$OPTARG
;;
d)
ADD_PORT=$OPTARG
;;
\?)
echo -e "Invalid option: -$OPTARG \n" >&2
echo -e $USAGE >&2
exit 1
;;
:)
echo -e "Option -$OPTARG requires an argument. \n"
echo -e $USAGE >&2
exit 1
;;
esac
done
# return usage if no options are passed
if [ $OPTIND -eq 1 ]
then
echo -e "No options were passed. \n" >&2
echo -e $USAGE >&2
exit 1
fi
# required options
if [ "$USERLIST" == "" ]; then echo "option -u is missing, but required">&2 && exit 1; fi
if [ "$MASTERPW" == "" ]; then echo "option -p is missing, but required">&2 && exit 1; fi
# default values
if [ "$IMAGE" == "" ]; then IMAGE=jupyter/base-notebook; fi
if [ "$MEMORY" == "" ]; then MEMORY=16g; fi
if [ "$CPU" == "" ]; then CPU=4; fi
if [ "$CONTAINER_NAME" == "" ]; then CONTAINER_NAME=jupyter; fi
if [ "$ADD_ADMIN" == "" ]; then ADD_ADMIN=yes; fi
if [ "$ADD_PORT" == "" ]; then ADD_PORT=no; fi
# create general volume that will be populated with read-only data
docker volume create data
# create general volume for group work
docker volume create group
# generate hash for master password
# HASH=`python3 ./generate_pw_hash.py -p $MASTERPW`
# generate admin container
# the option -e GRANT_SUDO=yes is required to e.g. change permissions in the root directory
docker volume create jovyan
if [ "$ADD_ADMIN" == "yes" ]
then
docker run \
-d \
--name "$CONTAINER_NAME"_admin \
--mount source=jovyan,target=/home/jovyan/workdir \
--mount source=data,target=/data \
--mount source=group,target=/group_work \
--user root \
--restart=on-failure:10 \
-e NB_UMASK=002 \
-e CHOWN_EXTRA="/group_work" \
-e GRANT_SUDO=yes \
-e JUPYTER_ENABLE_LAB=yes \
-e JUPYTER_TOKEN=$MASTERPW \
-p 10000:8888 \
$IMAGE \
start-notebook.sh
fi
# remove carriage returns and spaces and pipe to while loop
cat $USERLIST | tr -d '\015\040' | while read port user password
do
# generate hash from password
# HASH=`python3 ./generate_pw_hash.py -p $password`
# create user volume. If the container crashes the volume keeps existing.
docker volume create $user
# generate container for each user
# mount user volume, data volume (general, read only) and group work volume (general, read and write)
# the permissions of general volumes (data and group work) need to be changed with the master container (this can probably be coded as well)
if [ "$ADD_PORT" == "yes" ]
then
ADD_PORT="-p $((port-2000)):$((port-2000))"
else
ADD_PORT=""
fi
docker run \
-d \
--cpus=$CPU \
--memory=$MEMORY \
--restart=on-failure:10 \
--name "$CONTAINER_NAME"_"$user" \
--user root \
-e NB_UMASK=002 \
-e CHOWN_EXTRA=/home/jovyan/workdir \
--mount source=$user,target=/home/jovyan/workdir \
--mount source=data,target=/data,readonly \
--mount source=group,target=/group_work \
-e JUPYTER_ENABLE_LAB=yes \
-e JUPYTER_TOKEN=$password \
-p $port:8888 \
$ADD_PORT \
$IMAGE \
start-notebook.sh
done
# stop all containers:
# docker stop $(docker ps -aq)
# remove all volumes:
# docker volume rm $(docker volume ls -q)
# check a user-specific volume with:
# USER=user01
# docker run -it --rm --mount source=$USER,target=/$USER -w /$USER ubuntu
# to add https:
# --ServerApp.keyfile=/etc/ssl/notebook/notebook.key \
# --ServerApp.certfile=/etc/ssl/notebook/notebook.pem