build 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. set -euo pipefail
  6. PROFILE="$1"
  7. ARTIFACT="$2"
  8. # ensure dir
  9. cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")"
  10. PKG_VSN="${PKG_VSN:-$(./pkg-vsn.sh)}"
  11. export PKG_VSN
  12. SYSTEM="$(./scripts/get-distro.sh)"
  13. ARCH="$(uname -m)"
  14. case "$ARCH" in
  15. x86_64)
  16. ARCH='amd64'
  17. ;;
  18. aarch64)
  19. ARCH='arm64'
  20. ;;
  21. arm*)
  22. ARCH=arm
  23. ;;
  24. esac
  25. export ARCH
  26. ##
  27. ## Support RPM and Debian based linux systems
  28. ##
  29. if [ "$(uname -s)" = 'Linux' ]; then
  30. case "${SYSTEM:-}" in
  31. ubuntu*|debian*|raspbian*)
  32. PKGERDIR='deb'
  33. ;;
  34. *)
  35. PKGERDIR='rpm'
  36. ;;
  37. esac
  38. fi
  39. if [ "${SYSTEM}" = 'windows' ]; then
  40. # windows does not like the find
  41. FIND="/usr/bin/find"
  42. else
  43. FIND='find'
  44. fi
  45. log() {
  46. local msg="$1"
  47. # rebar3 prints ===>, so we print ===<
  48. echo "===< $msg"
  49. }
  50. make_doc() {
  51. local libs_dir1 libs_dir2
  52. libs_dir1="$("$FIND" "_build/default/lib/" -maxdepth 2 -name ebin -type d)"
  53. libs_dir2="$("$FIND" "_build/$PROFILE/lib/" -maxdepth 2 -name ebin -type d)"
  54. # shellcheck disable=SC2086
  55. erl -noshell -pa $libs_dir1 $libs_dir2 -eval \
  56. "F = filename:join(['_build', '${PROFILE}', lib, emqx_dashboard, priv, www, static, 'config.md']), \
  57. io:format(\"===< Generating: ~s~n\", [F]),
  58. ok = emqx_conf:gen_doc(F), \
  59. halt(0)."
  60. }
  61. make_rel() {
  62. # shellcheck disable=SC1010
  63. ./rebar3 as "$PROFILE" do tar
  64. if [ "$("$FIND" "_build/$PROFILE/rel/emqx/lib/" -maxdepth 1 -name 'gpb-*' -type d)" != "" ]; then
  65. echo "gpb should not be included in the release"
  66. exit 1
  67. fi
  68. }
  69. ## extract previous version .tar.gz files to _build/$PROFILE/rel/emqx before making relup
  70. make_relup() {
  71. local rel_dir="_build/$PROFILE/rel/emqx"
  72. mkdir -p "${rel_dir}/lib"
  73. mkdir -p "${rel_dir}/releases"
  74. local releases=()
  75. while read -r tgzfile ; do
  76. local base_vsn
  77. base_vsn="$(echo "$tgzfile" | grep -oE "[0-9]+\.[0-9]+\.[0-9]+(-(alpha|beta)\.[0-9])?(-[0-9a-f]{8})?" | head -1)"
  78. tar -C "$rel_dir" -zxf ---keep-old-files "$tgzfile" emqx/releases emqx/lib
  79. releases+=( "$base_vsn" )
  80. done < <("$FIND" _upgrade_base -maxdepth 1 -name "$PROFILE*${SYSTEM}-${ARCH}.tar.gz" -type f)
  81. if [ ${#releases[@]} -eq 0 ]; then
  82. log "No upgrade base found, relup ignored"
  83. return 0
  84. fi
  85. RELX_BASE_VERSIONS="$(IFS=, ; echo "${releases[*]}")"
  86. export RELX_BASE_VERSIONS
  87. ./rebar3 as "$PROFILE" relup --relname emqx --relvsn "${PKG_VSN}"
  88. }
  89. cp_dyn_libs() {
  90. local rel_dir="$1"
  91. local target_dir="${rel_dir}/dynlibs"
  92. if ! [ "$(uname -s)" = 'Linux' ]; then
  93. return 0;
  94. fi
  95. mkdir -p "$target_dir"
  96. while read -r so_file; do
  97. cp -L "$so_file" "$target_dir/"
  98. done < <("$FIND" "$rel_dir" -type f \( -name "*.so*" -o -name "beam.smp" \) -print0 \
  99. | xargs -0 ldd \
  100. | grep -E '(libcrypto)|(libtinfo)|(libatomic)' \
  101. | awk '{print $3}' \
  102. | sort -u)
  103. }
  104. ## Re-pack the relx assembled .tar.gz to EMQ X's package naming scheme
  105. ## It assumes the .tar.gz has been built -- relies on Makefile dependency
  106. make_tgz() {
  107. # build the tarball again to ensure relup is included
  108. make_rel
  109. tard="/tmp/emqx_untar_${PKG_VSN}"
  110. rm -rf "${tard}"
  111. mkdir -p "${tard}/emqx"
  112. local relpath="_build/${PROFILE}/rel/emqx"
  113. local pkgpath="_packages/${PROFILE}"
  114. mkdir -p "${pkgpath}"
  115. local tarball="${relpath}/emqx-${PKG_VSN}.tar.gz"
  116. if [ ! -f "$tarball" ]; then
  117. log "ERROR: $tarball is not found"
  118. fi
  119. local target
  120. target="${pkgpath}/${PROFILE}-${PKG_VSN}-otp${OTP_VSN}-${SYSTEM}-${ARCH}.tar.gz"
  121. tar zxf "${tarball}" -C "${tard}/emqx"
  122. ## try to be portable for tar.gz packages.
  123. ## for DEB and RPM packages the dependencies are resoved by yum and apt
  124. cp_dyn_libs "${tard}/emqx"
  125. (cd "${tard}" && tar -cz emqx) > "${target}"
  126. log "Tarball successfully repacked: ${target}"
  127. }
  128. ## This function builds the default docker image based on alpine:3.14 (by default)
  129. make_docker() {
  130. EMQX_BUILDER="${EMQX_BUILDER:-${EMQX_DEFAULT_BUILDER}}"
  131. EMQX_RUNNER="${EMQX_RUNNER:-${EMQX_DEFAULT_RUNNER}}"
  132. set -x
  133. docker build --no-cache --pull \
  134. --build-arg BUILD_FROM="${EMQX_BUILDER}" \
  135. --build-arg RUN_FROM="${EMQX_RUNNER}" \
  136. --build-arg EMQX_NAME="$PROFILE" \
  137. --tag "emqx/$PROFILE:${PKG_VSN}" \
  138. -f "${DOCKERFILE}" .
  139. }
  140. ## This function accepts any base docker image,
  141. ## a emqx tgz-image, and a image tag (for the image to be built),
  142. ## to build a docker image which runs EMQ X
  143. ##
  144. ## Export below variables to quickly build an image
  145. ##
  146. ## Name Default Example
  147. ## ---------------------------------------------------------------------
  148. ## EMQX_BASE_IMAGE current os centos:7
  149. ## EMQX_TGZ_packages/<current-tgz-target> /tmp/emqx-4.4.0-otp23.3.4.9-3-centos7-amd64.tar.gz
  150. ## EMQX_IMAGE_TAG emqx/emqx:<current-vns-rel> emqx/emqx:testing-tag
  151. ##
  152. make_docker_testing() {
  153. if [ -z "${EMQX_BASE_IMAGE:-}" ]; then
  154. case "$SYSTEM" in
  155. ubuntu20*)
  156. EMQX_BASE_IMAGE="ubuntu:20.04"
  157. ;;
  158. centos8)
  159. EMQX_BASE_IMAGE="centos:8"
  160. ;;
  161. *)
  162. echo "Unsupported testing base image for $SYSTEM"
  163. exit 1
  164. ;;
  165. esac
  166. fi
  167. EMQX_IMAGE_TAG="${EMQX_IMAGE_TAG:-emqx/$PROFILE:${PKG_VSN}-otp${OTP_VSN}-${SYSTEM}}"
  168. local default_tgz
  169. default_tgz="_packages/${PROFILE}/${PROFILE}-${PKG_VSN}-otp${OTP_VSN}-${SYSTEM}-${ARCH}.tar.gz"
  170. local tgz="${EMQX_TGZ_PACKAGE:-$default_tgz}"
  171. if [ ! -f "$tgz" ]; then
  172. log "ERROR: $tgz not built?"
  173. exit 1
  174. fi
  175. set -x
  176. docker build \
  177. --build-arg BUILD_FROM="${EMQX_BASE_IMAGE}" \
  178. --build-arg EMQX_TGZ_PACKAGE="${tgz}" \
  179. --tag "$EMQX_IMAGE_TAG" \
  180. -f "${DOCKERFILE_TESTING}" .
  181. }
  182. log "building artifact=$ARTIFACT for profile=$PROFILE"
  183. case "$ARTIFACT" in
  184. doc)
  185. make_doc
  186. ;;
  187. rel)
  188. make_rel
  189. ;;
  190. relup)
  191. make_relup
  192. ;;
  193. tgz)
  194. make_tgz
  195. ;;
  196. pkg)
  197. if [ -z "${PKGERDIR:-}" ]; then
  198. log "Skipped making deb/rpm package for $SYSTEM"
  199. exit 0
  200. fi
  201. make -C "deploy/packages/${PKGERDIR}" clean
  202. EMQX_REL="$(pwd)" EMQX_BUILD="${PROFILE}" SYSTEM="${SYSTEM}" make -C "deploy/packages/${PKGERDIR}"
  203. ;;
  204. docker)
  205. make_docker
  206. ;;
  207. docker-testing)
  208. make_docker_testing
  209. ;;
  210. *)
  211. log "Unknown artifact $ARTIFACT"
  212. exit 1
  213. ;;
  214. esac