create-debs.sh 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/usr/bin/env bash
  2. # create-debs.sh - build the smartbotic-vectorapi + -webui + -runner .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. RUNNER_CONFIG_DIR="$SOURCE_DIR/runner/config"
  14. API_DIR="$SOURCE_DIR/api"
  15. WEBUI_DIST="${WEBUI_DIST:-$BUILD_DIR/webui/dist}"
  16. VERSION="$(cat "$SOURCE_DIR/VERSION")"
  17. DEB_VERSION="${VERSION}-${BUILD_DEB_REVISION}"
  18. echo "=== smartbotic-vectorapi deb builder ==="
  19. echo "VERSION=$VERSION DEB_VERSION=$DEB_VERSION"
  20. echo "BUILD_DIR=$BUILD_DIR OUTPUT_DIR=$OUTPUT_DIR WEBUI_DIST=$WEBUI_DIST"
  21. mkdir -p "$OUTPUT_DIR"
  22. calc_installed_size() {
  23. local staging_dir="$1" total=0
  24. for d in "$staging_dir"/*/; do
  25. [ "$(basename "$d")" = "DEBIAN" ] && continue
  26. total=$(( total + $(du -sk "$d" | awk '{print $1}') ))
  27. done
  28. echo "$total"
  29. }
  30. build_deb() {
  31. local pkg_name="$1" staging_dir="$2" arch="$3"
  32. local installed_size; installed_size="$(calc_installed_size "$staging_dir")"
  33. # {{VERSION}} is the full deb version (upstream-revision); {{UPSTREAM_VERSION}}
  34. # omits the revision, for inter-package deps that track the API version rather
  35. # than the packaging revision (lets the webui ship without bumping the server).
  36. sed -i -e "s/{{VERSION}}/${DEB_VERSION}/g" -e "s/{{UPSTREAM_VERSION}}/${VERSION}/g" \
  37. -e "s/{{INSTALLED_SIZE}}/${installed_size}/g" "$staging_dir/DEBIAN/control"
  38. find "$staging_dir" -type d -exec chmod 0755 {} +
  39. find "$staging_dir/DEBIAN" -type f -exec chmod 0644 {} +
  40. for f in postinst prerm postrm preinst; do
  41. [ -f "$staging_dir/DEBIAN/$f" ] && chmod 0755 "$staging_dir/DEBIAN/$f"
  42. done
  43. local deb_file="${OUTPUT_DIR}/${pkg_name}_${DEB_VERSION}_${arch}.deb"
  44. dpkg-deb --build --root-owner-group "$staging_dir" "$deb_file"
  45. echo " -> $deb_file"
  46. }
  47. # ---- Package 1: smartbotic-vectorapi (server) ----
  48. # SKIP_SERVER_DEB=1 builds only the arch:all webui package. Use when shipping a
  49. # webui-only change to a host whose server must not move (e.g. one pinned to an
  50. # older libsmartbotic-db-client — rebuilding the server against a newer client
  51. # would force that ABI upgrade).
  52. PKG1=""
  53. if [ "${SKIP_SERVER_DEB:-0}" = "1" ]; then
  54. echo "--- smartbotic-vectorapi: SKIPPED (SKIP_SERVER_DEB=1) ---"
  55. else
  56. echo "--- smartbotic-vectorapi ---"
  57. PKG1="$(mktemp -d)"; trap 'rm -rf "$PKG1"' EXIT
  58. mkdir -p "$PKG1/DEBIAN" "$PKG1/usr/bin" "$PKG1/etc/smartbotic-vectorapi" \
  59. "$PKG1/lib/systemd/system" "$PKG1/usr/share/smartbotic-vectorapi"
  60. [ -f "$BUILD_DIR/src/smartbotic-vectorapi" ] || { echo "ERROR: binary not found at $BUILD_DIR/src/smartbotic-vectorapi" >&2; exit 1; }
  61. cp "$BUILD_DIR/src/smartbotic-vectorapi" "$PKG1/usr/bin/smartbotic-vectorapi"
  62. chmod 0755 "$PKG1/usr/bin/smartbotic-vectorapi"
  63. cp "$CONFIG_DIR/config.json" "$PKG1/etc/smartbotic-vectorapi/config.json"
  64. cp "$SYSTEMD_DIR/smartbotic-vectorapi.service" "$PKG1/lib/systemd/system/"
  65. cp "$API_DIR/openapi.json" "$API_DIR/llms.txt" "$PKG1/usr/share/smartbotic-vectorapi/"
  66. mkdir -p "$PKG1/usr/share/smartbotic-vectorapi/docs"
  67. cp -a "$API_DIR/docs/." "$PKG1/usr/share/smartbotic-vectorapi/docs/"
  68. cp "$TEMPLATE_DIR/control.server" "$PKG1/DEBIAN/control"
  69. cp "$SCRIPTS_DIR/server.postinst" "$PKG1/DEBIAN/postinst"
  70. cp "$SCRIPTS_DIR/server.prerm" "$PKG1/DEBIAN/prerm"
  71. cp "$SCRIPTS_DIR/server.postrm" "$PKG1/DEBIAN/postrm"
  72. printf '/etc/smartbotic-vectorapi/config.json\n' > "$PKG1/DEBIAN/conffiles"
  73. build_deb "smartbotic-vectorapi" "$PKG1" "amd64"
  74. fi
  75. # ---- Package 2: smartbotic-vectorapi-webui (static assets) ----
  76. echo "--- smartbotic-vectorapi-webui ---"
  77. PKG2="$(mktemp -d)"; trap 'rm -rf "$PKG1" "$PKG2"' EXIT
  78. mkdir -p "$PKG2/DEBIAN" "$PKG2/usr/share/smartbotic-vectorapi/webui"
  79. if [ -d "$WEBUI_DIST" ] && [ -n "$(ls -A "$WEBUI_DIST" 2>/dev/null)" ]; then
  80. cp -a "$WEBUI_DIST"/. "$PKG2/usr/share/smartbotic-vectorapi/webui/"
  81. else
  82. echo "ERROR: webui dist not found at $WEBUI_DIST (build with -DBUILD_WEBUI=ON)" >&2; exit 1
  83. fi
  84. cp "$TEMPLATE_DIR/control.webui" "$PKG2/DEBIAN/control"
  85. build_deb "smartbotic-vectorapi-webui" "$PKG2" "all"
  86. # ---- Package 3: smartbotic-vectorapi-runner (function runner) ----
  87. # The runner is an opt-in build (-DBUILD_RUNNER=ON; it vendors QuickJS over the
  88. # network at configure time), so a missing binary is a configuration choice, not
  89. # a broken build: warn and skip rather than fail the whole package set, the same
  90. # posture as the OPTIONAL install() of runner.json in the root CMakeLists.
  91. # SKIP_RUNNER_DEB=1 silences the warning when the omission is deliberate.
  92. RUNNER_BIN="$BUILD_DIR/runner/smartbotic-vectorapi-runner"
  93. if [ "${SKIP_RUNNER_DEB:-0}" = "1" ]; then
  94. echo "--- smartbotic-vectorapi-runner: SKIPPED (SKIP_RUNNER_DEB=1) ---"
  95. elif [ ! -f "$RUNNER_BIN" ]; then
  96. echo "--- smartbotic-vectorapi-runner: SKIPPED ---"
  97. echo "WARNING: $RUNNER_BIN not found; configure with -DBUILD_RUNNER=ON to ship it" >&2
  98. else
  99. echo "--- smartbotic-vectorapi-runner ---"
  100. PKG3="$(mktemp -d)"; trap 'rm -rf "$PKG1" "$PKG2" "$PKG3"' EXIT
  101. mkdir -p "$PKG3/DEBIAN" "$PKG3/usr/bin" "$PKG3/etc/smartbotic-vectorapi" \
  102. "$PKG3/lib/systemd/system"
  103. cp "$RUNNER_BIN" "$PKG3/usr/bin/smartbotic-vectorapi-runner"
  104. chmod 0755 "$PKG3/usr/bin/smartbotic-vectorapi-runner"
  105. # runner.json only. config.json belongs to the server package and must not be
  106. # claimed twice: two packages shipping one conffile is a dpkg overwrite error.
  107. cp "$RUNNER_CONFIG_DIR/runner.json" "$PKG3/etc/smartbotic-vectorapi/runner.json"
  108. cp "$SYSTEMD_DIR/smartbotic-vectorapi-runner.service" "$PKG3/lib/systemd/system/"
  109. cp "$TEMPLATE_DIR/control.runner" "$PKG3/DEBIAN/control"
  110. cp "$SCRIPTS_DIR/runner.postinst" "$PKG3/DEBIAN/postinst"
  111. cp "$SCRIPTS_DIR/runner.prerm" "$PKG3/DEBIAN/prerm"
  112. cp "$SCRIPTS_DIR/runner.postrm" "$PKG3/DEBIAN/postrm"
  113. # runner.env is NOT here on purpose: postinst creates it 0640 if absent, so an
  114. # operator's function secrets are never a dpkg-tracked conffile.
  115. printf '/etc/smartbotic-vectorapi/runner.json\n' > "$PKG3/DEBIAN/conffiles"
  116. build_deb "smartbotic-vectorapi-runner" "$PKG3" "amd64"
  117. fi
  118. echo "=== Done. Packages in $OUTPUT_DIR ==="
  119. ls -lh "$OUTPUT_DIR"/*.deb