ops/images.sh 9.6 KiB 271 lines Raw
1 #!/bin/sh2 # Build one self-contained bundle image from the bundle's own Dockerfile.3 #4 # The host never compiles an engine and never runs one. `prepare` copies the5 # one bundle and the shared SDK workspace into a temporary Docker context; the6 # bundle's Dockerfile decides what to build and what to copy. The host only7 # stamps the generic labels, records the final digest, and proves that no8 # weight became a layer.9 #10 # The final tag is content-derived:11 #12 #   inf3r/<bundle-id>:sha-<12>   the staged context digest13 #14 # The digest is recorded in ${CARGO_TARGET_DIR:-target}/images.lock. `check`15 # recomputes the source digest and compares it with the lock and the image's16 # source label, then checks that the image exists and carries no model/ tree or17 # weight file. `tag` prints the final reference for a deploy caller.18 set -eu19 20 root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)21 target=${CARGO_TARGET_DIR:-$root/target}22 case "$target" in23     /*) ;;24     *) target=$root/$target ;;25 esac26 lock=$target/images.lock27 28 usage() {29     cat >&2 <<'USAGE'30 usage: ops/images.sh <command> <bundle-id>31   build <bundle-id>   build the bundle's self-contained image; record the32                       source digest and image digest in target/images.lock33   check <bundle-id>   verify the lock, source digest, image labels, and that34                       no model/ tree or weight file is in the image35   tag <bundle-id>     print the final image reference36 USAGE37     exit 238 }39 40 die() {41     printf 'images: %s\n' "$*" >&242     exit 143 }44 45 # A weight format with no legitimate use in a bundle image. These names are46 # refused at any size.47 weight_strong() {48     name=$(printf '%s' "$1" | tr '[:upper:]' '[:lower:]')49     case $name in50         *.gguf|*.safetensors|*.nemo|*.onnx|*.ckpt|*.tflite|*.trt|*.plan|*.pdparams|*.weights)51             return 052             ;;53     esac54     return 155 }56 57 # A shared name that also appears in Python packages and OS/CUDA data. A file58 # with one of these names is only a weight candidate above 1 MiB.59 weight_ambiguous() {60     name=$(printf '%s' "$1" | tr '[:upper:]' '[:lower:]')61     case $name in62         *.bin|*.pt|*.pth|*.h5|*.hdf5|*.pkl|*.pickle|*.npy|*.npz|*.ot|*.msgpack|*.model)63             return 064             ;;65     esac66     return 167 }68 69 # A magic prefix that a serialized tensor file uses. This catches a weight70 # without a telling extension; it is a second gate, not a replacement for the71 # name check.72 weight_magic() {73     magic=$(od -An -tx1 -N8 "$1" 2>/dev/null | tr -d ' \n')74     case $magic in75         47475546*|934e554d*|89484446*|54464c33*|504b0304*|80*)76             return 077             ;;78     esac79     return 180 }81 82 [ $# -ge 2 ] || usage83 command=$184 bundle=$285 [ $# -eq 2 ] || usage86 87 case "$bundle" in88     /*|*/|*//*|*..*) die "invalid bundle id: $bundle" ;;89 esac90 dir=$root/plugins/$bundle91 recipe=$dir/Dockerfile92 [ -f "$dir/bundle.toml" ] || die "$bundle has no bundle.toml"93 [ -f "$recipe" ] || die "$bundle has no Dockerfile"94 binary=$(sed -n 's/^binary[[:space:]]*=[[:space:]]*"\([^"]*\)".*/\1/p' "$dir/bundle.toml")95 [ -n "$binary" ] || die "$bundle has no binary"96 97 work=98 trap 'rm -rf "$work"' EXIT HUP INT TERM99 100 context_tree() {101     (cd "$ctx" && find . -type f -print0 | LC_ALL=C sort -z |102         xargs -0 sha256sum | sha256sum | awk '{print $1}')103 }104 105 prepare() {106     work=$(mktemp -d "${TMPDIR:-/tmp}/inf3r-image.XXXXXX") || die "cannot make a context"107     ctx=$work/context108     mkdir -p "$ctx/plugins/$bundle"109 110     # The generic context is one bundle plus the shared SDK workspace. A Rust111     # bundle uses the manifests and crates by path; any other language can112     # ignore them. The bundle's Dockerfile owns every language-specific choice.113     cp "$root/Cargo.toml" "$root/Cargo.lock" "$root/rust-toolchain.toml" "$ctx/"114     cp -a "$root/crates" "$ctx/crates"115     (cd "$dir" && tar \116         --exclude=./model --exclude=./model/* \117         --exclude=./target --exclude=./target/* \118         -cf - .) | (cd "$ctx/plugins/$bundle" && tar -xf -)119 120     if [ -e "$ctx/plugins/$bundle/model" ]; then121         die "$bundle staged a model/ path"122     fi123     if find "$ctx" -type l -print -quit | grep -q .; then124         die "$bundle staged a symlink"125     fi126     if find "$ctx" -type f -print | while IFS= read -r file; do127         weight_strong "$(basename "$file")" && printf '%s\n' "$file"128     done | grep -q .; then129         die "$bundle staged a weight file"130     fi131     if find "$ctx" -type f -size +1M -print | while IFS= read -r file; do132         weight_ambiguous "$(basename "$file")" && printf '%s\n' "$file"133     done | grep -q .; then134         die "$bundle staged a weight file"135     fi136     if find "$ctx" -type f -size +1M -print | while IFS= read -r file; do137         weight_magic "$file" && printf '%s\n' "$file"138     done | grep -q .; then139         die "$bundle staged a weight-like file"140     fi141 142     source=$(context_tree)143     final_tag=inf3r/$bundle:sha-$(printf '%s' "$source" | cut -c1-12)144 }145 146 image_id() {147     docker image inspect --format '{{.Id}}' "$1" 2>/dev/null148 }149 150 image_label() {151     docker image inspect --format "{{index .Config.Labels \"$2\"}}" "$1" 2>/dev/null152 }153 154 check_source() {155     now=$(context_tree)156     [ "$now" = "$source" ] || die "$bundle source changed; rebuild and record it"157 }158 159 check_labels() {160     reference=$1161     for pair in \162         "org.inf3r.bundle.id=$bundle" \163         "org.inf3r.bundle.binary=$binary" \164         "org.inf3r.source.sha256=$source"165     do166         key=${pair%%=*}167         expected=${pair#*=}168         actual=$(image_label "$reference" "$key")169         [ "$actual" = "$expected" ] ||170             die "$reference has $key=$actual, expected $expected"171     done172 }173 174 record() {175     mkdir -p "$target"176     tmp=$lock.$$177     {178         printf '# bundle\tfinal_tag\tfinal_digest\tsource_sha256\n'179         if [ -f "$lock" ]; then180             awk -F '\t' -v b="$bundle" '181                 !/^#/ && NF == 4 && $1 != b { print }182                 !/^#/ && NF == 6 && $1 != b { print $1 "\t" $4 "\t" $5 "\t" $6 }183             ' "$lock"184         fi185         printf '%s\t%s\t%s\t%s\n' "$bundle" "$final_tag" "$final_digest" "$source"186     } >"$tmp"187     mv "$tmp" "$lock"188 }189 190 read_lock() {191     [ -f "$lock" ] || die "$bundle has no record in $lock; run build first"192     row=$(awk -F '\t' -v b="$bundle" '!/^#/ && $1 == b { print; exit }' "$lock")193     [ -n "$row" ] || die "$bundle has no record in $lock; run build first"194     fields=$(printf '%s\n' "$row" | awk -F '\t' '{print NF}')195     [ "$fields" -eq 4 ] || die "$bundle has a malformed record in $lock"196     locked_final_tag=$(printf '%s' "$row" | cut -f2)197     locked_final_digest=$(printf '%s' "$row" | cut -f3)198     locked_source=$(printf '%s' "$row" | cut -f4)199 }200 201 no_weights() {202     image=$1203     # The whole root filesystem, not only /opt/inf3r. A missing shell, find, or204     # Docker failure is an error: an unread image is not a clean image.205     #206     # Python and CUDA images legitimately contain small .pth, .bin, .npy, and207     # .pkl files in site-packages and OS data. Those extensions are only208     # suspicious above 1 MiB. The unambiguous weight formats stay zero209     # tolerance, and a bundle's own model/ directory is always zero tolerance.210     found=$(docker run --rm --pull=never --network none --read-only --tmpfs /tmp \211         --entrypoint /bin/sh "$image" -c \212         'set -eu213          find / -xdev -type d -path "$1" -print -quit214          find / -xdev -type f \215              \( -iname "*.gguf" -o -iname "*.safetensors" -o -iname "*.nemo" \216                 -o -iname "*.onnx" -o -iname "*.ckpt" -o -iname "*.tflite" \217                 -o -iname "*.trt" -o -iname "*.plan" -o -iname "*.pdparams" \218                 -o -iname "*.weights" \) -print -quit219          find / -xdev -type f -size +1M \220              \( -iname "*.bin" -o -iname "*.pt" -o -iname "*.pth" \221                 -o -iname "*.h5" -o -iname "*.hdf5" -o -iname "*.pkl" \222                 -o -iname "*.pickle" -o -iname "*.npy" -o -iname "*.npz" \223                 -o -iname "*.ot" -o -iname "*.msgpack" -o -iname "*.model" \) \224              -print -quit' sh "/opt/inf3r/plugins/$bundle/model" 2>/dev/null) ||225         die "$image weight scan failed (Docker, /bin/sh, or find)"226     [ -z "$found" ] || die "$image contains $found"227 }228 229 case "$command" in230     build)231         prepare232         docker build --pull=false -f "$recipe" \233             --label "org.inf3r.bundle.id=$bundle" \234             --label "org.inf3r.bundle.binary=$binary" \235             --label "org.inf3r.source.sha256=$source" \236             -t "$final_tag" "$ctx" >&2237         final_digest=$(image_id "$final_tag")238         [ -n "$final_digest" ] || die "the final image has no digest"239         check_labels "$final_tag"240         no_weights "$final_tag"241         # Compose names a stable local tag; the content tag above remains the242         # authority and the lock records its digest. This alias is never used243         # by a production deploy file and is not pulled.244         alias=inf3r/$bundle:local245         docker tag "$final_tag" "$alias"246         record247         printf 'image %s %s\n' "$final_tag" "$final_digest"248         printf 'alias %s\n' "$alias"249         printf 'lock %s\n' "$lock"250         ;;251     check)252         prepare253         read_lock254         check_source255         [ "$source" = "$locked_source" ] || die "the source digest does not match the lock"256         [ "$locked_final_tag" = "$final_tag" ] ||257             die "the final tag changed: $locked_final_tag -> $final_tag"258         [ "$(image_id "$locked_final_tag")" = "$locked_final_digest" ] ||259             die "the final image digest does not match the lock"260         check_labels "$locked_final_tag"261         no_weights "$locked_final_tag"262         printf 'check %s %s\n' "$bundle" "$locked_final_digest"263         ;;264     tag)265         read_lock266         printf '%s\n' "$locked_final_tag"267         ;;268     *)269         usage270         ;;271 esac