build 13 KB

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