-
Notifications
You must be signed in to change notification settings - Fork 0
/
dr
executable file
·199 lines (177 loc) · 6.15 KB
/
dr
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
#!/usr/bin/env bash
# Copyright (c) 2021 Microchip Technology Inc. and its subsidiaries.
# SPDX-License-Identifier: MIT
unset MCHP_DOCKER_DEBUG
unset MCHP_DOCKER_PULL
unset MCHP_DOCKER_VERBOSE
unset MCHP_DOCKER_INTERACTIVE
unset MCHP_DOCKER_NO_FILE
unset MCHP_DOCKER_TOP
script=$(basename $0)
denv=".docker.env"
top=$(pwd)
hostname=dr-$RANDOM-$(hostname)
usage() {
echo "$script usage:"
echo " $script [options] [command]"
echo ""
echo "Options:"
echo " -d Debug mode. Specify image by \"name:latest\" and ignore tag and digest"
echo " even if they are present in $denv. This way you don't have to update"
echo " tag and digest after each docker build in order to test the image."
echo " -h Show this help."
echo " -p Pull docker image unconditionally using name:tag (ignore digest)."
echo " -n Ignore $denv file and use environment variables."
echo " -t Overwrite the top folder, can be used only with -n option."
echo " -v Verbose mode."
echo ""
echo "Command:"
echo " Optional command to run - default command depends on docker image."
echo ""
echo "File $denv:"
echo " This file must be present somewhere between the current directory and"
echo " the root of the filesystem (\"/\") when the $script script is used."
echo " It determines which docker image to run and will eventually pull it"
echo " if not found in the local cache."
echo ""
echo " The following variables are supported:"
echo " MCHP_DOCKER_NAME - The name of the repository (required)."
echo " MCHP_DOCKER_TAG - The tag (optional). Default is \"latest\"."
echo " MCHP_DOCKER_DIGEST - The digest (optional). If present, the image is"
echo " identified as \"name@digest\". If missing or -d is "
echo " given the docker image is identified as \"name:tag\"."
echo " MCHP_DOCKER_OPTIONS - Extra options to the docker run command (optional)."
echo ""
echo " The $denv file is sourced in a bash envirinment and it is possible to do all"
echo " kind of processing/verfication and eventually call exit 1 on errors."
echo ""
echo " It is possible to define one or more variables and transfer them into the"
echo " container by adding an -e option in MCHP_DOCKER_OPTIONS"
echo ""
echo "The Docker container is run interactively (-it) and is removed on exit (--rm)."
echo "The directory containing the $denv file is mounted 1:1 inside the container"
echo "and the current directory inside the container will be the same as outside."
exit 1
}
# Only echo if verbose
vecho() { if [[ -v MCHP_DOCKER_VERBOSE ]]; then echo "$@"; fi }
while getopts ":dhpvnt:" opt; do
case $opt in
d)
MCHP_DOCKER_DEBUG=1
;;
h)
usage
;;
p)
MCHP_DOCKER_PULL=1
;;
v)
MCHP_DOCKER_VERBOSE=1
;;
n)
MCHP_DOCKER_NO_FILE=1
;;
t)
MCHP_DOCKER_TOP=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" 1>&2
usage
;;
esac
done
shift $((OPTIND - 1))
if [[ -v MCHP_DOCKER_NO_FILE ]]; then
if [[ -v MCHP_DOCKER_TOP ]]; then
top="$(readlink -f "$MCHP_DOCKER_TOP")"
fi
else
unset MCHP_DOCKER_NAME
unset MCHP_DOCKER_TAG
unset MCHP_DOCKER_DIGEST
unset MCHP_DOCKER_OPTIONS
# Search for .docker.env from current directory and back to root directory "/"
while [[ "$top" != "/" && ! -e "$top/$denv" ]]; do
top="$(readlink -f "$top"/..)"
done
if [[ ! -f "$top/$denv" ]]; then
echo "No $denv file found in parent directories" 1>&2
exit 1
fi
source $top/$denv
fi
if [[ ! -v MCHP_DOCKER_NAME ]]; then
echo "Docker image name not defined, check $top/$denv" 1>&2
exit 1
fi
if [[ -v MCHP_DOCKER_DEBUG || ! -v MCHP_DOCKER_TAG ]]; then
MCHP_DOCKER_TAG="latest"
fi
pull="${MCHP_DOCKER_NAME}:${MCHP_DOCKER_TAG}"
if [[ -v MCHP_DOCKER_PULL ]]; then
echo "Pull $pull"
docker pull "$pull"
fi
if [[ -v MCHP_DOCKER_DEBUG || ! -v MCHP_DOCKER_DIGEST ]]; then
unset MCHP_DOCKER_DIGEST
image="$pull"
match="${MCHP_DOCKER_NAME}_${MCHP_DOCKER_TAG}"
format="{{.Repository}}_{{.Tag}}"
else
image="${MCHP_DOCKER_NAME}@${MCHP_DOCKER_DIGEST}"
match="${MCHP_DOCKER_NAME}_${MCHP_DOCKER_TAG}_${MCHP_DOCKER_DIGEST}"
format="{{.Repository}}_{{.Tag}}_{{.Digest}}"
fi
vecho "Top : $top"
vecho "Name : $MCHP_DOCKER_NAME"
vecho "Tag : $MCHP_DOCKER_TAG"
vecho "Digest : $MCHP_DOCKER_DIGEST"
vecho "Options : $MCHP_DOCKER_OPTIONS"
vecho "Pull : $pull"
vecho "Image : $image"
vecho "Match : $match"
vecho "Format : $format"
vecho "Command : $@"
# Check if the image is present in local cache.
# If not then pull image from hub.
docker images --format $format | grep -F --quiet $match
if [ $? -ne 0 ]; then
docker pull $pull
if [ $? -ne 0 ]; then
echo "Failed to pull docker image $pull" 1>&2
exit 1
fi
if [[ -v MCHP_DOCKER_DIGEST ]]; then
# Check that the image just pulled via name:tag has the expected digest
docker images --format $format | grep -F --quiet $match
if [ $? -ne 0 ]; then
echo "Mismatch between tag and digest, check $top/$denv" 1>&2
exit 1
fi
fi
fi
if [[ -t 0 && -t 1 ]]; then
MCHP_DOCKER_INTERACTIVE='-it' # We have tty's
fi
# $MCHP_DOCKER_IT : Set to '-it' if run interactively
# --rm : Remove the container at exit
# -v : Mount everything from the directory where .docker.env is located.
# -w : Working directory inside the container is the same as outside.
# $MCHP_DOCKER_OPTIONS : User supplied options in .docker.env
args=(run $MCHP_DOCKER_INTERACTIVE --hostname $hostname --rm -v $top:$top -w $(pwd) $MCHP_DOCKER_OPTIONS)
# parse all the other MCHP_DOCKER_OPTIONS$i options, these contains white spaces
for i in $(seq 0 1024)
do
combined=MCHP_DOCKER_OPTIONS${i}
if [ ! -z "${!combined}" ]
then
var1=$(eval echo ${!combined} | cut -d '=' -f 1)
var2=$(eval echo ${!combined} | cut -d '=' -f 2)
args+=(-e ${var1}="${var2}")
fi
done
# $image : Docker image to run
args+=($image "$@")
vecho "docker ${args[@]}"
docker "${args[@]}"