233 lines
6.7 KiB
MoonBit
233 lines
6.7 KiB
MoonBit
// 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 `<dir>/files` and push it with the enriched Orbit config blob.
|
|
fn publish(
|
|
ch : Channel,
|
|
dir : String,
|
|
also_latest : Bool,
|
|
) -> Unit raise OrbitError {
|
|
let text = match read_text(dir + "/orbit.json") {
|
|
Some(t) => t
|
|
None => raise OrbitError("\{dir}/orbit.json not found")
|
|
}
|
|
let m = parse_manifest(text)
|
|
let config = package_config(text, m)
|
|
let files = dir + "/files"
|
|
if !exists(files) {
|
|
raise OrbitError("\{files} not found (package contents go in files/)")
|
|
}
|
|
for b in m.bins {
|
|
if sh("test -f " + sq(files + "/" + b)) != 0 {
|
|
raise OrbitError("bin '\{b}' listed in orbit.json is missing from files/")
|
|
}
|
|
}
|
|
let tmp = trim(capture("mktemp -d"))
|
|
sh_ok(
|
|
"tar -czf " +
|
|
sq(tmp + "/pkg.tar.gz") +
|
|
" --owner=0 --group=0 -C " +
|
|
sq(files) +
|
|
" .",
|
|
)
|
|
write_text_atomic(tmp + "/orbit.json", config)
|
|
let tags = if also_latest { m.version + ",latest" } else { m.version }
|
|
let push = oras(
|
|
ch,
|
|
"push " +
|
|
sq(repo_of(ch, m.name) + ":" + tags) +
|
|
" --config " +
|
|
sq("orbit.json:" + CONFIG_MEDIA_TYPE) +
|
|
" --annotation " +
|
|
sq("org.opencontainers.image.version=" + m.version) +
|
|
(if m.description != "" {
|
|
" --annotation " +
|
|
sq("org.opencontainers.image.description=" + m.description)
|
|
} else {
|
|
""
|
|
}) +
|
|
" " +
|
|
sq("pkg.tar.gz:" + LAYER_MEDIA_TYPE),
|
|
)
|
|
let rc = sh("cd " + sq(tmp) + " && " + push)
|
|
sh("rm -rf " + sq(tmp)) |> ignore
|
|
if rc != 0 {
|
|
raise OrbitError("oras push failed (exit \{rc})")
|
|
}
|
|
println("published \{m.name} \{m.version} to \{repo_of(ch, m.name)}")
|
|
}
|