build 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #!/usr/bin/env bash
  2. # This script helps to build release artifacts.
  3. # arg1: profile, e.g. emqx | emqx-edge | emqx-pkg | emqx-edge-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="$1"
  14. ARTIFACT="$2"
  15. # ensure dir
  16. cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")"
  17. PKG_VSN="${PKG_VSN:-$(./pkg-vsn.sh "$PROFILE")}"
  18. export PKG_VSN
  19. SYSTEM="$(./scripts/get-distro.sh)"
  20. ARCH="$(uname -m)"
  21. case "$ARCH" in
  22. x86_64)
  23. ARCH='amd64'
  24. ;;
  25. aarch64)
  26. ARCH='arm64'
  27. ;;
  28. arm*)
  29. ARCH='arm64'
  30. ;;
  31. esac
  32. export ARCH
  33. ##
  34. ## Support RPM and Debian based linux systems
  35. ##
  36. if [ "$(uname -s)" = 'Linux' ]; then
  37. case "${SYSTEM:-}" in
  38. ubuntu*|debian*|raspbian*)
  39. PKGERDIR='deb'
  40. ;;
  41. *)
  42. PKGERDIR='rpm'
  43. ;;
  44. esac
  45. fi
  46. if [ "${SYSTEM}" = 'windows' ]; then
  47. # windows does not like the find
  48. FIND="/usr/bin/find"
  49. else
  50. FIND='find'
  51. fi
  52. log() {
  53. local msg="$1"
  54. # rebar3 prints ===>, so we print ===<
  55. echo "===< $msg"
  56. }
  57. make_doc() {
  58. local libs_dir1 libs_dir2
  59. libs_dir1="$("$FIND" "_build/default/lib/" -maxdepth 2 -name ebin -type d)"
  60. libs_dir2="$("$FIND" "_build/$PROFILE/lib/" -maxdepth 2 -name ebin -type d)"
  61. case $PROFILE in
  62. emqx-enterprise)
  63. SCHEMA_MODULE='emqx_enterprise_conf_schema'
  64. ;;
  65. *)
  66. SCHEMA_MODULE='emqx_conf_schema'
  67. ;;
  68. esac
  69. # shellcheck disable=SC2086
  70. erl -noshell -pa $libs_dir1 $libs_dir2 -eval \
  71. "Dir = filename:join(['_build', '${PROFILE}', lib, emqx_dashboard, priv, www, static]), \
  72. ok = emqx_conf:dump_schema(Dir, $SCHEMA_MODULE), \
  73. halt(0)."
  74. }
  75. make_rel() {
  76. ./rebar3 as "$PROFILE" tar
  77. if [ "$("$FIND" "_build/$PROFILE/rel/emqx/lib/" -maxdepth 1 -name 'gpb-*' -type d)" != "" ]; then
  78. echo "gpb should not be included in the release"
  79. exit 1
  80. fi
  81. }
  82. make_elixir_rel() {
  83. export_release_vars "$PROFILE"
  84. mix release --overwrite
  85. }
  86. ## extract previous version .tar.gz files to _build/$PROFILE/rel/emqx before making relup
  87. make_relup() {
  88. local rel_dir="_build/$PROFILE/rel/emqx"
  89. mkdir -p "${rel_dir}/lib"
  90. mkdir -p "${rel_dir}/releases"
  91. local name_pattern
  92. name_pattern="${PROFILE}-$(./pkg-vsn.sh "$PROFILE" --vsn_matcher)"
  93. local releases=()
  94. while read -r tgzfile ; do
  95. local base_vsn
  96. base_vsn="$(echo "$tgzfile" | grep -oE "[0-9]+\.[0-9]+\.[0-9]+(-(alpha|beta)\.[0-9])?(-[0-9a-f]{8})?" | head -1)"
  97. tar -C "$rel_dir" -zxf ---keep-old-files "$tgzfile" emqx/releases emqx/lib
  98. releases+=( "$base_vsn" )
  99. done < <("$FIND" _upgrade_base -maxdepth 1 -name "${name_pattern}.tar.gz" -type f)
  100. if [ ${#releases[@]} -eq 0 ]; then
  101. log "No upgrade base found, relup ignored"
  102. return 0
  103. fi
  104. RELX_BASE_VERSIONS="$(IFS=, ; echo "${releases[*]}")"
  105. export RELX_BASE_VERSIONS
  106. ./rebar3 as "$PROFILE" relup --relname emqx --relvsn "${PKG_VSN}"
  107. }
  108. cp_dyn_libs() {
  109. local rel_dir="$1"
  110. local target_dir="${rel_dir}/dynlibs"
  111. if ! [ "$(uname -s)" = 'Linux' ]; then
  112. return 0;
  113. fi
  114. mkdir -p "$target_dir"
  115. while read -r so_file; do
  116. cp -L "$so_file" "$target_dir/"
  117. done < <("$FIND" "$rel_dir" -type f \( -name "*.so*" -o -name "beam.smp" \) -print0 \
  118. | xargs -0 ldd \
  119. | grep -E '(libcrypto)|(libtinfo)|(libatomic)' \
  120. | awk '{print $3}' \
  121. | sort -u)
  122. }
  123. ## Re-pack the relx assembled .tar.gz to EMQX's package naming scheme
  124. ## It assumes the .tar.gz has been built -- relies on Makefile dependency
  125. make_tgz() {
  126. local pkgpath="_packages/${PROFILE}"
  127. local src_tarball
  128. local target_name
  129. local target
  130. if [ "${IS_ELIXIR:-no}" = "yes" ]
  131. then
  132. # ensure src_tarball exists
  133. ELIXIR_MAKE_TAR=yes make_elixir_rel
  134. local relpath="_build/${PROFILE}"
  135. full_vsn="$(./pkg-vsn.sh "$PROFILE" --long --elixir)"
  136. else
  137. # build the src_tarball again to ensure relup is included
  138. # elixir does not have relup yet.
  139. make_rel
  140. local relpath="_build/${PROFILE}/rel/emqx"
  141. full_vsn="$(./pkg-vsn.sh "$PROFILE" --long)"
  142. fi
  143. target_name="${PROFILE}-${full_vsn}.tar.gz"
  144. target="${pkgpath}/${target_name}"
  145. src_tarball="${relpath}/emqx-${PKG_VSN}.tar.gz"
  146. tard="tmp/emqx_untar_${PKG_VSN}"
  147. rm -rf "${tard}"
  148. mkdir -p "${tard}/emqx"
  149. mkdir -p "${pkgpath}"
  150. if [ ! -f "$src_tarball" ]; then
  151. log "ERROR: $src_tarball is not found"
  152. fi
  153. tar zxf "${src_tarball}" -C "${tard}/emqx"
  154. ## try to be portable for tar.gz packages.
  155. ## for DEB and RPM packages the dependencies are resoved by yum and apt
  156. cp_dyn_libs "${tard}/emqx"
  157. ## create tar after change dir (for windows)
  158. pushd "${tard}" >/dev/null
  159. tar -czf "${target_name}" emqx
  160. popd >/dev/null
  161. mv "${tard}/${target_name}" "${target}"
  162. case "$SYSTEM" in
  163. macos*)
  164. # sha256sum may not be available on macos
  165. openssl dgst -sha256 "${target}" | cut -d ' ' -f 2 > "${target}.sha256"
  166. ;;
  167. *)
  168. sha256sum "${target}" | head -c 64 > "${target}.sha256"
  169. ;;
  170. esac
  171. log "Tarball successfully repacked: ${target}"
  172. log "Tarball sha256sum: $(cat "${target}.sha256")"
  173. }
  174. ## This function builds the default docker image based on alpine:3.14 (by default)
  175. make_docker() {
  176. EMQX_BUILDER="${EMQX_BUILDER:-${EMQX_DEFAULT_BUILDER}}"
  177. EMQX_RUNNER="${EMQX_RUNNER:-${EMQX_DEFAULT_RUNNER}}"
  178. if [[ "$PROFILE" = *-elixir ]]
  179. then
  180. PKG_VSN="$PKG_VSN-elixir"
  181. fi
  182. set -x
  183. docker build --no-cache --pull \
  184. --build-arg BUILD_FROM="${EMQX_BUILDER}" \
  185. --build-arg RUN_FROM="${EMQX_RUNNER}" \
  186. --build-arg EMQX_NAME="$PROFILE" \
  187. --tag "emqx/${PROFILE%%-elixir}:${PKG_VSN}" \
  188. -f "${DOCKERFILE}" .
  189. }
  190. function join {
  191. local IFS="$1"
  192. shift
  193. echo "$*"
  194. }
  195. # used to control the Elixir Mix Release output
  196. # see docstring in `mix.exs`
  197. export_release_vars() {
  198. local profile="$1"
  199. case "$profile" in
  200. emqx|emqx-edge|emqx-enterprise)
  201. export ELIXIR_MAKE_TAR=${ELIXIR_MAKE_TAR:-no}
  202. ;;
  203. emqx-pkg|emqx-edge-pkg|emqx-enterprise-pkg)
  204. export ELIXIR_MAKE_TAR=${ELIXIR_MAKE_TAR:-yes}
  205. ;;
  206. *)
  207. echo Invalid profile "$profile"
  208. exit 1
  209. esac
  210. export MIX_ENV="$profile"
  211. local erl_opts=()
  212. case "$profile" in
  213. *enterprise*)
  214. erl_opts+=( "{d, 'EMQX_RELEASE_EDITION', ee}" )
  215. ;;
  216. *edge*)
  217. erl_opts+=( "{d, 'EMQX_RELEASE_EDITION', edge}" )
  218. ;;
  219. *)
  220. erl_opts+=( "{d, 'EMQX_RELEASE_EDITION', ce}" )
  221. ;;
  222. esac
  223. # At this time, Mix provides no easy way to pass `erl_opts' to
  224. # dependencies. The workaround is to set this variable before
  225. # compiling the project, so that `emqx_release.erl' picks up
  226. # `emqx_vsn' as if it was compiled by rebar3.
  227. erl_opts+=( "{compile_info,[{emqx_vsn,\"${PKG_VSN}\"}]}" )
  228. ERL_COMPILER_OPTIONS="[$(join , "${erl_opts[@]}")]"
  229. export ERL_COMPILER_OPTIONS
  230. }
  231. log "building artifact=$ARTIFACT for profile=$PROFILE"
  232. case "$ARTIFACT" in
  233. doc)
  234. make_doc
  235. ;;
  236. rel)
  237. make_rel
  238. ;;
  239. relup)
  240. make_relup
  241. ;;
  242. tgz)
  243. make_tgz
  244. ;;
  245. pkg)
  246. if [ -z "${PKGERDIR:-}" ]; then
  247. log "Skipped making deb/rpm package for $SYSTEM"
  248. exit 0
  249. fi
  250. make -C "deploy/packages/${PKGERDIR}" clean
  251. env EMQX_REL="$(pwd)" \
  252. EMQX_BUILD="${PROFILE}" \
  253. SYSTEM="${SYSTEM}" \
  254. make -C "deploy/packages/${PKGERDIR}"
  255. ;;
  256. docker)
  257. make_docker
  258. ;;
  259. elixir)
  260. make_elixir_rel
  261. ;;
  262. *)
  263. log "Unknown artifact $ARTIFACT"
  264. exit 1
  265. ;;
  266. esac