okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-ds-out ac306afb2 into f-own-intaff3a132f

main
6.7 KiB113 linesraw
1#!/bin/sh
2# Build a Strata image from an engine component package. Run it on a Linux x86_64 machine with Docker (it needs the internet
3# for the base image and for the Terminal's pinned Python and locked wheels; the engine package is local).
4#
5#   docker/build.sh --flavor cuda --engine ~/pkgs/0.1.45.3-linux-x86_64-cuda-cuda13 --tag strata:0.2.7-cuda13
6#   docker/build.sh --flavor hip  --engine ~/pkgs/0.1.45.3-linux-x86_64-hip-gfx120X  --tag strata:0.2.7-rocm-gfx120x
7#   docker/build.sh --flavor sycl --engine ~/pkgs/0.1.45.3-linux-x86_64-sycl         --tag strata:0.2.7-sycl
8#
9# --engine is the package FOLDER that packaging/package_engine.py wrote (engine-component.json inside). It can be given more than
10# once to put several packages in one image (the Terminal picks the one that fits the card).
11#
12#   --flavor cuda|hip|sycl   which system libraries the runtime adds (see docker/Dockerfile)
13#   --tag NAME               image name (default strata:<version>-<flavor>)
14#   --base IMAGE             base image (default ubuntu:24.04; sycl: ubuntu:26.04)
15#   --repo DIR               the Strata source tree (default: the one this script is in)
16#   --version V              label (default: read from CMakeLists.txt)
17#   --ctx DIR                keep the build context here (default: a temporary folder, removed after the build)
18#   --no-cache               docker build --no-cache
19#   --ctx-only               assemble the context and stop
20# Environment: STRATA_DOCKER_BUILD_OPTS adds options to `docker build` (for example --network host).
21# Nothing is pushed anywhere; the image stays in the local Docker.
22set -eu
23
24here=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd -P)
25repo=$(CDPATH= cd -- "$here/.." && pwd -P)
26flavor=""; tag=""; base=""; version=""; ctx=""; nocache=""; ctxonly=""
27engines=""
28while [ $# -gt 0 ]; do
29  case "$1" in
30    --flavor) flavor=$2; shift 2 ;;
31    --engine) engines="$engines
32$2"; shift 2 ;;
33    --tag) tag=$2; shift 2 ;;
34    --base) base=$2; shift 2 ;;
35    --repo) repo=$(CDPATH= cd -- "$2" && pwd -P); shift 2 ;;
36    --version) version=$2; shift 2 ;;
37    --ctx) ctx=$2; shift 2 ;;
38    --no-cache) nocache=--no-cache; shift ;;
39    --ctx-only) ctxonly=1; shift ;;
40    -h|--help) sed -n '2,20p' "$0"; exit 0 ;;
41    *) echo "build.sh: unknown option $1" >&2; exit 2 ;;
42  esac
43done
44[ -n "$flavor" ] || { echo "build.sh: --flavor cuda|hip|sycl is required" >&2; exit 2; }
45case "$flavor" in cuda|hip|sycl) ;; *) echo "build.sh: --flavor must be cuda, hip or sycl" >&2; exit 2 ;; esac
46[ -n "$engines" ] || { echo "build.sh: --engine <package folder> is required" >&2; exit 2; }
47[ -n "$base" ] || { [ "$flavor" = sycl ] && base=ubuntu:26.04 || base=ubuntu:24.04; }
48if [ -z "$version" ]; then
49  version=$(sed -n 's/^[[:space:]]*project([^)]*VERSION[[:space:]]*\([0-9][0-9.]*\).*/\1/p' "$repo/CMakeLists.txt" | head -1)
50  version=${version:-dev}
51fi
52
53own_ctx=""
54if [ -z "$ctx" ]; then ctx=$(mktemp -d "${TMPDIR:-/tmp}/strata-docker-ctx.XXXXXX"); own_ctx=1; fi
55mkdir -p "$ctx/terminal-src" "$ctx/engine" "$ctx/docker"
56cleanup() { if [ -n "$own_ctx" ] && [ -z "$ctxonly" ]; then rm -rf "$ctx"; fi; }
57trap cleanup EXIT
58
59# the commit the sources are from (the build context has no .git; package_terminal.py writes it into the archive's COMMIT)
60commit=${STRATA_BUILD_COMMIT:-$(git -C "$repo" rev-parse HEAD 2>/dev/null || true)}
61case "$commit" in *[!0-9a-f]*|"") commit="" ;; esac
62
63# 1. the Terminal's sources: exactly what packaging/package_terminal.py copies (the working tree, so local edits are in the image)
64# tools/: every .py at any depth (package_terminal.py may name a nested tool; the Dockerfile's terminal stage stops at "installer tool missing"
65# when one is not in the context)
66cd "$repo"
67# metal/tools/: the two repack scripts the bundle carries (INSTALLER_METAL_TOOLS in package_terminal.py; without them the terminal stage
68# stops at "No such file or directory: '/src/metal/tools/metal_repack.py'", as the v0.2.11 image build did)
69paths="packaging bin strata.py strata_control strata_cli serve requirements.txt requirements-monitor.txt docs/MANAGEMENT_PROTOCOL.md docs/TERMINAL_LAUNCH.md data bench/prompts/standard-v1 setup.py ref/load.py CMakeLists.txt metal/tools/metal_repack.py metal/tools/stream_experts.py"
70if git rev-parse --git-dir >/dev/null 2>&1; then
71  # shellcheck disable=SC2086
72  { git ls-files -z -- $paths ':(glob)tools/**/*.py'; } | tar --null -T - -cf - | tar -xf - -C "$ctx/terminal-src"
73else
74  tools_py=$(find tools -name '*.py')          # nested too
75  # shellcheck disable=SC2086
76  tar -cf - --exclude='__pycache__' $paths $tools_py | tar -xf - -C "$ctx/terminal-src"
77fi
78
79# 2. the engine package(s): hard-linked when on the same file system (no second copy of GBs), copied otherwise
80for e in $engines; do
81  [ -f "$e/engine-component.json" ] || { echo "build.sh: $e has no engine-component.json (give the package folder)" >&2; exit 2; }
82  dest="$ctx/engine/$(basename "$e")"
83  rm -rf "$dest"
84  cp -al "$e" "$dest" 2>/dev/null || { rm -rf "$dest"; cp -a "$e" "$dest"; }     # (a failed hard-link attempt leaves a half-made folder)
85  # a package that went through a zip or a Windows share lost its executable bit: the manifest hashes content only, so restore it on
86  # OUR copy (break the hard link first, so the original is not changed)
87  entry=$(sed -n 's/.*"entrypoint": *"\([^"]*\)".*/\1/p' "$dest/engine-component.json" | head -1)
88  if [ -n "$entry" ] && [ -f "$dest/$entry" ] && [ ! -x "$dest/$entry" ]; then
89    echo "note: $entry in $e is not executable; making it executable in the image copy"
90    cp --remove-destination "$e/$entry" "$dest/$entry" && chmod 0755 "$dest/$entry"
91  fi
92done
93
94# 3. the Dockerfile and the container scripts
95cp "$here/Dockerfile" "$ctx/Dockerfile"
96cp "$here/entrypoint.sh" "$here/strata-wrapper.sh" "$here/healthcheck.sh" "$ctx/docker/"
97tr -d '\r' < "$here/entrypoint.sh" > "$ctx/docker/entrypoint.sh"          # a checkout with CRLF line ends must not break the scripts
98tr -d '\r' < "$here/strata-wrapper.sh" > "$ctx/docker/strata-wrapper.sh"
99tr -d '\r' < "$here/healthcheck.sh" > "$ctx/docker/healthcheck.sh"
100
101engine_label=$(for e in $engines; do basename "$e"; done | tr '\n' ' ' | sed 's/ $//')
102[ -n "$tag" ] || tag="strata:$version-$flavor"
103echo "context: $ctx"
104echo "flavor $flavor, base $base, engine(s): $engine_label, tag $tag"
105[ -n "$ctxonly" ] && { echo "context kept (--ctx-only)"; exit 0; }
106
107# shellcheck disable=SC2086
108docker build $nocache ${STRATA_DOCKER_BUILD_OPTS:-} \
109  --build-arg "BASE_IMAGE=$base" --build-arg "FLAVOR=$flavor" \
110  --build-arg "STRATA_COMMIT=$commit" --build-arg "STRATA_VERSION=$version" --build-arg "ENGINE_LABEL=$engine_label" \
111  -t "$tag" "$ctx"
112docker image inspect "$tag" --format 'built {{.RepoTags}} {{.Size}} bytes'
113echo "Not pushed anywhere. Size on disk: $(docker image inspect "$tag" --format '{{.Size}}') bytes."