-
Notifications
You must be signed in to change notification settings - Fork 2
/
dosh
executable file
·966 lines (830 loc) · 22.6 KB
/
dosh
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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
#!/bin/bash
#
# Copyright 2017-2020,2022-2024 Gaël PORTAY
#
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# Called as program loader (shebang #!/usr/bin/dosh)
__="$_"
if [ "$__" != "/bin/bash" ] && [ "$__" = "$1" ]
then
exec <"$1"
shift
set -- -s "$@"
exec "$0" "$@"
elif [ "$__" != "/bin/bash" ] && [ "$__" = "$2" ]
then
# Warning: Splitting the single shebang argument into many is not
# portable! Please consider using env(1) instead:
#
# #!/usr/bin/env -S dosh [ARGS...]
read -r -a args <<<"$1"
shift
exec <"$1"
shift
set -- "${args[@]}" -s "$@"
exec "$0" "$@"
fi
set -e
set -u
set -o pipefail
VERSION="6"
usage() {
cat <<EOF
Usage: Typical shell commands:
${0##*/} [(-|+)abefhmnuvxC] [(-|+)o shopt] [SCRIPT_FILE [ARGS...]]
${0##*/} -c [(-|+)abefhmnuvxC] [(-|+)o shopt] COMMAND [NAME [ARGS...]]
${0##*/} -i [(-|+)abefhmnuvxC] [(-|+)o shopt] [SCRIPT_FILE [ARGS...]]
${0##*/} -s [(-|+)abefhmnuvxC] [(-|+)o shopt] [ARGS...]
Advanced dosh container commands:
${0##*/} --detach [OPTIONS]
${0##*/} --exec CONTAINER [OPTIONS] [ARGS...]
${0##*/} --attach CONTAINER [OPTIONS]
Advanced dosh image management commands:
${0##*/} --rmi [OPTIONS]
${0##*/} --ls
${0##*/} --gc
Extra docker frontend commands:
${0##*/} --attach [OPTIONS] [DOCKER_OPTIONS] CONTAINER
${0##*/} --kill [OPTIONS] [DOCKER_OPTIONS] CONTAINER [CONTAINER...]
${0##*/} --rm [OPTIONS] [DOCKER_OPTIONS] CONTAINER [CONTAINER...]
Run a shell as user in a container and bind mount cwd.
POSIX Shell related options:
-c Read commands from command-line.
-i Set interactive.
-s Read commands from standard-input.
-abefhmnuvxC or -o shopt,
+abefhmnuvxC or +o shopt For a more thorough description of shopts,
please refers to sh help.
Bash specific options:
-klprtBDEHIPT or -O shopt,
+klprtBDEHIPT or +O shopt For a more thorough description of shopts,
please refers to bash help.
Dash specific options:
-pqEIV, +pqEIV For a more thorough description of shopts,
please refers to dash help.
Zsh specific options:
-0123456789dgklprtwyBDEFGHIJKLMNOPQRSTUVWXYZ,
+0123456789dgklprtwyBDEFGHIJKLMNOPQRSTUVWXYZ
For a more thorough description of shopts,
please refers to zsh help.
Docker related options:
--dockerfile FILE Path to the Dockerfile to use.
--context TAR Path to the context to send to docker daemon.
--no-auto-context Disable automatic context sent to docker daemon.
--no-extra-options Disable extra options given to docker commands.
--no-doshprofile Disable read of ~/.dosh_profile.
--no-doshrc Disable read of ./doshrc.
--directory DIR Change to directory before doing anything else.
--working-directory DIR Working directory inside the container.
--root Run as root.
--dind Run dosh in dosh.
--home Bind mount home directory.
--mount-options OPTIONS Set bind mount volume options.
For a more thorough description please refers to
docker-run(1) manual.
--shell SHELL Set shell interpretor.
Equivalent to set DOSHELL=<SHELL>.
--ls List images and exit.
--gc Cleanup unused images and exit.
--build-only Build image if needed and exit.
--build Build image.
--rebuild Build image again, verbosely.
Equivalent to --build --verbose.
--rmi Remove image and exit.
--detach Detach container.
--exec CONTAINER Execute in container.
--attach CONTAINER Attach container.
--kill CONTAINER Kill container.
--rm CONTAINER Remove container.
--tag Print docker tag and exit.
--dry-run Do nothing; echo docker commands.
Miscellaneous options:
--verbose Turn on verbose mode.
--version Print version.
--help Print usage.
Environment variables:
DOSHELL The full pathname to the shell to run in docker
image.
Equivalent to --shell <SHELL>.
DOSHLVL Incremented by one each time an instance of dosh
is started.
DOSH_DOCKER The docker executable.
DOSH_DOCKERFILE The filename of the Dockerfile to use.
Equivalent to --dockerfile <FILE>.
DOSH_DOCKER_RUN_EXTRA_OPTS
Set additionnal parameters to docker run
command.
DOSH_DOCKER_EXEC_EXTRA_OPTS
Set additionnal parameters to docker exec
command.
DOSH_DOCKER_ATTACH_EXTRA_OPTS
Set additionnal parameters to docker attach
command.
DOSH_DOCKER_KILL_EXTRA_OPTS
Set additionnal parameters to docker kill
command.
DOSH_DOCKER_RM_EXTRA_OPTS
Set additionnal parameters to docker rm
command.
DOSH_DOCKER_BUILD_EXTRA_OPTS
Set additionnal parameters to docker build
command.
DOSH_DOCKER_RMI_EXTRA_OPTS
Set additionnal parameters to docker rmi
command.
EOF
}
run() {
# Prepend echo to output the command instead
if [[ ${dryrun:-} ]]
then
# Preserve exec keyword as first argument if set
if [ "$1" == "exec" ]
then
set -- "$1" echo "${@:1}"
else
set -- echo "$@"
fi
# Preserve spaces in command line if any
set -- "${@// /\\ }"
fi
"$@"
}
is_set_and_not_array() {
compgen -A variable "$1" >/dev/null && \
! compgen -A arrayvar "$1" >/dev/null
}
checksum() {
local sha256
read -r -a sha256 < <(echo -n "$1" | sha256sum -)
echo "${sha256[0]}"
}
file_checksum() {
local sha256
read -r -a sha256 < <(sha256sum "$1" 2>/dev/null || echo "<none>" "$1")
echo "${sha256[0]}"
}
add_symlink() {
local file
local checksum
file="$(realpath "$1")"
checksum="$(checksum "$PWD")"
mkdir -p "$cachedir/$checksum"
ln -sf "$file" "$cachedir/$checksum/$2"
}
remove_symlink() {
rm "$cachedir/$1/$2"
rmdir --ignore-fail-on-non-empty "$cachedir/$1"
}
get_tag() {
local file
local user
local checksum
file="$(realpath "$1")"
checksum="$(file_checksum "$file")"
user="$2"
if [[ ! "${2:-}" ]]
then
user="$(id -un)"
fi
user="${user//[^[:alnum:]_.-]/_}"
echo "dosh-$user-$checksum"
}
get_images() {
local image
local images
mapfile -t images < <(docker images "dosh-*" --format "{{.Repository}}")
for image in "${images[@]}"
do
local symlink
for symlink in "$cachedir"/*/"$image"
do
local file
local status
local checksum
local dockerfile
local dockerfile_checksum
status="Ready"
file="${symlink%/*}"
file="${file##*/}"
checksum="${symlink##*/dosh-}"
dockerfile="$(readlink "$symlink" || echo "<none>")"
dockerfile_checksum="$(file_checksum "$dockerfile")"
if [[ "$dockerfile" == "<none>" ]] ||
[[ "$dockerfile_checksum" == "<none>" ]]
then
status="Deleted"
elif [[ "$dockerfile_checksum" != "$checksum" ]]
then
status="Outdated"
fi
if [[ ! -e "$symlink" ]]
then
file="<none>"
fi
echo "$image" "$file" "$status" "$dockerfile_checksum" "$dockerfile"
done
done
}
list() {
local image
local images
mapfile -t images < <(get_images)
printf "%-69s %-8s %-64s %s\n" "TAG" "STATUS" "CHECKSUM" "DOCKERFILE"
for image in "${images[@]}"
do
local tag
local file
local status
local checksum
local dockerfile
read -r tag file status checksum dockerfile _ <<<"$image"
printf "%-69s %-8s %-64s %s\n" "$tag" "$status" "$checksum" "$dockerfile"
done
}
prune() {
local image
local images
local unused
local symlink
local images_ready
local images_unused
mapfile -t images < <(get_images)
# first pass: store the list of both ready and unused (outdated and
# deleted) images
for image in "${images[@]}"
do
local tag
local file
local status
local checksum
local dockerfile
read -r tag file status checksum dockerfile _ <<<"$image"
if [[ "$status" == "Ready" ]]
then
images_ready+=("$tag")
continue
fi
images_unused+=("$file/$tag")
done
# second pass: unlink the outdated images and remove both outdated and
# deleted images
for unused in "${images_unused[@]}"
do
local tag
local rmiopts
# Verify the image is actually unused
for image in "${images[@]}"
do
local tag
local file
local status
local checksum
local dockerfile
read -r tag file status checksum dockerfile _ <<<"$image"
if [[ "$tag" == "${unused##*/}" ]] &&
[[ "$status" == "Ready" ]]
then
break
fi
done
# The image is actually used, skip it...
if [[ "$tag" == "${unused##*/}" ]] && [[ "$status" == "Ready" ]]
then
continue
fi
IFS=/ read -r _ tag _ <<<"$unused"
# Append the extra options if any
if [ "${DOSH_DOCKER_RMI_EXTRA_OPTS:-}" ]
then
rmiopts+=("${DOSH_DOCKER_RMI_EXTRA_OPTS[@]}")
fi
run "${docker[@]}" rmi "${rmiopts[@]}" "$tag" "$@"
done
# third pass: consolidate the database
for symlink in "$cachedir"/*/"dosh-"*
do
local tag
local checksum
if [[ -e "$symlink" ]]
then
continue
fi
checksum="${symlink%/*}"
checksum="${checksum##*/}"
tag="${symlink##*/}"
remove_symlink "$checksum" "$tag"
done
}
# According to docker-build(1)
#
# DESCRIPTION
#
# Warning, this will send a lot of data to the Docker daemon depending on the
# contents of the current directory. The build is run by the Docker daemon, not
# by the CLI, so the whole context must be transferred to the daemon. The
# Docker CLI reports "Sending build context to Docker daemon" when the context
# is sent to the daemon.
#
# docker_build() tries to limit the context sent to the docker daemon by doing
# a very limited magic if the Dockerfile contains ADD or COPY instruction to
# send local files.
#
# Consider option --no-auto-context do disable this very limited feature as it
# does not fully support Dockerfile as defined in the documentation:
#
# https://docs.docker.com/engine/reference/builder/
docker_build() {
local did
local files
local context_file
if [[ ${DOSH_NOBUILD:-} ]]
then
return
fi
if [[ ! -e "$1" ]]
then
echo "$1: No such Dockerfile" >&2
exit 1
fi
# Inject both user and group id to the Dockerfile
if ! IFS=":" read -r -a did < <(grep '^docker:' /etc/group)
then
did=( "$USER" x "${GROUPS[0]}" )
fi
cat "$1" - <<EOF >doshfile
USER root
RUN grep -q "^$USER:" /etc/group \
|| groupadd --non-unique --gid ${GROUPS[0]} $USER \
|| addgroup -g ${GROUPS[0]} $USER; \
grep -q "^$USER:" /etc/passwd \
|| useradd --non-unique --gid ${GROUPS[0]} --uid $UID --create-home --home-dir $HOME --shell /bin/sh $USER \
|| adduser -G $USER -u $UID -h $HOME -s /bin/sh -D $USER; \
grep -q "^dind:" /etc/group \
|| groupadd --non-unique --gid ${did[2]} dind \
|| addgroup -g ${did[2]} dind; \
grep -q "^dind:x:${did[2]}:.*$USER" /etc/group \
|| usermod --append --group dind $USER \
|| addgroup $USER dind; \
if test -d /etc/sudoers.d; then echo "%$USER ALL=(ALL) NOPASSWD: ALL" >>"/etc/sudoers.d/$USER"; fi
EOF
# List the files from the Dockerfile to send to the docker daemon if no
# context file is given
files=(doshfile)
context_file="${3:-}"
if ! [[ ${context_file:-} ]]
then
local f
local word
local words
while read -r -a words
do
if ! [[ ${words[0]:-} ]]
then
continue
elif [[ ${words[0]^^} =~ ^(ADD|COPY)$ ]]
then
# Remove keyword and destination path
unset 'words[0]'
unset 'words[-1]'
for word in "${words[@]}"
do
# Skip long option and sources with
# schema://
if [[ $word =~ ^-- ]] ||
[[ $word =~ ^.*\:// ]]
then
continue
fi
# Carry out filename expansion
for f in $word
do
files+=("$f")
done
done
elif [[ ${words[0]^^} =~ ^ENTRYPOINT$ ]]
then
echo "Info: ENTRYPOINT is overridden by dosh"
fi
done <doshfile >&2
fi
# Append the extra options if any
if [[ ${DOSH_DOCKER_BUILD_EXTRA_OPTS:-} ]]
then
buildopts+=("${DOSH_DOCKER_BUILD_EXTRA_OPTS[@]}")
fi
buildopts+=(--build-arg "DOSH_USER=$USER")
buildopts+=(--build-arg "DOSH_UID=$UID")
buildopts+=(--build-arg "DOSH_GID=${GROUPS[0]}")
buildopts+=(--build-arg "DOSH_GROUPS=${GROUPS[*]}")
buildopts+=(--build-arg "DOSH_HOME=$HOME")
# Do not send the build context to the daemon if neither ADD nor COPY
# instructions in Dockerfile.
if [[ ${#files[@]} -gt 1 ]] && [[ ${no_auto_context:-} ]]
then
echo "Info: ADD or COPY instructions sends build context to daemon." >&2
echo " Consider option --context <TAR> to speed up the build of image." >&2
echo " First, generate the context archive as suggested by the command below:" >&2
echo " tar cf context.tar ${files[*]}" >&2
echo " Then, run $0 again and tell it to use the context archive:" >&2
echo " $0 --context context.tar ${BASH_ARGV[*]}" >&2
run "${docker[@]}" build "${buildopts[@]}" --tag "$2" --file doshfile .
else
# Or send the files as a context archive
if ! [[ ${context_file:-} ]]
then
run tar c "${files[@]}"
# Or send the given context file
else
run tar rf "$context_file" "doshfile"
run cat "$context_file"
fi | \
run "${docker[@]}" build "${buildopts[@]}" --tag "$2" --file doshfile -
fi
rm -f doshfile
add_symlink "$1" "$2"
}
docker_attach() {
local attachopts
# Append the extra options if any
if [ "${DOSH_DOCKER_ATTACH_EXTRA_OPTS:-}" ]
then
attachopts+=("${DOSH_DOCKER_ATTACH_EXTRA_OPTS[@]}")
fi
run "${docker[@]}" attach "${attachopts[@]}" "$@"
}
docker_kill() {
local killopts
# Append the extra options if any
if [ "${DOSH_DOCKER_KILL_EXTRA_OPTS:-}" ]
then
killopts+=("${DOSH_DOCKER_KILL_EXTRA_OPTS[@]}")
fi
run "${docker[@]}" kill "${killopts[@]}" "$@"
}
docker_rm() {
local rmopts
# Append the extra options if any
if [ "${DOSH_DOCKER_RM_EXTRA_OPTS:-}" ]
then
rmopts+=("${DOSH_DOCKER_RM_EXTRA_OPTS[@]}")
fi
run "${docker[@]}" rm "${rmopts[@]}" "$@"
}
docker_rmi() {
local rmiopts
local checksum
# Append the extra options if any
if [ "${DOSH_DOCKER_RMI_EXTRA_OPTS:-}" ]
then
rmiopts+=("${DOSH_DOCKER_RMI_EXTRA_OPTS[@]}")
fi
run "${docker[@]}" rmi "${rmiopts[@]}" "$@"
checksum="$(checksum "$PWD")"
remove_symlink "$checksum" "$1"
}
has_positional_parameter() {
local parameter
for parameter in "$@"
do
if ! [[ "$parameter" =~ ^[-+] ]]
then
return 0
fi
done
return 1
}
is_doshopt() {
if [[ "$1" =~ ^--(no-auto-context|no-extra-options|no-doshprofile|no-doshrc|root|dind|home|ls|gc|build|rebuild|build-only|rmi|detach|kill|rm|tag)$ ]]
then
return 0
fi
return 1
}
is_doshopt_argument() {
if [[ "$1" =~ ^--(dockerfile|context|directory|working-directory|mount-options|shell|exec|attach)$ ]]
then
return 0
fi
return 1
}
is_shopt() {
local dosh
dosh="${shell=$DOSHELL}"
dosh="${dosh##*/}"
# Do not handle -o shoptname here
# sh (only)
if [[ "$1" =~ ^-[cis]$ ]] || [[ "$1" =~ ^[-+][abefhmnuvxC]$ ]]
then
return 0
fi
# bash (specific)
if [ "$dosh" == "bash" ] && [[ "$1" =~ ^[-+][klprtBDEHIPT]$ ]]
then
return 0
fi
# dash (specific)
if [ "$dosh" == "dash" ] && [[ "$1" =~ ^[-+][pqEIV]$ ]]
then
return 0
fi
# zsh (specific)
if [ "$dosh" == "zsh" ] && [[ "$1" =~ ^[-+][0123456789dgklprtwyBDEFGHIJKLMNOPQRSTUVWXYZ]$ ]]
then
return 0
fi
return 1
}
is_shopt_argument() {
local dosh
dosh="${shell=$DOSHELL}"
dosh="${dosh##*/}"
# sh (only)
if [[ "$1" =~ ^[-+][o]$ ]]
then
return 0
fi
# bash (specific)
if [ "$dosh" == "bash" ] && [[ "$1" =~ ^[-+]O$ ]]
then
return 0
fi
return 1
}
shopts=()
read -r -a docker <<<"${DOSH_DOCKER[*]:-docker}"
dockerfile="${DOSH_DOCKERFILE:-Dockerfile}"
directory="."
working_directory="$PWD"
opts=()
buildopts=("--quiet")
cachedir="${XDG_CACHE_HOME:-$HOME/.cache}/dosh"
DOSHELL="${DOSHELL:-/bin/sh}"
DOSHLVL="${DOSHLVL:-0}"
while [ "$#" -ne 0 ]
do
if [ "$1" = "--help" ]
then
usage
exit
elif [ "$1" = "--version" ]
then
echo "$VERSION"
exit
elif [ "$1" = "--dry-run" ]
then
dryrun=true
elif [ "$1" = "--verbose" ]
then
verbose=true
buildopts=()
# It is a dosh option without argument (i.e. --root, --home...)
elif is_doshopt "$1"
then
optname="${1//-/_}"
optname="${optname:2}"
eval "$optname=1"
# It is a dosh option with argument (i.e. --dockerfile Dockerfile.alt,
# --directory subdir...)
elif is_doshopt_argument "$1"
then
optname="${1//-/_}"
optname="${optname:2}"
eval "$optname=\"$2\""
shift
# It is a shell option without argument (i.e. -c, -i, -s...)
elif is_shopt "$1"
then
shopts+=("$1")
eval "opt_${1:1:1}=1"
# It is a shell option with argument (i.e. -o errexit, +o verbose...)
elif is_shopt_argument "$1"
then
shopts+=("$1")
eval "opt_${1:1:1}=\"${2:-1}\""
if [ $# -gt 1 ]
then
shopts+=("$2")
shift
fi
# It is a serie of a shell options (i.e. -ex, -ic...)
elif [[ "$1" =~ ^-([^-]){2,} ]]
then
[[ "$1" =~ ^${1//?/(.)} ]]
shift
BASH_REMATCH=("${BASH_REMATCH[@]:2}")
set -- "$1" "${BASH_REMATCH[@]/#/-}" "${@:1}"
elif [ "$1" = "--" ]
then
shift
break
else
break
fi
shift
done
# Print the tag and exit
if [[ "${tag:-}" ]]
then
# Print tag
get_tag "$directory/$dockerfile" "${USER:-}"
exit
fi
# Apply the dosh options to the dosh environment variables
# --shell SHELL to DOSHELL
if [[ ${shell:-} ]]
then
DOSHELL="$shell"
fi
# --dockerfile FILE to DOSH_DOCKERFILE
if [[ ${dockerfile:-} ]]
then
DOSH_DOCKERFILE="$dockerfile"
fi
# --no-extra-options resets DOSH_DOCKER_*_EXTRA_OPTS
if [[ ${no_extra_options:-} ]]
then
DOSH_DOCKER_RUN_EXTRA_OPTS=
DOSH_DOCKER_EXEC_EXTRA_OPTS=
DOSH_DOCKER_BUILD_EXTRA_OPTS=
DOSH_DOCKER_RMI_EXTRA_OPTS=
fi
# Change the current directory
cd "$directory"
# Source the profile files
set +euo pipefail
if [[ ! ${no_doshprofile:-} ]] && [ -e ~/.dosh_profile ]
then
. ~/.dosh_profile
fi
if [[ ! ${no_doshrc:-} ]] && [ -e ./doshrc ]
then
. ./doshrc
fi
set -euo pipefail
# Convert the DOSH_DOCKER_*_EXTRA_OPTS variables to arrays; use a backslash to
# escape whitespace and preserve it.
if is_set_and_not_array DOSH_DOCKER_RUN_EXTRA_OPTS
then
# shellcheck disable=SC2162
read -a DOSH_DOCKER_RUN_EXTRA_OPTS <<<"${DOSH_DOCKER_RUN_EXTRA_OPTS:-}"
fi
if is_set_and_not_array DOSH_DOCKER_EXEC_EXTRA_OPTS
then
# shellcheck disable=SC2162
read -a DOSH_DOCKER_EXEC_EXTRA_OPTS <<<"${DOSH_DOCKER_EXEC_EXTRA_OPTS:-}"
fi
if is_set_and_not_array DOSH_DOCKER_BUILD_EXTRA_OPTS
then
# shellcheck disable=SC2162
read -a DOSH_DOCKER_BUILD_EXTRA_OPTS <<<"${DOSH_DOCKER_BUILD_EXTRA_OPTS:-}"
fi
if is_set_and_not_array DOSH_DOCKER_RMI_EXTRA_OPTS
then
# shellcheck disable=SC2162
read -a DOSH_DOCKER_RMI_EXTRA_OPTS <<<"${DOSH_DOCKER_RMI_EXTRA_OPTS:-}"
fi
# List the images and exit
if [[ ${ls:-} ]]
then
# List images
list
exit
fi
# Remove the deleted and outdated images and exit
if [[ ${gc:-} ]]
then
# Garbage collect unused images
prune "$@"
exit
fi
# Attach the container and exit
if [[ ${attach:-} ]]
then
# Attach a detached container
docker_attach "$attach" "$@"
exit
fi
# Kill the container and exit
if [[ ${kill:-} ]]
then
# Kill a detached container
docker_kill "$@" >&2
exit
fi
# Remove the container and exit
if [[ ${rm:-} ]]
then
# Remove a detached container
docker_rm "$@" >&2
exit
fi
# Remove the image and exit
if [[ ${rmi:-} ]]
then
# Remove an existent image
tag="$(get_tag "$DOSH_DOCKERFILE" "${USER:-}")"
docker_rmi "$tag" "$@" >&2
exit
fi
# Run in a new container
if ! [[ ${exec:-} ]]
then
# Rebuild or automatically build the image if it does not exist
tag="$(get_tag "$DOSH_DOCKERFILE" "${USER:-}")"
imageid="$("${docker[@]}" images -q "$tag")"
if [[ ${build:-} ]] || [[ ${rebuild:-} ]] || ! [[ ${imageid:-} ]]
then
# Reset the quiet option when the image does not exist or it
# rebuild or if verbose option is set
if ! [[ ${imageid:-} ]] || [[ ${rebuild:-} ]] ||
[[ ${verbose:-} ]]
then
buildopts=()
fi
docker_build "$DOSH_DOCKERFILE" "$tag" "${context:-}" >&2
fi
# Detach the container
if [[ ${detach:-} ]]
then
opts+=("--detach")
# Or remove the container on exit
else
opts+=("--rm")
fi
# Bind mount the home directory
if [[ ${home:-} ]]
then
opts+=("--volume" "$HOME:$HOME:${mount_options:-rw}")
# Or, bind mount the current working directory
else
opts+=("--volume" "$PWD:$PWD:${mount_options:-rw}")
fi
fi
# Image is built already, do not run a container and exit
if [[ ${build_only:-} ]]
then
# Do nothing
exit
fi
# Set the current user privileges for dosh-in-dosh
if [[ ${dind:-} ]]
then
opts+=("--user" "$UID:dind")
opts+=("--env" "USER=$USER")
opts+=("--env" "HOME=$HOME")
opts+=("--volume" "/var/run/docker.sock:/var/run/docker.sock")
opts+=("--volume" "$0:/bin/dosh:ro")
# Or set the current user privileges
elif ! [[ ${root:-} ]]
then
opts+=("--user" "$UID:${GROUPS[0]}")
opts+=("--env" "USER=$USER")
opts+=("--env" "HOME=$HOME")
fi
# Set the interactive options
if [[ ${opt_i:-} ]] || [[ ${opt_s:-} ]] || ! has_positional_parameter "$@"
then
opts+=("--interactive")
# Allocate a pseudo-TTY if stdin/stderr are TTY
if [ -t 0 ] && [ -t 2 ]
then
opts+=("--tty")
fi
fi
# Prepend the shell options if any
if [ ${#shopts[*]} -gt 0 ]
then
set -- "${shopts[@]:-}" "$@"
fi
# Set the working directory
opts+=("--workdir" "$working_directory")
# Incremented by one level instance
opts+=("--env" "DOSHLVL=$((DOSHLVL+1))")
# Execute in an existing container and exit
if [[ ${exec:-} ]]
then
# Append the extra options if any
if [[ ${DOSH_DOCKER_EXEC_EXTRA_OPTS:-} ]]
then
opts+=("${DOSH_DOCKER_EXEC_EXTRA_OPTS[@]}")
fi
run exec "${docker[@]}" exec "${opts[@]}" "$exec" "$DOSHELL" "$@"
# Never reached!
exit 127
fi
# Run in a new container
opts+=("--entrypoint" "$DOSHELL")
if [[ ${verbose:-} ]]
then
echo "$tag"
fi >&2
# Append the extra options if any
if [[ ${DOSH_DOCKER_RUN_EXTRA_OPTS:-} ]]
then
opts+=("${DOSH_DOCKER_RUN_EXTRA_OPTS[@]}")
fi
run exec "${docker[@]}" run "${opts[@]}" "$tag" "$@"
# Never reached!
exit 127