build 8.3 KB

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