build 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. #!/usr/bin/env bash
  2. # This script helps to build release artifacts.
  3. # arg1: profile, e.g. emqx | emqx-enterprise
  4. # arg2: artifact, e.g. rel | relup | tgz | pkg
  5. set -euo pipefail
  6. [ "${DEBUG:-0}" -eq 1 ] && set -x
  7. PROFILE_ARG="$1"
  8. ARTIFACT="$2"
  9. is_enterprise() {
  10. case "$1" in
  11. *enterprise*)
  12. echo 'yes'
  13. ;;
  14. *)
  15. echo 'no'
  16. ;;
  17. esac
  18. }
  19. PROFILE_ENV="${PROFILE:-${PROFILE_ARG}}"
  20. case "$(is_enterprise "$PROFILE_ARG"),$(is_enterprise "$PROFILE_ENV")" in
  21. 'yes,yes')
  22. true
  23. ;;
  24. 'no,no')
  25. true
  26. ;;
  27. *)
  28. echo "PROFILE env var is set to '$PROFILE_ENV', but '$0' arg1 is '$PROFILE_ARG'"
  29. exit 1
  30. ;;
  31. esac
  32. # make sure PROFILE is exported, it is needed by rebar.config.erl
  33. PROFILE=$PROFILE_ARG
  34. export PROFILE
  35. # ensure dir
  36. cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")"
  37. PKG_VSN="${PKG_VSN:-$(./pkg-vsn.sh "$PROFILE")}"
  38. export PKG_VSN
  39. SYSTEM="$(./scripts/get-distro.sh)"
  40. ARCH="$(uname -m)"
  41. case "$ARCH" in
  42. x86_64)
  43. ARCH='amd64'
  44. ;;
  45. aarch64)
  46. ARCH='arm64'
  47. ;;
  48. arm*)
  49. ARCH='arm64'
  50. ;;
  51. esac
  52. export ARCH
  53. ##
  54. ## Support RPM and Debian based linux systems
  55. ##
  56. if [ "$(uname -s)" = 'Linux' ]; then
  57. case "${SYSTEM:-}" in
  58. ubuntu*|debian*|raspbian*)
  59. PKGERDIR='deb'
  60. ;;
  61. *)
  62. PKGERDIR='rpm'
  63. ;;
  64. esac
  65. fi
  66. if [ "${SYSTEM}" = 'windows' ]; then
  67. # windows does not like the find
  68. FIND="/usr/bin/find"
  69. TAR="/usr/bin/tar"
  70. export BUILD_WITHOUT_ROCKSDB="on"
  71. else
  72. FIND='find'
  73. TAR='tar'
  74. fi
  75. log() {
  76. local msg="$1"
  77. # rebar3 prints ===>, so we print ===<
  78. echo "===< $msg"
  79. }
  80. prepare_erl_libs() {
  81. local libs_dir="$1"
  82. local erl_libs="${ERL_LIBS:-}"
  83. local sep
  84. if [ "${SYSTEM}" = 'windows' ]; then
  85. sep=';'
  86. else
  87. sep=':'
  88. fi
  89. for app in "${libs_dir}"/*; do
  90. if [ -d "${app}/ebin" ]; then
  91. if [ -n "$erl_libs" ]; then
  92. erl_libs="${erl_libs}${sep}${app}"
  93. else
  94. erl_libs="${app}"
  95. fi
  96. fi
  97. done
  98. export ERL_LIBS="$erl_libs"
  99. }
  100. make_docs() {
  101. case "$(is_enterprise "$PROFILE")" in
  102. 'yes')
  103. SCHEMA_MODULE='emqx_enterprise_schema'
  104. ;;
  105. 'no')
  106. SCHEMA_MODULE='emqx_conf_schema'
  107. ;;
  108. esac
  109. prepare_erl_libs "_build/$PROFILE/checkouts"
  110. prepare_erl_libs "_build/$PROFILE/lib"
  111. local docdir="_build/docgen/$PROFILE"
  112. mkdir -p "$docdir"
  113. # shellcheck disable=SC2086
  114. erl -noshell -eval \
  115. "ok = emqx_conf:dump_schema('$docdir', $SCHEMA_MODULE), \
  116. halt(0)."
  117. }
  118. ## arg1 is the profile for which the following args (as app names) should be excluded
  119. assert_no_excluded_deps() {
  120. local profile="$1"
  121. shift 1
  122. if [ "$PROFILE" != "$profile" ]; then
  123. # not currently building the profile which has apps to be excluded
  124. return 0
  125. fi
  126. local rel_dir="_build/$PROFILE/rel/emqx/lib"
  127. local excluded_apps=( "$@" )
  128. local found
  129. for app in "${excluded_apps[@]}"; do
  130. found="$($FIND "$rel_dir" -maxdepth 1 -type d -name "$app-*")"
  131. if [ -n "${found}" ]; then
  132. echo "ERROR: ${app} should not be included in ${PROFILE}"
  133. echo "ERROR: found ${app} in ${rel_dir}"
  134. exit 1
  135. fi
  136. done
  137. }
  138. just_compile() {
  139. ./scripts/pre-compile.sh "$PROFILE"
  140. # make_elixir_rel always create rebar.lock
  141. # delete it to make git clone + checkout work because we use shallow close for rebar deps
  142. rm -f rebar.lock
  143. # compile all beams
  144. ./rebar3 as "$PROFILE" compile
  145. make_docs
  146. }
  147. make_rel() {
  148. local release_or_tar="${1}"
  149. just_compile
  150. # now assemble the release tar
  151. ./rebar3 as "$PROFILE" "$release_or_tar"
  152. assert_no_excluded_deps emqx-enterprise emqx_telemetry
  153. }
  154. make_elixir_rel() {
  155. ./scripts/pre-compile.sh "$PROFILE"
  156. export_elixir_release_vars "$PROFILE"
  157. # for some reason, this has to be run outside "do"...
  158. mix local.rebar --if-missing --force
  159. # shellcheck disable=SC1010
  160. mix do local.hex --if-missing --force, \
  161. local.rebar rebar3 "${PWD}/rebar3" --if-missing --force, \
  162. deps.get
  163. mix release --overwrite
  164. assert_no_excluded_deps emqx-enterprise emqx_telemetry
  165. }
  166. ## extract previous version .tar.gz files to _build/$PROFILE/rel/emqx before making relup
  167. make_relup() {
  168. local rel_dir="_build/$PROFILE/rel/emqx"
  169. local name_pattern
  170. name_pattern="${PROFILE}-$(./pkg-vsn.sh "$PROFILE" --vsn_matcher --long)"
  171. local releases=()
  172. mkdir -p _upgrade_base
  173. while read -r tgzfile ; do
  174. local base_vsn
  175. base_vsn="$(echo "$tgzfile" | grep -oE "[0-9]+\.[0-9]+\.[0-9]+(-(alpha|beta|rc)\.[0-9])?(-[0-9a-f]{8})?" | head -1)"
  176. ## we have to create tmp dir to untar old tgz, as `tar --skip-old-files` is not supported on all plantforms
  177. local tmp_dir
  178. tmp_dir="$(mktemp -d -t emqx.XXXXXXX)"
  179. $TAR -C "$tmp_dir" -zxf "$tgzfile"
  180. mkdir -p "${rel_dir}/releases/"
  181. cp -npr "$tmp_dir/releases"/* "${rel_dir}/releases/"
  182. ## There is for some reason a copy of the '$PROFILE.rel' file to releases dir,
  183. ## the content is duplicated to releases/5.0.0/$PROFILE.rel.
  184. ## This file seems to be useless, but yet confusing as it does not change after upgrade/downgrade
  185. ## Hence we force delete this file.
  186. rm -f "${rel_dir}/releases/${PROFILE}.rel"
  187. mkdir -p "${rel_dir}/lib/"
  188. cp -npr "$tmp_dir/lib"/* "${rel_dir}/lib/"
  189. rm -rf "$tmp_dir"
  190. releases+=( "$base_vsn" )
  191. done < <("$FIND" _upgrade_base -maxdepth 1 -name "${name_pattern}.tar.gz" -type f)
  192. if [ ${#releases[@]} -eq 0 ]; then
  193. log "No upgrade base found, relup ignored"
  194. return 0
  195. fi
  196. RELX_BASE_VERSIONS="$(IFS=, ; echo "${releases[*]}")"
  197. export RELX_BASE_VERSIONS
  198. ./rebar3 as "$PROFILE" relup --relname emqx --relvsn "${PKG_VSN}"
  199. }
  200. cp_dyn_libs() {
  201. local rel_dir="$1"
  202. local target_dir="${rel_dir}/dynlibs"
  203. if ! [ "$(uname -s)" = 'Linux' ]; then
  204. return 0;
  205. fi
  206. mkdir -p "$target_dir"
  207. while read -r so_file; do
  208. cp -L "$so_file" "$target_dir/"
  209. done < <("$FIND" "$rel_dir" -type f \( -name "*.so*" -o -name "beam.smp" \) -print0 \
  210. | xargs -0 ldd \
  211. | grep -E '(libcrypto)|(libtinfo)|(libatomic)' \
  212. | awk '{print $3}' \
  213. | sort -u)
  214. }
  215. ## Re-pack the relx assembled .tar.gz to EMQX's package naming scheme
  216. ## It assumes the .tar.gz has been built -- relies on Makefile dependency
  217. make_tgz() {
  218. local pkgpath="_packages/${PROFILE}"
  219. local src_tarball
  220. local target_name
  221. local target
  222. if [ "${IS_ELIXIR:-no}" = "yes" ]
  223. then
  224. # ensure src_tarball exists
  225. ELIXIR_MAKE_TAR=yes make_elixir_rel
  226. local relpath="_build/${PROFILE}"
  227. full_vsn="$(./pkg-vsn.sh "$PROFILE" --long --elixir)"
  228. else
  229. # build the src_tarball again to ensure relup is included
  230. # elixir does not have relup yet.
  231. make_rel tar
  232. local relpath="_build/${PROFILE}/rel/emqx"
  233. full_vsn="$(./pkg-vsn.sh "$PROFILE" --long)"
  234. fi
  235. case "$SYSTEM" in
  236. macos*)
  237. target_name="${PROFILE}-${full_vsn}.zip"
  238. ;;
  239. windows*)
  240. target_name="${PROFILE}-${full_vsn}.zip"
  241. ;;
  242. *)
  243. target_name="${PROFILE}-${full_vsn}.tar.gz"
  244. ;;
  245. esac
  246. target="${pkgpath}/${target_name}"
  247. src_tarball="${relpath}/emqx-${PKG_VSN}.tar.gz"
  248. tard="$(mktemp -d -t emqx.XXXXXXX)"
  249. mkdir -p "${tard}/emqx"
  250. mkdir -p "${pkgpath}"
  251. if [ ! -f "$src_tarball" ]; then
  252. log "ERROR: $src_tarball is not found"
  253. fi
  254. $TAR zxf "${src_tarball}" -C "${tard}/emqx"
  255. if [ -f "${tard}/emqx/releases/${PKG_VSN}/relup" ]; then
  256. ./scripts/relup-build/inject-relup.escript "${tard}/emqx/releases/${PKG_VSN}/relup"
  257. fi
  258. ## try to be portable for tar.gz packages.
  259. ## for DEB and RPM packages the dependencies are resoved by yum and apt
  260. cp_dyn_libs "${tard}/emqx"
  261. case "$SYSTEM" in
  262. macos*)
  263. # if the flag to sign macos binaries is set, but developer certificate
  264. # or certificate password is not configured, reset the flag
  265. # could happen, for example, when people submit PR from a fork, in this
  266. # case they cannot access secrets
  267. if [[ "${APPLE_SIGN_BINARIES:-0}" == 1 && \
  268. ( "${APPLE_DEVELOPER_ID_BUNDLE:-0}" == 0 || \
  269. "${APPLE_DEVELOPER_ID_BUNDLE_PASSWORD:-0}" == 0 ) ]]; then
  270. echo "Apple developer certificate is not configured, skip signing"
  271. APPLE_SIGN_BINARIES=0
  272. fi
  273. if [ "${APPLE_SIGN_BINARIES:-0}" = 1 ]; then
  274. ./scripts/macos-sign-binaries.sh "${tard}/emqx"
  275. fi
  276. ## create zip after change dir
  277. ## to avoid creating an extra level of 'emqx' dir in the .zip file
  278. pushd "${tard}/emqx" >/dev/null
  279. zip -r "../${target_name}" -- * >/dev/null
  280. popd >/dev/null
  281. mv "${tard}/${target_name}" "${target}"
  282. if [ "${APPLE_SIGN_BINARIES:-0}" = 1 ]; then
  283. # notarize the package
  284. # if fails, check what went wrong with this command:
  285. # xcrun notarytool log --apple-id <apple id> \
  286. # --apple-id <apple id> \
  287. # --password <apple id password>
  288. # --team-id <apple team id> <submission-id>
  289. echo 'Submitting the package for notarization to Apple (normally takes about a minute)'
  290. notarytool_output="$(xcrun notarytool submit \
  291. --apple-id "${APPLE_ID}" \
  292. --password "${APPLE_ID_PASSWORD}" \
  293. --team-id "${APPLE_TEAM_ID}" "${target}" \
  294. --no-progress \
  295. --wait)"
  296. echo "$notarytool_output"
  297. echo "$notarytool_output" | grep -q 'status: Accepted' || {
  298. echo 'Notarization failed';
  299. exit 1;
  300. }
  301. fi
  302. # sha256sum may not be available on macos
  303. openssl dgst -sha256 "${target}" | cut -d ' ' -f 2 > "${target}.sha256"
  304. ;;
  305. windows*)
  306. pushd "${tard}" >/dev/null
  307. 7z a "${target_name}" ./emqx/* >/dev/null
  308. popd >/dev/null
  309. mv "${tard}/${target_name}" "${target}"
  310. sha256sum "${target}" | head -c 64 > "${target}.sha256"
  311. ;;
  312. *)
  313. ## create tar after change dir
  314. ## to avoid creating an extra level of 'emqx' dir in the .tar.gz file
  315. pushd "${tard}/emqx" >/dev/null
  316. $TAR -zcf "../${target_name}" -- *
  317. popd >/dev/null
  318. mv "${tard}/${target_name}" "${target}"
  319. sha256sum "${target}" | head -c 64 > "${target}.sha256"
  320. ;;
  321. esac
  322. log "Archive successfully repacked: ${target}"
  323. log "Archive sha256sum: $(cat "${target}.sha256")"
  324. }
  325. trap docker_cleanup EXIT
  326. docker_cleanup() {
  327. rm -f ./.dockerignore >/dev/null
  328. }
  329. ## This function builds the default docker image based on debian 11
  330. make_docker() {
  331. EMQX_BUILDER="${EMQX_BUILDER:-${EMQX_DEFAULT_BUILDER}}"
  332. EMQX_RUNNER="${EMQX_RUNNER:-${EMQX_DEFAULT_RUNNER}}"
  333. EMQX_DOCKERFILE="${EMQX_DOCKERFILE:-deploy/docker/Dockerfile}"
  334. if [[ "$PROFILE" = *-elixir ]]; then
  335. PKG_VSN="$PKG_VSN-elixir"
  336. fi
  337. local default_tag="emqx/${PROFILE%%-elixir}:${PKG_VSN}"
  338. EMQX_IMAGE_TAG="${EMQX_IMAGE_TAG:-$default_tag}"
  339. ## extra_deps is a comma separated list of debian 11 package names
  340. local extra_deps=''
  341. if [[ "$PROFILE" = *enterprise* ]]; then
  342. extra_deps='libsasl2-2'
  343. fi
  344. echo '_build' >> ./.dockerignore
  345. set -x
  346. docker build --no-cache --pull \
  347. --build-arg BUILD_FROM="${EMQX_BUILDER}" \
  348. --build-arg RUN_FROM="${EMQX_RUNNER}" \
  349. --build-arg EMQX_NAME="${PROFILE}" \
  350. --build-arg EXTRA_DEPS="${extra_deps}" \
  351. --tag "${EMQX_IMAGE_TAG}" \
  352. -f "${EMQX_DOCKERFILE}" .
  353. [[ "${DEBUG:-}" -eq 1 ]] || set +x
  354. }
  355. function join {
  356. local IFS="$1"
  357. shift
  358. echo "$*"
  359. }
  360. # used to control the Elixir Mix Release output
  361. # see docstring in `mix.exs`
  362. export_elixir_release_vars() {
  363. local profile="$1"
  364. case "$profile" in
  365. emqx|emqx-enterprise)
  366. export ELIXIR_MAKE_TAR=${ELIXIR_MAKE_TAR:-no}
  367. ;;
  368. emqx-pkg|emqx-enterprise-pkg)
  369. export ELIXIR_MAKE_TAR=${ELIXIR_MAKE_TAR:-yes}
  370. ;;
  371. *)
  372. echo Invalid profile "$profile"
  373. exit 1
  374. esac
  375. export MIX_ENV="$profile"
  376. }
  377. log "building artifact=$ARTIFACT for profile=$PROFILE"
  378. case "$ARTIFACT" in
  379. apps)
  380. just_compile
  381. ;;
  382. doc|docs)
  383. make_docs
  384. ;;
  385. rel)
  386. make_rel release
  387. ;;
  388. relup)
  389. make_relup
  390. ;;
  391. tgz)
  392. make_tgz
  393. ;;
  394. pkg)
  395. # this only affect build artifacts, such as schema doc
  396. export EMQX_ETC_DIR='/etc/emqx/'
  397. if [ -z "${PKGERDIR:-}" ]; then
  398. log "Skipped making deb/rpm package for $SYSTEM"
  399. exit 0
  400. fi
  401. export EMQX_REL_FORM="$PKGERDIR"
  402. if [ "${IS_ELIXIR:-}" = 'yes' ]; then
  403. make_elixir_rel
  404. else
  405. make_rel tar
  406. fi
  407. env EMQX_REL="$(pwd)" \
  408. EMQX_BUILD="${PROFILE}" \
  409. make -C "deploy/packages/${PKGERDIR}" clean
  410. env EMQX_REL="$(pwd)" \
  411. EMQX_BUILD="${PROFILE}" \
  412. make -C "deploy/packages/${PKGERDIR}"
  413. ;;
  414. docker)
  415. make_docker
  416. ;;
  417. elixir)
  418. make_elixir_rel
  419. ;;
  420. *)
  421. log "Unknown artifact $ARTIFACT"
  422. exit 1
  423. ;;
  424. esac