-
Notifications
You must be signed in to change notification settings - Fork 1
/
kvbuild.sh
executable file
·232 lines (201 loc) · 6.03 KB
/
kvbuild.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
#! /bin/bash
export DOCKER_BUILDKIT=1
set -euo pipefail
kvuser=kvalobs
kvuserid=5010
mode="test"
#kafka_version=1.9.0-1.cflt~ubu20
kafka_version=2.4.0-1.cflt~ub20
kafka_version_jammy=1.8.0-1build1
VERSION="$(./version.sh)"
BUILDDATE=$(date +'%Y%m%d')
default_os="focal"
os=focal
registry="registry.met.no/met/obsklim/bakkeobservasjoner/data-og-kvalitet/kvalobs/kvbuild"
targets=
targets_in=
tag=latest
tag_and_latest="false"
avalable_targets="kvbuilddep kvbuild kvcpp kvdatainputd kvqabased kvmanagerd"
nocache=
build="true"
push="true"
KV_BUILD_DATE=${KV_BUILD_DATE:-}
if [ -n "${KV_BUILD_DATE}" ]; then
BUILDDATE=$KV_BUILD_DATE
fi
use() {
usage="\
Usage: $0 [--help] [--no-cache] [--os os] [--staging|--prod|--test] [--kvbuild] [--buildep] [--tag-version] [--tag tag] [--tag-and-latest tag] [--tag-with-build-date] [--list]
[--all-targets] [--push-only] [--build-only] targets
This script build kvalobs containers.
If --staging or --prod is given it is copied to the
container registry at $registry.
If --test, the default, is used it will not be copied
to the registry.
Options:
--help display this help and exit.
--list list targets.
--tag tagname tag the image with the name tagname, default latest.
--tag-and-latest tagname
tag the image with the name tagname and also create latest tag.
--tag-with-build-date
tag with version and build date on the form version-YYYYMMDD
and set latest. If the enviroment variable KV_BUILD_DATE is set use
this as the build date. Format KV_BUILD_DATE YYYYMMDD.
--tag-version Use version from configure.ac as tag. Also tag latest.
--staging build and push to staging.
--prod build and push to prod.
--test only build, default
--kvbuild Build kvalobs, ie the kvbuild container.
--kvcpp Build kvcpp and kvruntime. kvdev has all kvcpp development files
and kvruntime has all runtime files needed for kvcpp
--builddep Build the buildep container. The dafault is not to build
the container.
--os os The os to build for. There must exist deinition files in docker/kvalobs/os
Default: $os
--build-only Only build the container(s). No push. Default: $build
--push-only Push only the container(s). No build. Default: $push
The containers must prevosly been buildt whith the same flags.
--no-cache Do not use the docker build cache.
--all-targets Build all targets.
targets this is a list of targets to build. Available targets is:
'$avalable_targets'
"
echo -e "$usage\n\n"
}
validate_targets() {
local target_to_validate=$1
for target_in in $target_to_validate ; do
found=false
for target in $avalable_targets ; do
if [ "$target_in" = "$target" ]; then
found=true
break
fi
done
if [ "$found" == "false" ]; then
echo "Invalid target '$targets_in'"
exit 1
fi
done
}
while test $# -ne 0; do
case $1 in
--tag)
tag="$2"
shift
;;
--tag-and-latest)
tag="$2"
tag_and_latest=true
shift
;;
--tag-version)
tag="$VERSION"
tag_and_latest=true
;;
--tag-with-build-date)
tag="$VERSION-$BUILDDATE"
tag_and_latest=true
;;
--all-targets)
targets_in="$avalable_targets"
;;
--help)
use
exit 0;;
--os)
os="$2"
shift
;;
--staging) mode="staging";;
--prod) mode="prod";;
--test) mode="test";;
--kvbuild)
targets_in="$targets_in kvbuild";;
--builddep)
targets_in="$targets_in kvbuilddep";;
--kvcpp)
targets_in="$targets_in kvcpp";;
--list)
echo -e "\nTargets: all $avalable_targets\n\n"
exit 0
;;
--no-cache)
nocache="--no-cache";;
--build-only)
push="false";;
--push-only)
build="false";;
-*) use
echo "Invalid option $1"
exit 1
;;
*) targets_in="$targets_in $1";;
esac
shift
done
if [ "$os" = "jammy" ]; then
kafka_version="$kafka_version_jammy"
fi
validate_targets "$targets_in"
# Make the target build in correct sequence, ie kvbuilddep, kvbuild, kvcpp, etc ...
# The correct sequence is given by avalable_targets.
for target in $avalable_targets ; do
for target_in in $targets_in ; do
if [ "$target" = "$target_in" ]; then
if [ "$target" = "kvcpp" ]; then
targets="$targets kvcpp-dev kvcpp-runtime"
else
targets="$targets $target"
fi
break
fi
done
done
if [ $mode = test ]; then
registry="$os/"
kvuserid=$(id -u)
elif [ "$os" = "$default_os" ]; then
registry="$registry/$mode/"
else
registry="$registry/$mode/$os/"
fi
echo "tag: $tag"
echo "targets_in: $targets_in"
echo "Targets: $targets"
echo "mode: $mode"
echo "os: $os"
echo "registry: $registry"
echo "nocache: $nocache"
echo "kafka_version: ${kafka_version}"
echo "build: $build"
echo "push: $push"
echo "VERSION: $VERSION"
chmod +x gitref.sh
./gitref.sh
echo "Build targets: $targets"
if [ "$build" = "true" ]; then
for target in $targets; do
dockerfile="docker/kvalobs/${os}/${target}.dockerfile"
echo "Building dockerfile: $dockerfile"
docker build $nocache --build-arg "REGISTRY=${registry}" --build-arg "BASE_IMAGE_TAG=${tag}" \
--build-arg "kvuser=$kvuser" --build-arg "kvuserid=$kvuserid" \
--build-arg "kafka_VERSION=${kafka_version}" \
-f "$dockerfile" --tag "${registry}${target}:$tag" .
if [ "$tag_and_latest" = "true" ] && [ "$tag" != "latest" ]; then
docker tag "${registry}${target}:$tag" "${registry}${target}:latest"
fi
done
fi
if [ "$push" = "true" ] && [ "$mode" != "test" ]; then
for target in $targets; do
echo "Pushing: ${registry}${target}:$tag"
docker push "${registry}${target}:$tag"
if [ "$tag_and_latest" = "true" ] && [ "$tag" != "latest" ]; then
echo "Pushing: ${registry}${target}:latest"
docker push "${registry}${target}:latest"
fi
done
fi