Skip to content

Commit

Permalink
feat: #180 make the tarball target runnable (#290)
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Eagle <[email protected]>
  • Loading branch information
ewhauser and alexeagle authored Jul 12, 2023
1 parent d43a22e commit 15561fd
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 12 deletions.
4 changes: 1 addition & 3 deletions docs/static_content.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
```

Expand Down
3 changes: 1 addition & 2 deletions docs/tarball.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
3 changes: 1 addition & 2 deletions e2e/wasm/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions oci/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ exports_files([
"image.sh.tpl",
"image_index.sh.tpl",
"tarball.sh.tpl",
"tarball_run.sh.tpl",
"push.sh.tpl",
])

Expand Down
29 changes: 24 additions & 5 deletions oci/private/tarball.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
"""
Expand All @@ -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"),
}

Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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(
Expand All @@ -78,4 +96,5 @@ oci_tarball = rule(
toolchains = [
"@aspect_bazel_lib//lib:yq_toolchain_type",
],
executable = True,
)
15 changes: 15 additions & 0 deletions oci/private/tarball_run.sh.tpl
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 15561fd

Please sign in to comment.