]> go.fuhry.dev Git - runtime.git/commitdiff
[bazel] rewrite nfpm_package as a bazel rule main
authorDan Fuhry <dan@fuhry.com>
Fri, 6 Nov 2026 02:46:43 +0000 (21:46 -0500)
committerDan Fuhry <dan@fuhry.com>
Fri, 6 Nov 2026 02:46:43 +0000 (21:46 -0500)
- new rule implementation
- macro to create packages for multiple distros/architectures and a filegroup containing all of them
- update existing nfpm_package references

bazel/pkg.bzl
cmd/apcups_exporter/BUILD.bazel
cmd/http_proxy/BUILD.bazel
cmd/mtls_supervisor/BUILD.bazel
pkg/BUILD.bazel

index eda01218eb8056d9a114ab59e479f558b46071c7..050e53edc62dff70bd770b8d9f41dee631efc3b7 100644 (file)
@@ -1,17 +1,7 @@
 load("@bazel_skylib//lib:paths.bzl", "paths")
 
-def _nfpm_package(
-    package_name,
-    config,
-    exes,
-    data,
-    packager,
-    os,
-    arch,
-):
-    extension_map = {
-        "archlinux": "pkg.tar.zst",
-    }
+def _nfpm_config_impl(ctx):
+    # ctx.file.template
     distro_vars = {
         "archlinux": {
             "ENV_DIR": "/etc/conf.d",
@@ -23,74 +13,220 @@ def _nfpm_package(
             "ENV_DIR": "/etc/default",
         },
     }
-    extra_args = []
-    _exes = []
-    exe_suffix = ""
-    if os != "linux" or arch != "amd64":
-        exe_suffix = "_{}_{}".format(os, arch)
-        extra_args.append("--platform={}".format(os))
-        extra_args.append("--arch={}".format(arch))
-
-    for exe in exes:
-        if ":" not in exe:
-            exe += ":" + paths.basename(exe)
-        exe += exe_suffix
-        _exes.append(exe)
-
-    pkg_prefix = "{}_{}".format(package_name.replace("-", "_"), packager)
-    if arch != "amd64":
-        pkg_prefix += "_{}".format(arch)
-    for target in data + _exes:
-        extra_args.append('--include="$(location {})"'.format(target))
-
-    for var, val in distro_vars.get(packager, {}).items():
-        extra_args.append('--var="{}={}"'.format(var, val))
-
-    native.genrule(
-        name = "{}_nfpm_config".format(pkg_prefix),
-        tools = [
-            "//pkg/nfpmgen",
-        ],
-        srcs = [config] + data + _exes,
-        outs = [
-            "{}_nfpm_config.yaml".format(pkg_prefix),
-        ],
-        cmd_bash = """
-        $(location //pkg/nfpmgen) --in="$(location {})" --out="$@" {}
-        """.format(config, " ".join(extra_args)),
+
+    out_filename = ctx.file.template.basename[:-5] + "_{}_{}_{}.yaml".format(
+        ctx.attr.packager,
+        ctx.attr.os,
+        ctx.attr.arch,
+    )
+    outf = ctx.actions.declare_file(out_filename)
+
+    transitive_deps = []
+    for target in ctx.attr.data:
+        transitive_deps.append(depset(target.files.to_list()))
+
+    nfpmgen_args = [
+        "--verbose",
+        "--in", ctx.file.template.path,
+        "--out", outf.path,
+    ]
+    if ctx.attr.os != "linux" or ctx.attr.arch != "amd64":
+        nfpmgen_args.extend(["--platform", ctx.attr.os])
+        nfpmgen_args.extend(["--arch", ctx.attr.arch])
+    for dat in ctx.attr.data:
+        for f in dat.files.to_list():
+            nfpmgen_args.extend(["--include", f.path])
+
+    for var, val in distro_vars.get(ctx.attr.packager, {}).items():
+        nfpmgen_args.extend(['--var', '{}={}'.format(var, val)])
+
+    deps = depset([ctx.file.template], transitive = transitive_deps)
+
+    ctx.actions.run(
+        inputs = deps,
+        outputs = [outf],
+        executable = ctx.executable.nfpmgen,
+        arguments = nfpmgen_args,
+    )
+
+    return [
+        DefaultInfo(files = depset([outf]))
+    ]
+
+_nfpm_config = rule(
+    implementation = _nfpm_config_impl,
+    attrs = {
+        "template": attr.label(
+            doc = "base nfpm config",
+            allow_single_file = [".yaml"],
+            mandatory = True,
+        ),
+        "data": attr.label_list(
+            doc = "executables and data files to include in the package",
+            cfg = "target",
+            mandatory = False,
+        ),
+        "os": attr.string(
+            doc = "operating system (usually 'linux')",
+            default = "linux",
+        ),
+        "arch": attr.string(
+            doc = "architecture (usually 'amd64')",
+            default = "amd64",
+        ),
+        "packager": attr.string(
+            doc = "type of the package (deb, rpm, apk, archlinux)",
+        ),
+        "nfpmgen": attr.label(
+            doc = "nfpmgen executable (usually //pkg/nfpmgen)",
+            executable = True,
+            cfg = "exec",
+            default = Label("//pkg/nfpmgen:nfpmgen"),
+        ),
+    },
+)
+
+def _nfpm_package_impl(ctx):
+    pkg_ext = {
+        "archlinux": "pkg.tar.zst",
+    }.get(ctx.attr.packager, ctx.attr.packager)
+
+    name_arch_separator = {
+        "archlinux": "-",
+        "rpm": "-",
+        "deb": "_",
+    }.get(ctx.attr.packager)
+
+    pkg_arch = {
+        "archlinux": {
+            "amd64": "x86_64",
+            "arm64": "aarch64",
+        },
+        "rpm": {
+            "arm64": "aarch64",
+        },
+    }.get(ctx.attr.packager, {}).get(ctx.attr.arch, ctx.attr.arch)
+
+    output_filename = "{}{}{}.{}".format(
+        ctx.attr.package_name,
+        name_arch_separator,
+        pkg_arch,
+        pkg_ext,
     )
 
-    native.genrule(
-        name = "{}_nfpm_pkg".format(pkg_prefix),
-        tools = [
-            "@com_github_goreleaser_nfpm_v2//cmd/nfpm:nfpm",
-        ],
-        srcs = [
-            ":{}_nfpm_config".format(pkg_prefix),
-        ] + data + _exes,
-        outs = [
-            "{}_{}.{}".format(package_name, arch, extension_map.get(packager, packager)),
-        ],
-        cmd = " ".join([
-            "$(location @com_github_goreleaser_nfpm_v2//cmd/nfpm:nfpm)",
-            "package",
-            "-p {}".format(packager),
-            "-f $(location :{}_nfpm_config)".format(pkg_prefix),
-            "-t $@",
-        ]),
+    output = ctx.actions.declare_file(output_filename)
+
+    nfpmgen_args = [
+        "package",
+        "-p", ctx.attr.packager,
+        "-f", ctx.file.config.path,
+        "-t", output.path,
+    ]
+
+    deps = depset([ctx.file.config], transitive = [])
+
+    ctx.actions.run(
+        inputs = deps,
+        outputs = [output],
+        executable = ctx.executable.nfpm,
+        arguments = nfpmgen_args,
     )
 
+    return [
+        DefaultInfo(files = depset([output]))
+    ]
+
+
+_nfpm_package = rule(
+    implementation = _nfpm_package_impl,
+    attrs = {
+        "package_name": attr.string(
+            mandatory = True,
+        ),
+        "config": attr.label(
+            doc = "NFPM generated config",
+            mandatory = True,
+            allow_single_file = True,
+        ),
+        "os": attr.string(
+            doc = "operating system (usually 'linux')",
+            mandatory = True,
+        ),
+        "arch": attr.string(
+            doc = "architecture (usually 'amd64')",
+            mandatory = True,
+        ),
+        "packager": attr.string(
+            doc = "type of the package (deb, rpm, apk, archlinux)",
+            mandatory = True,
+        ),
+        "nfpm": attr.label(
+            doc = "nfpm executable",
+            executable = True,
+            cfg = "exec",
+            default = Label("@com_github_goreleaser_nfpm_v2//cmd/nfpm:nfpm"),
+        ),
+    },
+)
+
 def nfpm_package(
-    name,  # type: str
     config,  # type: str
     exes = [],  # type: list[str]
     data = [],  # type: list[str]
     linux_arm64 = False,  # type: bool
 ):
-    #for packager in ["deb", "archlinux", "rpm"]:
-    for packager in ["archlinux", "apk", "deb"]:
-        _nfpm_package(name, config, exes, data, packager, "linux", "amd64")
+    package_name = config.removesuffix(".yaml")
+    if ":" in package_name:
+        package_name = package_name[package_name.index(":")+1:]
+
+    # ensure all exe targets are fully qualified label names
+    exes = [
+        exe if ":" in exe
+        else "{}:{}".format(exe, paths.basename(exe))
+        for exe in exes
+    ]
 
-        if linux_arm64:
-            _nfpm_package(name, config, exes, data, packager, "linux", "arm64")
-            _nfpm_package(name, config, exes, data, packager, "linux", "arm")
\ No newline at end of file
+    os_arch_pairs = [("linux", "amd64")]
+
+    if linux_arm64:
+        os_arch_pairs.append(("linux", "arm64"))
+
+    package_rules = []
+
+    for os, arch in os_arch_pairs:
+        for packager in ["archlinux", "deb", "rpm"]:
+            config_rule_name = "{}_{}_{}_{}_nfpm_config".format(package_name, packager, os, arch)
+            package_rule_name = "{}_{}_{}_{}_nfpm_pkg".format(package_name, packager, os, arch)
+
+            arch_exes = (
+                exes if os == "linux" and arch == "amd64"
+                else [
+                    "{}_{}_{}".format(exe, os, arch)
+                    for exe in exes
+                ]
+            )
+
+            _nfpm_config(
+                name = config_rule_name,
+                template = config,
+                data = arch_exes + data,
+                packager = packager,
+                os = os,
+                arch = arch,
+            )
+
+            _nfpm_package(
+                name = package_rule_name,
+                package_name = config[config.index(":")+1:-5],
+                config = ":{}".format(config_rule_name),
+                os = os,
+                arch = arch,
+                packager = packager,
+            )
+
+            package_rules.append(':{}'.format(package_rule_name))
+
+    native.filegroup(
+        name = package_name,
+        srcs = package_rules
+    )
index 1f99536e6b9439662fba41132051c950c297d83b..875e57ee714f099b65d38d6fba0d1caf6eb8cfa4 100644 (file)
@@ -1,6 +1,9 @@
+load("//bazel:cross.bzl", "go_cross_binaries")
 load("//bazel:go.bzl", "go_multi_binary", "go_multi_library")
 load("//bazel:svc.bzl", "systemd_service")
 
+package(default_visibility = ["//visibility:public"])
+
 go_multi_library(
     name = "apcups_exporter_lib",
     srcs = ["main.go"],
@@ -21,6 +24,11 @@ go_multi_binary(
     visibility = ["//visibility:public"],
 )
 
+go_cross_binaries(
+    base_target = "apcups_exporter",
+    linux_arm = True,
+)
+
 systemd_service(
     name = "apcupsd-exporter",
     description = "apcupsd-exporter polls APC uninterruptible power supplies (UPSes) via apcupsd and exports metrics via metric-collector",
index 221e765b20140eb88d714aaf413525b8f080e762..15ed006760f4bebee9e52dd5dc6a66b56b1d413f 100644 (file)
@@ -3,7 +3,6 @@ load("//bazel:go.bzl", "go_multi_binary", "go_multi_library")
 load("//bazel:sqfs.bzl", "sqfs_package")
 load("//bazel:svc.bzl", "systemd_service")
 
-
 package(default_visibility = ["//visibility:public"])
 
 go_multi_library(
index e209e91d544c089a886bd6e3435d5ee08a74aab8..a00f7749acf55aa65da5f70fdd7e93ffb1210499 100644 (file)
@@ -30,4 +30,4 @@ go_multi_binary(
 go_cross_binaries(
     base_target = "mtls_supervisor",
     linux_arm = True,
-)
\ No newline at end of file
+)
index 512bc9af241a1fd268660fb82138f6c977071a65..730b59d262b6f812bd5d39ab33bf95a784d0ede5 100644 (file)
@@ -2,7 +2,6 @@ load("//bazel:pkg.bzl", "nfpm_package")
 load("//bazel:sqfs.bzl", "sqfs_package")
 
 nfpm_package(
-    name = "runtime-utils",
     config = ":runtime-utils.yaml",
     data = [
         "//cmd/metricbus_server:metric_collector_systemd_service",
@@ -42,7 +41,6 @@ sqfs_package(
 )
 
 nfpm_package(
-    name = "apcupsd-exporter",
     config = ":apcupsd-exporter.yaml",
     data = [
         "//cmd/apcups_exporter:apcupsd_exporter@_systemd_service",
@@ -51,10 +49,10 @@ nfpm_package(
     exes = [
         "//cmd/apcups_exporter",
     ],
+    linux_arm64 = True,
 )
 
 nfpm_package(
-    name = "http-proxy",
     config = ":http-proxy.yaml",
     data = [
         "//cmd/http_proxy:http_proxy@_systemd_service",
@@ -66,7 +64,6 @@ nfpm_package(
 )
 
 nfpm_package(
-    name = "ldap-health-exporter",
     config = ":ldap-health-exporter.yaml",
     data = [
         "//cmd/ldap_health_exporter:ldap_health_exporter@_systemd_service",
@@ -77,7 +74,6 @@ nfpm_package(
 )
 
 nfpm_package(
-    name = "prometheus-helpers",
     config = ":prometheus-helpers.yaml",
     data = [
         "//cmd/prometheus_http_discovery:prometheus_http_discovery_systemd_service",
@@ -88,7 +84,6 @@ nfpm_package(
 )
 
 nfpm_package(
-    name = "k8s-cert-sync",
     config = ":k8s-cert-sync.yaml",
     data = [
         "//cmd/k8s_cert_sync:k8s_cert_sync_systemd_service",
@@ -99,7 +94,6 @@ nfpm_package(
 )
 
 nfpm_package(
-    name = "coredns",
     config = ":coredns.yaml",
     exes = [
         "//thirdparty/coredns",