aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/build_defs/docker/layers.bzl
blob: e011f025f3e643a54d569e40e10508b53f17c877 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Copyright 2017 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tools for dealing with Docker Image layers."""

load(":list.bzl", "reverse")
load(":path.bzl", _get_runfile_path="runfile")

def get_from_target(unused_ctx, target):
  if hasattr(target, "docker_layers"):
    return target.docker_layers
  else:
    # TODO(mattmoor): Use containerregistry.client's FromTarball
    # to create an entry from a tarball base image.
    return []


def assemble(ctx, layers, tags_to_names, output):
  """Create the full image from the list of layers."""
  layers = [l["layer"] for l in layers]
  args = [
      "--output=" + output.path,
  ] + [
      "--tags=" + tag + "=@" + tags_to_names[tag].path
      for tag in tags_to_names
  ] + ["--layer=" + l.path for l in layers]
  inputs = layers + tags_to_names.values()
  ctx.action(
      executable = ctx.executable.join_layers,
      arguments = args,
      inputs = inputs,
      outputs = [output],
      mnemonic = "JoinLayers"
  )


def incremental_load(ctx, layers, images, output):
  """Generate the incremental load statement."""
  ctx.template_action(
      template = ctx.file.incremental_load_template,
      substitutions = {
          "%{load_statements}": "\n".join([
              "incr_load '%s' '%s' '%s'" % (_get_runfile_path(ctx, l["name"]),
                                            _get_runfile_path(ctx, l["id"]),
                                            _get_runfile_path(ctx, l["layer"]))
              # The last layer is the first in the list of layers.
              # We reverse to load the layer from the parent to the child.
              for l in reverse(layers)]),
          "%{tag_statements}": "\n".join([
              "tag_layer '%s' '%s' '%s'" % (
                  img,
                  _get_runfile_path(ctx, images[img]["name"]),
                  _get_runfile_path(ctx, images[img]["id"]))
              for img in images
          ])
      },
      output = output,
      executable = True)


tools = {
    "incremental_load_template": attr.label(
        default=Label("//tools/build_defs/docker:incremental_load_template"),
        single_file=True,
        allow_files=True),
    "join_layers": attr.label(
        default=Label("//tools/build_defs/docker:join_layers"),
        cfg="host",
        executable=True,
        allow_files=True)
}