|
| 1 | +""" oci_image_layer """ |
| 2 | + |
| 3 | +load("//oci:providers.bzl", "OCIDescriptor") |
| 4 | + |
| 5 | +def oci_image_layer( |
| 6 | + *, |
| 7 | + name, |
| 8 | + directory = "/", # str |
| 9 | + file_map = None, # dict[label, str] |
| 10 | + files = None, # list[label] |
| 11 | + symlinks = None, # dict[str, str] |
| 12 | + **kwargs): |
| 13 | + # -> None |
| 14 | + """ oci_image_layer |
| 15 | +
|
| 16 | + Args: |
| 17 | + name: A unique name for this rule |
| 18 | + directory: Directory in the tarball to place the `files` |
| 19 | + file_map: Dictionary of file -> file location in tarball |
| 20 | + files: List of files to include under `directory` |
| 21 | + symlinks: Dictionary of symlink -> target entries to place in the tarball |
| 22 | + **kwargs: Additional arguments to pass to the rule, e.g. `tags` or `visibility` |
| 23 | + """ |
| 24 | + file_map = file_map or {} |
| 25 | + files = files or [] |
| 26 | + symlinks = symlinks or {} |
| 27 | + |
| 28 | + if len(files) == 0 and len(file_map) == 0: |
| 29 | + fail("At least one of `files` or `file_map` must be provided") |
| 30 | + |
| 31 | + _oci_image_layer( |
| 32 | + name = name, |
| 33 | + directory = directory, |
| 34 | + file_map = file_map, |
| 35 | + files = files, |
| 36 | + symlinks = symlinks, |
| 37 | + **kwargs |
| 38 | + ) |
| 39 | + |
| 40 | +def _impl(ctx): |
| 41 | + toolchain = ctx.toolchains["//oci:toolchain"] |
| 42 | + |
| 43 | + descriptor_file = ctx.actions.declare_file("{}.descriptor.json".format(ctx.label.name)) |
| 44 | + |
| 45 | + ctx.actions.run( |
| 46 | + executable = toolchain.sdk.ocitool, |
| 47 | + arguments = [ |
| 48 | + "create-layer", |
| 49 | + "--out={}".format(ctx.outputs.layer.path), |
| 50 | + "--outd={}".format(descriptor_file.path), |
| 51 | + "--dir={}".format(ctx.attr.directory), |
| 52 | + "--bazel-label={}".format(ctx.label), |
| 53 | + ] + |
| 54 | + ["--file={}".format(f.path) for f in ctx.files.files] + |
| 55 | + ["--symlink={}={}".format(k, v) for k, v in ctx.attr.symlinks.items()] + |
| 56 | + ["--file-map={}={}".format(k.files.to_list()[0].path, v) for k, v in ctx.attr.file_map.items()], |
| 57 | + inputs = ctx.files.files + ctx.files.file_map, |
| 58 | + outputs = [ |
| 59 | + descriptor_file, |
| 60 | + ctx.outputs.layer, |
| 61 | + ], |
| 62 | + ) |
| 63 | + |
| 64 | + return [ |
| 65 | + DefaultInfo( |
| 66 | + files = depset([ctx.outputs.layer, descriptor_file]), |
| 67 | + ), |
| 68 | + OCIDescriptor( |
| 69 | + descriptor_file = descriptor_file, |
| 70 | + file = ctx.outputs.layer, |
| 71 | + ), |
| 72 | + ] |
| 73 | + |
| 74 | +_oci_image_layer = rule( |
| 75 | + implementation = _impl, |
| 76 | + attrs = { |
| 77 | + "files": attr.label_list(allow_files = True), |
| 78 | + "directory": attr.string(), |
| 79 | + "symlinks": attr.string_dict(), |
| 80 | + "file_map": attr.label_keyed_string_dict(allow_files = True), |
| 81 | + }, |
| 82 | + toolchains = ["//oci:toolchain"], |
| 83 | + outputs = { |
| 84 | + "layer": "%{name}-layer.tar.gz", |
| 85 | + }, |
| 86 | +) |
0 commit comments