-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpull.bzl
More file actions
139 lines (123 loc) · 4.01 KB
/
Copy pathpull.bzl
File metadata and controls
139 lines (123 loc) · 4.01 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
""" pull """
# A directory to store cached OCI artifacts
# TODO(griffin) currently not used, but going to start depending on this for
# integration into the bzl wrapper.
OCI_CACHE_DIR_ENV = "OCI_CACHE_DIR"
# XXX(griffin): quick hack to get Bazel to spit out debug info for oci_pull
DEBUG = False
def failout(msg, cmd_result):
fail(
"{msg}\n stdout: {stdout} \n stderr: {stderr}"
.format(msg = msg, stdout = cmd_result.stdout, stderr = cmd_result.stderr),
)
# buildifier: disable=function-docstring
def pull(rctx, layout_root, repository, digest, registry = "", shallow = False, debug = False):
cmd = [
rctx.path(_repo_toolchain(rctx, "ocitool")),
]
if debug:
cmd.append("--debug")
cmd.extend([
"--layout={layout_root}".format(layout_root = layout_root),
"pull",
"--shallow={shallow}".format(shallow = shallow),
"{registry}/{repository}@{digest}".format(
registry = registry,
repository = repository,
digest = digest,
),
])
res = rctx.execute(cmd, quiet = not DEBUG)
if res.return_code > 0:
failout("failed to pull manifest", res)
def generate_build_files(rctx, layout_root, digest = ""):
cmd = [
rctx.path(_repo_toolchain(rctx, "ocitool")),
"--debug",
"--layout={layout_root}".format(layout_root = layout_root),
"generate-build-files",
"--image-digest={digest}".format(digest = digest),
]
res = rctx.execute(cmd, quiet = not DEBUG)
if res.return_code > 0:
failout("failed to pull manifest", res)
def _oci_pull_impl(rctx):
pull(
rctx,
rctx.path("."),
repository = rctx.attr.repository,
digest = rctx.attr.digest,
registry = rctx.attr.registry,
shallow = rctx.attr.shallow,
)
generate_build_files(
rctx,
rctx.path("."),
digest = rctx.attr.digest,
)
oci_pull = repository_rule(
implementation = _oci_pull_impl,
doc = """
""",
attrs = {
"registry": attr.string(
mandatory = True,
doc = """
""",
),
"repository": attr.string(
mandatory = True,
doc = """
""",
),
# XXX We're specifically *NOT* supporting pulling by tags as it's
# difficult for users to control when the tag resolution is done.
"digest": attr.string(
mandatory = True,
doc = """
""",
),
"debug": attr.bool(
default = False,
doc = "Enable ocitool debug output",
),
"shallow": attr.bool(
default = True,
doc = """
""",
),
"_ocitool_darwin_amd64": attr.label(
default = "@com_github_datadog_rules_oci//bin:ocitool-darwin-amd64",
),
"_ocitool_darwin_arm64": attr.label(
default = "@com_github_datadog_rules_oci//bin:ocitool-darwin-arm64",
),
"_ocitool_linux_amd64": attr.label(
default = "@com_github_datadog_rules_oci//bin:ocitool-linux-amd64",
),
"_ocitool_linux_arm64": attr.label(
default = "@com_github_datadog_rules_oci//bin:ocitool-linux-arm64",
),
},
environ = [
OCI_CACHE_DIR_ENV,
],
)
def _repo_toolchain(rctx, tool_name):
goos = ""
goarch = ""
if rctx.os.name.lower().startswith("mac os"):
goos = "darwin"
elif rctx.os.name.lower().startswith("linux"):
goos = "linux"
else:
fail("unknown os: {}".format(rctx.os.name))
# TODO update to use rctx.os.arch when released
arch = rctx.execute(["uname", "-m"]).stdout.strip()
if arch.lower().find("x86") != -1:
goarch = "amd64"
elif arch.lower().find("arm64") != -1 or arch.lower().find("aarch64") != -1:
goarch = "arm64"
else:
fail("unknown arch: {}".format(rctx.os.arch))
return getattr(rctx.attr, "_{tool}_{os}_{arch}".format(tool = tool_name, os = goos, arch = goarch))