Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove dependency on rules_pkg #63

Merged
merged 6 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ module(

# Lower-bound dependency versions.
# Do not change unless the rules no longer work with the current version.
bazel_dep(name = "bazel_skylib", version = "1.6.1")
bazel_dep(name = "aspect_bazel_lib", version = "2.5.3")
bazel_dep(name = "bazel_skylib", version = "1.5.0")
bazel_dep(name = "platforms", version = "0.0.8")
bazel_dep(name = "rules_oci", version = "1.7.4")
bazel_dep(name = "rules_pkg", version = "0.9.1")
bazel_dep(name = "rules_python", version = "0.29.0")

# Development dependencies which are not exposed to users
bazel_dep(name = "aspect_bazel_lib", version = "2.5.3", dev_dependency = True)
bazel_dep(name = "aspect_rules_py", version = "0.7.3", dev_dependency = True)
bazel_dep(name = "buildifier_prebuilt", version = "6.4.0", dev_dependency = True)
bazel_dep(name = "container_structure_test", version = "1.16.0", dev_dependency = True)
Expand Down
2 changes: 1 addition & 1 deletion aws/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ bzl_library(
name = "py_lambda",
srcs = [
"py_lambda.bzl",
"@rules_pkg//pkg:bzl_srcs",
"@aspect_bazel_lib//lib:tar",
],
visibility = ["//aws:__subpackages__"],
)
Expand Down
116 changes: 46 additions & 70 deletions aws/private/py_lambda.bzl
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
"Rule to produce tar files with py_binary deps and app"

# buildifier: disable=bzl-visibility
load(
"@rules_pkg//pkg/private:pkg_files.bzl",
"add_empty_file",
"add_single_file",
"write_manifest",
)
load("@aspect_bazel_lib//lib:tar.bzl", "tar")

# Write these two separate layers, so application changes are a small delta when pushing to a registry
_LAYERS = ["app", "deps"]

def _short_path(file_):
# Remove prefixes for external and generated files.
Expand All @@ -18,99 +15,78 @@ def _short_path(file_):
short_path = short_path[second_slash + 1:]
return short_path

# Copied from aspect-bazel-lib/lib/private/tar.bzl
def _mtree_line(file, type, content = None, uid = "0", gid = "0", time = "1672560000", mode = "0644"):
spec = [
file,
"uid=" + uid,
"gid=" + gid,
"time=" + time,
"mode=" + mode,
"type=" + type,
]
if content:
spec.append("content=" + content)
return " ".join(spec)

def _py_lambda_tar_impl(ctx):
deps = ctx.attr.target[DefaultInfo].default_runfiles.files
content_map = {} # content handled in the manifest
files = [] # Files needed by rule implementation at runtime
args = ctx.actions.args()
args.add("--output", ctx.outputs.output.path)

# NB: this creates one of the parent directories, but others are implicit
mtree = [_mtree_line(ctx.attr.prefix, type = "dir", mode = "0755")]

for dep in deps.to_list():
short_path = _short_path(dep)

if dep.owner.workspace_name == "" and ctx.attr.kind == "app":
add_single_file(
content_map,
ctx.attr.prefix + "/" + dep.short_path,
dep,
ctx.label,
)
mtree.append(_mtree_line(ctx.attr.prefix + "/" + dep.short_path, type = "file", content = dep.path))
elif short_path.startswith("site-packages") and ctx.attr.kind == "deps":
short_path = short_path[len("site-packages"):]
add_single_file(
content_map,
ctx.attr.prefix + short_path,
dep,
ctx.label,
)
mtree.append(_mtree_line(ctx.attr.prefix + short_path[len("site-packages"):], type = "file", content = dep.path))

if ctx.attr.kind == "app" and ctx.attr.init_files:
path = ""
for dir in ctx.attr.init_files.split("/"):
path = path + "/" + dir
add_empty_file(
content_map,
ctx.attr.prefix + path + "/__init__.py",
ctx.label,
)
mtree.append(_mtree_line(ctx.attr.prefix + path + "/__init__.py", type = "file"))

manifest_file = ctx.actions.declare_file(ctx.label.name + ".manifest")
files.append(manifest_file)
write_manifest(ctx, manifest_file, content_map)
args.add("--manifest", manifest_file.path)
args.add("--directory", "/")
args.set_param_file_format("flag_per_line")
args.use_param_file("@%s", use_always = False)
inputs = depset(direct = files, transitive = [deps])
ctx.actions.run(
outputs = [ctx.outputs.output],
inputs = inputs,
executable = ctx.executable._tar,
arguments = [args],
progress_message = "Creating archive...",
mnemonic = "PackageTar",
)
mtree.append("")
ctx.actions.write(ctx.outputs.output, "\n".join(mtree))

out = depset(direct = [ctx.outputs.output])
return [
DefaultInfo(files = out),
OutputGroupInfo(all_files = out),
]
return [DefaultInfo(files = out)]

_py_lambda_tar = rule(
implementation = _py_lambda_tar_impl,
attrs = {
"target": attr.label(
# require PyRuntimeInfo provider to be sure it's a py_binary ?
),
"_tar": attr.label(
default = Label("@rules_pkg//pkg/private/tar:build_tar"),
cfg = "exec",
executable = True,
),
"prefix": attr.string(doc = "path prefix for each entry in the tar"),
"init_files": attr.string(doc = "path where __init__ files will be placed"),
"kind": attr.string(values = ["app", "deps"]),
"kind": attr.string(values = _LAYERS),
"output": attr.output(),
},
)

def py_lambda_tars(name, target, prefix = "/var/task", init_files = "examples/python_lambda", **kwargs):
_py_lambda_tar(
def py_lambda_tars(name, target, prefix = "var/task", init_files = "examples/python_lambda", **kwargs):
for kind in _LAYERS:
_py_lambda_tar(
name = "_{}_{}_mf".format(name, kind),
kind = kind,
target = target,
prefix = prefix,
init_files = init_files,
output = "{}.{}.spec".format(name, kind),
**kwargs
)

tar(
name = name,
kind = "app",
target = target,
prefix = prefix,
init_files = init_files,
output = name + ".app.tar",
**kwargs
srcs = [target],
mtree = ":_{}_{}_mf".format(name, "app"),
)

_py_lambda_tar(
tar(
name = name + ".deps",
kind = "deps",
target = target,
prefix = prefix,
output = name + ".deps.tar",
**kwargs
srcs = [target],
mtree = ":_{}_{}_mf".format(name, "deps"),
)
17 changes: 4 additions & 13 deletions aws/private/toolchains_repo.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,11 @@ selects.config_setting_group(
)

alias(
name = "tar_bin",
name = "aws_cli_layer",
actual = select({{
"@platforms//os:macos": "@{repo_name}_darwin//installed:bin_files",
":linux_x86_64": "@{repo_name}_linux-x86_64//installed:bin_files",
":linux_arm64": "@{repo_name}_linux-aarch64//installed:bin_files",
}}),
)

alias(
name = "tar_files",
actual = select({{
"@platforms//os:macos": "@{repo_name}_darwin//installed:files",
":linux_x86_64": "@{repo_name}_linux-x86_64//installed:files",
":linux_arm64": "@{repo_name}_linux-aarch64//installed:files",
"@platforms//os:macos": "@{repo_name}_darwin//installed:aws_cli_layer",
":linux_x86_64": "@{repo_name}_linux-x86_64//installed:aws_cli_layer",
":linux_arm64": "@{repo_name}_linux-aarch64//installed:aws_cli_layer",
}}),
)

Expand Down
45 changes: 22 additions & 23 deletions aws/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,11 @@ def rules_aws_dependencies():
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.6.1/bazel-skylib-1.6.1.tar.gz",
],
)

http_archive(
name = "rules_pkg",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_pkg/releases/download/0.9.1/rules_pkg-0.9.1.tar.gz",
"https://github.com/bazelbuild/rules_pkg/releases/download/0.9.1/rules_pkg-0.9.1.tar.gz",
],
sha256 = "8f9ee2dc10c1ae514ee599a8b42ed99fa262b757058f65ad3c384289ff70c4b8",
name = "aspect_bazel_lib",
sha256 = "6c25c59581041ede31e117693047f972cc4700c89acf913658dc89d04c338f8d",
strip_prefix = "bazel-lib-2.5.3",
url = "https://github.com/aspect-build/bazel-lib/releases/download/v2.5.3/bazel-lib-v2.5.3.tar.gz",
)

