Skip to content

Commit 09ca2c4

Browse files
committed
[bazel] Switch from rules_docker to rules_oci
1 parent 55720d8 commit 09ca2c4

File tree

5 files changed

+240
-121
lines changed

5 files changed

+240
-121
lines changed

WORKSPACE

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -241,51 +241,48 @@ load("@rules_pkg//:deps.bzl", "rules_pkg_dependencies")
241241
rules_pkg_dependencies()
242242

243243
http_archive(
244-
name = "io_bazel_rules_docker",
245-
sha256 = "b1e80761a8a8243d03ebca8845e9cc1ba6c82ce7c5179ce2b295cd36f7e394bf",
246-
urls = ["https://github.com/bazelbuild/rules_docker/releases/download/v0.25.0/rules_docker-v0.25.0.tar.gz"],
244+
name = "rules_oci",
245+
sha256 = "db57efd706f01eb3ce771468366baa1614b5b25f4cce99757e2b8d942155b8ec",
246+
strip_prefix = "rules_oci-1.0.0",
247+
url = "https://github.com/bazel-contrib/rules_oci/releases/download/v1.0.0/rules_oci-v1.0.0.tar.gz",
247248
)
248249

249-
load(
250-
"@io_bazel_rules_docker//repositories:repositories.bzl",
251-
container_repositories = "repositories",
252-
)
253-
254-
container_repositories()
250+
load("@rules_oci//oci:dependencies.bzl", "rules_oci_dependencies")
255251

256-
load("@io_bazel_rules_docker//repositories:deps.bzl", container_deps = "deps")
252+
rules_oci_dependencies()
257253

258-
container_deps()
254+
load("@rules_oci//oci:repositories.bzl", "LATEST_CRANE_VERSION", "LATEST_ZOT_VERSION", "oci_register_toolchains")
259255

