build 8.7 KB

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