// Everything that talks to the registry goes through the `oras` CLI. // Authentication is whatever `oras login` has stored. ///| fn oras(ch : Channel, args : String) -> String { "oras " + args + (if ch.plain_http { " --plain-http" } else { "" }) } ///| fn repo_of(ch : Channel, name : String) -> String { ch.url + "/" + name } ///| /// Whether oras output says the repository doesn't exist, as opposed to the /// registry being unreachable or refusing access. fn is_missing_repo(err : String) -> Bool { contains_str(err, "name unknown") || contains_str(err, "response status code 404") } ///| /// The tags of `name` in a channel, or None if the channel doesn't have it. /// Any other failure is an error: falling through to a lower priority channel /// could install a different package with the same name. fn list_tags(ch : Channel, name : String) -> Array[String]? raise OrbitError { let errfile = trim(capture("mktemp")) let cmd = oras(ch, "repo tags " + sq(repo_of(ch, name))) + " 2>" + sq(errfile) let out = Some(capture(cmd)) catch { _ => None } let err = match read_text(errfile) { Some(t) => trim(t) None => "" } sh("rm -f " + sq(errfile)) |> ignore match out { Some(o) => Some(lines(o)) None => if is_missing_repo(err) { None } else { raise OrbitError("channel '\{ch.name}' (\{ch.url}): \{err}") } } } ///| fn resolve_digest( ch : Channel, name : String, tag : String, ) -> String raise OrbitError { let d = trim( capture(oras(ch, "resolve " + sq(repo_of(ch, name) + ":" + tag))), ) if !(has_prefix(d, "sha256:") || has_prefix(d, "sha512:")) { raise OrbitError("unexpected digest for \{name}:\{tag}: '\{d}'") } d } ///| /// Fetch only the config blob (orbit.json); no layer download needed to plan. fn fetch_manifest( ch : Channel, name : String, digest : String, ) -> Manifest raise OrbitError { let text = capture( oras(ch, "manifest fetch-config " + sq(repo_of(ch, name) + "@" + digest)), ) let m = parse_manifest(text) if m.name != name { raise OrbitError( "\{name}@\{digest} declares name '\{m.name}'; refusing to install", ) } m } ///| /// Pull a package layer by digest and unpack it into the store. /// Unpacks into a temp dir first, then renames, so a store dir is either /// complete or absent. fn fetch_into_store( ctx : Ctx, ch : Channel, name : String, digest : String, ) -> String raise OrbitError { let dir = store_dir(ctx, name, digest) if exists(dir) { return dir } let tmp = ctx.root + "/tmp/" + name + "-" + store_id(digest) sh_ok("rm -rf " + sq(tmp) + " && mkdir -p " + sq(tmp + "/root")) sh_ok( oras(ch, "pull -o " + sq(tmp) + " " + sq(repo_of(ch, name) + "@" + digest)) + " >/dev/null", ) if !exists(tmp + "/pkg.tar.gz") { raise OrbitError("\{name}@\{digest} has no pkg.tar.gz layer") } sh_ok( "tar -xzf " + sq(tmp + "/pkg.tar.gz") + " -C " + sq(tmp + "/root") + " --no-same-owner", ) sh_ok( "mkdir -p " + sq(ctx.root + "/store/" + name) + " && mv " + sq(tmp + "/root") + " " + sq(dir) + " && rm -rf " + sq(tmp), ) dir } ///| /// Gitea reads package-page metadata from OCI image config labels, not /// manifest annotations. Keep the Orbit manifest at the top level for pulls. fn package_config(text : String, m : Manifest) -> String raise OrbitError { if m.description == "" && m.documentation == "" { return text } let config = parse_object(text, "orbit.json") let image = match config.get("config") { Some(Json::Object(image)) => image None => Map([]) _ => raise OrbitError("orbit.json: config must be an object") } let labels = match image.get("Labels") { Some(Json::Object(labels)) => labels None => Map([]) _ => raise OrbitError("orbit.json: config.Labels must be an object") } if m.description != "" { labels["org.opencontainers.image.description"] = Json::string(m.description) } if m.documentation != "" { labels["org.opencontainers.image.documentation"] = Json::string( m.documentation, ) } image["Labels"] = Json::object(labels) config["config"] = Json::object(image) Json::object(config).stringify(indent=2) + "\n" } ///| test "package config exposes Gitea labels and preserves Orbit metadata" { let text = #|{"name":"greet","version":"1.0.1","description":"A greeting","documentation":"https://example.com/greet","depends":{"hello":"^1"},"config":{"Labels":{"custom":"keep"}}} let published = package_config(text, parse_manifest(text)) let m = parse_manifest(published) assert_eq(m.name, "greet") assert_eq(m.depends.get("hello"), Some("^1")) let config = parse_object(published, "orbit.json") guard config.get("config") is Some(Json::Object(image)) else { fail("missing OCI config") } guard image.get("Labels") is Some(Json::Object(labels)) else { fail("missing OCI labels") } assert_eq(jstr(labels, "custom", ""), "keep") assert_eq(jstr(labels, "org.opencontainers.image.description", ""), "A greeting") assert_eq( jstr(labels, "org.opencontainers.image.documentation", ""), "https://example.com/greet", ) } ///| /// Pack `