260-
load(
261-
"@io_bazel_rules_docker//container:container.bzl",
262-
"container_pull",
256+
oci_register_toolchains(
257+
name = "oci",
258+
crane_version = LATEST_CRANE_VERSION,
259+
# Uncommenting the zot toolchain will cause it to be used instead of crane for some tasks.
260+
# Note that it does not support docker-format images.
261+
# zot_version = LATEST_ZOT_VERSION,
263262
)
264263

264+
load("@rules_oci//oci:pull.bzl", "oci_pull")
265+
265266
# Examine https://console.cloud.google.com/gcr/images/distroless/GLOBAL/java?gcrImageListsize=30 to find
266267
# the latest version when updating
267-
container_pull(
268+
oci_pull(
268269
name = "java_image_base",
269-
# This pulls the java 11 version of the java base image
270-
digest = "sha256:97c7eae86c65819664fcb7f36e8dee54bbbbc09c2cb6b448cbee06e1b42df81b",
271-
registry = "gcr.io",
272-
repository = "distroless/java",
270+
digest = "sha256:161a1d97d592b3f1919801578c3a47c8e932071168a96267698f4b669c24c76d",
271+
image = "gcr.io/distroless/java17",
273272
)
274273

275-
container_pull(
274+
oci_pull(
276275
name = "firefox_standalone",
277-
# selenium/standalone-firefox-debug:3.141.59
278-
digest = "sha256:ecc9861eafb3c2f999126fa4cc0434e9fbe6658ba1241998457bb088c99dd0d0",
276+
digest = "sha256:b6d8279268b3183d0d33e667e82fec1824298902f77718764076de763673124f",
279277
registry = "index.docker.io",
280-
repository = "selenium/standalone-firefox-debug",
278+
repository = "selenium/standalone-firefox",
281279
)
282280

283-
container_pull(
281+
oci_pull(
284282
name = "chrome_standalone",
285-
# selenium/standalone-chrome-debug:3.141.59
286-
digest = "sha256:c3a2174ac31b3918ae9d93c43ed8165fc2346b8c9e16d38ebac691fbb242667f",
283+
digest = "sha256:1b809a961a0a77787a7cccac74ddc5570b7e89747f925b8469ddb9a6624d4ece",
287284
registry = "index.docker.io",
288-
repository = "selenium/standalone-chrome-debug",
285+
repository = "selenium/standalone-chrome",
289286
)
290287

291288
load("//common:repositories.bzl", "pin_browsers")

common/private/passwd.bzl

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
# Copyright 2017 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""Rules for creating password files and entries."""
15+
16+
load("@bazel_skylib//lib:paths.bzl", "paths")
17+
18+
_join_path = paths.join
19+
20+
PasswdFileContentProviderInfo = provider(
21+
fields = [
22+
"username",
23+
"uid",
24+
"gid",
25+
"info",
26+
"home",
27+
"create_home",
28+
"shell",
29+
"name",
30+
],
31+
)
32+
33+
def _passwd_entry_impl(ctx):
34+
"""Creates a passwd_file_content_provider containing a single entry."""
35+
return [PasswdFileContentProviderInfo(
36+
username = ctx.attr.username,
37+
uid = ctx.attr.uid,
38+
gid = ctx.attr.gid,
39+
info = ctx.attr.info,
40+
home = ctx.attr.home,
41+
create_home = ctx.attr.create_home,
42+
shell = ctx.attr.shell,
43+
name = ctx.attr.name,
44+
)]
45+
46+
def _passwd_file_impl(ctx):
47+
"""Core implementation of passwd_file."""
48+
f = "".join(["%s:x:%s:%s:%s:%s:%s\n" % (
49+
entry[PasswdFileContentProviderInfo].username,
50+
entry[PasswdFileContentProviderInfo].uid,
51+
entry[PasswdFileContentProviderInfo].gid,
52+
entry[PasswdFileContentProviderInfo].info,
53+
entry[PasswdFileContentProviderInfo].home,
54+
entry[PasswdFileContentProviderInfo].shell,
55+
) for entry in ctx.attr.entries])
56+
passwd_file = ctx.actions.declare_file(ctx.label.name)
57+
ctx.actions.write(output = passwd_file, content = f)
58+
return DefaultInfo(files = depset([passwd_file]))
59+
60+
def _format_onwer(t):
61+
return ("--owners=%s=%s" % (t[0], t[1]))
62+
63+
def _build_homedirs_tar(ctx, passwd_file):
64+
homedirs = []
65+
owners_map = {}
66+
for entry in ctx.attr.entries:
67+
if entry[PasswdFileContentProviderInfo].create_home:
68+
homedir = entry[PasswdFileContentProviderInfo].home
69+
owners_map[homedir] = "{uid}.{gid}".format(
70+
uid = entry[PasswdFileContentProviderInfo].uid,
71+
gid = entry[PasswdFileContentProviderInfo].gid,
72+
)
73+
homedirs.append(homedir)
74+
dest_file = _join_path(
75+
ctx.attr.passwd_file_pkg_dir,
76+
ctx.label.name,
77+
)
78+
args = ctx.actions.args()
79+
args.add(ctx.outputs.passwd_tar, format = "--output=%s")
80+
args.add("--mode=0o700")
81+
args.add(passwd_file, format = "--file=%s=" + dest_file)
82+
args.add(dest_file, format = "--modes=%s=" + ctx.attr.passwd_file_mode)
83+
84+
args.add_all(homedirs, format_each = "--empty_dir=%s")
85+
args.add_all(owners_map.items(), map_each = _format_onwer)
86+
ctx.actions.run(
87+
executable = ctx.executable.build_tar,
88+
inputs = [passwd_file],
89+
outputs = [ctx.outputs.passwd_tar],
90+
mnemonic = "PasswdTar",
91+
arguments = [args],
92+
)
93+
94+
def _passwd_tar_impl(ctx):
95+
"""Core implementation of passwd_tar."""
96+
f = "".join(["%s:x:%s:%s:%s:%s:%s\n" % (
97+
entry[PasswdFileContentProviderInfo].username,
98+
entry[PasswdFileContentProviderInfo].uid,
99+
entry[PasswdFileContentProviderInfo].gid,
100+
entry[PasswdFileContentProviderInfo].info,
101+
entry[PasswdFileContentProviderInfo].home,
102+
entry[PasswdFileContentProviderInfo].shell,
103+
) for entry in ctx.attr.entries])
104+
105+
passwd_file = ctx.actions.declare_file(ctx.label.name)
106+
ctx.actions.write(output = passwd_file, content = f)
107+
108+
_build_homedirs_tar(ctx, passwd_file)
109+
110+
return DefaultInfo(files = depset([ctx.outputs.passwd_tar]))
111+
112+
passwd_entry = rule(
113+
attrs = {
114+
"create_home": attr.bool(default = True),
115+
"gid": attr.int(default = 1000),
116+
"home": attr.string(default = "/home"),
117+
"info": attr.string(default = "user"),
118+
"shell": attr.string(default = "/bin/bash"),
119+
"uid": attr.int(default = 1000),
120+
"username": attr.string(mandatory = True),
121+
},
122+
implementation = _passwd_entry_impl,
123+
)
124+
125+
passwd_file = rule(
126+
attrs = {
127+
"entries": attr.label_list(
128+
allow_empty = False,
129+
providers = [PasswdFileContentProviderInfo],
130+
),
131+
},
132+
executable = False,
133+
implementation = _passwd_file_impl,
134+
)
135+
136+
passwd_tar = rule(
137+
attrs = {
138+
"build_tar": attr.label(
139+
default = Label("//container:build_tar"),
140+
cfg = "exec",
141+
executable = True,
142+
allow_files = True,
143+
),
144+
"entries": attr.label_list(
145+
allow_empty = False,
146+
providers = [PasswdFileContentProviderInfo],
147+
),
148+
"passwd_file_mode": attr.string(default = "0o644"),
149+
"passwd_file_pkg_dir": attr.string(mandatory = True),
150+
},
151+
executable = False,
152+
outputs = {
153+
"passwd_tar": "%{name}.tar",
154+
},
155+
implementation = _passwd_tar_impl,
156+
)

0 commit comments

Comments
 (0)