-
Notifications
You must be signed in to change notification settings - Fork 10
/
Linux_launch.sh
executable file
·707 lines (626 loc) · 26.2 KB
/
Linux_launch.sh
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
#!/bin/bash
BASEDIR=$(dirname "$(readlink -f "$0")")
# Run pre_launch_test.sh, stop if it fails
/bin/bash $BASEDIR/.tools/bash_tools/pre_launch_test.sh || exit 1
# Function with the text to describe the usage of the bash script
usage() {
cat << EOF # remove the space between << and EOF, this is due to web plugin issue
Welcome to DL4MicEverywhere!
Providing an easy way to apply deep learning to microscopy using interactive Jupyter notebooks.
DL4MicEverywhere enables you to build/pull and run a notebook docker image.
Below, you'll find examples of the most basic usage case, as well as all the available options for a more advanced experience.
The most basic usage case involves providing three paths (these are always required):
- The path to the configuration file 'configuration.yaml'.
- The path to the folder containing the data for your notebook.
- The path to the folder where you wish to save your notebook's results.
Code example:
$(basename "${BASH_SOURCE[0]}") -c configuration_path -d dataset_path -o output_path
Here is a list of all available arguments:
-h Display this help message and exit. (optional)
-c Path to the configuration file 'configuration.yaml'.
-d Path to the folder containing the data for your notebook.
-o Path to the folder where you wish to save your notebook's results.
-g Flag to indicate if GPU should be used. (optional)
-n Path to a local notebook file 'notebook.ipynb'. (optional)
-r Path to a local requirements file 'requirements.txt'. (optional)
-t Tag to be added to the docker image during building. (optional)
-p Port number where to open the notebook.
-x Flag to indicate if it is a test run. This allows for the printing of useful debugging information. (optional)
Code example:
$(basename "${BASH_SOURCE[0]}") -c configuration_path -d dataset_path -o output_path [-h|t|g] [-n notebook_path] [-r requirements_path]
EOF
exit
}
# Function to check if a given argument exists or to rename it in case is needed
rename_parsed_argument() {
variable_name="$1"
config_variable_name="config_dl4miceverywhere_$1"
eval "$variable_name=\$$config_variable_name"
}
check_parsed_argument() {
variable_name="$1"
config_variable_name="config_dl4miceverywhere_$1"
if [ -z "${!config_variable_name}" ]; then
if [ -z "${!variable_name}" ]; then
# Close the terminal
echo ""
echo "------------------------------------"
echo "$variable_name parameter is not specified on the configuration yaml."
echo "Please specify the $variable_name parameter on the configuration yaml."
echo "If the problem persists, please create an issue on GitHub:"
echo " https://github.com/HenriquesLab/DL4MicEverywhere/issues"
read -p "Press enter to close the terminal."
echo "------------------------------------"
exit 1
fi
else
rename_parsed_argument $variable_name
fi
}
function cache_gui {
echo "data_path : $1
result_path : $2
selected_folder : $3
selected_notebook : $4
config_path : $5
notebook_path : $6
requirements_path : $7
gpu_flag : $8
tag : $9
advanced_options : ${10}" > $BASEDIR/.tools/.cache_gui
}
# Function to parse and read the configuration yaml file
function parse_yaml {
local prefix=$2
local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
sed -ne "s|^\($s\):|\1|" \
-e "s|^\($s\)\($w\)$s:$s[\"']\(.*\)[\"']$s\$|\1$fs\2$fs\3|p" \
-e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" "$1" |
awk -F$fs '{
indent = length($1)/2;
vname[indent] = $2;
value = $3;
# Remove inline comments only if they are at the beginning of a line or after whitespace
gsub(/[[:space:]]#.*/, "", value);
for (i in vname) {if (i > indent) {delete vname[i]}}
if (length($3) > 0) {
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
printf("%s%s%s=\"%s\"\n", "'$prefix'", vn, $2, value);
}
}'
}
# Let's define the default values for the flags
gpu_flag=0
test_flag=0
local_notebook_flag=0
local_requirements_flag=0
# Let's parse the arguments
while getopts :hc:d:o:gn:r:t:p:x flag;do
case $flag in
h)
usage ;;
c)
config_path="$OPTARG" ;;
d)
data_path="$OPTARG" ;;
o)
result_path="$OPTARG" ;;
g)
if nvidia-smi &> /dev/null; then
gpu_flag=1
else
echo ""
echo "Sorry, there is no configured Nvidia graphic card on your device, the docker image will be created without GPU."
echo ""
fi ;;
n)
notebook_path="$OPTARG" ;;
r)
requirements_path="$OPTARG" ;;
t)
docker_tag="$OPTARG" ;;
p)
port_number="$OPTARG" ;;
x)
test_flag=1 ;;
\?)
echo "Invalid option: -$OPTARG"
echo "Try bash ./launch.sh -h for more information."
# Close the terminal
exit 1 ;;
esac
done
# If no arguments are provided, set the GUI flag
if [ $# -eq 0 ]; then
gui_flag=1
else
gui_flag=0
fi
# Check if test mode is active
if [ "$test_flag" -eq 1 ]; then
echo 'Test mode is enabled.'
fi
if [ $gui_flag -eq 0 ]; then
# If GUI is not requested
if [ "$test_flag" -eq 1 ]; then
echo "GUI is not requested, proceeding without GUI."
fi
else
# If the GUI flag has been specified, run the function to show the GUI and read the arguments
gui_arguments=$(wish $BASEDIR/.tools/tcl_tools/main_gui.tcl $BASEDIR $OSTYPE)
if [ -z "$gui_arguments" ]; then
# No arguments were provided, this means that the GUI has been closed, so close the terminal
exit 1
fi
IFS=$'\n' read -d '' -r -a strarr <<<"$gui_arguments"
advanced_options=${strarr[0]}
if [ $advanced_options -eq 0 ]; then
data_path="${strarr[1]}"
result_path="${strarr[2]}"
selectedFolder="${strarr[3]}"
selectedNotebook="${strarr[4]}"
gpu_flag="${strarr[5]}"
tag_aux="${strarr[6]}"
cache_gui "$data_path" "$result_path" "$selectedFolder" "$selectedNotebook" "" "" "" "$gpu_flag" "$tag_aux" "$advanced_options"
if [ "$tag_aux" != "-" ]; then
docker_tag="$tag_aux"
fi
config_path=$BASEDIR/notebooks/$selectedFolder/$selectedNotebook/configuration.yaml
else
data_path="${strarr[1]}"
result_path="${strarr[2]}"
config_path="${strarr[3]}"
notebook_aux="${strarr[4]}"
requirements_aux="${strarr[5]}"
gpu_flag="${strarr[6]}"
tag_aux="${strarr[7]}"
cache_gui "$data_path" "$result_path" "" "" "$config_path" "$notebook_aux" "$requirements_aux" "$gpu_flag" "$tag_aux" "$advanced_options"
if [ "$notebook_aux" != "-" ]; then
notebook_path="$notebook_aux"
fi
if [ "$requirements_aux" != "-" ]; then
requirements_path="$requirements_aux"
fi
if [ "$tag_aux" != "-" ]; then
docker_tag="$tag_aux"
fi
fi
fi
if [ -z "$config_path" ]; then
# If no configuration path has been specified, then exit with the error
# Close the terminal
echo ""
echo "------------------------------------"
echo "No path to the configuration.yaml file has been specified."
echo "If you are using the CLI, please make sure to use -c argument and give a value to it."
echo "If you are using the GUI, please make sure to use that you have selected a default"
echo "notebook or a local oath to a configuration."
read -p "Press enter to close the terminal."
echo "------------------------------------"
exit 1
else
# If a configuration path has been specified, check if it is valid
if [[ -d "$config_path" ]]; then
if [ "$test_flag" -eq 1 ]; then
echo "Path to the configuration folder: $config_path"
fi
config_path="$config_path/configuration.yaml"
elif [[ -f "$config_path" ]]; then
if [ "$test_flag" -eq 1 ]; then
echo "Path to the configuration folder: $config_path"
fi
else
# Close the terminal
echo ""
echo "------------------------------------"
echo "The give path to the configuration is not valid: $config_path"
echo "Please, check that this path is correct and exists."
read -p "Press enter to close the terminal."
echo "------------------------------------"
exit 1
fi
fi
if [ -z "$data_path" ]; then
# Exit with an error if no data path is specified
# Close the terminal
echo ""
echo "------------------------------------"
echo "No path to the data folder has been specified."
echo "If you are using the CLI, please make sure to use -d argument and give a value to it."
echo "If you are using the GUI, please make sure to use that you have selected a path to the data folder."
read -p "Press enter to close the terminal."
echo "------------------------------------"
exit 1
else
# Validate the specified data path
if [[ -d "$data_path" ]]; then
if [ "$test_flag" -eq 1 ]; then
echo "Data path: $data_path"
fi
else
# Close the terminal
echo ""
echo "------------------------------------"
echo "The give path to the data folder is not valid: $data_path"
echo "Please, check that this path is correct and exists."
read -p "Press enter to close the terminal."
echo "------------------------------------"
exit 1
fi
fi
if [ -z "$result_path" ]; then
# Exit with an error if no result path is specified
# Close the terminal
echo ""
echo "------------------------------------"
echo "No path to the output folder has been specified."
echo "If you are using the CLI, please make sure to use -o argument and give a value to it."
echo "If you are using the GUI, please make sure to use that you have selected a path to the output folder."
read -p "Press enter to close the terminal."
echo "------------------------------------"
exit 1
else
# Validate the specified result path
if [[ -d "$result_path" ]]; then
if [ "$test_flag" -eq 1 ]; then
echo "Result path: $result_path"
fi
else
# Close the terminal
echo ""
echo "------------------------------------"
echo "The give path to the output folder is not valid: $result_path"
echo "Please, check that this path is correct and exists."
read -p "Press enter to close the terminal."
echo "------------------------------------"
exit 1
fi
fi
if [ "$test_flag" -eq 1 ]; then
# If the test flag is set, print whether the GPU flag has been set
if [ "$gpu_flag" -eq 1 ]; then
echo 'GPU usage is enabled.'
else
echo 'GPU usage is disabled.'
fi
fi
# Read the variables from the yaml file
eval $(parse_yaml "$config_path")
# Check the parsed variables
check_parsed_argument notebook_url
check_parsed_argument requirements_url
check_parsed_argument cuda_version
check_parsed_argument cudnn_version
check_parsed_argument ubuntu_version
check_parsed_argument python_version
check_parsed_argument notebook_version
rename_parsed_argument description # Not required to be present and therefore the cheking is skipped
rename_parsed_argument sections_to_remove # Not required to be present and therefore the cheking is skipped
rename_parsed_argument dl4miceverywhere_version # Not required to be present and therefore the cheking is skipped
rename_parsed_argument docker_hub_image # Not required to be present and therefore the cheking is skipped
if [ -z "$notebook_path" ]; then
# Use the URL from the configuration file if no local notebook path is specified
notebook_path="${notebook_url}"
# Set the docker's tag if not specified
if [ -z "$docker_hub_image" ]; then
aux_docker_tag="$(basename "$notebook_path" .ipynb)"
else
aux_docker_tag="${docker_hub_image}"
fi
if [ "$test_flag" -eq 1 ]; then
echo "Since no notebook was specified, the notebook URL from 'configuration.yaml' will be used."
fi
else
# Otherwise check if the path is valid
# For the docker's tag if not specified
if [ -z "$docker_hub_image" ]; then
aux_docker_tag="$(basename "$notebook_path" .ipynb)"
else
aux_docker_tag="${docker_hub_image}"
fi
if [ -f "$notebook_path" ]; then
if [ "$test_flag" -eq 1 ]; then
echo "Path to the notebook: $notebook_path"
fi
# If the notebook path is not valid, activate its flag for future processing
local_notebook_flag=1
else
echo ""
echo "------------------------------------"
echo "The give path to the notebook.ipynb is not valid: $notebook_path"
echo "Please, check that this path is correct and exists."
read -p "Press enter to close the terminal."
echo "------------------------------------"
exit 1
fi
fi
if [ -z "$requirements_path" ]; then
# If no local requirements path has been specified, then the URL from the configuration file will be used
requirements_path="${requirements_url}"
local_requirements_flag=0
if [ "$test_flag" -eq 1 ]; then
echo "No requirements file has been specified, therefore the requirements url specified on 'configuration.yaml' will be used."
fi
else
# Otherwise check if the path is valid
if [ -f "$requirements_path" ]; then
if [ "$test_flag" -eq 1 ]; then
echo "Path to the requirements file: $requirements_path"
fi
# If the notebook path is not valid, activate its flag for future processing
local_requirements_flag=1
else
echo ""
echo "------------------------------------"
echo "The give path to the requirementes.txt is not valid: $requirements_path"
echo "Please, check that this path is correct and exists."
read -p "Press enter to close the terminal."
echo "------------------------------------"
exit 1
fi
fi
if [ -z "$docker_tag" ]; then
# If no tag has been specified for the docker image, then the default tag will be used (the name of the notebook)
docker_tag=$aux_docker_tag
if [ "$test_flag" -eq 1 ]; then
echo "No tag has been specified for the docker image, therefore the default tag $docker_tag will be used."
fi
if [ -z "$docker_hub_image" ]; then
# Get the notebook type of the configuration file
if [[ "$config_path" = *'ZeroCostDL4Mic_notebooks'* ]]; then
notebook_type='z'
elif [[ "$config_path" = *'External_notebooks'* ]]; then
notebook_type='e'
elif [[ "$config_path" = *'Bespoke_notebooks'* ]]; then
notebook_type='b'
else
# Is a custom configuration that is not in any of these notebook types
# therefore the notebook type will be 'n'
notebook_type='n'
fi
# In case the configuration file does not have a docker_hub_image attribute
docker_tag=$(echo $docker_tag | tr '[:upper:]' '[:lower:]')
docker_tag=henriqueslab/dl4miceverywhere:$docker_tag
if [ -z "$dl4miceverywhere_version" ]; then
docker_tag=$docker_tag-$notebook_type$notebook_version-d___
else
docker_tag=$docker_tag-$notebook_type$notebook_version-d$dl4miceverywhere_version
fi
if [ "$gpu_flag" -eq 1 ]; then
docker_tag=$docker_tag-gpu
fi
else
# In case the configuration file already has a docker_hub_image attribute
docker_tag=henriqueslab/dl4miceverywhere:$docker_hub_image
if [ "$gpu_flag" -eq 1 ]; then
docker_tag=$docker_tag-gpu
fi
fi
fi
# Set the docker's tag
if [ "$test_flag" -eq 1 ]; then
echo ""
echo "ubuntu_version: $ubuntu_version"
echo "cuda_version: $cuda_version"
echo "cudnn_version: $cudnn_version"
echo "python_version: $python_version"
echo "notebook_path: $notebook_path"
echo "requirements_path: $requirements_path"
echo "sections_to_remove: $sections_to_remove"
echo "notebook_version: $notebook_version"
echo "description: $description"
echo "docker_tag: $docker_tag"
echo ""
fi
notebook_name="$(basename "$notebook_path")"
# Local files, if included, need to be remocreated in same folder as the dockerfile,
# then they will be deleted
if [ "$local_notebook_flag" -eq 1 ]; then
cp "$notebook_path" "$BASEDIR/notebook.ipynb"
notebook_path=./notebook.ipynb
fi
if [ "$local_requirements_flag" -eq 1 ]; then
cp "$requirements_path" "$BASEDIR/requirements.txt"
requirements_path=./requirements.txt
fi
# Check if there is the errata in ~/.docker/config.json where credsStore should be credStore
if grep -q credsStore ~/.docker/config.json; then
# Apparently, on MaxOS, it returns:
# ERROR: failed to solve: error getting credentials - err: exit status 1, out: “
# It can be solved by changing this argument in the configuration file (working also on Linux with this change).
perl -pi -e "s/credsStore/credStore/g" ~/.docker/config.json
fi
# Execute the pre building tests
/bin/bash $BASEDIR/.tools/bash_tools/pre_build_test.sh || exit 1
# Check if an image with that tag exists locally and ask if the user whants to replace it.
build_flag=0
# In case testing is chossing, the building is forced to be done, without questions
if [ "$test_flag" -eq 1 ]; then
# In case of testing, the building is always done
build_flag=2
else
if docker image inspect $docker_tag >/dev/null 2>&1; then
if [ "$gui_flag" -eq 1 ]; then
# If the GUI flag has been specified, show a window for ansewring local question
build_flag=$(wish $BASEDIR/.tools/tcl_tools/local_img_gui.tcl $OSTYPE)
else
echo "Image exists locally. Do you want to build and replace the existing one?"
select yn in "Yes" "No"; do
case $yn in
Yes ) build_flag=2; break;;
No ) build_flag=1; break;;
esac
done
fi
fi
if [ "$build_flag" -ne 1 ]; then
# In case the local image option has not been selected
if docker manifest inspect "${docker_tag}" >/dev/null 2>&1; then
# In case the image is available on docker hub
# Get the architecture of the machine
local_arch=$(uname -m)
if [ "$local_arch" == "x86_64" ]; then
local_arch="amd64"
fi
# Count the ocurrences of that architecture in the docker manifest of that image
arch_count=$(docker manifest inspect "${docker_tag}" -v | grep 'architecture' | grep -c $local_arch)
if [ "$arch_count" -gt 0 ]; then
# In case the architecture is available
if [ "$gui_flag" -eq 1 ]; then
# If the GUI flag has been specified, show a window for ansewring hub question
build_flag=$(wish $BASEDIR/.tools/tcl_tools/hub_img_gui.tcl $OSTYPE)
else
echo "The image ${docker_tag} is already available on docker hub. Do you preffer to pull it (faster option) instead of building it?"
select yn in "Yes" "No"; do
case $yn in
Yes ) build_flag=3; break;;
No ) build_flag=2; break;;
esac
done
fi
else
# In case the architecture is not available
build_flag=2
fi
else
build_flag=2
fi
fi
fi
# Pull the docker image from docker hub
if [ "$build_flag" -eq 3 ]; then
docker pull "$docker_tag"
DOCKER_OUT=$? # Gets if the docker image has been pulled
else
# Build the docker image without GUI
if [ "$build_flag" -eq 2 ]; then
if [ "$gpu_flag" -eq 1 ]; then
echo "To build the docker image, you need to provide root access by entering your password."
echo "Otherwise, you can choose the option of getting the image from Docker Hub or follow"
echo "the steps in our documentation."
sudo docker build --file $BASEDIR/Dockerfile.gpu -t $docker_tag $BASEDIR\
--build-arg UBUNTU_VERSION="${ubuntu_version}" \
--build-arg CUDA_VERSION="${cuda_version}" \
--build-arg CUDNN_VERSION="${cudnn_version}" \
--build-arg GPU_FLAG="${gpu_flag}" \
--build-arg PYTHON_VERSION="${python_version}" \
--build-arg PATH_TO_NOTEBOOK="${notebook_path}" \
--build-arg PATH_TO_REQUIREMENTS="${requirements_path}" \
--build-arg NOTEBOOK_NAME="${notebook_name}" \
--build-arg SECTIONS_TO_REMOVE="${sections_to_remove}" \
--build-arg CACHEBUST=$(date +%s)
else
echo "To build the docker image, you need to provide root access by entering your password."
echo "Otherwise, you can choose the option of getting the image from Docker Hub or follow"
echo "the steps in our documentation."
sudo docker build --file $BASEDIR/Dockerfile -t $docker_tag $BASEDIR\
--build-arg UBUNTU_VERSION="${ubuntu_version}" \
--build-arg GPU_FLAG="${gpu_flag}" \
--build-arg PYTHON_VERSION="${python_version}" \
--build-arg PATH_TO_NOTEBOOK="${notebook_path}" \
--build-arg PATH_TO_REQUIREMENTS="${requirements_path}" \
--build-arg NOTEBOOK_NAME="${notebook_name}" \
--build-arg SECTIONS_TO_REMOVE="${sections_to_remove}" \
--build-arg CACHEBUST=$(date +%s)
fi
DOCKER_OUT=$? # Gets if the docker image has been built
else
if [ "$build_flag" -eq 1 ]; then
DOCKER_OUT=0 # In case that is already built, it is good to run
else
# build flag is still 0, an error ocurred
echo ""
echo "------------------------------------"
echo "Error looking for existing docker"
echo "image with the given tag: $docker_tag"
read -p "Press enter to close the terminal."
echo "------------------------------------"
# Close the terminal
exit 1
fi
fi
fi
# Execute the post building tests
/bin/bash $BASEDIR/.tools/bash_tools/post_build_test.sh || exit 1
sleep 3
# Local files, if included, need to be removed to avoid the overcrowding the folder
if [ "$local_notebook_flag" -eq 1 ]; then
rm $BASEDIR/notebook.ipynb
fi
if [ "$local_requirements_flag" -eq 1 ]; then
rm $BASEDIR/requirements.txt
fi
# If it has been built, run the docker
if [ "$DOCKER_OUT" -eq 0 ]; then
if [ $test_flag -eq 1 ]; then
# In case ,testing is done, only building is required, exit before running
exit 0
fi
# Choose an initial port
if [ -z "$port_number" ]; then
# In case user does not provide a port number, use the default 8888 port
port=8888
else
# Else, use the port provided by the user
port="$port_number"
fi
# Check if selected port is available if not try next one until finding a usable port.
if [[ "$(systemd-detect-virt)" == "wsl"* ]]; then
# Linux inside the Windows Subsystem for Linux needs to look differently to the ports
while ( netstat -a | grep :$port &> /dev/null )
do
echo WARNING: Port $port is already allocated.
port=$((port+1))
if [ $port -gt 9000 ]; then
# We want the port to be between 8000 and 9000
port=8000
sleep 1
fi
done
else
while ( lsof -i:$port &> /dev/null )
do
echo WARNING: Port $port is already allocated.
port=$((port+1))
if [ $port -gt 9000 ]; then
# We want the port to be between 8000 and 9000
port=8000
sleep 1
fi
done
fi
echo SUCCESS: Port $port will be used.
# Based on the openssl command and the base64 encoding, a 50 characters token is generated
notebook_token=$(openssl rand -base64 50 | tr -dc 'a-zA-Z0-9')
echo ""
echo "################################################################################################################################"
echo ""
echo " The generated token for the notebook is: $notebook_token"
echo ""
echo "################################################################################################################################"
echo ""
# Launch a subprocess to open the browser with the port in 10 seconds
/bin/bash $BASEDIR/.tools/bash_tools/open_browser.sh http://localhost:$port/lab/tree/$notebook_name/?token=$notebook_token &
# Define the command that will be run when the docker image is launched
docker_command="jupyter lab --ip='0.0.0.0' --port=$port --no-browser --allow-root --NotebookApp.token=$notebook_token; cp /home/docker_info.txt /home/results/docker_info.txt; cp /home/$notebook_name /home/results/$notebook_name;"
if [ "$gpu_flag" -eq 1 ]; then
# Run the docker image activating the GPU, allowing the port connection for the notebook and the volume with the data
docker run -it --gpus all -p $port:$port -v "$data_path:/home/data" -v "$result_path:/home/results" --shm-size=256m "$docker_tag" /bin/bash -c "$docker_command"
else
# Run the docker image without activating the GPU
docker run -it -p $port:$port -v "$data_path:/home/data" -v "$result_path:/home/results" --shm-size=256m "$docker_tag" /bin/bash -c "$docker_command"
fi
else
echo ""
echo "------------------------------------"
echo "Error during the building of the docker image. Please check the logs."
read -p "Press enter to close the terminal."
echo "------------------------------------"
exit 1
fi
# Close the terminal when user press enter
echo ""
echo "------------------------------------"
read -p "Press enter to close the terminal."
echo "------------------------------------"
exit 1