Skip to content

Commit

Permalink
feat: implement flatten rule (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
thesayyn authored Dec 9, 2023
1 parent 813ac09 commit a268611
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 10 deletions.
5 changes: 2 additions & 3 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ module(
)

bazel_dep(name = "bazel_skylib", version = "1.5.0")
bazel_dep(name = "aspect_bazel_lib", version = "2.0.0-rc1")
bazel_dep(name = "aspect_bazel_lib", version = "2.0.3")

bazel_lib_toolchains = use_extension("@aspect_bazel_lib//lib:extensions.bzl", "toolchains")

# bazel_lib_toolchains.tar()
bazel_lib_toolchains.tar()
use_repo(bazel_lib_toolchains, "bsd_tar_toolchains")

bazel_dep(name = "gazelle", version = "0.34.0", dev_dependency = True, repo_name = "bazel_gazelle")
Expand Down
1 change: 1 addition & 0 deletions distroless/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ bzl_library(
visibility = ["//visibility:public"],
deps = [
"//distroless/private:cacerts",
"//distroless/private:flatten",
"//distroless/private:group",
"//distroless/private:home",
"//distroless/private:java_keystore",
Expand Down
2 changes: 2 additions & 0 deletions distroless/defs.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"Public API re-exports"

load("//distroless/private:cacerts.bzl", _cacerts = "cacerts")
load("//distroless/private:flatten.bzl", _flatten = "flatten")
load("//distroless/private:group.bzl", _group = "group")
load("//distroless/private:home.bzl", _home = "home")
load("//distroless/private:java_keystore.bzl", _java_keystore = "java_keystore")
Expand All @@ -15,3 +16,4 @@ group = _group
passwd = _passwd
java_keystore = _java_keystore
home = _home
flatten = _flatten
8 changes: 8 additions & 0 deletions distroless/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,16 @@ bzl_library(
],
)

bzl_library(
name = "flatten",
srcs = ["flatten.bzl"],
visibility = ["//distroless:__subpackages__"],
deps = [":tar"],
)

bzl_library(
name = "tar",
srcs = ["tar.bzl"],
visibility = ["//distroless:__subpackages__"],
deps = ["@aspect_bazel_lib//lib:tar"],
)
47 changes: 47 additions & 0 deletions distroless/private/flatten.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"flatten"

load(":tar.bzl", "tar_lib")

_DOC = """Flatten multiple archives into single archive."""

def _flatten_impl(ctx):
bsdtar = ctx.toolchains[tar_lib.TOOLCHAIN_TYPE]

ext = tar_lib.common.compression_to_extension[ctx.attr.compression] if ctx.attr.compression else ".tar"
output = ctx.actions.declare_file(ctx.attr.name + ext)

args = ctx.actions.args()
args.add("--create")
tar_lib.common.add_compression_args(ctx.attr.compression, args)
args.add("--file", output)
args.add_all(ctx.files.tars, format_each = "@%s")

ctx.actions.run(
executable = bsdtar.tarinfo.binary,
inputs = ctx.files.tars,
outputs = [output],
tools = bsdtar.default.files,
arguments = [args],
)

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

flatten = rule(
doc = _DOC,
attrs = {
"tars": attr.label_list(
allow_files = tar_lib.common.accepted_tar_extensions,
mandatory = True,
allow_empty = False,
doc = "List of tars to flatten",
),
"compression": attr.string(
doc = "Compress the archive file with a supported algorithm.",
values = tar_lib.common.accepted_compression_types,
),
},
implementation = _flatten_impl,
toolchains = [tar_lib.TOOLCHAIN_TYPE],
)
2 changes: 1 addition & 1 deletion distroless/private/home.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def home(name, dirs, **kwargs):

for home in dirs:
mtree.extend(
tar_lib.add_directory_with_parents(home["home"], uid = str(home["uid"]), gid = str(home["gid"])),
tar_lib.mtree.add_directory_with_parents(home["home"], uid = str(home["uid"]), gid = str(home["gid"])),
)

tar(
Expand Down
15 changes: 9 additions & 6 deletions distroless/private/tar.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"mtree helpers"

BSDTAR_TOOLCHAIN = "@aspect_bazel_lib//lib:tar_toolchain_type"
load("@aspect_bazel_lib//lib:tar.bzl", tar = "tar_lib")

DEFAULT_GID = "0"
DEFAULT_UID = "0"
Expand Down Expand Up @@ -50,7 +50,7 @@ def _add_directory_with_parents(path, **kwargs):
return lines

def _build_tar(ctx, mtree, output, inputs = [], compression = "gzip", mnemonic = "Tar"):
bsdtar = ctx.toolchains[BSDTAR_TOOLCHAIN]
bsdtar = ctx.toolchains[tar.toolchain_type]

inputs = inputs[:]
inputs.append(mtree)
Expand Down Expand Up @@ -88,9 +88,12 @@ def _create_mtree(ctx):
)

tar_lib = struct(
TOOLCHAIN_TYPE = tar.toolchain_type,
create_mtree = _create_mtree,
line = _mtree_line,
add_directory_with_parents = _add_directory_with_parents,
add_file_with_parents = _add_file_with_parents,
TOOLCHAIN_TYPE = BSDTAR_TOOLCHAIN,
mtree = struct(
line = _mtree_line,
add_directory_with_parents = _add_directory_with_parents,
add_file_with_parents = _add_file_with_parents,
),
common = tar.common,
)
20 changes: 20 additions & 0 deletions docs/rules.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 66 additions & 0 deletions examples/flatten/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
load("@aspect_bazel_lib//lib:tar.bzl", "tar")
load("//distroless:defs.bzl", "flatten", "home", "passwd")
load("//distroless/tests:asserts.bzl", "assert_tar_listing")

passwd(
name = "passwd",
passwds = [
{
"gecos": ["root"],
"gid": 0,
"home": "root",
"shell": "usr/bin/bash",
"uid": 0,
"username": "root",
},
],
)

home(
name = "home",
dirs = [
{
"home": "root",
"uid": 0,
"gid": 0,
},
{
"home": "home/nonroot",
"uid": 666,
"gid": 666,
},
],
)

tar(
name = "source",
srcs = glob(["dir/**/*"]),
compress = "xz",
)

flatten(
name = "flatten",
tars = [
":passwd",
":home",
":source",
],
)

assert_tar_listing(
name = "test_flatten",
actual = "flatten",
expected = """\
#mtree
./etc/passwd nlink=0 time=0.0 mode=700 gid=0 uid=0 type=file size=33 cksum=3891093834 sha1digest=94f013494b98f8ed618ce2e670d405f818ec3915
./examples time=1672560000.0 mode=755 gid=0 uid=0 type=dir
./examples/flatten time=1672560000.0 mode=755 gid=0 uid=0 type=dir
./examples/flatten/dir time=1672560000.0 mode=755 gid=0 uid=0 type=dir
./examples/flatten/dir/changelog nlink=0 time=1672560000.0 mode=755 gid=0 uid=0 type=file size=0 cksum=4294967295 sha1digest=da39a3ee5e6b4b0d3255bfef95601890afd80709
./examples/flatten/dir/sub time=1672560000.0 mode=755 gid=0 uid=0 type=dir
./examples/flatten/dir/sub/content.txt nlink=0 time=1672560000.0 mode=755 gid=0 uid=0 type=file size=0 cksum=4294967295 sha1digest=da39a3ee5e6b4b0d3255bfef95601890afd80709
./home time=0.0 mode=755 gid=0 uid=0 type=dir
./home/nonroot time=0.0 mode=755 gid=666 uid=666 type=dir
./root time=0.0 mode=755 gid=0 uid=0 type=dir
""",
)
Empty file added examples/flatten/dir/changelog
Empty file.
Empty file.

0 comments on commit a268611

Please sign in to comment.