create-debs.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/env bash
  2. # create-debs.sh — build the smartbotic-vectorapi + -webui .deb packages
  3. set -euo pipefail
  4. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  5. SOURCE_DIR="${SOURCE_DIR:-$(cd "$SCRIPT_DIR/../.." && pwd)}"
  6. BUILD_DIR="${BUILD_DIR:-$SOURCE_DIR/build}"
  7. OUTPUT_DIR="${OUTPUT_DIR:-$SOURCE_DIR/dist}"
  8. BUILD_DEB_REVISION="${BUILD_DEB_REVISION:-1}"
  9. TEMPLATE_DIR="$SCRIPT_DIR/templates"
  10. SCRIPTS_DIR="$SCRIPT_DIR/scripts"
  11. SYSTEMD_DIR="$SCRIPT_DIR/systemd"
  12. CONFIG_DIR="$SOURCE_DIR/config"
  13. API_DIR="$SOURCE_DIR/api"
  14. WEBUI_DIST="${WEBUI_DIST:-$BUILD_DIR/webui/dist}"
  15. VERSION="$(cat "$SOURCE_DIR/VERSION")"
  16. DEB_VERSION="${VERSION}-${BUILD_DEB_REVISION}"
  17. echo "=== smartbotic-vectorapi deb builder ==="
  18. echo "VERSION=$VERSION DEB_VERSION=$DEB_VERSION"
  19. echo "BUILD_DIR=$BUILD_DIR OUTPUT_DIR=$OUTPUT_DIR WEBUI_DIST=$WEBUI_DIST"
  20. mkdir -p "$OUTPUT_DIR"
  21. calc_installed_size() {
  22. local staging_dir="$1" total=0
  23. for d in "$staging_dir"/*/; do
  24. [ "$(basename "$d")" = "DEBIAN" ] && continue
  25. total=$(( total + $(du -sk "$d" | awk '{print $1}') ))
  26. done
  27. echo "$total"
  28. }
  29. build_deb() {
  30. local pkg_name="$1" staging_dir="$2" arch="$3"
  31. local installed_size; installed_size="$(calc_installed_size "$staging_dir")"
  32. # {{VERSION}} is the full deb version (upstream-revision); {{UPSTREAM_VERSION}}
  33. # omits the revision, for inter-package deps that track the API version rather
  34. # than the packaging revision (lets the webui ship without bumping the server).
  35. sed -i -e "s/{{VERSION}}/${DEB_VERSION}/g" -e "s/{{UPSTREAM_VERSION}}/${VERSION}/g" \
  36. -e "s/{{INSTALLED_SIZE}}/${installed_size}/g" "$staging_dir/DEBIAN/control"
  37. find "$staging_dir" -type d -exec chmod 0755 {} +
  38. find "$staging_dir/DEBIAN" -type f -exec chmod 0644 {} +
  39. for f in postinst prerm postrm preinst; do
  40. [ -f "$staging_dir/DEBIAN/$f" ] && chmod 0755 "$staging_dir/DEBIAN/$f"
  41. done
  42. local deb_file="${OUTPUT_DIR}/${pkg_name}_${DEB_VERSION}_${arch}.deb"
  43. dpkg-deb --build --root-owner-group "$staging_dir" "$deb_file"
  44. echo " -> $deb_file"
  45. }
  46. # ---- Package 1: smartbotic-vectorapi (server) ----
  47. # SKIP_SERVER_DEB=1 builds only the arch:all webui package. Use when shipping a
  48. # webui-only change to a host whose server must not move (e.g. one pinned to an
  49. # older libsmartbotic-db-client — rebuilding the server against a newer client
  50. # would force that ABI upgrade).
  51. PKG1=""
  52. if [ "${SKIP_SERVER_DEB:-0}" = "1" ]; then
  53. echo "--- smartbotic-vectorapi: SKIPPED (SKIP_SERVER_DEB=1) ---"
  54. else
  55. echo "--- smartbotic-vectorapi ---"
  56. PKG1="$(mktemp -d)"; trap 'rm -rf "$PKG1"' EXIT
  57. mkdir -p "$PKG1/DEBIAN" "$PKG1/usr/bin" "$PKG1/etc/smartbotic-vectorapi" \
  58. "$PKG1/lib/systemd/system" "$PKG1/usr/share/smartbotic-vectorapi"
  59. [ -f "$BUILD_DIR/src/smartbotic-vectorapi" ] || { echo "ERROR: binary not found at $BUILD_DIR/src/smartbotic-vectorapi" >&2; exit 1; }
  60. cp "$BUILD_DIR/src/smartbotic-vectorapi" "$PKG1/usr/bin/smartbotic-vectorapi"
  61. chmod 0755 "$PKG1/usr/bin/smartbotic-vectorapi"
  62. cp "$CONFIG_DIR/config.json" "$PKG1/etc/smartbotic-vectorapi/config.json"
  63. cp "$SYSTEMD_DIR/smartbotic-vectorapi.service" "$PKG1/lib/systemd/system/"
  64. cp "$API_DIR/openapi.json" "$API_DIR/llms.txt" "$PKG1/usr/share/smartbotic-vectorapi/"
  65. mkdir -p "$PKG1/usr/share/smartbotic-vectorapi/docs"
  66. cp -a "$API_DIR/docs/." "$PKG1/usr/share/smartbotic-vectorapi/docs/"
  67. cp "$TEMPLATE_DIR/control.server" "$PKG1/DEBIAN/control"
  68. cp "$SCRIPTS_DIR/server.postinst" "$PKG1/DEBIAN/postinst"
  69. cp "$SCRIPTS_DIR/server.prerm" "$PKG1/DEBIAN/prerm"
  70. cp "$SCRIPTS_DIR/server.postrm" "$PKG1/DEBIAN/postrm"
  71. printf '/etc/smartbotic-vectorapi/config.json\n' > "$PKG1/DEBIAN/conffiles"
  72. build_deb "smartbotic-vectorapi" "$PKG1" "amd64"
  73. fi
  74. # ---- Package 2: smartbotic-vectorapi-webui (static assets) ----
  75. echo "--- smartbotic-vectorapi-webui ---"
  76. PKG2="$(mktemp -d)"; trap 'rm -rf "$PKG1" "$PKG2"' EXIT
  77. mkdir -p "$PKG2/DEBIAN" "$PKG2/usr/share/smartbotic-vectorapi/webui"
  78. if [ -d "$WEBUI_DIST" ] && [ -n "$(ls -A "$WEBUI_DIST" 2>/dev/null)" ]; then
  79. cp -a "$WEBUI_DIST"/. "$PKG2/usr/share/smartbotic-vectorapi/webui/"
  80. else
  81. echo "ERROR: webui dist not found at $WEBUI_DIST (build with -DBUILD_WEBUI=ON)" >&2; exit 1
  82. fi
  83. cp "$TEMPLATE_DIR/control.webui" "$PKG2/DEBIAN/control"
  84. build_deb "smartbotic-vectorapi-webui" "$PKG2" "all"
  85. echo "=== Done. Packages in $OUTPUT_DIR ==="
  86. ls -lh "$OUTPUT_DIR"/*.deb