-
Notifications
You must be signed in to change notification settings - Fork 1
/
dockerw
executable file
·293 lines (246 loc) · 11.4 KB
/
dockerw
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
#! /usr/bin/env bash
#*******************************************************************************
# Copyright (c) 2018 Eclipse Foundation and others.
# This program and the accompanying materials are made available
# under the terms of the Eclipse Public License 2.0
# which is available at http://www.eclipse.org/legal/epl-v20.html,
# or the MIT License which is available at https://opensource.org/licenses/MIT.
# SPDX-License-Identifier: EPL-2.0 OR MIT
#*******************************************************************************
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
IFS=$'\n\t'
SCRIPT_FOLDER="$(dirname "$(readlink -f "${0}")")"
push() {
local image="${1}"
local tag="${2}"
local retry="${3:-5}"
n=0
until [ $n -ge "${retry}" ]; do
docker push "${image}:${tag}" && break
echo "Try #$n failed ($image:${tag})... sleeping for 15 seconds"
n=$((n+1))
sleep 15
done
}
digest() {
local image="${1}"
local tag="${2}"
local ref="${image}${tag:+":${tag}"}"
if docker image inspect "${ref}" &> /dev/null; then
docker pull -q "${ref}" &> /dev/null
fi
(docker inspect --format='{{index .RepoDigests 0}}' "${ref}" | sed -E 's/.*@(.*)/\1/g') || :
}
expand_build_args() {
local tpl="${1}"
mapfile -t buildargs < <(echo "${@:2}" | sed -e 's/--opt build-arg://g' | tr ' ' '\n')
str="${tpl}"
for arg in "${buildargs[@]}"; do
str=$(echo "${str}" | sed -E 's/\$\{?'"${arg%%=*}"'\}?/'"${arg##*=}"'/g')
done
echo "${str}"
}
build2() {
local images="${1}"
local dockerfile="${2:-Dockerfile}"
local context="${3:-"$(dirname "${dockerfile}")"}"
local push="${4:-"false"}"
# BUILDKIT_PORT="${BUILDKIT_PORT:-$(shuf -i 52000-55000 -n 1)}"
# export BUILDKIT_PORT
# BUILDKIT_VOLUMENAME="${BUILDKIT_VOLUMENAME:-"buildkitd"-$$}"
# export BUILDKIT_VOLUMENAME
# export BUILDKIT_REMOVE_VOLUME_AFTER_USE=${BUILDKIT_REMOVE_VOLUME_AFTER_USE:-false}
# export BUILDCTL_STOP_DAEMON_AFTER_USE=${BUILDCTL_STOP_DAEMON_AFTER_USE:-false}
if [[ $# -gt 4 ]]; then
shift 4
else
shift $#
fi
local first_image_name
first_image_name="$(echo "${images}" | cut -d, -f1)"
local parent_image
parent_image=$(grep -E " *FROM" "${dockerfile}" | tail -1 | sed -E 's/ *FROM +([^ ]*).*/\1/')
parent_image="$(expand_build_args "${parent_image}" "${@}")"
docker pull "${parent_image}" &>> "${dockerfile}.buildlog" || :
if ! docker pull "${first_image_name}" &>> "${dockerfile}.buildlog"; then
echo "INFO: Cannot find or pull ${first_image_name}"
echo "INFO: Building ${dockerfile} for the first time"
echo "INFO: Will tag image as ${images}"
"${SCRIPT_FOLDER}/buildctl" build2 "${images}" "${dockerfile}" "${context}" "${push}" "${@}"
else
local check_for_update_logs check_for_update_logs_parent
check_for_update_logs=$(mktemp)
check_for_update_logs_parent=$(mktemp)
if "${SCRIPT_FOLDER}/check-for-update.sh" "${first_image_name}" > "${check_for_update_logs}"; then
# ${first_image_name} packages are up to date, build with cache
echo "INFO: Building ${dockerfile} with cache from ${first_image_name} (all packages are up to date)"
echo "INFO: Will tag image as ${images}"
"${SCRIPT_FOLDER}/buildctl" build_with_cache2 "${images}" "${dockerfile}" "${context}" "${push}" "${@}"
elif "${SCRIPT_FOLDER}/check-for-update.sh" "${parent_image}" > "${check_for_update_logs_parent}"; then
# ${first_image_name} packages are outdated, but ${parent_image} packages are up to date, build with no cache
echo "INFO: Building ${dockerfile} with no cache (some packages are outdated)"
echo "INFO: Will tag image as ${images}"
"${SCRIPT_FOLDER}/buildctl" build2 "${images}" "${dockerfile}" "${context}" "${push}" "${@}"
elif [[ $(diff "${check_for_update_logs}" "${check_for_update_logs_parent}" | grep -c '^<') -gt 0 ]]; then
# Both ${first_image_name} and ${parent_image} packages are outdated,
# some packages from ${first_image_name} are not outdated/installed in ${parent_image}, build with no cache
echo "INFO: Building ${dockerfile} with no cache (some packages are outdated)"
echo "INFO: Will tag image as ${images}"
echo "DEBUG: Package difference between ${first_image_name} <-> ${parent_image}"
diff "${check_for_update_logs}" "${check_for_update_logs_parent}" || :
"${SCRIPT_FOLDER}/buildctl" build2 "${images}" "${dockerfile}" "${context}" "${push}" "${@}"
else
# Both ${first_image_name} and ${parent_image} packages are outdated,
# but all outdated packages from ${first_image_name} come from ${parent_image}, build with cache
echo "INFO: Building ${dockerfile} with cache from ${first_image_name} (some packages are outdated, but they are controlled by parent image exclusively)"
echo "INFO: Will tag image as ${images}"
echo "DEBUG: Package difference between ${first_image_name} <-> ${parent_image}"
diff "${check_for_update_logs}" "${check_for_update_logs_parent}" || :
"${SCRIPT_FOLDER}/buildctl" build_with_cache2 "${images}" "${dockerfile}" "${context}" "${push}" "${@}"
fi
rm -f "${check_for_update_logs}" "${check_for_update_logs_parent}"
fi
}
build() {
local image="${1}"
local tag="${2}"
local dockerfile="${3:-Dockerfile}"
local context="${4:-"$(dirname "${dockerfile}")"}"
local push="${5:-"false"}"
local latest="${6:-"false"}"
# BUILDKIT_PORT="${BUILDKIT_PORT:-$(shuf -i 52000-55000 -n 1)}"
# export BUILDKIT_PORT
# BUILDKIT_VOLUMENAME="${BUILDKIT_VOLUMENAME:-"buildkitd"-$$}"
# export BUILDKIT_VOLUMENAME
# export BUILDKIT_REMOVE_VOLUME_AFTER_USE=${BUILDKIT_REMOVE_VOLUME_AFTER_USE:-false}
# export BUILDCTL_STOP_DAEMON_AFTER_USE=${BUILDCTL_STOP_DAEMON_AFTER_USE:-false}
if [[ $# -gt 6 ]]; then
shift 6
else
shift $#
fi
rm -f "${dockerfile}.buildlog"
touch "${dockerfile}.buildlog"
local parent_image
parent_image=$(grep -E " *FROM" "${dockerfile}" | tail -1 | sed -E 's/ *FROM +([^ ]*).*/\1/')
parent_image="$(expand_build_args "${parent_image}" "${@}")"
docker pull "${parent_image}" &>> "${dockerfile}.buildlog" || :
if ! docker pull "${image}:${tag}" &>> "${dockerfile}.buildlog"; then
echo "INFO: Cannot find ${image}:${tag} on local repository" | tee -a "${dockerfile}.buildlog" >&2
echo "INFO: Building ${dockerfile} for the first time" | tee -a "${dockerfile}.buildlog" >&2
"${SCRIPT_FOLDER}/buildctl" build "${image}" "${tag}" "${dockerfile}" "${context}" "${push}" "${latest}" "${@}" | tee -a "${dockerfile}.buildlog" >&2
else
local check_for_update_logs
check_for_update_logs=$(mktemp)
local check_for_update_logs_parent
check_for_update_logs_parent=$(mktemp)
if "${SCRIPT_FOLDER}/check-for-update.sh" "${image}:${tag}" > "${check_for_update_logs}"; then
# ${image}:${tag} packages are up to date, build with cache
echo "INFO: Building ${dockerfile} with cache from ${image}:${tag} (all packages are up to date)" | tee -a "${dockerfile}.buildlog" >&2
"${SCRIPT_FOLDER}/buildctl" build_with_cache "${image}" "${tag}" "${dockerfile}" "${context}" "${push}" "${latest}" "${@}" | tee -a "${dockerfile}.buildlog" >&2
elif "${SCRIPT_FOLDER}/check-for-update.sh" "${parent_image}" > "${check_for_update_logs_parent}"; then
# ${image}:${tag} packages are outdated, but ${parent_image} packages are up to date, build with no cache
echo "INFO: Building ${dockerfile} with no cache (some packages are outdated)" | tee -a "${dockerfile}.buildlog" >&2
"${SCRIPT_FOLDER}/buildctl" build "${image}" "${tag}" "${dockerfile}" "${context}" "${push}" "${latest}" "${@}" | tee -a "${dockerfile}.buildlog" >&2
elif [[ $(diff "${check_for_update_logs}" "${check_for_update_logs_parent}" | grep -c '^<') -gt 0 ]]; then
# Both ${image}:${tag} and ${parent_image} packages are outdated,
# some packages from ${image}:${tag} are not outdated/installed in ${parent_image}, build with no cache
echo "INFO: Building ${dockerfile} with no cache (some packages are outdated)" | tee -a "${dockerfile}.buildlog" >&2
{
echo "INFO: Packages outdated in ${image}:${tag}"
cat "${check_for_update_logs}"
echo "INFO: Packages outdated in ${parent_image}"
cat "${check_for_update_logs_parent}"
echo "INFO: Diff ${image}:${tag} <-> ${parent_image}"
diff "${check_for_update_logs}" "${check_for_update_logs_parent}" || :
} >> "${dockerfile}.buildlog"
"${SCRIPT_FOLDER}/buildctl" build "${image}" "${tag}" "${dockerfile}" "${context}" "${push}" "${latest}" "${@}" | tee -a "${dockerfile}.buildlog" >&2
else
# Both ${image}:${tag} and ${parent_image} packages are outdated,
# but all outdated packages from ${image}:${tag} come from ${parent_image}, build with cache
echo "INFO: Building ${dockerfile} with cache from ${image}:${tag} (some packages are outdated, but they are controlled by parent image exclusively)" | tee -a "${dockerfile}.buildlog" >&2
{
echo "INFO: Packages outdated in ${image}:${tag}"
cat "${check_for_update_logs}"
echo "INFO: Packages outdated in ${parent_image}"
cat "${check_for_update_logs_parent}"
echo "INFO: Diff ${image}:${tag} <-> ${parent_image}"
diff "${check_for_update_logs}" "${check_for_update_logs_parent}" || :
} >> "${dockerfile}.buildlog"
"${SCRIPT_FOLDER}/buildctl" build_with_cache "${image}" "${tag}" "${dockerfile}" "${context}" "${push}" "${latest}" "${@}" | tee -a "${dockerfile}.buildlog" >&2
fi
rm -f "${check_for_update_logs}" "${check_for_update_logs_parent}"
fi
}
build_all() {
local repo="${1}"
local from="${2}"
local image="${3:-}"
mapfile -t dockerfiles < <(find -L "${from}" -iname '*Dockerfile' | sed 's|\./||' | sort -V)
for dockerfile in "${dockerfiles[@]}"; do
local build_dir
build_dir=$(dirname "$dockerfile")
local tag=${build_dir##*\/}
: "${image:=$(basename "${from}")}"
if [[ -z "${tag:-}" ]] || [[ "${tag}" == "$(basename "${from}")" ]]; then
tag="latest"
fi
build "${repo}/${image}" "${tag}" "${dockerfile}"
if [[ "${dockerfile}" = "${dockerfiles[-1]}" ]] && ! find "${from}" -type l -name "latest" | grep '.*'; then
tag_alias "${repo}/${image}" "${tag}" "latest"
fi
done
}
push_all() {
local repo="${1}"
local from="${2}"
local image="${3:-}"
mapfile -t dockerfiles < <(find -L "${from}" -iname '*Dockerfile' | sed 's|\./||' | sort -V)
for dockerfile in "${dockerfiles[@]}"; do
local build_dir
build_dir=$(dirname "$dockerfile")
local tag="${build_dir##*\/}"
: "${image:=$(basename "${from}")}"
if [[ -z "${tag:-}" ]] || [[ "${tag}" == "$(basename "${from}")" ]]; then
tag="latest"
fi
push "${repo}/${image}" "${tag}"
if [[ "${dockerfile}" = "${dockerfiles[-1]}" ]] && ! find "${from}" -type l -name "latest" | grep '.*'; then
push "${repo}/${image}" "latest"
fi
done
}
tag_alias() {
local image="${1}"
local tag="${2}"
local newtag="${3}"
docker tag "${image}:${tag}" "${image}:${newtag}"
}
clean() {
docker container prune -f
docker image prune -f
docker volume prune -f
}
rmi_all() {
local r="${1}"
if [[ $(docker image list "${r}" | wc -l) -gt 1 ]]; then
docker image list "${r}" | tr -s ' ' | tail -n +2 | cut -d ' ' -f 3 | sort -u | xargs docker image rm -f
fi
}
main() {
echo "You must enter a valid command"
exit 1
}
run() {
local args=$*
local f="${1:-}"
if [[ "$f" == "" ]]; then
main
else
$args
fi
}
run "$@"