build 12 KB

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