-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcreate-images.sh
executable file
·361 lines (300 loc) · 10.5 KB
/
create-images.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
#!/bin/bash
set -e
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
: "${MAISTRA_VERSION:=2.4}"
: "${MAISTRA_PROJECT:=https://github.com/maistra}"
: "${HUB:=quay.io/maistra-dev}"
: "${TAG:="${MAISTRA_VERSION}"}"
: "${ISTIO_REPO:="${MAISTRA_PROJECT}/istio.git"}"
: "${ISTIO_BRANCH:="maistra-${MAISTRA_VERSION}"}"
: "${REPOSDIR:="${DIR}/tmp"}"
: "${RM:=rm}"
: "${MAKE:=make}"
: "${GIT:=git}"
: "${CONTAINER_CLI:=docker}"
MAISTRA_DEFAULT_COMPONENTS=(
"istio"
"istio-operator"
"istio-must-gather"
"ratelimit"
"prometheus"
)
COMPONENTS="${MAISTRA_COMPONENTS:-${MAISTRA_DEFAULT_COMPONENTS[@]}}"
DEFAULT_IMAGES=(
"pilot"
"proxyv2"
"istio-cni"
"istio-operator"
"istio-must-gather"
"ratelimit"
"prometheus"
)
IMAGES="${ISTIO_IMAGES:-${DEFAULT_IMAGES[@]}}"
PUSH_LATEST="${PUSH_LATEST:-false}"
function verify_podman() {
[ "${EUID}" == "0" ] && return
if [ "${CONTAINER_CLI}" = "podman" ] || ([ "${CONTAINER_CLI}" = "docker" ] && docker --version | grep -q podman) ; then
echo "****************************************************************************************"
echo
echo "You are using podman in rootless mode. Some images may not work in this mode. Aborting."
echo
echo "****************************************************************************************"
exit 1
fi
}
function usage() {
[[ -n "${1}" ]] && echo "${1}"
cat <<EOF
Usage: ${BASH_SOURCE[0]} [options ...]"
options:
-t <TAG> TAG to use for operations on images. Defaults to "${TAG}". Special tag 'DAILY' will be replaced with today's date in the format ${MAISTRA_VERSION}-daily-yyyy-mm-dd
-h <HUB> Docker hub + username. Defaults to "${HUB}"
-c <COMPONENTS> Specify which Maistra components should be built
-i <IMAGES> Specify which images should be deleted
-b Build locally images
-d Delete images
-p Build and push images
-k Handle bookinfo images in addition to others
At least one of -b, -d or -p is required.
Environment Variables:
- MAISTRA_COMPONENTS: Specify which Maistra components should be built/pushed (CLI flag -c takes precedence). Default: "${MAISTRA_DEFAULT_COMPONENTS[@]}"
- ISTIO_IMAGES: Specify which images should be deleted (CLI flag -i takes precedence). Default: "${DEFAULT_IMAGES[@]}"
- ISTIO_REPO: Istio repository URL to clone, when building bookinfo images. Default: ${ISTIO_REPO}
- ISTIO_BRANCH: Istio repository branch to clone, when building bookinfo images. Default: ${ISTIO_BRANCH}
EOF
exit 2
}
function suffix() {
if [ "${1}" == "istio-must-gather" ]; then
return
fi
echo "-ubi8"
}
function get_image_name() {
local image="${1}"
case ${image} in
"istio-operator")
echo "${HUB}/istio-ubi8-operator:${TAG}"
;;
"ratelimit")
echo "${HUB}/${image}$(suffix "${image}")"
;;
*)
echo "${HUB}/${image}$(suffix "${image}"):${TAG}"
;;
esac
}
function build_bookinfo() {
local dir
dir="$(mktemp -d)"
${GIT} clone --depth=1 -b "${ISTIO_BRANCH}" "${ISTIO_REPO}" "${dir}"
local src="${dir}/samples/bookinfo/src"
pushd "${src}/productpage"
${CONTAINER_CLI} build --pull -t "${HUB}/examples-bookinfo-productpage-v1:${TAG}" .
#flooding
${CONTAINER_CLI} build --pull -t "${HUB}/examples-bookinfo-productpage-v-flooding:${TAG}" --build-arg flood_factor=100 .
popd
pushd "${src}/details"
#plain build -- no calling external book service to fetch topics
${CONTAINER_CLI} build --pull -t "${HUB}/examples-bookinfo-details-v1:${TAG}" --build-arg service_version=v1 .
#with calling external book service to fetch topic for the book
${CONTAINER_CLI} build --pull -t "${HUB}/examples-bookinfo-details-v2:${TAG}" --build-arg service_version=v2 \
--build-arg enable_external_book_service=true .
popd
pushd "${src}/reviews"
#java build the app.
${CONTAINER_CLI} run --rm -u root -v "$(pwd)":/home/gradle/project -w /home/gradle/project gradle:4.8.1 gradle clean build
pushd reviews-wlpcfg
#plain build -- no ratings
${CONTAINER_CLI} build --pull -t "${HUB}/examples-bookinfo-reviews-v1:${TAG}" --build-arg service_version=v1 .
#with ratings black stars
${CONTAINER_CLI} build --pull -t "${HUB}/examples-bookinfo-reviews-v2:${TAG}" --build-arg service_version=v2 \
--build-arg enable_ratings=true .
#with ratings red stars
${CONTAINER_CLI} build --pull -t "${HUB}/examples-bookinfo-reviews-v3:${TAG}" --build-arg service_version=v3 \
--build-arg enable_ratings=true --build-arg star_color=red .
popd
${CONTAINER_CLI} run --rm -u root -v "$(pwd)":/home/gradle/project -w /home/gradle/project gradle:4.8.1 sh -c 'gradle clean ; rm -rf .gradle'
popd
pushd "${src}/ratings"
${CONTAINER_CLI} build --pull -t "${HUB}/examples-bookinfo-ratings-v1:${TAG}" --build-arg service_version=v1 .
${CONTAINER_CLI} build --pull -t "${HUB}/examples-bookinfo-ratings-v2:${TAG}" --build-arg service_version=v2 .
${CONTAINER_CLI} build --pull -t "${HUB}/examples-bookinfo-ratings-v-faulty:${TAG}" --build-arg service_version=v-faulty .
${CONTAINER_CLI} build --pull -t "${HUB}/examples-bookinfo-ratings-v-delayed:${TAG}" --build-arg service_version=v-delayed .
${CONTAINER_CLI} build --pull -t "${HUB}/examples-bookinfo-ratings-v-unavailable:${TAG}" --build-arg service_version=v-unavailable .
${CONTAINER_CLI} build --pull -t "${HUB}/examples-bookinfo-ratings-v-unhealthy:${TAG}" --build-arg service_version=v-unhealthy .
popd
pushd "${src}/mysql"
${CONTAINER_CLI} build --pull -t "${HUB}/examples-bookinfo-mysqldb:${TAG}" .
popd
pushd "${src}/mongodb"
${CONTAINER_CLI} build --pull -t "${HUB}/examples-bookinfo-mongodb:${TAG}" .
popd
${RM} -rf "${dir}"
}
function exec_bookinfo_images() {
local cmd="${1}"
local images
local image
images="$(${CONTAINER_CLI} images --format "{{.Repository}}:{{.Tag}}" | grep -E "examples-bookinfo.*$TAG")"
echo "$images"
for image in ${images}; do
${CONTAINER_CLI} "${cmd}" "${image}"
done
}
function get_repo() {
if [ $# -ne 1 ]; then
echo "Usage: get_repo REPOSITORY_NAME"
exit 1
fi
local repo_name=$1
${GIT} clone "${MAISTRA_PROJECT}/${repo_name}"
}
function exec_build() {
if [ $# -ne 2 ]; then
echo "ERROR"
echo "Usage: exec_build COMPONENT_NAME build|push"
exit 1
fi
if [ "$2" != "build" ] && [ "$2" != "push" ]; then
echo "ERROR"
echo "Usage: exec_build COMPONENT_NAME build|push"
exit 1
fi
local component=$1
local push=false
if [ "$2" == "push" ]; then
push=true
fi
local image
image="$(get_image_name "${component}")"
case ${component} in
"istio"|"maistra-istio"|"istio-maistra") #Possibility to test with other Maistra Istio names (ex: forked repos)
${GIT} checkout "${ISTIO_BRANCH}"
make_target="maistra-image"
if ${push}; then
make_target="maistra-image.push"
fi
make_vars=("HUB=${HUB}" "TAG=${TAG}")
;;
"istio-must-gather")
#TODO: delete this sed when podman will be a variable (cf. https://github.com/maistra/istio-must-gather/blob/maistra-2.2/Makefile#L23-L27)
sed -i -e "s/podman/${CONTAINER_CLI}/g" "${REPOSDIR}/${component}/Makefile"
make_target="image"
if ${push}; then
make_target="push"
fi
make_vars=("HUB=${HUB}" "TAG=${TAG}")
;;
"istio-operator")
${GIT} checkout ${ISTIO_BRANCH}
echo "${REPOSDIR}/${component} - ${MAKE} IMAGE=${image} image"
${MAKE} IMAGE="${image}" image
if ${push}; then
${CONTAINER_CLI} push "${image}"
fi
;;
"ratelimit")
${GIT} checkout "${ISTIO_BRANCH}"
make_target="docker_image_without_tests"
if ${push}; then
make_target="docker_push_without_tests"
fi
make_vars=("IMAGE=${image}" "VERSION=${TAG}")
;;
"prometheus")
# This is the last maintained branch
${GIT} checkout "maistra-2.1"
cp "${DIR}/Dockerfile.prometheus" "${REPOSDIR}/${component}/Dockerfile.maistra"
${CONTAINER_CLI} build "${REPOSDIR}/${component}" -f Dockerfile.maistra -t "${image}"
if ${push}; then
${CONTAINER_CLI} push "${image}"
fi
;;
*)
echo "${component} is not in the maistra components list"
;;
esac
#Istio-operator and Prometheus builds are specific
if [ "${component}" != "prometheus" ] && [ "${component}" != "istio-operator" ]; then
${MAKE} "${make_vars[@]}" "${make_target}"
fi
}
function push_latest_tag() {
if [ "${PUSH_LATEST}" != "true" ] || [ -z "${PUSH}" ]; then
return
fi
local images
local image
local repo
local latest_tag="${MAISTRA_VERSION}-latest"
images="$(${CONTAINER_CLI} images --format "{{.Repository}}:{{.Tag}}" | grep -E ".*$TAG")"
for image in ${images}; do
repo=$(echo "${image}" | cut -d: -f1)
${CONTAINER_CLI} tag "${image}" "${repo}:${latest_tag}"
${CONTAINER_CLI} push "${repo}:${latest_tag}"
done
}
## MAIN
while getopts ":t:h:i:c:bdpk" opt; do
case ${opt} in
t) TAG="${OPTARG}";;
h) HUB="${OPTARG}";;
b) BUILD=true;;
d) DELETE=true;;
p) PUSH=true;;
c) COMPONENTS="${OPTARG}";;
i) IMAGES="${OPTARG}";;
k) BOOKINFO=true;;
*) usage;;
esac
done
[[ -z "${TAG}" ]] && usage "Missing TAG"
[[ -z "${BUILD}" && -z "${DELETE}" && -z "${PUSH}" ]] && usage
if [ "${TAG}" == "DAILY" ]; then
TAG="${MAISTRA_VERSION}-daily-$(date +%Y-%m-%d)"
PUSH_LATEST="true"
fi
verify_podman
if [ -n "${DELETE}" ]; then
for image in ${IMAGES}; do
echo "Deleting image ${image}..."
if [ "${image}" == "ratelimit" ]; then
image_name="$(get_image_name "$image"):${TAG}"
else
image_name="$(get_image_name "$image")"
fi
${CONTAINER_CLI} rmi "${image_name}"
done
if [ -n "${BOOKINFO}" ]; then
exec_bookinfo_images rmi
fi
fi
if [ -n "${BUILD}" ] || [ -n "${PUSH}" ]; then
[ ! -d "${REPOSDIR}" ] && mkdir "${REPOSDIR}"
trap 'echo "Removing ${REPOSDIR}" && ${RM} -rf "${REPOSDIR}"' EXIT
cd "${REPOSDIR}"
for component in ${COMPONENTS}; do
echo "[${component}] Clone the git repository "
get_repo "${component}"
cd "${REPOSDIR}/${component}"
if [ -n "${BUILD}" ]; then
echo "[${component}] Execute the container image build"
exec_build "${component}" "build"
fi
if [ -n "${PUSH}" ]; then
echo "[${component}] Execute the build and push container image build"
exec_build "${component}" "push"
fi
cd "${REPOSDIR}/"
echo "Done"
echo
done
if [ -n "${BOOKINFO}" ]; then
build_bookinfo
if [ -n "${PUSH}" ]; then
exec_bookinfo_images push
fi
fi
fi
push_latest_tag