2020-10-16 00:19:21 +00:00
|
|
|
"""Helpers for Docker image generation."""
|
|
|
|
|
|
|
|
def _docker_image_impl(ctx):
|
|
|
|
importer = ctx.actions.declare_file(ctx.label.name)
|
2020-10-23 04:21:16 +00:00
|
|
|
|
2020-10-16 00:19:21 +00:00
|
|
|
importer_content = [
|
|
|
|
"#!/bin/bash",
|
|
|
|
"set -euo pipefail",
|
2020-10-23 04:21:16 +00:00
|
|
|
"source_file='%s'" % ctx.file.data.path,
|
|
|
|
"if [[ ! -f \"$source_file\" ]]; then",
|
|
|
|
" source_file='%s'" % ctx.file.data.short_path,
|
|
|
|
"fi",
|
2020-10-16 00:19:21 +00:00
|
|
|
"exec docker import " + " ".join([
|
|
|
|
"-c '%s'" % attr
|
|
|
|
for attr in ctx.attr.statements
|
2020-10-23 04:21:16 +00:00
|
|
|
]) + " \"$source_file\" $1",
|
2020-10-16 00:19:21 +00:00
|
|
|
"",
|
|
|
|
]
|
2020-10-23 04:21:16 +00:00
|
|
|
|
2020-10-16 00:19:21 +00:00
|
|
|
ctx.actions.write(importer, "\n".join(importer_content), is_executable = True)
|
|
|
|
return [DefaultInfo(
|
2020-10-23 04:21:16 +00:00
|
|
|
runfiles = ctx.runfiles([ctx.file.data]),
|
2020-10-16 00:19:21 +00:00
|
|
|
executable = importer,
|
|
|
|
)]
|
|
|
|
|
|
|
|
docker_image = rule(
|
|
|
|
implementation = _docker_image_impl,
|
2020-10-23 04:21:16 +00:00
|
|
|
doc = "Tool to import a Docker image; takes a single parameter (image name).",
|
2020-10-16 00:19:21 +00:00
|
|
|
attrs = {
|
|
|
|
"statements": attr.string_list(doc = "Extra Dockerfile directives."),
|
2020-10-23 04:21:16 +00:00
|
|
|
"data": attr.label(doc = "Image filesystem tarball", allow_single_file = [".tgz", ".tar.gz"]),
|
2020-10-16 00:19:21 +00:00
|
|
|
},
|
|
|
|
executable = True,
|
|
|
|
)
|