forked from bazelbuild/rules_docker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpull.bzl
More file actions
119 lines (101 loc) · 3.84 KB
/
pull.bzl
File metadata and controls
119 lines (101 loc) · 3.84 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# 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.
"""An implementation of container_pull based on google/containerregistry.
This wraps the containerregistry.tools.fast_puller executable in a
Bazel rule for downloading base images without a Docker client to
construct new images.
"""
def python(repository_ctx):
if "BAZEL_PYTHON" in repository_ctx.os.environ:
return repository_ctx.os.environ.get("BAZEL_PYTHON")
python_path = repository_ctx.which("python")
if not python_path:
python_path = repository_ctx.which("python.exe")
if python_path:
return python_path
fail("rules_docker requires a python interpreter installed. " +
"Please set BAZEL_PYTHON, or put it on your path.")
def _impl(repository_ctx):
"""Core implementation of container_pull."""
# Add an empty top-level BUILD file.
repository_ctx.file("BUILD", "")
repository_ctx.file("image/BUILD", """
package(default_visibility = ["//visibility:public"])
load("@io_bazel_rules_docker//container:import.bzl", "container_import")
container_import(
name = "image",
config = "config.json",
layers = glob(["*.tar.gz"]),
)
""")
args = [
python(repository_ctx),
repository_ctx.path(repository_ctx.attr._puller),
"--directory",
repository_ctx.path("image"),
]
# If a digest is specified, then pull by digest. Otherwise, pull by tag.
if repository_ctx.attr.digest:
args += [
"--name",
"{registry}/{repository}@{digest}".format(
registry = repository_ctx.attr.registry,
repository = repository_ctx.attr.repository,
digest = repository_ctx.attr.digest,
),
]
else:
args += [
"--name",
"{registry}/{repository}:{tag}".format(
registry = repository_ctx.attr.registry,
repository = repository_ctx.attr.repository,
tag = repository_ctx.attr.tag,
),
]
kwargs = {}
if "PULLER_TIMEOUT" in repository_ctx.os.environ:
kwargs["timeout"] = int(repository_ctx.os.environ.get("PULLER_TIMEOUT"))
if repository_ctx.attr.print_progress:
kwargs["quiet"] = False
args += ["--print-progress"]
result = repository_ctx.execute(args, **kwargs)
if result.return_code:
fail("Pull command failed: %s (%s)" % (result.stderr, " ".join(args)))
container_pull = repository_rule(
attrs = {
"registry": attr.string(mandatory = True),
"repository": attr.string(mandatory = True),
"digest": attr.string(),
"tag": attr.string(default = "latest"),
"print_progress": attr.bool(),
"_puller": attr.label(
executable = True,
default = Label("@puller//file:downloaded"),
cfg = "host",
),
},
implementation = _impl,
)
"""Pulls a container image.
This rule pulls a container image into our intermediate format. The
output of this rule can be used interchangeably with `docker_build`.
Args:
name: name of the rule.
registry: the registry from which we are pulling.
repository: the name of the image.
tag: (optional) the tag of the image, default to 'latest' if this
and 'digest' remain unspecified.
digest: (optional) the digest of the image to pull.
"""