-
Notifications
You must be signed in to change notification settings - Fork 2
/
future
executable file
·400 lines (359 loc) · 10.3 KB
/
future
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#!/bin/bash
DEFAULT_IMAGE="judft/future"
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
title(){
if [ "$1" == " ERROR " ]
then
color=$RED
else
color=$GREEN
fi
if [ -n "$1" ]
then
echo -e "${GREEN}===============$1===================${NC}"
else
echo -e "${GREEN}============================================${NC}"
fi
}
report(){
echo -e "${GREEN}DONE: ${NC}$1"
}
info(){
echo -e "${GREEN}INFO: ${NC}$1"
}
failed(){
echo -e "${RED}FAILED: ${NC}$1"
}
FHOME=$HOME/.future
welcome(){
title
echo
echo "Welcome to "
echo -e "${GREEN} F U T U R E${NC}"
echo -e " the ${GREEN}F${NC}LEUR "
echo -e " ${GREEN}U${NC}ser "
echo -e " ${GREEN}T${NC}utorial "
echo -e " ${GREEN}U${NC}sing "
echo -e " ${GREEN}R${NC}esources in a container "
echo -e " ${GREEN} E${NC}nvironment"
echo -e "${GREEN} F U T U R E${NC}"
echo
title
}
find_docker(){
DOCKER=${DOCKER:-`which docker`}
DOCKER=${DOCKER:-`which podman`}
if [ -r $FHOME/DOCKER ]
then
DOCKER=`cat $FHOME/DOCKER`
fi
if ! which "$DOCKER" >/dev/null
then
title " ERROR "
echo -e "${RED}No container environment found"
echo "To use this tutorial you need to install either docker or "
echo "podman or another compatible container environment"
echo "Please check http://docker.com for possible downloads."
echo "If you actually have such an enviroment installed but it "
echo "is not found here, please either:"
echo " - set the DOCKER environment variable: e.g 'export DOCKER=path_to_your_docker_executable'"
echo " - use 'future config' to set the full path'"
echo -e "${NC}"
title " ERROR "
exit
fi
}
define_image(){
mkdir -p $FHOME
if [ -r $FHOME/IMAGE ]
then
IMAGE=`cat $FHOME/IMAGE`
else
IMAGE=$DEFAULT_IMAGE
fi
}
find_local_image(){
if ! $DOCKER image ls $IMAGE >/dev/null
then
unset IMAGE
fi
}
download_image(){
#Download image (will only update if needed)
define_image
info "Pulling the tutorial image from the server."
info "This may take some time (>6GB image to download)."
$DOCKER pull $IMAGE &> $FHOME/log
report "Image downloaded."
}
config(){
title " STORAGE "
echo "You can 'mount' a directory from your host system into the future container. This will be found in the folder 'persistent_storage'"
echo "This is usefull if you want to transfer data in/out of the container or need persistent storage. It is not required"
read -p "Enter a path to be mounted into the image for persistent storage (otherwise press enter not to mount any storage):" persist
if [ -n "$persist" ]
then
echo $persist >$FHOME/PERSIST
else
rm $FHOME/PERSIST 2>/dev/null
fi
title " DOCKER "
echo "Usually docker or podman will be found automatically if properly installed on your system."
echo "Only if this fails and you see corresponding please set the path here"
read -p "Enter the full-path to the docker command (otherwise press enter to try to find it automatically):" docker
if [ -n "$docker" ]
then
echo $docker >$FHOME/DOCKER
else
rm $FHOME/DOCKER 2>/dev/null
fi
title "IMAGE NAME"
echo -e "${RED}This option should only used if you have a good reason${NC}"
read -p "Enter the name of the tutorial image(otherwise press enter to use default):" image
if [ -n "$image" ]
then
echo $image >$FHOME/IMAGE
else
rm $FHOME/image 2>/dev/null
fi
}
start_container(){
stat=$(check_status)
if [ "$stat" == "OK" ]
then
failed "Future already running."
failed "Use 'future open' to connect to it or"
failed "use 'future stop' to stop the running container."
exit
fi
if [ "$stat" == "STOPPED" ]
then
$DOCKER start future >/dev/null
report "Restarted stopped container."
status
exit
fi
if $DOCKER image ls $IMAGE >/dev/null
then
if [ -r $FHOME/PERSIST ]
then
PERSIST=`cat $FHOME/PERSIST`
report "Using persistant storage at:$PERSIST"
fi
printf -- " Starting the container "
if [ -n "$PERSIST" ]
then
$DOCKER run -p 8888:8888 --name future -v $PERSIST:/home/jovyan/persistent_storage -e GRANT_SUDO=yes --user root $IMAGE &> $FHOME/log &
else
$DOCKER run -p 8888:8888 --name future -e GRANT_SUDO=yes --user root $IMAGE &> $FHOME/log &
fi
while ! ( grep -q 'http://127.0.0.1' $FHOME/log || grep -q 'Error response from daemon' $FHOME/log )
do
printf -- "."
sleep 5
done
if grep -q 'Error response from daemon' $FHOME/log
then
title " ERROR "
failed "Could not start the image!! Try the following command to inspect the log:"
failed "cat $FHOME/log"
rm $FHOME/URL 2> /dev/null
title " ERROR "
exit
fi
URL=`grep "http://127.0." $FHOME/log |tail -1 |cut -b 8-100|sed "s/lab/lab\/tree\/Welcome.ipynb/"`
echo $URL > $FHOME/URL
else
failed "$IMAGE not found. Use 'future download' first"
fi
status
}
stop_container(){
if $DOCKER ps |grep future >/dev/null
then
$DOCKER stop future >/dev/null
fi
status
}
delete_container(){
stat=$(check_status)
if [ "$stat" == "OK" ]
then
title " ERROR "
failed "Before deleting the container, you have to run 'future stop'"
exit
fi
echo -e "${RED}WARNING :${NC} This command deletes the container. You might loose all data not stored on a persistent storage"
echo " Press return to continue or CTRL-c to abort"
read
$DOCKER rm future >/dev/null
rm $FHOME/URL 2>/dev/null
report "future-container was deleted"
status
}
check_status(){
if $DOCKER ps |grep future >/dev/null
then
if [ -r $FHOME/URL ]
then
echo "OK"
else
echo "Error: Running, No URL"
fi
else
if $DOCKER ps -a |grep future >/dev/null
then
if [ -r $FHOME/URL ]
then
echo "STOPPED"
else
echo "Error: Stopped,No URL"
fi
else
echo "No Container"
fi
fi
}
build(){
info "Trying to build the container image locally"
if [ -r Dockerfile.future ]
then
info "Found local Dockerfile, starting to build"
$DOCKER build -f Dockerfile.future -t $IMAGE . |tee build.log
info "Done, you might want to check the log in build.log"
else
title " ERROR "
failed "Building only works in a directory cloned from the repo of the tutorial"
title " ERROR "
exit
fi
}
status(){
info "Container environment found at $DOCKER"
if [ -n "$IMAGE" ]
then
info "Image found as $IMAGE"
fi
stat=$(check_status)
if [ "$stat" == "OK" ]
then
title "Compute info"
printf -- "${GREEN}INFO: ${NC}No of CPU:"
$DOCKER exec future /bin/sh -c 'grep processor /proc/cpuinfo |wc -l'
printf -- "${GREEN}INFO: ${NC}Available Mem:"
$DOCKER exec future /bin/sh -c 'free -h |head -2 |tail -1|cut -c 75-'
title " STATUS "
info "future is running, to connect use"
info " - 'future open' if supported or"
URL=`cat $FHOME/URL`
info " - use a webbrowser to open:\n $URL"
info " - To stop future use 'future stop'"
title
elif [ "$stat" == "STOPPED" ]
then
title " STATUS "
info "FUTURE is not running."
info "The container has been stopped and can be restarted."
title
elif [ "$stat" == "No Container" ]
then
title " STATUS "
info "FUTURE is not running."
title
else
title " ERROR "
echo -e "${RED}ERROR: ${NC} status detected:$stat"
echo -e "ERROR: try to do 'future delete' and start again"
title " ERROR "
fi
}
open_webpage(){
stat=$(check_status)
if [ "$stat" == "OK" ]
then
URL=`cat $FHOME/URL`
if [[ `uname` == 'Darwin' ]]
then
open -u "$URL"
else
python -m webbrowser $URL
fi
else
failed "Container not running. Use 'future start' first."
exit
fi
title
info "Please use your browser to open:\n $URL"
}
help(){
cat <<EOF
Usage: future COMMAND
where COMMAND is one of:
download: download/update the future image
start : start the future-container
stop : stop the future-container
open : open future in your webbrowser
status : show if the future container is running
delete : delete the future-container
help : display this help page
config : set some configurations (usually not needed)
build : build the future image locally (usually only for development purpose)
The basic sequence of commands to use is:
- future download
- future start
- future open
This will open the tutorial in a webpage or show the URL to open.
You might then use:
- future stop
to stop the running tutorial and then later on
- future start
- future open
to use it again
EOF
}
process_arguments(){
case "$1" in
help) # display Help
help
exit;;
-h) # display Help
help
exit;;
start) # display Help
start_container
exit;;
stop) # display Help
stop_container
exit;;
delete) # display Help
delete_container
exit;;
open) # display Help
open_webpage
exit;;
download) # display Help
download_image
exit;;
build)
build
exit;;
status) # display Help
status
exit;;
*)
help
exit;;
esac
}
welcome
if [ "$1" == "config" ]
then
config
exit
fi
find_docker
define_image
find_local_image
process_arguments $1