build_deb.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #!/bin/bash
  2. set -e
  3. if [ -z "$ROOT_DIR" ]; then
  4. pushd $(dirname $(readlink -f "$BASH_SOURCE")) > /dev/null
  5. ROOT_DIR=$(cd .. && pwd)
  6. popd > /dev/null
  7. fi
  8. SRC_BIN=$ROOT_DIR/_output/bin
  9. SRC_BUILD=$ROOT_DIR/build
  10. OUTPUT_DIR=$ROOT_DIR/_output/debs
  11. PKG=$1
  12. BIN_PATH=${2:-/opt/yunion/bin}
  13. if [ -z "$PKG" ]; then
  14. echo "Usage: $0 <package>"
  15. exit 1
  16. fi
  17. BIN="$SRC_BIN/$PKG"
  18. ROOT="$SRC_BUILD/$PKG"
  19. if [ ! -x "$BIN" ]; then
  20. echo "$BIN not exists"
  21. exit 1
  22. fi
  23. if [ ! -x "$ROOT" ]; then
  24. echo "$ROOT not exists"
  25. exit 1
  26. fi
  27. . $ROOT/vars
  28. case $(uname -m) in
  29. x86_64)
  30. CURRENT_ARCH=amd64
  31. ;;
  32. aarch64)
  33. CURRENT_ARCH=arm64
  34. ;;
  35. riscv64)
  36. CURRENT_ARCH=riscv64
  37. ;;
  38. esac
  39. if [[ -n "$GOARCH" ]]; then
  40. case "$GOARCH" in
  41. "arm64" | "arm" | "aarch64")
  42. CURRENT_ARCH="arm64"
  43. ;;
  44. "x86" | "x86_64" | "i686" | "i386" | "amd64")
  45. CURRENT_ARCH="amd64"
  46. ;;
  47. "riscv64")
  48. CURRENT_ARCH="riscv64"
  49. ;;
  50. esac
  51. fi
  52. if [ -z "$VERSION" ]; then
  53. TAG=$(git describe --abbrev=0 --tags || echo 000000)
  54. VERSION=${TAG/\//-}
  55. VERSION=${VERSION/v/}
  56. VERSION=${VERSION/master-/}
  57. fi
  58. RELEASE=`date +"%y%m%d%H"`
  59. FULL_VERSION=$VERSION-$RELEASE
  60. BUILDROOT=$OUTPUT_DIR/yunion-${PKG}-${FULL_VERSION}_${CURRENT_ARCH}
  61. function cleanup {
  62. rm -rf "$BUILDROOT"
  63. echo "Deleted temp working directory $BUILDROOT"
  64. }
  65. # register the cleanup function to be called on the EXIT signal
  66. trap cleanup EXIT
  67. rm -rf $BUILDROOT
  68. mkdir -p $BUILDROOT/DEBIAN
  69. mkdir -p $BUILDROOT/$BIN_PATH
  70. cp -rf $BIN $BUILDROOT/$BIN_PATH
  71. if [ -d $ROOT/root ]; then
  72. cp -rf $ROOT/root/* $BUILDROOT/
  73. fi
  74. echo "Build root ${BUILDROOT}"
  75. echo "Package: yunion-$PKG
  76. Version: $FULL_VERSION
  77. Section: base
  78. Priority: optional
  79. Architecture: $CURRENT_ARCH
  80. Maintainer: wanyaoqi@yunionyun.com
  81. Description: $DESCRIPTION" > $BUILDROOT/DEBIAN/control
  82. if [ ${#REQUIRES[@]} -gt 0 ]; then
  83. DEPS=$(IFS=, ; echo "${REQUIRES[*]}")
  84. echo "Depends: $DEPS" >> $BUILDROOT/DEBIAN/control
  85. fi
  86. chmod 0755 $BUILDROOT/DEBIAN/control
  87. cat $BUILDROOT/DEBIAN/control
  88. echo "#!/bin/bash
  89. " > $BUILDROOT/DEBIAN/preinst
  90. if [ -f $ROOT/preinst ]; then
  91. cat $ROOT/preinst >> $BUILDROOT/DEBIAN/preinst
  92. else
  93. if [ "$SERVICE" == "yes" ] && [ -n "$OWNER" ]; then
  94. echo "
  95. getent group ${OWNER} >/dev/null || /usr/sbin/groupadd -r ${OWNER}
  96. getent passwd ${OWNER} >/dev/null || /usr/sbin/useradd -r -s /sbin/nologin -d /home/${OWNER} -M -g ${OWNER} ${OWNER}
  97. " >> $BUILDROOT/DEBIAN/preinst
  98. fi
  99. fi
  100. chmod 0755 $BUILDROOT/DEBIAN/preinst
  101. echo "#!/bin/bash
  102. " > $BUILDROOT/DEBIAN/postinst
  103. if [ -f $ROOT/postinst ]; then
  104. cat $ROOT/postinst >> $BUILDROOT/DEBIAN/postinst
  105. else
  106. if [ "$SERVICE" == "yes" ]; then
  107. echo "
  108. /usr/bin/systemctl preset yunion-${PKG}.service >/dev/null 2>&1 ||:
  109. " >> $BUILDROOT/DEBIAN/postinst
  110. fi
  111. fi
  112. chmod 0755 $BUILDROOT/DEBIAN/postinst
  113. echo "#!/bin/bash
  114. " > $BUILDROOT/DEBIAN/prerm
  115. if [ -f $ROOT/prerm ]; then
  116. cat $ROOT/prerm >> $BUILDROOT/DEBIAN/prerm
  117. else
  118. if [ "$SERVICE" == "yes" ]; then
  119. echo "
  120. /usr/bin/systemctl --no-reload disable yunion-${PKG}.service >/dev/null 2>&1 || :
  121. /usr/bin/systemctl stop yunion-${PKG}.service >/dev/null 2>&1 ||:
  122. " >> $BUILDROOT/DEBIAN/prerm
  123. fi
  124. fi
  125. chmod 0755 $BUILDROOT/DEBIAN/prerm
  126. echo "#!/bin/bash
  127. " > $BUILDROOT/DEBIAN/postrm
  128. if [ -f $ROOT/postrm ]; then
  129. cat $ROOT/postrm >> $BUILDROOT/DEBIAN/postrm
  130. else
  131. if [ "$SERVICE" == "yes" ]; then
  132. echo "
  133. /usr/bin/systemctl daemon-reload >/dev/null 2>&1 ||:
  134. " >> $BUILDROOT/DEBIAN/postrm
  135. fi
  136. fi
  137. chmod 0755 $BUILDROOT/DEBIAN/postrm
  138. dpkg-deb --build $BUILDROOT
  139. case "$CURRENT_ARCH" in
  140. "amd64")
  141. DSTARCH="x86_64"
  142. ;;
  143. "arm64")
  144. DSTARCH="aarch64"
  145. ;;
  146. "riscv64")
  147. DSTARCH="riscv64"
  148. ;;
  149. esac
  150. mkdir -p ${OUTPUT_DIR}/${DSTARCH}
  151. mv ${BUILDROOT}.deb ${OUTPUT_DIR}/${DSTARCH}/