forked from emqx/emqx
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build
executable file
·417 lines (381 loc) · 12.3 KB
/
build
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
#!/usr/bin/env bash
# This script helps to build release artifacts.
# arg1: profile, e.g. emqx | emqx-pkg
# arg2: artifact, e.g. rel | relup | tgz | pkg
if [[ -n "$DEBUG" ]]; then
set -x
fi
set -euo pipefail
DEBUG="${DEBUG:-0}"
if [ "$DEBUG" -eq 1 ]; then
set -x
fi
PROFILE_ARG="$1"
ARTIFACT="$2"
is_enterprise() {
case "$1" in
*enterprise*)
echo 'yes'
;;
*)
echo 'no'
;;
esac
}
PROFILE_ENV="${PROFILE:-${PROFILE_ARG}}"
case "$(is_enterprise "$PROFILE_ARG"),$(is_enterprise "$PROFILE_ENV")" in
'yes,yes')
true
;;
'no,no')
true
;;
*)
echo "PROFILE env var is set to '$PROFILE_ENV', but '$0' arg1 is '$PROFILE_ARG'"
exit 1
;;
esac
# make sure PROFILE is exported, it is needed by rebar.config.erl
PROFILE=$PROFILE_ARG
export PROFILE
# ensure dir
cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")"
PKG_VSN="${PKG_VSN:-$(./pkg-vsn.sh "$PROFILE")}"
export PKG_VSN
SYSTEM="$(./scripts/get-distro.sh)"
ARCH="$(uname -m)"
case "$ARCH" in
x86_64)
ARCH='amd64'
;;
aarch64)
ARCH='arm64'
;;
arm*)
ARCH='arm64'
;;
esac
export ARCH
##
## Support RPM and Debian based linux systems
##
if [ "$(uname -s)" = 'Linux' ]; then
case "${SYSTEM:-}" in
ubuntu*|debian*|raspbian*)
PKGERDIR='deb'
;;
*)
PKGERDIR='rpm'
;;
esac
fi
if [ "${SYSTEM}" = 'windows' ]; then
# windows does not like the find
FIND="/usr/bin/find"
TAR="/usr/bin/tar"
export BUILD_WITHOUT_ROCKSDB="on"
else
FIND='find'
TAR='tar'
fi
log() {
local msg="$1"
# rebar3 prints ===>, so we print ===<
echo "===< $msg"
}
make_docs() {
local libs_dir1 libs_dir2 libs_dir3
libs_dir1="$("$FIND" "_build/$PROFILE/lib/" -maxdepth 2 -name ebin -type d)"
if [ -d "_build/default/lib/" ]; then
libs_dir2="$("$FIND" "_build/default/lib/" -maxdepth 2 -name ebin -type d)"
else
libs_dir2=''
fi
if [ -d "_build/$PROFILE/checkouts" ]; then
libs_dir3="$("$FIND" "_build/$PROFILE/checkouts/" -maxdepth 2 -name ebin -type d 2>/dev/null || true)"
else
libs_dir3=''
fi
case "$(is_enterprise "$PROFILE")" in
'yes')
SCHEMA_MODULE='emqx_enterprise_conf_schema'
;;
'no')
SCHEMA_MODULE='emqx_conf_schema'
;;
esac
# shellcheck disable=SC2086
erl -noshell -pa $libs_dir1 $libs_dir2 $libs_dir3 -eval \
"Dir = filename:join([apps, emqx_dashboard, priv, www, static]), \
I18nFile = filename:join([apps, emqx_dashboard, priv, 'i18n.conf']), \
ok = emqx_conf:dump_schema(Dir, $SCHEMA_MODULE, I18nFile), \
halt(0)."
}
assert_no_compile_time_only_deps() {
if [ "$("$FIND" "_build/$PROFILE/rel/emqx/lib/" -maxdepth 1 -name 'gpb-*' -type d)" != "" ]; then
echo "gpb should not be included in the release"
exit 1
fi
}
make_rel() {
./scripts/pre-compile.sh "$PROFILE"
# compile all beams
./rebar3 as "$PROFILE" compile
# generate docs (require beam compiled), generated to etc and priv dirs
make_docs
# now assemble the release tar
./rebar3 as "$PROFILE" tar
assert_no_compile_time_only_deps
}
make_elixir_rel() {
./scripts/pre-compile.sh "$PROFILE"
export_release_vars "$PROFILE"
mix release --overwrite
assert_no_compile_time_only_deps
}
## extract previous version .tar.gz files to _build/$PROFILE/rel/emqx before making relup
make_relup() {
local rel_dir="_build/$PROFILE/rel/emqx"
local name_pattern
name_pattern="${PROFILE}-$(./pkg-vsn.sh "$PROFILE" --vsn_matcher --long)"
local releases=()
mkdir -p _upgrade_base
while read -r tgzfile ; do
local base_vsn
base_vsn="$(echo "$tgzfile" | grep -oE "[0-9]+\.[0-9]+\.[0-9]+(-(alpha|beta|rc)\.[0-9])?(-[0-9a-f]{8})?" | head -1)"
## we have to create tmp dir to untar old tgz, as `tar --skip-old-files` is not supported on all plantforms
local tmp_dir
tmp_dir="$(mktemp -d -t emqx.XXXXXXX)"
$TAR -C "$tmp_dir" -zxf "$tgzfile"
mkdir -p "${rel_dir}/releases/"
cp -npr "$tmp_dir/releases"/* "${rel_dir}/releases/"
## There is for some reason a copy of the '$PROFILE.rel' file to releases dir,
## the content is duplicated to releases/5.0.0/$PROFILE.rel.
## This file seems to be useless, but yet confusing as it does not change after upgrade/downgrade
## Hence we force delete this file.
rm -f "${rel_dir}/releases/${PROFILE}.rel"
mkdir -p "${rel_dir}/lib/"
cp -npr "$tmp_dir/lib"/* "${rel_dir}/lib/"
rm -rf "$tmp_dir"
releases+=( "$base_vsn" )
done < <("$FIND" _upgrade_base -maxdepth 1 -name "${name_pattern}.tar.gz" -type f)
if [ ${#releases[@]} -eq 0 ]; then
log "No upgrade base found, relup ignored"
return 0
fi
RELX_BASE_VERSIONS="$(IFS=, ; echo "${releases[*]}")"
export RELX_BASE_VERSIONS
./rebar3 as "$PROFILE" relup --relname emqx --relvsn "${PKG_VSN}"
}
cp_dyn_libs() {
local rel_dir="$1"
local target_dir="${rel_dir}/dynlibs"
if ! [ "$(uname -s)" = 'Linux' ]; then
return 0;
fi
mkdir -p "$target_dir"
while read -r so_file; do
cp -L "$so_file" "$target_dir/"
done < <("$FIND" "$rel_dir" -type f \( -name "*.so*" -o -name "beam.smp" \) -print0 \
| xargs -0 ldd \
| grep -E '(libcrypto)|(libtinfo)|(libatomic)' \
| awk '{print $3}' \
| sort -u)
}
## Re-pack the relx assembled .tar.gz to EMQX's package naming scheme
## It assumes the .tar.gz has been built -- relies on Makefile dependency
make_tgz() {
local pkgpath="_packages/${PROFILE}"
local src_tarball
local target_name
local target
if [ "${IS_ELIXIR:-no}" = "yes" ]
then
# ensure src_tarball exists
ELIXIR_MAKE_TAR=yes make_elixir_rel
local relpath="_build/${PROFILE}"
full_vsn="$(./pkg-vsn.sh "$PROFILE" --long --elixir)"
else
# build the src_tarball again to ensure relup is included
# elixir does not have relup yet.
make_rel
local relpath="_build/${PROFILE}/rel/emqx"
full_vsn="$(./pkg-vsn.sh "$PROFILE" --long)"
fi
case "$SYSTEM" in
macos*)
target_name="${PROFILE}-${full_vsn}.zip"
;;
*)
target_name="${PROFILE}-${full_vsn}.tar.gz"
;;
esac
target="${pkgpath}/${target_name}"
src_tarball="${relpath}/emqx-${PKG_VSN}.tar.gz"
tard="$(mktemp -d -t emqx.XXXXXXX)"
mkdir -p "${tard}/emqx"
mkdir -p "${pkgpath}"
if [ ! -f "$src_tarball" ]; then
log "ERROR: $src_tarball is not found"
fi
$TAR zxf "${src_tarball}" -C "${tard}/emqx"
if [ -f "${tard}/emqx/releases/${PKG_VSN}/relup" ]; then
./scripts/relup-build/inject-relup.escript "${tard}/emqx/releases/${PKG_VSN}/relup"
fi
## try to be portable for tar.gz packages.
## for DEB and RPM packages the dependencies are resoved by yum and apt
cp_dyn_libs "${tard}/emqx"
case "$SYSTEM" in
macos*)
# if the flag to sign macos binaries is set, but developer certificate
# or certificate password is not configured, reset the flag
# could happen, for example, when people submit PR from a fork, in this
# case they cannot access secrets
if [[ "${APPLE_SIGN_BINARIES:-0}" == 1 && \
( "${APPLE_DEVELOPER_ID_BUNDLE:-0}" == 0 || \
"${APPLE_DEVELOPER_ID_BUNDLE_PASSWORD:-0}" == 0 ) ]]; then
echo "Apple developer certificate is not configured, skip signing"
APPLE_SIGN_BINARIES=0
fi
if [ "${APPLE_SIGN_BINARIES:-0}" = 1 ]; then
./scripts/macos-sign-binaries.sh "${tard}/emqx"
fi
## create zip after change dir
## to avoid creating an extra level of 'emqx' dir in the .zip file
pushd "${tard}/emqx" >/dev/null
zip -r "../${target_name}" -- * >/dev/null
popd >/dev/null
mv "${tard}/${target_name}" "${target}"
if [ "${APPLE_SIGN_BINARIES:-0}" = 1 ]; then
# notarize the package
# if fails, check what went wrong with this command:
# xcrun notarytool log --apple-id <apple id> \
# --apple-id <apple id> \
# --password <apple id password>
# --team-id <apple team id> <submission-id>
xcrun notarytool submit \
--apple-id "${APPLE_ID}" \
--password "${APPLE_ID_PASSWORD}" \
--team-id "${APPLE_TEAM_ID}" "${target}" --wait
fi
# sha256sum may not be available on macos
openssl dgst -sha256 "${target}" | cut -d ' ' -f 2 > "${target}.sha256"
;;
*)
## create tar after change dir
## to avoid creating an extra level of 'emqx' dir in the .tar.gz file
pushd "${tard}/emqx" >/dev/null
$TAR -zcf "../${target_name}" -- *
popd >/dev/null
mv "${tard}/${target_name}" "${target}"
sha256sum "${target}" | head -c 64 > "${target}.sha256"
;;
esac
log "Archive successfully repacked: ${target}"
log "Archive sha256sum: $(cat "${target}.sha256")"
}
## This function builds the default docker image based on debian 11
make_docker() {
EMQX_BUILDER="${EMQX_BUILDER:-${EMQX_DEFAULT_BUILDER}}"
EMQX_RUNNER="${EMQX_RUNNER:-${EMQX_DEFAULT_RUNNER}}"
if [ -z "${EMQX_DOCKERFILE:-}" ]; then
if [[ "$EMQX_BUILDER" =~ "alpine" ]]; then
EMQX_DOCKERFILE='deploy/docker/Dockerfile.alpine'
else
EMQX_DOCKERFILE='deploy/docker/Dockerfile'
fi
fi
if [[ "$PROFILE" = *-elixir ]]; then
PKG_VSN="$PKG_VSN-elixir"
fi
set -x
docker build --no-cache --pull \
--build-arg BUILD_FROM="${EMQX_BUILDER}" \
--build-arg RUN_FROM="${EMQX_RUNNER}" \
--build-arg EMQX_NAME="$PROFILE" \
--tag "emqx/${PROFILE%%-elixir}:${PKG_VSN}" \
-f "${EMQX_DOCKERFILE}" .
}
function join {
local IFS="$1"
shift
echo "$*"
}
# used to control the Elixir Mix Release output
# see docstring in `mix.exs`
export_release_vars() {
local profile="$1"
case "$profile" in
emqx|emqx-enterprise)
export ELIXIR_MAKE_TAR=${ELIXIR_MAKE_TAR:-no}
;;
emqx-pkg|emqx-enterprise-pkg)
export ELIXIR_MAKE_TAR=${ELIXIR_MAKE_TAR:-yes}
;;
*)
echo Invalid profile "$profile"
exit 1
esac
export MIX_ENV="$profile"
local erl_opts=()
case "$(is_enterprise "$profile")" in
'yes')
erl_opts+=( "{d, 'EMQX_RELEASE_EDITION', ee}" )
;;
'no')
erl_opts+=( "{d, 'EMQX_RELEASE_EDITION', ce}" )
;;
esac
# At this time, Mix provides no easy way to pass `erl_opts' to
# dependencies. The workaround is to set this variable before
# compiling the project, so that `emqx_release.erl' picks up
# `emqx_vsn' as if it was compiled by rebar3.
erl_opts+=( "{compile_info,[{emqx_vsn,\"${PKG_VSN}\"}]}" )
ERL_COMPILER_OPTIONS="[$(join , "${erl_opts[@]}")]"
export ERL_COMPILER_OPTIONS
}
log "building artifact=$ARTIFACT for profile=$PROFILE"
case "$ARTIFACT" in
doc|docs)
make_docs
;;
rel)
make_rel
;;
relup)
make_relup
;;
tgz)
make_tgz
;;
pkg)
# this only affect build artifacts, such as schema doc
export EMQX_ETC_DIR='/etc/emqx/'
if [ -z "${PKGERDIR:-}" ]; then
log "Skipped making deb/rpm package for $SYSTEM"
exit 0
fi
export EMQX_REL_FORM="$PKGERDIR"
if [ "${IS_ELIXIR:-}" = 'yes' ]; then
make_elixir_rel
else
make_rel
fi
env EMQX_REL="$(pwd)" \
EMQX_BUILD="${PROFILE}" \
make -C "deploy/packages/${PKGERDIR}" clean
env EMQX_REL="$(pwd)" \
EMQX_BUILD="${PROFILE}" \
make -C "deploy/packages/${PKGERDIR}"
;;
docker)
make_docker
;;
elixir)
make_elixir_rel
;;
*)
log "Unknown artifact $ARTIFACT"
exit 1
;;
esac