build 13 KB

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