aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/docker/docker_pull.bzl
blob: 8fab57fe883c9af0f5ac5d6c55f141060b2c1fce (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
# Copyright 2016 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.

"""Quick and not really nice docker_pull rules based on the docker daemon."""

def _impl(repository_ctx):
  docker = repository_ctx.which("docker")
  if docker == None and repository_ctx.attr.optional:
    repository_ctx.file("BUILD", """
load("@bazel_tools//tools/build_defs/docker:docker.bzl", "docker_build")

# an empty image to still allow building despite not having the base
# image.
docker_build(
    name = "image",
    visibility = ['//visibility:public'],
)
""")
    repository_ctx.file("image.tar")
    return

  repository_ctx.file("BUILD", """
load("@bazel_tools//tools/build_defs/docker:docker.bzl", "docker_build")

docker_build(
    name = "image",
    base = ":base.tar",
    visibility = ["//visibility:public"],
)
""")
  tag = repository_ctx.attr.tag
  cmd = "pull"
  if repository_ctx.attr.dockerfile:
    dockerfile = repository_ctx.path(repository_ctx.attr.dockerfile)
    cmd = "build"
    print("Running `docker build`")
    result = repository_ctx.execute([
        docker,
        "build",
        "-q",
        "-t",
        tag,
        "-f",
        dockerfile,
        dockerfile.dirname,
    ], quiet=False, timeout=3600)
  else:
    print("Running `docker pull`")
    result = repository_ctx.execute([docker, "pull", tag], quiet=False, timeout=3600)
  if result.return_code:
    fail("docker %s failed with error code %s:\n%s" % (
        cmd,
        result.return_code,
        result.stderr))
  result = repository_ctx.execute([
      docker, "save", "-o", repository_ctx.path("base.tar"), tag])
  if result.return_code:
    fail("docker save failed with error code %s:\n%s" % (
        result.return_code,
        result.stderr))

docker_pull = repository_rule(
    implementation = _impl,
    attrs = {
        "tag": attr.string(mandatory=True),
        "dockerfile": attr.label(default=None),
        "optional": attr.bool(default=False),
    },
)