########
Expand Down Expand Up @@ -127,27 +124,29 @@ alias(name = "aws", actual = "//installed:{}/aws", visibility = ["//visibility:p

rctx.file("installed/BUILD.bazel", """\
# Generated by aws/repositories.bzl
load("@rules_pkg//pkg:mappings.bzl", "pkg_attributes", "pkg_files")
load("@aspect_bazel_lib//lib:tar.bzl", "mtree_spec", "tar")

package(default_visibility=["//visibility:public"])

_BINS = ["{v}/aws*"]
_SRCS = glob(["{v}/**"])

pkg_files(
name = "bin_files",
strip_prefix = "{v}",
srcs = glob(_BINS),
attributes = pkg_attributes(
mode = "0755",
),
mtree_spec(
name = "mtree",
srcs = _SRCS,
)

pkg_files(
name = "files",
strip_prefix = "{v}",
srcs = glob(
["{v}/**"],
exclude = _BINS,
)
# "install" files from unpacked location
genrule(
name = "mutate",
srcs = [":mtree"],
outs = [":mutated"],
cmd = "sed 's#installed/{v}#usr/local/aws-cli#' <$< >$@",
)

tar(
name = "aws_cli_layer",
srcs = _SRCS,
mtree = ":mutated",
)
""".format(v = version_dir))

Expand Down
14 changes: 1 addition & 13 deletions examples/cli/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
load("@aspect_bazel_lib//lib:testing.bzl", "assert_contains")
load("@aspect_bazel_lib//lib:transitions.bzl", "platform_transition_filegroup")
load("@rules_oci//oci:defs.bzl", "oci_image", "oci_tarball")
load("@rules_pkg//:pkg.bzl", "pkg_tar")

# Demonstrates that you can use the CLI in a genrule.
# The toolchain provides a "Make variable" named AWS_CLI_BIN that you can expand.
Expand All @@ -20,22 +19,11 @@ assert_contains(
)

# Demonstrate that the CLI can be included in a container image
pkg_tar(
name = "aws_cli_layer",
srcs = [
"@aws_toolchains//:tar_bin",
"@aws_toolchains//:tar_files",
],
include_runfiles = True,
package_dir = "/usr/local/aws-cli",
strip_prefix = ".",
)

oci_image(
name = "image",
base = "@ubuntu",
entrypoint = ["/usr/local/aws-cli/aws"],
tars = [":aws_cli_layer"],
tars = ["@aws_toolchains//:aws_cli_layer"],
)

platform(
Expand Down