build.sh 17 KB

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