okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
df20d7784
main
1#!/usr/bin/env python3 2"""Fetch the engine packages for the Docker images from a release, checked against the signed update feed. 3 4python3 docker/fetch-engines.py --release-tag v0.2.7 --flavors cuda13,cuda12,rocm-gfx120x --out ./pkgs 5python3 docker/fetch-engines.py --source /path/to/release-folder --flavors cuda13 --out ./pkgs 6python3 docker/fetch-engines.py --release-tag v0.2.7 --list 7 8What it does, in the same order and with the same code as `strata update`: 9 101. reads strata-feed.json and strata-feed.json.sig from the release (GitHub release assets, or a folder / URL given with --source) 11and checks the Ed25519 signature against the public key in strata_control/update_keys.json; 122. for each flavor picks the linux-x86_64 engine package the feed lists for it (see FLAVORS); 133. downloads the archive and checks its size and SHA-256 against the signed feed (a mismatch deletes the file and stops); 144. unpacks it (the archive's names and links are checked, its size may not exceed what the feed declares), checks that the 15manifest is the one the feed names and says what the feed says, then the package signature and every file's hash. 16 17Stdout gets one line per flavor, tab separated: `<flavor> <build.sh --flavor value> <package folder>`; everything else goes to 18stderr. Nothing is installed anywhere and nothing is pushed. Needs only the Python standard library and this repository. 19""" 20from __future__import annotations 21 22import argparse 23import shutil 24import sys 25from pathlib import Path 26 27REPO_ROOT = Path (__file__ ).resolve ().parent .parent 28sys .path .insert (0 ,str (REPO_ROOT )) 29 30from strata_control import engine_component as ec ,feed as feedmod ,signing ,transfer ,updater # noqa: E402 31 32#: image flavor (the tag suffix) -> (feed backend, feed variant or None for any, docker/build.sh --flavor) 33FLAVORS = { 34"cuda13" : ("cuda" ,"cuda13" ,"cuda" ), 35"cuda12" : ("cuda" ,"cuda12" ,"cuda" ), 36"rocm-gfx120x" : ("hip" ,"rx9070" ,"hip" ),# RX 9000, Radeon AI PRO R9700 (gfx1200 / gfx1201) 37"rocm-gfx110x" : ("hip" ,"rx7900" ,"hip" ),# RX 7000 (gfx1100 / gfx1101 / gfx1102) 38"rocm-gfx1151" : ("hip" ,"halo" ,"hip" ),# Strix Halo (gfx1151) 39"sycl" : ("sycl" ,None ,"sycl" ),# Intel Arc; only when the release has a Linux SYCL package 40} 41PLATFORM = "linux-x86_64" 42 43 44def log (message :str )-> None : 45message ,file = sys .stderr ,flush = True ) 46 47 48def pick (feed :feedmod .Feed ,flavor :str )-> feedmod .EnginePackage | None : 49backend ,variant ,_ = FLAVORS [flavor ] 50found = [p for p in feed .engine if p .platform == PLATFORM and p .backend == backend and (variant is None or p .variant == variant )] 51if not found : 52return None 53return max (found ,key = lambda p :feedmod .version_key (p .version )) 54 55 56def fetch_one (feed :feedmod .Feed ,pkg :feedmod .EnginePackage ,out :Path ,trusted :dict )-> Path : 57folder = out / updater .folder_name (pkg ) 58archive = out / ".partial" / pkg .url .rsplit ("/" ,1 )[- 1 ] 59if folder .is_dir ()and (folder / ec .MANIFEST ).is_file ()and transfer .sha256_file (folder / ec .MANIFEST )== pkg .manifest_sha256 : 60log (f" { folder . name } : already unpacked, manifest matches the feed" ) 61else : 62log (f" downloading { pkg . url } ( { pkg . size / ( 1 << 20 ):.0f } MiB)" ) 63try : 64transfer .download (feed .url (pkg .url ),archive ,size = pkg .size ,sha256 = pkg .sha256 ) 65except transfer .TransferError as exc : 66raise SystemExit (f"fetch-engines: { exc } " ) 67log (" size and SHA-256 match the signed feed; unpacking" ) 68try : 69updater .extract (archive ,folder ,pkg .format ,pkg .installed_bytes ) 70except updater .UpdateError as exc : 71raise SystemExit (f"fetch-engines: { exc } " ) 72finally : 73archive .unlink (missing_ok = True ) 74manifest = folder / ec .MANIFEST 75if transfer .sha256_file (manifest )!= pkg .manifest_sha256 : 76raise SystemExit (f"fetch-engines: { folder . name } has a manifest the signed feed does not name" ) 77try : 78component = ec .read_manifest (folder ) 79except ec .ComponentProblem as exc : 80raise SystemExit (f"fetch-engines: { folder . name } : the package manifest is invalid: { exc } " ) 81for name ,a ,b in (("version" ,component .version ,pkg .version ), ("platform" ,component .platform ,pkg .platform ), 82 ("backend" ,component .backend ,pkg .backend ), ("variant" ,component .variant ,pkg .variant ), 83 ("build_id" ,component .build_id ,pkg .build_id ), ("archs" ,set (component .archs ),set (pkg .archs ))): 84if a != b : 85raise SystemExit (f"fetch-engines: { folder . name } : the manifest says a different { name } than the signed feed" ) 86outer = ec ._verifier 87signing .install_component_verifier (trusted ) 88try : 89ec .verify_signature (component , {ec .ENV_REQUIRE_SIGNED :"1" }) 90ec .verify (component ,full = True ) 91except ec .ComponentProblem as exc : 92raise SystemExit (f"fetch-engines: { folder . name } failed its check: { exc } " ) 93finally : 94ec .register_signature_verifier (outer ) 95log (f" { folder . name } : signature and every file verified (build { pkg . build_id } )" ) 96return folder 97 98 99def main ()-> int : 100ap = argparse .ArgumentParser (description = __doc__ .split ("\n\n" )[0 ]) 101ap .add_argument ("--release-tag" ,help = "the GitHub release tag, for example v0.2.7" ) 102ap .add_argument ("--repo" ,default = "Niko1221/Strata" ,help = "owner/name of the GitHub repository (default %(default)s)" ) 103ap .add_argument ("--source" ,help = "a release folder or base URL holding strata-feed.json(.sig) and the archives (instead of --release-tag)" ) 104ap .add_argument ("--flavors" ,default = "," .join (FLAVORS ),help = "comma separated; default: all (%s)" % ", " .join (FLAVORS )) 105ap .add_argument ("--out" ,type = Path ,help = "where the unpacked package folders go" ) 106ap .add_argument ("--skip-missing" ,action = "store_true" ,help = "a flavor the feed has no package for is skipped (default: an error)" ) 107ap .add_argument ("--list" ,action = "store_true" ,help = "only print which package each flavor would use" ) 108a = ap .parse_args () 109if bool (a .release_tag )== bool (a .source ): 110ap .error ("give exactly one of --release-tag and --source" ) 111if not a .list and not a .out : 112ap .error ("--out is required" ) 113flavors = [f .strip ()for f in a .flavors .split ("," )if f .strip ()] 114unknown = [f for f in flavors if f not in FLAVORS ] 115if unknown : 116ap .error (f"unknown flavor { ', ' . join ( unknown ) } (known: { ', ' . join ( FLAVORS ) } )" ) 117source = a .source or f"https://github.com/ { a . repo } /releases/download/ { a . release_tag } " 118trusted = signing .load_trusted_keys () 119log (f"feed: { source } " ) 120try : 121feed = feedmod .fetch (source ,trusted ) 122except feedmod .FeedError as exc : 123raise SystemExit (f"fetch-engines: { exc } " ) 124log (f"feed signature OK (key { feed . key_id } ), channel { feed . channel } , sequence { feed . sequence } " ) 125status = 0 126for flavor in flavors : 127pkg = pick (feed ,flavor ) 128if pkg is None : 129log (f" { flavor } : the feed has no { PLATFORM } package for it" ) 130if not a .skip_missing : 131status = 3 132else : 133f" { flavor } \t { FLAVORS [ flavor ][ 2 ] } \tSKIP" ) 134continue 135log (f" { flavor } : { pkg . label } ( { pkg . url } )" ) 136if a .list : 137f" { flavor } \t { FLAVORS [ flavor ][ 2 ] } \t { pkg . url } \t { pkg . size } \t { pkg . sha256 } " ) 138continue 139a .out .mkdir (parents = True ,exist_ok = True ) 140folder = fetch_one (feed ,pkg ,a .out ,trusted ) 141f" { flavor } \t { FLAVORS [ flavor ][ 2 ] } \t { folder } " ,flush = True ) 142if a .out and (a .out / ".partial" ).is_dir (): 143shutil .rmtree (a .out / ".partial" ,ignore_errors = True ) 144return status 145 146 147if __name__ == "__main__" : 148sys .exit (main ())