build.sh 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. #!/usr/bin/env bash
  2. #
  3. # packaging/build.sh — Top-level build orchestrator for smartbotic-vectorapi
  4. #
  5. # Supports two modes:
  6. # Default: Docker build targeting Debian 13 (production .debs)
  7. # --local: Native build on current system (development/testing).
  8. # .deb packaging only happens on a Debian-family host; elsewhere
  9. # (e.g. Void Linux) it compiles and tests only - packages for
  10. # release always come from the Docker/Debian-13 path.
  11. #
  12. set -euo pipefail
  13. # ---------------------------------------------------------------------------
  14. # Paths
  15. # ---------------------------------------------------------------------------
  16. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  17. PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
  18. BASE_IMAGE_NAME="smartbotic-vectorapi-build-base:debian13"
  19. # Client version the server deb is compiled against. Must equal the version
  20. # installed on the deploy target: every 2.x client shares one SONAME, so a
  21. # mismatch installs cleanly and only segfaults on the next service restart.
  22. DB_CLIENT_VERSION="${DB_CLIENT_VERSION:-2.20.0-1}"
  23. BUILD_JOBS="${BUILD_JOBS:-$(nproc)}"
  24. DEB_REPO_DIR="${DEB_REPO_DIR:-/data/dev/smartbotics/smartbotics-deb-repo}"
  25. # Output directories — separate to prevent local/Docker overwriting each other
  26. # dist/local/ — native build for current system
  27. # dist/debian13/ — Docker build for Debian 13 production
  28. DIST_BASE="${PROJECT_DIR}/dist"
  29. VERSION="$(cat "$PROJECT_DIR/VERSION")"
  30. # VERSION and api/openapi.json's info.version must agree, checked HERE so it
  31. # covers every path - local, Docker and publish alike. They drifted once and
  32. # 0.10.0 was published with the docs claiming 0.9.2; openapi.json ships in the
  33. # server package and is served at /openapi.json, so a stale value is live, and
  34. # because a published version is never reused for different content, correcting
  35. # it cost a whole release. A release step that relies on remembering is a
  36. # release step that gets skipped.
  37. "$PROJECT_DIR/scripts/check-version-sync.sh"
  38. GIT_COMMIT="$(git -C "$PROJECT_DIR" rev-parse --short HEAD 2>/dev/null || echo "unknown")"
  39. # ---------------------------------------------------------------------------
  40. # Defaults
  41. # ---------------------------------------------------------------------------
  42. LOCAL_MODE=0
  43. INSTALL_AFTER=0
  44. SKIP_TESTS=0
  45. DEB_REVISION=1
  46. REBUILD_BASE=0
  47. NO_CACHE=0
  48. DO_REPO=0
  49. SUITE=""
  50. DO_SYNC=0
  51. MAKE_DEBS=1
  52. # The function runner is part of a release: ON here, even though the CMake
  53. # option defaults to OFF (it vendors QuickJS from GitHub at configure time, and
  54. # the ordinary dev build must not start depending on a network fetch). The
  55. # release build already fetches cpp-httplib the same way, so the Docker build
  56. # demonstrably has network. --no-runner is the escape hatch for an offline or
  57. # runner-less build.
  58. BUILD_RUNNER=ON
  59. # ---------------------------------------------------------------------------
  60. # Colored logging
  61. # ---------------------------------------------------------------------------
  62. log_info() { printf '\033[1;34m[INFO]\033[0m %s\n' "$*"; }
  63. log_success() { printf '\033[1;32m[OK]\033[0m %s\n' "$*"; }
  64. log_warn() { printf '\033[1;33m[WARN]\033[0m %s\n' "$*"; }
  65. log_error() { printf '\033[1;31m[ERROR]\033[0m %s\n' "$*"; }
  66. # ---------------------------------------------------------------------------
  67. # Host detection
  68. # ---------------------------------------------------------------------------
  69. # .debs are only meaningful when produced on a Debian-family host: a package
  70. # linked against another distro's glibc/abseil/gRPC will not install (or will
  71. # crash-loop) on Debian 13. Everything we ship comes from the Docker path.
  72. host_is_debian() {
  73. [[ -r /etc/os-release ]] || return 1
  74. local ID="" ID_LIKE=""
  75. # shellcheck disable=SC1091
  76. . /etc/os-release
  77. [[ "$ID" == "debian" || "$ID" == "ubuntu" || "$ID_LIKE" == *debian* ]]
  78. }
  79. host_name() {
  80. if [[ -r /etc/os-release ]]; then
  81. ( . /etc/os-release; echo "${PRETTY_NAME:-${NAME:-unknown}}" )
  82. else
  83. echo "unknown"
  84. fi
  85. }
  86. # ---------------------------------------------------------------------------
  87. # Usage
  88. # ---------------------------------------------------------------------------
  89. usage() {
  90. cat <<EOF
  91. Usage: packaging/build.sh [OPTIONS]
  92. Build modes:
  93. (default) Docker build targeting Debian 13 (production)
  94. --local Native compile + tests for current system (development).
  95. Builds .debs only on a Debian-family host; on any other
  96. distro packaging is refused - use the Docker mode.
  97. Options:
  98. --install Install .debs locally after build (--local, Debian host only)
  99. --no-runner Do not build the QuickJS function runner (skips the
  100. smartbotic-vectorapi-runner package; needed for a build
  101. with no network access to github.com)
  102. --skip-tests Skip building and running the test suite (--local only)
  103. --deb-revision N Debian revision suffix (default: 1)
  104. --rebuild-base Force rebuild Docker base image
  105. --no-cache Build without Docker caches
  106. --repo Add packages to local APT repository
  107. --suite NAME Distribution suite (e.g., trixie)
  108. --sync Add to repo and upload to repository.smartbotics.ai
  109. Environment variables:
  110. BUILD_JOBS Parallel build jobs (default: nproc)
  111. DEB_REPO_DIR Path to smartbotics-deb-repo (default: /data/dev/smartbotics/smartbotics-deb-repo)
  112. DB_CLIENT_VERSION libsmartbotic-db-client version to build against; must match
  113. the deploy target (default: 2.17.2-1)
  114. EOF
  115. exit 0
  116. }
  117. # ---------------------------------------------------------------------------
  118. # Argument parsing
  119. # ---------------------------------------------------------------------------
  120. while [[ $# -gt 0 ]]; do
  121. case "$1" in
  122. --local) LOCAL_MODE=1; shift ;;
  123. --install) INSTALL_AFTER=1; shift ;;
  124. --no-runner) BUILD_RUNNER=OFF; shift ;;
  125. --skip-tests) SKIP_TESTS=1; shift ;;
  126. --deb-revision) DEB_REVISION="$2"; shift 2 ;;
  127. --rebuild-base) REBUILD_BASE=1; shift ;;
  128. --no-cache) NO_CACHE=1; shift ;;
  129. --repo) DO_REPO=1; shift ;;
  130. --suite) SUITE="$2"; shift 2 ;;
  131. --sync) DO_SYNC=1; shift ;;
  132. -h|--help) usage ;;
  133. *)
  134. log_error "Unknown option: $1"
  135. usage
  136. ;;
  137. esac
  138. done
  139. # Validate option combinations
  140. if [[ $INSTALL_AFTER -eq 1 && $LOCAL_MODE -eq 0 ]]; then
  141. log_error "--install is only supported with --local"
  142. exit 1
  143. fi
  144. if [[ $DO_SYNC -eq 1 && $DO_REPO -eq 0 ]]; then
  145. DO_REPO=1
  146. fi
  147. if [[ $DO_REPO -eq 1 && -z "$SUITE" ]]; then
  148. log_error "--repo requires --suite NAME"
  149. exit 1
  150. fi
  151. # Native packaging is only allowed on a Debian-family host. On anything else
  152. # (Void Linux on zeus, for example) --local compiles and tests, but must not
  153. # emit .debs: they would be linked against the wrong libc/abseil/gRPC.
  154. if [[ $LOCAL_MODE -eq 1 ]] && ! host_is_debian; then
  155. MAKE_DEBS=0
  156. log_warn "Non-Debian host detected ($(host_name)): --local will compile and test only."
  157. log_warn "Release packages must be built with the Docker mode: packaging/build.sh"
  158. if [[ $INSTALL_AFTER -eq 1 ]]; then
  159. log_error "--install needs .deb packages, which are not built on a non-Debian host."
  160. exit 1
  161. fi
  162. if [[ $DO_REPO -eq 1 ]]; then
  163. log_error "--repo/--sync need .deb packages; run the Docker mode (drop --local)."
  164. exit 1
  165. fi
  166. fi
  167. # ---------------------------------------------------------------------------
  168. # Set output directory based on mode
  169. # ---------------------------------------------------------------------------
  170. if [[ $LOCAL_MODE -eq 1 ]]; then
  171. OUTPUT_DIR="${DIST_BASE}/local"
  172. else
  173. OUTPUT_DIR="${DIST_BASE}/debian13"
  174. fi
  175. if [[ $MAKE_DEBS -eq 1 ]]; then
  176. mkdir -p "$OUTPUT_DIR"
  177. fi
  178. # ---------------------------------------------------------------------------
  179. # Banner
  180. # ---------------------------------------------------------------------------
  181. log_info "smartbotic-vectorapi build v${VERSION} (${GIT_COMMIT})"
  182. log_info "Build jobs: ${BUILD_JOBS}"
  183. if [[ $LOCAL_MODE -eq 1 ]]; then
  184. log_info "Mode: local (native, $(host_name))"
  185. else
  186. log_info "Mode: Docker (Debian 13)"
  187. fi
  188. if [[ $MAKE_DEBS -eq 1 ]]; then
  189. log_info "Output: ${OUTPUT_DIR}"
  190. else
  191. log_info "Output: ${PROJECT_DIR}/build (no packages on this host)"
  192. fi
  193. # ---------------------------------------------------------------------------
  194. # Local build
  195. # ---------------------------------------------------------------------------
  196. build_local() {
  197. local tests="ON"
  198. if [[ $SKIP_TESTS -eq 1 ]]; then
  199. tests="OFF"
  200. fi
  201. log_info "Configuring CMake (Release, BUILD_TESTS=${tests}, BUILD_RUNNER=${BUILD_RUNNER})..."
  202. cmake -B "$PROJECT_DIR/build" -G Ninja \
  203. -DCMAKE_BUILD_TYPE=Release \
  204. -DBUILD_WEBUI=ON \
  205. -DBUILD_TESTS="$tests" \
  206. -DBUILD_RUNNER="$BUILD_RUNNER" \
  207. -S "$PROJECT_DIR"
  208. log_info "Building with ${BUILD_JOBS} jobs..."
  209. cmake --build "$PROJECT_DIR/build" -j"$BUILD_JOBS"
  210. log_success "Build completed"
  211. if [[ $tests == "ON" ]]; then
  212. log_info "Running tests..."
  213. # Integration tests GTEST_SKIP() when smartbotic-database is unreachable.
  214. ctest --test-dir "$PROJECT_DIR/build" --output-on-failure
  215. log_success "Tests passed"
  216. fi
  217. if [[ $MAKE_DEBS -eq 0 ]]; then
  218. log_success "Native build verified: $PROJECT_DIR/build/src/smartbotic-vectorapi"
  219. return 0
  220. fi
  221. log_info "Creating .deb packages..."
  222. # SKIP_RUNNER_DEB keeps --no-runner from emitting a warning about a binary
  223. # we deliberately did not build.
  224. local skip_runner=0
  225. [[ "$BUILD_RUNNER" == "ON" ]] || skip_runner=1
  226. OUTPUT_DIR="$OUTPUT_DIR" BUILD_DEB_REVISION="$DEB_REVISION" \
  227. SKIP_RUNNER_DEB="$skip_runner" "$PROJECT_DIR/packaging/deb/create-debs.sh"
  228. log_success "Packages created"
  229. if [[ $INSTALL_AFTER -eq 1 ]]; then
  230. log_info "Installing .deb packages..."
  231. local debs=("$OUTPUT_DIR"/smartbotic-vectorapi_*.deb "$OUTPUT_DIR"/smartbotic-vectorapi-webui_*.deb)
  232. # Installed in the same dpkg run as the server when present: the runner
  233. # Breaks older server versions, so they have to be configured together.
  234. if compgen -G "$OUTPUT_DIR/smartbotic-vectorapi-runner_*.deb" >/dev/null; then
  235. debs+=("$OUTPUT_DIR"/smartbotic-vectorapi-runner_*.deb)
  236. fi
  237. sudo dpkg -i "${debs[@]}" || sudo apt-get install -f -y
  238. log_success "Packages installed"
  239. fi
  240. }
  241. # ---------------------------------------------------------------------------
  242. # Docker build
  243. # ---------------------------------------------------------------------------
  244. build_docker() {
  245. if ! command -v docker &>/dev/null; then
  246. log_error "Docker is not installed or not in PATH"
  247. exit 1
  248. fi
  249. # Read repo credentials from the .env file (key names: RepoUser / RepoPass)
  250. local env_file="${DEB_REPO_DIR}/.env"
  251. if [[ ! -f "$env_file" ]]; then
  252. log_error "Repository .env not found: $env_file"
  253. exit 1
  254. fi
  255. local REPO_USER REPO_PASS
  256. REPO_USER="$(grep '^RepoUser:' "$env_file" | awk '{print $2}')"
  257. REPO_PASS="$(grep '^RepoPass:' "$env_file" | awk '{print $2}')"
  258. if [[ -z "$REPO_USER" || -z "$REPO_PASS" ]]; then
  259. log_error "Could not parse RepoUser/RepoPass from $env_file"
  260. exit 1
  261. fi
  262. # Check / build base image
  263. if [[ $REBUILD_BASE -eq 1 ]] || ! docker image inspect "$BASE_IMAGE_NAME" &>/dev/null; then
  264. if [[ $REBUILD_BASE -eq 1 ]]; then
  265. log_info "Rebuilding base image (--rebuild-base)..."
  266. else
  267. log_info "Base image not found, building ${BASE_IMAGE_NAME}..."
  268. fi
  269. # Publishing re-indexes the apt pool, so the pinned client version is
  270. # usually gone from the repo index. Vendor the .debs from the local
  271. # deb-repo staging dir when they are there; otherwise the image falls
  272. # back to installing that version from the repo.
  273. local vendor_dir="$PROJECT_DIR/packaging/vendor"
  274. rm -f "$vendor_dir"/*.deb
  275. local found=0
  276. for pkg in libsmartbotic-db-client libsmartbotic-db-client-dev; do
  277. local deb="$DEB_REPO_DIR/dist/${pkg}_${DB_CLIENT_VERSION}_amd64.deb"
  278. if [[ -f "$deb" ]]; then cp "$deb" "$vendor_dir/"; found=$((found + 1)); fi
  279. done
  280. if [[ $found -eq 2 ]]; then
  281. log_info "Pinning db client ${DB_CLIENT_VERSION} from ${DEB_REPO_DIR}/dist"
  282. else
  283. log_warn "db client ${DB_CLIENT_VERSION} not found locally; the image will try the apt repo"
  284. fi
  285. local base_cache_args=()
  286. if [[ $NO_CACHE -eq 1 ]]; then
  287. base_cache_args+=("--no-cache")
  288. fi
  289. docker buildx build \
  290. -f "$PROJECT_DIR/packaging/Dockerfile.base" \
  291. -t "$BASE_IMAGE_NAME" \
  292. --build-arg "REPO_USER=${REPO_USER}" \
  293. --build-arg "REPO_PASS=${REPO_PASS}" \
  294. --build-arg "DB_CLIENT_VERSION=${DB_CLIENT_VERSION}" \
  295. "${base_cache_args[@]+"${base_cache_args[@]}"}" \
  296. "$PROJECT_DIR"
  297. log_success "Base image built: ${BASE_IMAGE_NAME}"
  298. else
  299. log_info "Using existing base image: ${BASE_IMAGE_NAME}"
  300. fi
  301. # Build packages
  302. log_info "Building packages in Docker (BUILD_RUNNER=${BUILD_RUNNER})..."
  303. # BUILD_RUNNER has to be consumed by Dockerfile.build's cmake invocation -
  304. # the flags live in the Dockerfile, not here. If that ARG is not declared
  305. # there, buildx silently ignores the build-arg and the runner package is
  306. # simply missing from the output, which is the kind of omission nobody
  307. # notices until functions stop working in production. So say so, loudly.
  308. if [[ "$BUILD_RUNNER" == "ON" ]] \
  309. && ! grep -q 'BUILD_RUNNER' "$PROJECT_DIR/packaging/Dockerfile.build"; then
  310. log_warn "packaging/Dockerfile.build does not consume BUILD_RUNNER:"
  311. log_warn " it needs 'ARG BUILD_RUNNER=ON' and -DBUILD_RUNNER=\${BUILD_RUNNER} on its"
  312. log_warn " cmake line. Until then this build produces NO smartbotic-vectorapi-runner deb."
  313. fi
  314. local build_cache_args=()
  315. if [[ $NO_CACHE -eq 1 ]]; then
  316. build_cache_args+=("--no-cache")
  317. fi
  318. docker buildx build \
  319. -f "$PROJECT_DIR/packaging/Dockerfile.build" \
  320. --build-arg "BASE_IMAGE=${BASE_IMAGE_NAME}" \
  321. --build-arg "BUILD_VERSION=${VERSION}" \
  322. --build-arg "BUILD_DEB_REVISION=${DEB_REVISION}" \
  323. --build-arg "BUILD_JOBS=${BUILD_JOBS}" \
  324. --build-arg "BUILD_GIT_COMMIT=${GIT_COMMIT}" \
  325. --build-arg "BUILD_RUNNER=${BUILD_RUNNER}" \
  326. --target packages \
  327. --output "type=local,dest=$OUTPUT_DIR" \
  328. "${build_cache_args[@]+"${build_cache_args[@]}"}" \
  329. "$PROJECT_DIR"
  330. log_success "Docker build completed"
  331. }
  332. # ---------------------------------------------------------------------------
  333. # Main
  334. # ---------------------------------------------------------------------------
  335. if [[ $LOCAL_MODE -eq 1 ]]; then
  336. build_local
  337. else
  338. build_docker
  339. fi
  340. # ---------------------------------------------------------------------------
  341. # List created packages
  342. # ---------------------------------------------------------------------------
  343. if [[ $MAKE_DEBS -eq 1 ]]; then
  344. echo ""
  345. log_info "Created packages:"
  346. ls -lh "$OUTPUT_DIR"/*.deb 2>/dev/null || {
  347. log_error "No .deb files found in $OUTPUT_DIR"
  348. exit 1
  349. }
  350. fi
  351. # ---------------------------------------------------------------------------
  352. # Repository operations
  353. # ---------------------------------------------------------------------------
  354. if [[ $DO_REPO -eq 1 ]]; then
  355. echo ""
  356. log_info "Adding packages to repository (suite: ${SUITE})..."
  357. if [[ ! -x "$DEB_REPO_DIR/scripts/add-packages.sh" ]]; then
  358. log_error "Repository tool not found: $DEB_REPO_DIR/scripts/add-packages.sh"
  359. exit 1
  360. fi
  361. "$DEB_REPO_DIR/scripts/add-packages.sh" "$OUTPUT_DIR"
  362. log_success "Packages added to staging"
  363. "$DEB_REPO_DIR/scripts/create-repo.sh" --suite "$SUITE"
  364. log_success "Repository created for suite: ${SUITE}"
  365. if [[ $DO_SYNC -eq 1 ]]; then
  366. log_info "Syncing repository to repository.smartbotics.ai..."
  367. "$DEB_REPO_DIR/scripts/sync-repo.sh"
  368. log_success "Repository synced"
  369. fi
  370. fi
  371. # ---------------------------------------------------------------------------
  372. # Next steps
  373. # ---------------------------------------------------------------------------
  374. echo ""
  375. if [[ $MAKE_DEBS -eq 1 ]]; then
  376. log_success "Build complete! (v${VERSION}-${DEB_REVISION})"
  377. else
  378. log_success "Native build complete! (v${VERSION}, ${GIT_COMMIT})"
  379. fi
  380. echo ""
  381. if [[ $MAKE_DEBS -eq 0 ]]; then
  382. log_info "To run the server in place:"
  383. echo " SMARTBOTIC_VECTORAPI_KEY=dev ${PROJECT_DIR}/build/src/smartbotic-vectorapi \\"
  384. echo " --config ${PROJECT_DIR}/config/config.json \\"
  385. echo " --share-dir ${PROJECT_DIR}/api \\"
  386. echo " --webui-dir ${PROJECT_DIR}/build/webui/dist"
  387. echo ""
  388. fi
  389. if [[ $MAKE_DEBS -eq 1 && $LOCAL_MODE -eq 1 && $INSTALL_AFTER -eq 0 ]]; then
  390. log_info "To install locally:"
  391. echo " sudo dpkg -i ${OUTPUT_DIR}/*.deb || sudo apt-get install -f -y"
  392. echo ""
  393. fi
  394. if [[ $DO_REPO -eq 0 ]]; then
  395. log_info "To build release packages and publish:"
  396. echo " packaging/build.sh --repo --suite trixie"
  397. echo ""
  398. fi
  399. if [[ $DO_REPO -eq 1 && $DO_SYNC -eq 0 ]]; then
  400. log_info "To sync repository to remote:"
  401. echo " packaging/build.sh --repo --suite ${SUITE} --sync"
  402. echo ""
  403. fi