Skip to content

Commit

Permalink
feat: implement oci_index
Browse files Browse the repository at this point in the history
  • Loading branch information
thesayyn committed Sep 25, 2022
1 parent 6e6c983 commit ea46991
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 1 deletion.
2 changes: 2 additions & 0 deletions oci/defs.bzl
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
load("//oci/private:tarball.bzl", _oci_tarball = "oci_tarball")
load("//oci/private:image.bzl", _oci_image = "oci_image")
load("//oci/private:index.bzl", _oci_index = "oci_index")
load("//oci/private:structure_test.bzl", _structure_test = "structure_test")

oci_tarball = _oci_tarball
oci_image = _oci_image
oci_index = _oci_index
structure_test = _structure_test
2 changes: 1 addition & 1 deletion oci/private/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

exports_files(["image.sh.tpl", "tarball.sh.tpl"])
exports_files(["image.sh.tpl", "index.sh.tpl", "tarball.sh.tpl"])

filegroup(
name = "package_content",
Expand Down
42 changes: 42 additions & 0 deletions oci/private/index.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

def _index_impl(ctx):
jq = ctx.toolchains["@aspect_bazel_lib//lib:jq_toolchain_type"]

launcher = ctx.actions.declare_file("index_%s.sh" % ctx.label.name)
ctx.actions.expand_template(
template = ctx.file._index_sh_tpl,
output = launcher,
is_executable = True,
substitutions = {
"{jq_path}": jq.jqinfo.bin.path,
}
)

output = ctx.actions.declare_directory(ctx.label.name)

args = ctx.actions.args()
args.add(output.path)
args.add_all(ctx.files.images, expand_directories = False)

ctx.actions.run(
inputs = ctx.files.images,
arguments = [args],
outputs = [output],
executable = launcher,
tools = [jq.jqinfo.bin],
progress_message = "OCI Index %{label}",
)

return DefaultInfo(files = depset([output]))


oci_index = rule(
implementation = _index_impl,
attrs = {
"images": attr.label_list(mandatory = True, doc = "TODO"),
"_index_sh_tpl": attr.label(default = "index.sh.tpl", allow_single_file = True),
},
toolchains = [
"@aspect_bazel_lib//lib:jq_toolchain_type"
]
)
51 changes: 51 additions & 0 deletions oci/private/index.sh.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env bash
set -o pipefail -o errexit -o nounset

JQ="{jq_path}"
INDEX="$1"
shift

mkdir -p $INDEX
$JQ -n '{imageLayoutVersion: "1.0.0"}' > "$INDEX/oci-layout"

MANIFEST_LIST=$(mktemp)
$JQ -n '{"schemaVersion": 2, mediaType: "application/vnd.oci.image.index.v1+json", manifests: []}' > $MANIFEST_LIST


for IMAGE in $@; do
if [ ! -d "$IMAGE" ]; then
echo "$IMAGE is not a directory."
exit 1
fi
if [ ! -f "$IMAGE/oci-layout" ]; then
echo "$IMAGE/oci-layout is not a valid oci-layout."
exit 1
fi

for P in $(cd "$IMAGE/blobs" && find . -maxdepth 2); do
if [ -d "$IMAGE/blobs/$P" ]; then
mkdir -p "$INDEX/blobs/$P"
fi
# TODO: find out why cp -r fails.
if [ -f "$IMAGE/blobs/$P" ]; then
cat "$IMAGE/blobs/$P" > "$INDEX/blobs/$P"
fi
done

for MANIFEST in $($JQ -c '.manifests[]' "$IMAGE/index.json"); do
MANIFEST_BLOB=$($JQ -r '.digest | sub(":"; "/")' <<< $MANIFEST)
CONFIG_BLOB=$($JQ -r '.config.digest | sub(":"; "/")' "$IMAGE/blobs/$MANIFEST_BLOB")

PLATFORM=$($JQ -c '{os: .os, architecture: .architecture, variant: .variant, "os.version": .["os.version"], "os.features": .["os.features"]} | with_entries(select( .value != null ))' < "$IMAGE/blobs/$CONFIG_BLOB")

TMP=$(mktemp)
$JQ --argjson platform "$PLATFORM" --argjson manifest "$MANIFEST" '.manifests += [$manifest + {platform: $platform}] | .' $MANIFEST_LIST > $TMP
MANIFEST_LIST=$TMP
done

done

SHA=$(shasum -a 256 $MANIFEST_LIST | cut -f 1 -d " ")
SIZE=$(wc -c < $MANIFEST_LIST)
mv $MANIFEST_LIST "$INDEX/blobs/sha256/$SHA"
$JQ -n --arg size $SIZE --arg sha $SHA '{schemaVersion: 2, manifests: [{mediaType: "application/vnd.oci.image.index.v1+json", size: ($size | tonumber), digest: ("sha256:" + $sha)}]}' > "$INDEX/index.json"

0 comments on commit ea46991

Please sign in to comment.