From 15561fd545329593b30b530d63a0e69533da585f Mon Sep 17 00:00:00 2001 From: Eric Hauser Date: Wed, 12 Jul 2023 14:44:43 -0600 Subject: [PATCH] feat: #180 make the tarball target runnable (#290) Co-authored-by: Alex Eagle --- docs/static_content.md | 4 +--- docs/tarball.md | 3 +-- e2e/wasm/BUILD.bazel | 3 +-- oci/private/BUILD.bazel | 1 + oci/private/tarball.bzl | 29 ++++++++++++++++++++++++----- oci/private/tarball_run.sh.tpl | 15 +++++++++++++++ 6 files changed, 43 insertions(+), 12 deletions(-) create mode 100644 oci/private/tarball_run.sh.tpl diff --git a/docs/static_content.md b/docs/static_content.md index 675ae537..ea0928a0 100644 --- a/docs/static_content.md +++ b/docs/static_content.md @@ -108,9 +108,7 @@ oci_image( ## Try running the container with docker ```bash -# File created by running bazel build //frontend:frontend_tarball -tarball_file="" -docker load --input "$tarball_file" +bazel run :frontend_tarball docker run --rm -p 8080:80 "ourfrontend:latest" ``` diff --git a/docs/tarball.md b/docs/tarball.md index ccecda1f..978f9fe0 100644 --- a/docs/tarball.md +++ b/docs/tarball.md @@ -15,8 +15,7 @@ oci_tarball( and then run it in a container like so: ``` -bazel build //path/to:tarball -docker load --input $(bazel cquery --output=files //path/to:tarball) +bazel run :tarball docker run --rm my-repository:latest ``` diff --git a/e2e/wasm/BUILD.bazel b/e2e/wasm/BUILD.bazel index 854b8976..2eaea954 100644 --- a/e2e/wasm/BUILD.bazel +++ b/e2e/wasm/BUILD.bazel @@ -46,8 +46,7 @@ build_test( # In order to run the image you need to follow instructions at https://docs.docker.com/desktop/wasm/ first. # then run the following; -# `bazel build :tarball` -# `docker load -i bazel-bin/tarball/tarball.tar` +# `bazel run :tarball`` # `docker run --runtime=io.containerd.wasmedge.v1 --platform=wasi/wasm32 --pull=never gcr.io/wasm:latest` oci_tarball( name = "tarball", diff --git a/oci/private/BUILD.bazel b/oci/private/BUILD.bazel index 44f6ddaf..3df83e5b 100644 --- a/oci/private/BUILD.bazel +++ b/oci/private/BUILD.bazel @@ -9,6 +9,7 @@ exports_files([ "image.sh.tpl", "image_index.sh.tpl", "tarball.sh.tpl", + "tarball_run.sh.tpl", "push.sh.tpl", ]) diff --git a/oci/private/tarball.bzl b/oci/private/tarball.bzl index ba9c6386..4c126077 100644 --- a/oci/private/tarball.bzl +++ b/oci/private/tarball.bzl @@ -13,8 +13,7 @@ oci_tarball( and then run it in a container like so: ``` -bazel build //path/to:tarball -docker load --input $(bazel cquery --output=files //path/to:tarball) +bazel run :tarball docker run --rm my-repository:latest ``` """ @@ -33,6 +32,13 @@ attrs = { allow_single_file = [".txt"], mandatory = True, ), + "_run_template": attr.label( + default = Label("//oci/private:tarball_run.sh.tpl"), + doc = """ \ + The template used to load the container. The default template uses Docker, but this template could be replaced to use podman, runc, or another runtime. Please reference the default template to see available substitutions. + """, + allow_single_file = True, + ), "_tarball_sh": attr.label(allow_single_file = True, default = "//oci/private:tarball.sh.tpl"), } @@ -41,6 +47,7 @@ def _tarball_impl(ctx): tarball = ctx.actions.declare_file("{}/tarball.tar".format(ctx.label.name)) yq_bin = ctx.toolchains["@aspect_bazel_lib//lib:yq_toolchain_type"].yqinfo.bin executable = ctx.actions.declare_file("{}/tarball.sh".format(ctx.label.name)) + repo_tags = ctx.file.repo_tags substitutions = { "{{yq}}": yq_bin.path, @@ -49,7 +56,7 @@ def _tarball_impl(ctx): } if ctx.attr.repo_tags: - substitutions["{{tags}}"] = ctx.file.repo_tags.path + substitutions["{{tags}}"] = repo_tags.path ctx.actions.expand_template( template = ctx.file._tarball_sh, @@ -60,15 +67,26 @@ def _tarball_impl(ctx): ctx.actions.run( executable = executable, - inputs = [image, ctx.file.repo_tags], + inputs = [image, repo_tags], outputs = [tarball], tools = [yq_bin], mnemonic = "OCITarball", progress_message = "OCI Tarball %{label}", ) + exe = ctx.actions.declare_file(ctx.label.name + ".sh") + + ctx.actions.expand_template( + template = ctx.file._run_template, + output = exe, + substitutions = { + "{{image_path}}": tarball.short_path, + }, + is_executable = True, + ) + return [ - DefaultInfo(files = depset([tarball])), + DefaultInfo(files = depset([tarball]), runfiles = ctx.runfiles(files = [tarball]), executable = exe), ] oci_tarball = rule( @@ -78,4 +96,5 @@ oci_tarball = rule( toolchains = [ "@aspect_bazel_lib//lib:yq_toolchain_type", ], + executable = True, ) diff --git a/oci/private/tarball_run.sh.tpl b/oci/private/tarball_run.sh.tpl new file mode 100644 index 00000000..1f64ecc6 --- /dev/null +++ b/oci/private/tarball_run.sh.tpl @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +set -o pipefail -o errexit -o nounset + +readonly IMAGE="{{image_path}}" +if 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." + exit 1 +fi + +"$CONTAINER_CLI" load --input "$IMAGE"