build 8.9 KB

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