build 9.7 KB

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