undocker/rules.bzl

113 lines
2.9 KiB
Python
Raw Normal View History

2021-05-24 00:11:58 +03:00
load("@rules_pkg//:pkg.bzl", "pkg_tar")
2021-05-24 00:11:58 +03:00
_undocker_cli = attr.label(
doc = "undocker cli; private and may not be overridden",
cfg = "host",
default = Label("//src/undocker:undocker"),
allow_single_file = True,
executable = True,
)
_input_container = attr.label(
doc = "Input container tarball",
mandatory = True,
allow_single_file = [".tar"],
)
2021-05-24 00:11:58 +03:00
def _rootfs_impl(ctx):
2021-05-24 00:11:58 +03:00
out = ctx.outputs.out
if out == None:
out = ctx.actions.declare_file(ctx.attr.name + ".tar")
2021-05-24 00:11:58 +03:00
ctx.actions.run(
2021-05-24 00:11:58 +03:00
outputs = [out],
2021-05-24 00:11:58 +03:00
inputs = ctx.files.src,
2021-05-24 00:11:58 +03:00
executable = ctx.executable._undocker,
2021-05-24 00:11:58 +03:00
arguments = [
2021-05-24 00:11:58 +03:00
"rootfs",
2021-05-24 00:11:58 +03:00
ctx.files.src[0].path,
out.path,
2021-05-24 00:11:58 +03:00
],
2021-05-24 00:11:58 +03:00
mnemonic = "RootFS",
2021-05-24 00:11:58 +03:00
)
2021-05-24 00:11:58 +03:00
return DefaultInfo(
2021-05-24 00:11:58 +03:00
files = depset([out]),
runfiles = ctx.runfiles(files = ctx.files.src),
2021-05-24 00:11:58 +03:00
)
2021-05-24 00:11:58 +03:00
rootfs = rule(
2021-05-24 00:11:58 +03:00
_rootfs_impl,
2021-05-24 00:11:58 +03:00
doc = "Generate a rootfs from a docker container image",
attrs = {
2021-05-24 00:11:58 +03:00
"src": _input_container,
2021-05-24 00:11:58 +03:00
"_undocker": _undocker_cli,
2021-05-24 00:11:58 +03:00
"out": attr.output(),
2021-05-24 00:11:58 +03:00
},
)
def _lxcconfig_impl(ctx):
2021-05-24 00:11:58 +03:00
out = ctx.actions.declare_file(ctx.attr.name)
2021-05-24 00:11:58 +03:00
ctx.actions.run(
outputs = [out],
inputs = ctx.files.src,
2021-05-24 00:11:58 +03:00
executable = ctx.executable._undocker,
2021-05-24 00:11:58 +03:00
arguments = [
"lxcconfig",
ctx.files.src[0].path,
out.path,
],
mnemonic = "LXCConfig",
)
2021-05-24 00:11:58 +03:00
return DefaultInfo(
2021-05-24 00:11:58 +03:00
files = depset([out]),
runfiles = ctx.runfiles(files = ctx.files.src),
2021-05-24 00:11:58 +03:00
)
2021-05-24 00:11:58 +03:00
2021-05-24 00:11:58 +03:00
def lxcbundle(name, src, version, overlay_tars = []):
2021-05-24 00:11:58 +03:00
if type(version) != "int":
fail("version must be an int, got {}".format(type(version)))
nameversion = "{}.{}".format(name, version)
stage0 = nameversion + "/_stage0/rootfs"
stage1 = nameversion + "/_stage1/rootfs"
2021-05-24 00:11:58 +03:00
rootfs(
name = stage0,
src = src,
out = stage0 + ".tar",
2021-05-24 00:11:58 +03:00
)
2021-05-24 00:11:58 +03:00
pkg_tar(
name = stage1,
deps = [stage0] + overlay_tars,
extension = "tar.xz",
out = stage1 + ".tar.xz",
)
lxcconfig(name, version, src = src, out = name + "/_stage1/meta.tar.xz")
2021-05-24 00:11:58 +03:00
2021-05-24 00:11:58 +03:00
pkg_tar(
name = nameversion,
2021-05-24 00:11:58 +03:00
srcs = [
nameversion + "/_stage1/rootfs",
nameversion + "/_stage1/meta",
2021-05-24 00:11:58 +03:00
],
out = "{}.tar".format(nameversion),
2021-05-24 00:11:58 +03:00
)
2021-05-24 00:11:58 +03:00
2021-05-24 00:11:58 +03:00
_lxcconfig = rule(
2021-05-24 00:11:58 +03:00
_lxcconfig_impl,
2021-05-24 00:11:58 +03:00
doc = "Generate lxc config from a docker container image",
attrs = {
"src": _input_container,
2021-05-24 00:11:58 +03:00
"_undocker": _undocker_cli,
2021-05-24 00:11:58 +03:00
},
)
2021-05-24 00:11:58 +03:00
def lxcconfig(name, version, src, out = None):
nameversion = "{}.{}".format(name, version)
_lxcconfig(name = nameversion + "/_stage0/config", src = src)
2021-05-24 00:11:58 +03:00
pkg_tar(
name = nameversion + "/_stage1/meta",
2021-05-24 00:11:58 +03:00
extension = "tar.xz",
srcs = [nameversion + "/_stage0/config"],
2021-05-24 00:11:58 +03:00
remap_paths = {
name: "",
},
out = out or "meta.tar.xz",
2021-05-24 00:11:58 +03:00
)