build 9.3 KB

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