forked from yairomer/mldock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtdock.sh
executable file
·718 lines (643 loc) · 20 KB
/
tdock.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
708
709
710
711
712
713
714
715
716
717
718
#!/bin/bash
set -e
# set -x # Debug: be verbose
# PS4='$LINENO: ' # Add lines number to debug output
## Main CLI function
## =================
app_name=tdock
repository="tamarott/"
image_name="tdock"
version_name="v0.1"
container_name="tdock"
main_cli() {
## Parse args
## ==========
usage() {
echo "A CLI tool for working with the $app_name docker"
echo ""
echo "usage: $app_name <command> [<options>]"
echo " or: $app_name -h to print this help message."
echo ""
echo "Commands"
echo " setup Create a link or a copy of the $app_name.sh script in the /usr/bin folder (requiers sudo)."
echo " build Build the image."
echo " run Run a command inside a new container."
echo " run_remote Run a command inside a new container on a remote machine."
echo " exec Execute a command inside an existing container."
echo " exec_remote Execute a command inside an existing container on a remote machine."
echo " stop Stop a running container."
echo "Use $app_name <command> -h for specific help on each command."
}
if [ "$#" -eq 1 ] && [ "$1" == "-h" ]; then
usage
exit 0
fi
while getopts ":h" opt; do
case $opt in
h )
usage
exit 0
;;
\? )
echo "Error: Invalid Option: -$opt" 1>&2
usage
exit 1
;;
esac
done
shift $((OPTIND -1))
if [ "$#" -lt 1 ]; then
echo "Error: Was expecting a command" 1>&2
usage
exit 1
fi
subcommand=$1; shift
ref_dir="$( cd $( dirname "$(readlink -f ${BASH_SOURCE[0]})" ) && pwd )"
case "$subcommand" in
setup)
setup_cli "$@"
;;
build)
build_cli "$@"
;;
run)
run_cli "$@"
;;
run_remote)
run_remote_cli "$@"
;;
exec)
exec_cli "$@"
;;
exec_remote)
exec_remote_cli "$@"
;;
stop)
stop_cli "$@"
;;
*)
echo "Error: unknown command $subcommand" 1>&2
usage
exit 1
esac
}
setup_cli() {
copy_script_file=false
usage () {
echo "Create a link or a copy of the $app_name.sh script in the /usr/bin folder (requiers sudo)."
echo ""
echo "usage: $app_name $subcommand"
echo " or: $app_name $subcommand -h to print this help message."
echo "Options:"
echo " -c Copy the script file to /usr/bin. By default a link is created to the current file."
}
if [ "$#" -eq 1 ] && [ "$1" == "-h" ]; then
usage
exit 0
fi
while getopts "c" opt; do
case $opt in
c)
copy_script_file=true
;;
:)
echo "Error: -$opt requires an argument" 1>&2
usage
exit 1
;;
\?)
echo "Error: unknown option -$opt" 1>&2
usage
exit 1
;;
esac
done
shift $((OPTIND -1))
if [ "$#" -gt 0 ]; then
echo "Error: Unexpected arguments: $@" 1>&2
usage
exit 1
fi
run_setup
}
build_cli() {
tag_as_latest=true
usage () {
echo "Build the image"
echo ""
echo "usage: $app_name $subcommand [<options>]"
echo " or: $app_name $subcommand -h to print this help message."
echo "Options:"
echo " -v version_name The version name to use for the build image. Default: \"$version_name\""
echo " -l Don't tag built image as latest"
}
if [ "$#" -eq 1 ] && [ "$1" == "-h" ]; then
usage
exit 0
fi
while getopts "v:l" opt; do
case $opt in
v)
version_name=$OPTARG
;;
l)
tag_as_latest=false
;;
:)
echo "Error: -$opt requires an argument" 1>&2
usage
exit 1
;;
\?)
echo "Error: unknown option -$opt" 1>&2
usage
exit 1
;;
esac
done
shift $((OPTIND -1))
if [ "$#" -gt 0 ]; then
echo "Error: Unexpected arguments: $@" 1>&2
usage
exit 1
fi
check_docker_for_sudo
build_image
}
run_cli() {
if xhost >& /dev/null ; then
## Display exist
connect_to_x_server=true
else
## No display
connect_to_x_server=false
fi
if [[ "$(whoami)" == "root" ]]; then
userstring="dockuser:4283:dockuser:4283"
else
userstring="$(whoami):$(id -u):$(id -gn):$(id -g)"
fi
map_host=true
detach_container=false
home_folder=""
command_to_run=""
use_tmux=false
extra_args=()
if nvidia-smi >& /dev/null ; then
use_nvidia_runtime=true
if docker run --rm --runtime=nvidia busybox echo test >& /dev/null ; then
use_gpus_all=false
else
use_gpus_all=true
fi
else
use_nvidia_runtime=false
fi
usage () {
echo "Run a command inside a new container"
echo ""
echo "usage: $app_name $subcommand [<options>] [command]"
echo " or: $app_name $subcommand -h to print this help message."
echo "Options:"
echo " -v version_name The version name to use for the build image. Default: \"$version_name\""
echo " -c container_name The name to for the created container. Default: \"$container_name\""
echo " -f home_folder A folder to map as the dockuser's home folder."
echo " -s Run the command as root."
echo " -u Run the command as dockuser user."
echo " -i username Run the command as *username*."
echo " -x Don't connect X-server"
echo " -r Don't map the root folder on the host machine to /host inside the container."
echo " -d Detach the container."
echo " -t Run in at TMUX session."
echo " -e extra_args Extra arguments to pass to the docker run command."
}
if [ "$#" -eq 1 ] && [ "$1" == "-h" ]; then
usage
exit 0
fi
while getopts "v:c:f:sui:xrdte:" opt; do
case $opt in
v)
version_name=$OPTARG
;;
c)
container_name=$OPTARG
;;
f)
home_folder=$OPTARG
;;
s)
userstring=""
;;
u)
userstring="dockuser:4283:dockuser:4283"
;;
i)
username=$OPTARG
userstring="$username:$(id -u $username):$(id -gn $username):$(id -g $username)"
;;
x)
connect_to_x_server=false
;;
r)
map_host=false
;;
d)
detach_container=true
;;
t)
use_tmux=true
;;
e)
IFS=$'\n' extra_args=( $(xargs -n1 printf "%s\n" <<< "$OPTARG") )
unset IFS
;;
:)
echo "Error: -$opt requires an argument" 1>&2
usage
exit 1
;;
\?)
echo "Error: unknown option -$opt" 1>&2
usage
exit 1
;;
esac
done
shift $((OPTIND -1))
userstring="${userstring// /-}"
if [ "$#" -gt 0 ]; then
command_to_run=( "$@" )
fi
check_docker_for_sudo
gen_command
run_command
}
run_remote_cli() {
if xhost >& /dev/null ; then
## Display exist
connect_to_x_server=true
else
## No display
connect_to_x_server=false
fi
if [[ "$(whoami)" == "root" ]]; then
userstring="dockuser:4283:dockuser:4283"
else
userstring="$(whoami):$(id -u):$(id -gn):$(id -g)"
fi
map_host=true
detach_container=false
home_folder=""
command_to_run=""
use_tmux=false
extra_args=()
usage () {
echo "Run a command inside a new container on a remote machine"
echo ""
echo "usage: $app_name $subcommand remote_ip [<options>] [command]"
echo " or: $app_name $subcommand -h to print this help message."
echo "Options:"
echo " -v version_name The version name to use for the build image. Default: \"$version_name\""
echo " -c container_name The name to for the created container. Default: \"$container_name\""
echo " -f home_folder A folder to map as the dockuser's home folder."
echo " -s Run the command as root."
echo " -u Run the command as dockuser user."
echo " -i username Run the command as *username*."
echo " -x Don't connect X-server"
echo " -r Don't map the root folder on the host machine to /host inside the container."
echo " -d Detach the container."
echo " -t Run in at TMUX session."
echo " -e extra_args Extra arguments to pass to the docker run command."
}
if [ "$#" -eq 1 ] && [ "$1" == "-h" ]; then
usage
exit 0
fi
if [ "$#" -lt 1 ]; then
echo "Error: Was expecting a remote ip" 1>&2
usage
exit 1
fi
remote_ip=$1; shift
while getopts "v:c:f:sui:xrdte:" opt; do
case $opt in
v)
version_name=$OPTARG
;;
c)
container_name=$OPTARG
;;
f)
home_folder=$OPTARG
;;
s)
userstring=""
;;
u)
userstring="dockuser:4283:dockuser:4283"
;;
i)
username=$OPTARG
userstring="$username:$(id -u $username):$(id -gn $username):$(id -g $username)"
;;
x)
connect_to_x_server=false
;;
r)
map_host=false
;;
d)
detach_container=true
;;
t)
use_tmux=true
;;
e)
IFS=$'\n' extra_args=( $(xargs -n1 printf "%s\n" <<< "$OPTARG") )
unset IFS
;;
:)
echo "Error: -$opt requires an argument" 1>&2
usage
exit 1
;;
\?)
echo "Error: unknown option -$opt" 1>&2
usage
exit 1
;;
esac
done
shift $((OPTIND -1))
userstring="${userstring// /-}"
if [ "$#" -gt 0 ]; then
command_to_run=( "$@" )
fi
if ssh $remote_ip nvidia-smi >& /dev/null ; then
use_nvidia_runtime=true
if ssh $remote_ip docker run --rm --runtime=nvidia busybox echo test >& /dev/null ; then
use_gpus_all=false
else
use_gpus_all=true
fi
else
use_nvidia_runtime=false
fi
check_docker_for_sudo
gen_command
run_remote_command
}
exec_cli() {
extra_args=()
command_to_run="bash"
usage () {
echo "Execute a command inside an existing container"
echo ""
echo "usage: $app_name $subcommand [<options>] [command_to_run]"
echo " or: $app_name $subcommand -h to print this help message."
echo "Options:"
echo " -c container_name The name to for the created container. default: \"$container_name\""
echo " -e extra_args Extra arguments to pass to the docker exec command."
}
if [ "$#" -eq 1 ] && [ "$1" == "-h" ]; then
usage
exit 0
fi
while getopts "c:u:e:" opt; do
case $opt in
c)
container_name=$OPTARG
;;
e)
IFS=$'\n' extra_args=( $(xargs -n1 printf "%s\n" <<< "$OPTARG") )
unset IFS
;;
:)
echo "Error: -$OPTARG requires an argument" 1>&2
usage
exit 1
;;
\?)
echo "Error: unknown option -$opt" 1>&2
usage
exit 1
;;
esac
done
shift $((OPTIND -1))
if [ "$#" -gt 0 ]; then
command_to_run=( "$@" )
fi
check_docker_for_sudo
exec_command
}
exec_remote_cli() {
extra_args=()
usage () {
echo "Execute a command inside an existing container"
echo ""
echo "usage: $app_name $subcommand remote_ip [<options>] [command_to_run]"
echo " or: $app_name $subcommand -h to print this help message."
echo "Options:"
echo " -c container_name The name to for the created container. default: \"$container_name\""
echo " -e extra_args Extra arguments to pass to the docker exec command."
}
if [ "$#" -eq 1 ] && [ "$1" == "-h" ]; then
usage
exit 0
fi
remote_ip=$1; shift
while getopts "c:u:e:" opt; do
case $opt in
c)
container_name=$OPTARG
;;
e)
IFS=$'\n' extra_args=( $(xargs -n1 printf "%s\n" <<< "$OPTARG") )
unset IFS
;;
:)
echo "Error: -$OPTARG requires an argument" 1>&2
usage
exit 1
;;
\?)
echo "Error: unknown option -$opt" 1>&2
usage
exit 1
;;
esac
done
shift $((OPTIND -1))
if [ "$#" -gt 0 ]; then
command_to_run=( "$@" )
fi
exec_remote_command
}
stop_cli() {
usage () {
echo "Stop a running container."
echo ""
echo "usage: $app_name $subcommand"
echo " or: $app_name $subcommand -h to print this help message."
echo "Options:"
echo " -c container_name The name to for the created container. default: \"$container_name\""
}
if [ "$#" -eq 1 ] && [ "$1" == "-h" ]; then
usage
exit 0
fi
while getopts "c:u:e:" opt; do
case $opt in
c)
container_name=$OPTARG
;;
:)
echo "Error: -$opt requires an argument" 1>&2
usage
exit 1
;;
\?)
echo "Error: unknown option -$opt" 1>&2
usage
exit 1
;;
esac
done
shift $((OPTIND -1))
if [ "$#" -gt 0 ]; then
echo "Error: Unexpected arguments: $@" 1>&2
usage
exit 1
fi
check_docker_for_sudo
stop_container
}
check_docker_for_sudo() {
if ! docker ps >& /dev/null; then
if sudo docker ps >& /dev/null; then
docker_sudo_prefix="sudo "
else
echo "!! Error: was not able to run \"docker ps\""
exit
fi
else
docker_sudo_prefix=""
fi
}
run_setup() {
if [ "$copy_script_file" = true ]; then
echo "-> Creating a copy of $ref_dir/$app_name.sh at /usr/bin/$app_name"
sudo cp --remove-destination $ref_dir/$app_name.sh /usr/bin/$app_name
else
echo "-> Creating a symbolic link to $ref_dir$app_name.sh at /usr/bin/$app_name"
sudo ln -sfT $ref_dir/$app_name.sh /usr/bin/$app_name
fi
}
build_image() {
echo "-> Building image from $ref_dir"
${docker_sudo_prefix}docker build -t $repository$image_name:$version_name $ref_dir
if [ "$tag_as_latest" = true ]; then
${docker_sudo_prefix}docker tag $repository$image_name:$version_name $repository$image_name:latest
fi
}
gen_command() {
# # if [ "user_host_user" = true ]; then
# if [ true = true ]; then
# extra_args="-v /etc/passwd:/etc/passwd -u $(id -u ${USER}):$(id -g ${USER})"
# fi
if [ "$connect_to_x_server" = true ]; then
xhost +local:root > /dev/null
extra_args+=("-e" "DISPLAY=${DISPLAY}" "-e" "MPLBACKEND=Qt5Agg" "-e" "QT_X11_NO_MITSHM=1" "-v" "/tmp/.X11-unix:/tmp/.X11-unix")
fi
if [ "$map_host" = true ]; then
extra_args+=("-v" "/:/host/")
fi
if [[ ! -z $userstring ]]; then
userstringsplit=(${userstring//:/ })
new_username=${userstringsplit[0]}
extra_args+=("-e" "USERSTRING=$userstring")
if [[ ! -z $home_folder ]]; then
home_folder_full=$(readlink -f $home_folder || echo "") >/dev/null 2>&1
if [[ ! -z $home_folder_full ]]; then
home_folder=$home_folder_full
fi
if [[ "$new_username" == "root" ]]; then
extra_args+=("-v" "$home_folder:/root/")
else
extra_args+=("-v" "$home_folder:/home/$new_username/")
fi
fi
fi
if [ "$use_tmux" = true ]; then
detach_tmux="$detach_container"
detach_container=true
fi
if [ "$detach_container" = true ]; then
extra_args+=("-d")
else
extra_args+=("-it")
fi
if [ "$use_nvidia_runtime" = true ]; then
if [ "$use_gpus_all" = true ]; then
extra_args+=("--gpus=all")
else
extra_args+=("--runtime=nvidia")
fi
fi
cmd+=("docker" "run" \
"--rm" \
"--network" "host" \
"--name" "$container_name")
cmd+=( "${extra_args[@]}" )
cmd+=("$repository$image_name:$version_name")
if [ "$use_tmux" = true ]; then
cmd+=("run_in_detached_tmux")
fi
if [[ ! -z "$command_to_run" ]]; then
cmd+=("${command_to_run[@]}")
fi
}
run_command() {
echo "Running: ${docker_sudo_prefix}${cmd[@]}"
echo ""
"${docker_sudo_prefix}${cmd[@]}"
if [ "$use_tmux" = true ] && [[ "$detach_tmux" = false ]]; then
new_username=$(${docker_sudo_prefix}docker exec $container_name cat /tmp/dock_config/username)
${docker_sudo_prefix}docker exec -it -u $new_username $container_name tmux a
fi
}
run_remote_command() {
echo "Running on $remote_ip: ${cmd[@]}"
echo ""
ssh $remote_ip -t "${cmd[@]}"
if [ "$use_tmux" = true ] && [[ "$detach_tmux" = false ]]; then
new_username=$(ssh $remote_ip -t docker exec $container_name cat /tmp/dock_config/username)
ssh $remote_ip -t docker exec -it -u $new_username $container_name tmux a
fi
}
exec_command() {
new_username=$(${docker_sudo_prefix}docker exec $container_name cat /tmp/dock_config/username)
if [[ ! -z "$new_username" ]]; then
extra_args="$extra_args -u $new_username -w /home/$new_username"
fi
${docker_sudo_prefix}docker exec -it $extra_args $container_name "${command_to_run[@]}"
}
exec_remote_command() {
new_username=$(ssh -o LogLevel=QUIET $remote_ip -t "docker exec $container_name cat /tmp/dock_config/username" | tr -d '\r')
cmd+=("docker" "exec" \
"-it")
if [[ ! -z "$new_username" ]]; then
extra_args+=("-u" "$new_username" "-w" "/home/$new_username")
fi
cmd+=( "${extra_args[@]}" )
cmd+=("$container_name")
cmd+=("${command_to_run[@]}")
run_remote_command
}
stop_container() {
echo "-> Stopping the container"
${docker_sudo_prefix}docker stop $container_name
}
main_cli "$@"