From e420a137665517145c584465e7750ec96ffd239e Mon Sep 17 00:00:00 2001 From: Josh Chorlton Date: Tue, 2 Jan 2024 00:10:02 +0000 Subject: [PATCH] feat: support passing label to load command (#441) Co-authored-by: Sahin Yort --- docs/tarball.md | 3 ++- oci/private/tarball.bzl | 15 ++++++++++++++- oci/private/tarball_run.sh.tpl | 6 ++++-- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/docs/tarball.md b/docs/tarball.md index 54609312..1f1d7dd0 100644 --- a/docs/tarball.md +++ b/docs/tarball.md @@ -25,7 +25,7 @@ docker run --rm my-repository:latest ## oci_tarball
-oci_tarball(name, format, image, repo_tags)
+oci_tarball(name, command, format, image, repo_tags)
 
Creates tarball from OCI layouts that can be loaded into docker daemon without needing to publish the image first. @@ -39,6 +39,7 @@ Passing anything other than oci_image to the image attribute will lead to build | Name | Description | Type | Mandatory | Default | | :------------- | :------------- | :------------- | :------------- | :------------- | | name | A unique name for this target. | Name | required | | +| command | target for a container cli tool (i.e. docker or podman or other) that will be used to load the image into the local engine when using 'bazel run //my/image'.", | Label | optional | None | | format | Format of image to generate. Options are: docker, oci. Currently, when the input image is an image_index, only oci is supported, and when the input image is an image, only docker is supported. Conversions between formats may be supported in the future. | String | optional | "docker" | | image | Label of a directory containing an OCI layout, typically oci_image | Label | required | | | repo_tags | a file containing repo_tags, one per line. | Label | required | | diff --git a/oci/private/tarball.bzl b/oci/private/tarball.bzl index d2b0118b..8e2313c0 100644 --- a/oci/private/tarball.bzl +++ b/oci/private/tarball.bzl @@ -39,6 +39,15 @@ attrs = { allow_single_file = [".txt"], mandatory = True, ), + "command": attr.label( + doc = """\ + target for a container cli tool (i.e. docker or podman or other) that will be used to load the image into the local engine when using 'bazel run //my/image'.", + """, + allow_single_file = True, + mandatory = False, + executable = True, + cfg = "target", + ), "_run_template": attr.label( default = Label("//oci/private:tarball_run.sh.tpl"), doc = """ \ @@ -90,12 +99,16 @@ def _tarball_impl(ctx): output = exe, substitutions = { "{{image_path}}": tarball.short_path, + "{{command}}": ctx.file.command.path if ctx.file.command else "", }, is_executable = True, ) + runfiles = [tarball] + if ctx.file.command: + runfiles.append(ctx.file.command) return [ - DefaultInfo(files = depset([tarball]), runfiles = ctx.runfiles(files = [tarball]), executable = exe), + DefaultInfo(files = depset([tarball]), runfiles = ctx.runfiles(files = runfiles), executable = exe), ] oci_tarball = rule( diff --git a/oci/private/tarball_run.sh.tpl b/oci/private/tarball_run.sh.tpl index 1f64ecc6..86445b69 100644 --- a/oci/private/tarball_run.sh.tpl +++ b/oci/private/tarball_run.sh.tpl @@ -2,13 +2,15 @@ set -o pipefail -o errexit -o nounset readonly IMAGE="{{image_path}}" -if command -v docker &> /dev/null; then +if [ -e "{{command}}" ]; then + CONTAINER_CLI="{{command}}" +elif command -v docker &> /dev/null; then CONTAINER_CLI="docker" elif command -v podman &> /dev/null; then CONTAINER_CLI="podman" else echo >&2 "Neither docker or podman could be found." - echo >&2 "If you wish to use another container runtime, please comment on https://github.com/bazel-contrib/rules_oci/issues/295." + echo >&2 "If you wish to use another container runtime, you can pass an executable to the 'command' attribute." exit 1 fi