build 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. make_docs() {
  81. local libs_dir1 libs_dir2 libs_dir3
  82. libs_dir1="$("$FIND" "_build/$PROFILE/lib/" -maxdepth 2 -name ebin -type d)"
  83. if [ -d "_build/default/lib/" ]; then
  84. libs_dir2="$("$FIND" "_build/default/lib/" -maxdepth 2 -name ebin -type d)"
  85. else
  86. libs_dir2=''
  87. fi
  88. if [ -d "_build/$PROFILE/checkouts" ]; then
  89. libs_dir3="$("$FIND" "_build/$PROFILE/checkouts/" -maxdepth 2 -name ebin -type d 2>/dev/null || true)"
  90. else
  91. libs_dir3=''
  92. fi
  93. case "$(is_enterprise "$PROFILE")" in
  94. 'yes')
  95. SCHEMA_MODULE='emqx_ee_conf_schema'
  96. ;;
  97. 'no')
  98. SCHEMA_MODULE='emqx_conf_schema'
  99. ;;
  100. esac
  101. # shellcheck disable=SC2086
  102. erl -noshell -pa $libs_dir1 $libs_dir2 $libs_dir3 -eval \
  103. "Dir = filename:join([apps, emqx_dashboard, priv, www, static]), \
  104. I18nFile = filename:join([apps, emqx_dashboard, priv, 'i18n.conf']), \
  105. ok = emqx_conf:dump_schema(Dir, $SCHEMA_MODULE, I18nFile), \
  106. halt(0)."
  107. }
  108. assert_no_compile_time_only_deps() {
  109. if [ "$("$FIND" "_build/$PROFILE/rel/emqx/lib/" -maxdepth 1 -name 'gpb-*' -type d)" != "" ]; then
  110. echo "gpb should not be included in the release"
  111. exit 1
  112. fi
  113. }
  114. make_rel() {
  115. ./scripts/pre-compile.sh "$PROFILE"
  116. # make_elixir_rel always create rebar.lock
  117. # delete it to make git clone + checkout work because we use shallow close for rebar deps
  118. rm -f rebar.lock
  119. # compile all beams
  120. ./rebar3 as "$PROFILE" compile
  121. # generate docs (require beam compiled), generated to etc and priv dirs
  122. make_docs
  123. # now assemble the release tar
  124. ./rebar3 as "$PROFILE" tar
  125. assert_no_compile_time_only_deps
  126. }
  127. make_elixir_rel() {
  128. ./scripts/pre-compile.sh "$PROFILE"
  129. export_release_vars "$PROFILE"
  130. # for some reason, this has to be run outside "do"...
  131. mix local.rebar --if-missing --force
  132. # shellcheck disable=SC1010
  133. mix do local.hex --if-missing --force, \
  134. local.rebar rebar3 "${PWD}/rebar3" --if-missing --force, \
  135. deps.get
  136. mix release --overwrite
  137. assert_no_compile_time_only_deps
  138. }
  139. ## extract previous version .tar.gz files to _build/$PROFILE/rel/emqx before making relup
  140. make_relup() {
  141. local rel_dir="_build/$PROFILE/rel/emqx"
  142. local name_pattern
  143. name_pattern="${PROFILE}-$(./pkg-vsn.sh "$PROFILE" --vsn_matcher --long)"
  144. local releases=()
  145. mkdir -p _upgrade_base
  146. while read -r tgzfile ; do
  147. local base_vsn
  148. base_vsn="$(echo "$tgzfile" | grep -oE "[0-9]+\.[0-9]+\.[0-9]+(-(alpha|beta|rc)\.[0-9])?(-[0-9a-f]{8})?" | head -1)"
  149. ## we have to create tmp dir to untar old tgz, as `tar --skip-old-files` is not supported on all plantforms
  150. local tmp_dir
  151. tmp_dir="$(mktemp -d -t emqx.XXXXXXX)"
  152. $TAR -C "$tmp_dir" -zxf "$tgzfile"
  153. mkdir -p "${rel_dir}/releases/"
  154. cp -npr "$tmp_dir/releases"/* "${rel_dir}/releases/"
  155. ## There is for some reason a copy of the '$PROFILE.rel' file to releases dir,
  156. ## the content is duplicated to releases/5.0.0/$PROFILE.rel.
  157. ## This file seems to be useless, but yet confusing as it does not change after upgrade/downgrade
  158. ## Hence we force delete this file.
  159. rm -f "${rel_dir}/releases/${PROFILE}.rel"
  160. mkdir -p "${rel_dir}/lib/"
  161. cp -npr "$tmp_dir/lib"/* "${rel_dir}/lib/"
  162. rm -rf "$tmp_dir"
  163. releases+=( "$base_vsn" )
  164. done < <("$FIND" _upgrade_base -maxdepth 1 -name "${name_pattern}.tar.gz" -type f)
  165. if [ ${#releases[@]} -eq 0 ]; then
  166. log "No upgrade base found, relup ignored"
  167. return 0
  168. fi
  169. RELX_BASE_VERSIONS="$(IFS=, ; echo "${releases[*]}")"
  170. export RELX_BASE_VERSIONS
  171. ./rebar3 as "$PROFILE" relup --relname emqx --relvsn "${PKG_VSN}"
  172. }
  173. cp_dyn_libs() {
  174. local rel_dir="$1"
  175. local target_dir="${rel_dir}/dynlibs"
  176. if ! [ "$(uname -s)" = 'Linux' ]; then
  177. return 0;
  178. fi
  179. mkdir -p "$target_dir"
  180. while read -r so_file; do
  181. cp -L "$so_file" "$target_dir/"
  182. done < <("$FIND" "$rel_dir" -type f \( -name "*.so*" -o -name "beam.smp" \) -print0 \
  183. | xargs -0 ldd \
  184. | grep -E '(libcrypto)|(libtinfo)|(libatomic)' \
  185. | awk '{print $3}' \
  186. | sort -u)
  187. }
  188. ## Re-pack the relx assembled .tar.gz to EMQX's package naming scheme
  189. ## It assumes the .tar.gz has been built -- relies on Makefile dependency
  190. make_tgz() {
  191. local pkgpath="_packages/${PROFILE}"
  192. local src_tarball
  193. local target_name
  194. local target
  195. if [ "${IS_ELIXIR:-no}" = "yes" ]
  196. then
  197. # ensure src_tarball exists
  198. ELIXIR_MAKE_TAR=yes make_elixir_rel
  199. local relpath="_build/${PROFILE}"
  200. full_vsn="$(./pkg-vsn.sh "$PROFILE" --long --elixir)"
  201. else
  202. # build the src_tarball again to ensure relup is included
  203. # elixir does not have relup yet.
  204. make_rel
  205. local relpath="_build/${PROFILE}/rel/emqx"
  206. full_vsn="$(./pkg-vsn.sh "$PROFILE" --long)"
  207. fi
  208. case "$SYSTEM" in
  209. macos*)
  210. target_name="${PROFILE}-${full_vsn}.zip"
  211. ;;
  212. *)
  213. target_name="${PROFILE}-${full_vsn}.tar.gz"
  214. ;;
  215. esac
  216. target="${pkgpath}/${target_name}"
  217. src_tarball="${relpath}/emqx-${PKG_VSN}.tar.gz"
  218. tard="$(mktemp -d -t emqx.XXXXXXX)"
  219. mkdir -p "${tard}/emqx"
  220. mkdir -p "${pkgpath}"
  221. if [ ! -f "$src_tarball" ]; then
  222. log "ERROR: $src_tarball is not found"
  223. fi
  224. $TAR zxf "${src_tarball}" -C "${tard}/emqx"
  225. if [ -f "${tard}/emqx/releases/${PKG_VSN}/relup" ]; then
  226. ./scripts/relup-build/inject-relup.escript "${tard}/emqx/releases/${PKG_VSN}/relup"
  227. fi
  228. ## try to be portable for tar.gz packages.
  229. ## for DEB and RPM packages the dependencies are resoved by yum and apt
  230. cp_dyn_libs "${tard}/emqx"
  231. case "$SYSTEM" in
  232. macos*)
  233. # if the flag to sign macos binaries is set, but developer certificate
  234. # or certificate password is not configured, reset the flag
  235. # could happen, for example, when people submit PR from a fork, in this
  236. # case they cannot access secrets
  237. if [[ "${APPLE_SIGN_BINARIES:-0}" == 1 && \
  238. ( "${APPLE_DEVELOPER_ID_BUNDLE:-0}" == 0 || \
  239. "${APPLE_DEVELOPER_ID_BUNDLE_PASSWORD:-0}" == 0 ) ]]; then
  240. echo "Apple developer certificate is not configured, skip signing"
  241. APPLE_SIGN_BINARIES=0
  242. fi
  243. if [ "${APPLE_SIGN_BINARIES:-0}" = 1 ]; then
  244. ./scripts/macos-sign-binaries.sh "${tard}/emqx"
  245. fi
  246. ## create zip after change dir
  247. ## to avoid creating an extra level of 'emqx' dir in the .zip file
  248. pushd "${tard}/emqx" >/dev/null
  249. zip -r "../${target_name}" -- * >/dev/null
  250. popd >/dev/null
  251. mv "${tard}/${target_name}" "${target}"
  252. if [ "${APPLE_SIGN_BINARIES:-0}" = 1 ]; then
  253. # notarize the package
  254. # if fails, check what went wrong with this command:
  255. # xcrun notarytool log --apple-id <apple id> \
  256. # --apple-id <apple id> \
  257. # --password <apple id password>
  258. # --team-id <apple team id> <submission-id>
  259. echo 'Submitting the package for notarization to Apple (normally takes about a minute)'
  260. notarytool_output="$(xcrun notarytool submit \
  261. --apple-id "${APPLE_ID}" \
  262. --password "${APPLE_ID_PASSWORD}" \
  263. --team-id "${APPLE_TEAM_ID}" "${target}" \
  264. --no-progress \
  265. --wait)"
  266. echo "$notarytool_output"
  267. echo "$notarytool_output" | grep -q 'status: Accepted' || {
  268. echo 'Notarization failed';
  269. exit 1;
  270. }
  271. fi
  272. # sha256sum may not be available on macos
  273. openssl dgst -sha256 "${target}" | cut -d ' ' -f 2 > "${target}.sha256"
  274. ;;
  275. *)
  276. ## create tar after change dir
  277. ## to avoid creating an extra level of 'emqx' dir in the .tar.gz file
  278. pushd "${tard}/emqx" >/dev/null
  279. $TAR -zcf "../${target_name}" -- *
  280. popd >/dev/null
  281. mv "${tard}/${target_name}" "${target}"
  282. sha256sum "${target}" | head -c 64 > "${target}.sha256"
  283. ;;
  284. esac
  285. log "Archive successfully repacked: ${target}"
  286. log "Archive sha256sum: $(cat "${target}.sha256")"
  287. }
  288. trap docker_cleanup EXIT
  289. docker_cleanup() {
  290. rm -f ./.dockerignore >/dev/null
  291. }
  292. ## This function builds the default docker image based on debian 11
  293. make_docker() {
  294. EMQX_BUILDER="${EMQX_BUILDER:-${EMQX_DEFAULT_BUILDER}}"
  295. EMQX_RUNNER="${EMQX_RUNNER:-${EMQX_DEFAULT_RUNNER}}"
  296. EMQX_DOCKERFILE="${EMQX_DOCKERFILE:-deploy/docker/Dockerfile}"
  297. if [[ "$PROFILE" = *-elixir ]]; then
  298. PKG_VSN="$PKG_VSN-elixir"
  299. fi
  300. local default_tag="emqx/${PROFILE%%-elixir}:${PKG_VSN}"
  301. EMQX_IMAGE_TAG="${EMQX_IMAGE_TAG:-$default_tag}"
  302. echo '_build' >> ./.dockerignore
  303. set -x
  304. docker build --no-cache --pull \
  305. --build-arg BUILD_FROM="${EMQX_BUILDER}" \
  306. --build-arg RUN_FROM="${EMQX_RUNNER}" \
  307. --build-arg EMQX_NAME="$PROFILE" \
  308. --tag "${EMQX_IMAGE_TAG}" \
  309. -f "${EMQX_DOCKERFILE}" .
  310. [[ "${DEBUG:-}" -eq 1 ]] || set +x
  311. }
  312. function join {
  313. local IFS="$1"
  314. shift
  315. echo "$*"
  316. }
  317. # used to control the Elixir Mix Release output
  318. # see docstring in `mix.exs`
  319. export_release_vars() {
  320. local profile="$1"
  321. case "$profile" in
  322. emqx|emqx-enterprise)
  323. export ELIXIR_MAKE_TAR=${ELIXIR_MAKE_TAR:-no}
  324. ;;
  325. emqx-pkg|emqx-enterprise-pkg)
  326. export ELIXIR_MAKE_TAR=${ELIXIR_MAKE_TAR:-yes}
  327. ;;
  328. *)
  329. echo Invalid profile "$profile"
  330. exit 1
  331. esac
  332. export MIX_ENV="$profile"
  333. local erl_opts=()
  334. case "$(is_enterprise "$profile")" in
  335. 'yes')
  336. erl_opts+=( "{d, 'EMQX_RELEASE_EDITION', ee}" )
  337. ;;
  338. 'no')
  339. erl_opts+=( "{d, 'EMQX_RELEASE_EDITION', ce}" )
  340. ;;
  341. esac
  342. # At this time, Mix provides no easy way to pass `erl_opts' to
  343. # dependencies. The workaround is to set this variable before
  344. # compiling the project, so that `emqx_release.erl' picks up
  345. # `emqx_vsn' as if it was compiled by rebar3.
  346. erl_opts+=( "{compile_info,[{emqx_vsn,\"${PKG_VSN}\"}]}" )
  347. erl_opts+=( "{d,snk_kind,msg}" )
  348. ERL_COMPILER_OPTIONS="[$(join , "${erl_opts[@]}")]"
  349. export ERL_COMPILER_OPTIONS
  350. }
  351. log "building artifact=$ARTIFACT for profile=$PROFILE"
  352. case "$ARTIFACT" in
  353. doc|docs)
  354. make_docs
  355. ;;
  356. rel)
  357. make_rel
  358. ;;
  359. relup)
  360. make_relup
  361. ;;
  362. tgz)
  363. make_tgz
  364. ;;
  365. pkg)
  366. # this only affect build artifacts, such as schema doc
  367. export EMQX_ETC_DIR='/etc/emqx/'
  368. if [ -z "${PKGERDIR:-}" ]; then
  369. log "Skipped making deb/rpm package for $SYSTEM"
  370. exit 0
  371. fi
  372. export EMQX_REL_FORM="$PKGERDIR"
  373. if [ "${IS_ELIXIR:-}" = 'yes' ]; then
  374. make_elixir_rel
  375. else
  376. make_rel
  377. fi
  378. env EMQX_REL="$(pwd)" \
  379. EMQX_BUILD="${PROFILE}" \
  380. make -C "deploy/packages/${PKGERDIR}" clean
  381. env EMQX_REL="$(pwd)" \
  382. EMQX_BUILD="${PROFILE}" \
  383. make -C "deploy/packages/${PKGERDIR}"
  384. ;;
  385. docker)
  386. make_docker
  387. ;;
  388. elixir)
  389. make_elixir_rel
  390. ;;
  391. *)
  392. log "Unknown artifact $ARTIFACT"
  393. exit 1
  394. ;;
  395. esac