Skip to content

Commit ddf90f1

Browse files
committed
revert changes to oci_load
1 parent c60c40f commit ddf90f1

File tree

6 files changed

+56
-90
lines changed

6 files changed

+56
-90
lines changed

examples/assertion/BUILD.bazel

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
load("@tar.bzl//tar:tar.bzl", "tar")
2-
load("@aspect_bazel_lib//lib:paths.bzl", "BASH_RLOCATION_FUNCTION", "to_rlocation_path")
2+
load("@aspect_bazel_lib//lib:paths.bzl", "BASH_RLOCATION_FUNCTION")
33
load("@aspect_bazel_lib//lib:testing.bzl", "assert_json_matches")
44
load("@aspect_bazel_lib//lib:transitions.bzl", "platform_transition_filegroup")
55
load("@bazel_skylib//rules:build_test.bzl", "build_test")
66
load("@bazel_skylib//rules:write_file.bzl", "write_file")
77
load("@rules_oci//oci:defs.bzl", "oci_image", "oci_load")
88
load("//examples:assert.bzl", "assert_oci_config")
99

10+
NOT_WINDOWS = select({
11+
"@platforms//os:windows": ["@platforms//:incompatible"],
12+
"//conditions:default": [],
13+
})
14+
1015
# Case 1: image name containing a capital case.
1116
oci_image(
1217
name = "imagE",
@@ -68,10 +73,7 @@ oci_load(
6873
name = "case4_load",
6974
image = ":case4_transition",
7075
repo_tags = ["case4:example"],
71-
target_compatible_with = select({
72-
"@aspect_bazel_lib//lib:enable_runfiles": [],
73-
"//conditions:default": ["@platforms//:incompatible"],
74-
}),
76+
target_compatible_with = NOT_WINDOWS,
7577
)
7678

7779
filegroup(
@@ -272,10 +274,7 @@ oci_load(
272274
name = "case9_tarball",
273275
image = ":case9_image",
274276
repo_tags = repo_tags,
275-
target_compatible_with = select({
276-
"@aspect_bazel_lib//lib:enable_runfiles": [],
277-
"//conditions:default": ["@platforms//:incompatible"],
278-
}),
277+
target_compatible_with = NOT_WINDOWS,
279278
)
280279

281280
# Not typically recommended: ask the tarball rule to write the .tar file
@@ -318,10 +317,7 @@ oci_load(
318317
name = "case10_tarball",
319318
image = ":case10",
320319
repo_tags = ["case10:example"],
321-
target_compatible_with = select({
322-
"@aspect_bazel_lib//lib:enable_runfiles": [],
323-
"//conditions:default": ["@platforms//:incompatible"],
324-
}),
320+
target_compatible_with = NOT_WINDOWS,
325321
)
326322

327323
genrule(
@@ -344,10 +340,7 @@ oci_load(
344340
name = "case11_tarball",
345341
image = ":case11",
346342
repo_tags = ["case11:example"],
347-
target_compatible_with = select({
348-
"@aspect_bazel_lib//lib:enable_runfiles": [],
349-
"//conditions:default": ["@platforms//:incompatible"],
350-
}),
343+
target_compatible_with = NOT_WINDOWS,
351344
)
352345

353346
write_file(
@@ -647,4 +640,18 @@ build_test(
647640
":case5_tar",
648641
":case10_run",
649642
],
643+
target_compatible_with = NOT_WINDOWS,
644+
)
645+
646+
# build them as test.
647+
build_test(
648+
name = "test_windows",
649+
targets = [
650+
":imagE",
651+
":case2",
652+
":case3",
653+
],
654+
target_compatible_with = [
655+
"@platforms//os:windows",
656+
],
650657
)

oci/private/image.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ function base_from_scratch() {
3434
function base_from() {
3535
local path="$1"
3636
# shellcheck disable=SC2045
37-
echo $(coreutils ls -1 -d "$path/blobs/"*/*)
3837
for blob in $(coreutils ls -1 -d "$path/blobs/"*/*); do
3938
local relative_to_blobs="${blob#"$path/blobs"}"
4039
coreutils mkdir -p "$OUTPUT/blobs/$(coreutils dirname "$relative_to_blobs")"

oci/private/load.bat.tpl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ set RUNFILES_MANIFEST_ONLY=1
77
{{BATCH_RLOCATION_FUNCTION}}
88

99
REM Equivalent of bash errexit - exit on any error
10-
rem if not defined RUNFILES_DIR (
11-
rem echo Error: RUNFILES_DIR not set >&2
12-
rem exit /b 1
13-
rem )
10+
if not defined RUNFILES_DIR (
11+
echo Error: RUNFILES_DIR not set >&2
12+
exit /b 1
13+
)
1414

1515
REM Set paths using runfiles resolution
1616
call :rlocation "{{tar}}" TAR

oci/private/load.bzl

Lines changed: 21 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ docker run --rm my-repository:latest
2020
"""
2121

2222
load("@aspect_bazel_lib//lib:paths.bzl", "BASH_RLOCATION_FUNCTION", "to_rlocation_path")
23-
load("@aspect_bazel_lib//lib:windows_utils.bzl", "BATCH_RLOCATION_FUNCTION")
2423
load("//oci/private:util.bzl", "util")
2524

2625
doc = """Loads an OCI layout into a container daemon without needing to publish the image first.
@@ -114,7 +113,7 @@ attrs = {
114113
115114
See the _run_template attribute for the script that calls this loader tool.
116115
""",
117-
allow_files = True,
116+
allow_single_file = True,
118117
mandatory = False,
119118
executable = True,
120119
cfg = "target",
@@ -129,33 +128,15 @@ attrs = {
129128
""",
130129
allow_single_file = True,
131130
),
132-
"_run_template_windows": attr.label(
133-
default = Label("//oci/private:load.bat.tpl"),
134-
doc = """ \
135-
The template used to load the container when using `bazel run` on this target.
136-
137-
See the `loader` attribute to replace the tool which is called.
138-
Please reference the default template to see available substitutions.
139-
""",
140-
allow_single_file = True,
141-
),
142131
"_tarball_sh": attr.label(allow_single_file = True, default = "//oci/private:tarball.sh.tpl"),
143132
"_runfiles": attr.label(default = "@bazel_tools//tools/bash/runfiles"),
144133
"_windows_constraint": attr.label(default = "@platforms//os:windows"),
145134
}
146135

147-
def _windows_host(ctx):
148-
"""Returns true if the host platform is windows.
149-
150-
The typical approach using ctx.target_platform_has_constraint does not work for transitioned
151-
build targets. We need to know the host platform, not the target platform.
152-
"""
153-
return ctx.configuration.host_path_separator == ";"
154-
155136
def _load_impl(ctx):
156137
jq = ctx.toolchains["@aspect_bazel_lib//lib:jq_toolchain_type"]
157138
coreutils = ctx.toolchains["@aspect_bazel_lib//lib:coreutils_toolchain_type"]
158-
bsdtar = ctx.toolchains["@tar.bzl//tar/toolchain:type"]
139+
bsdtar = ctx.toolchains["@aspect_bazel_lib//lib:tar_toolchain_type"]
159140

160141
image = ctx.file.image
161142
repo_tags = ctx.file.repo_tags
@@ -219,47 +200,29 @@ def _load_impl(ctx):
219200

220201
# Create an executable runner script that will create the tarball at runtime,
221202
# as opposed to at build to avoid uploading large artifacts to remote cache.
222-
if not _windows_host(ctx):
223-
runnable_loader = ctx.actions.declare_file(ctx.label.name + ".sh")
224-
ctx.actions.expand_template(
225-
template = ctx.file._run_template,
226-
output = runnable_loader,
227-
substitutions = {
228-
"{{BASH_RLOCATION_FUNCTION}}": BASH_RLOCATION_FUNCTION,
229-
"{{tar}}": to_rlocation_path(ctx, bsdtar.tarinfo.binary),
230-
"{{mtree_path}}": to_rlocation_path(ctx, mtree_spec),
231-
"{{loader}}": to_rlocation_path(ctx, ctx.executable.loader) if ctx.executable.loader else "",
232-
"{{manifest_root}}": manifest_json.root.path,
233-
"{{image_root}}": image.root.path,
234-
"{{workspace_name}}": ctx.workspace_name,
235-
},
236-
is_executable = True,
237-
)
238-
else:
239-
runnable_loader = ctx.actions.declare_file(ctx.label.name + ".bat")
240-
ctx.actions.expand_template(
241-
template = ctx.file._run_template_windows,
242-
output = runnable_loader,
243-
substitutions = {
244-
"{{BATCH_RLOCATION_FUNCTION}}": BATCH_RLOCATION_FUNCTION,
245-
"{{tar}}": to_rlocation_path(ctx, bsdtar.tarinfo.binary),
246-
"{{mtree_path}}": to_rlocation_path(ctx, mtree_spec),
247-
"{{loader}}": to_rlocation_path(ctx, ctx.executable.loader) if ctx.executable.loader else "",
248-
"{{manifest_root}}": manifest_json.root.path,
249-
"{{image_root}}": image.root.path,
250-
"{{workspace_name}}": ctx.workspace_name,
251-
},
252-
is_executable = True,
253-
)
203+
runnable_loader = ctx.actions.declare_file(ctx.label.name + ".sh")
254204

255205
runtime_deps = []
256-
if ctx.executable.loader:
257-
runtime_deps.append(ctx.executable.loader)
206+
if ctx.file.loader:
207+
runtime_deps.append(ctx.file.loader)
258208
runfiles = ctx.runfiles(runtime_deps, transitive_files = tar_inputs)
259209
runfiles = runfiles.merge(ctx.attr.image[DefaultInfo].default_runfiles)
260210
runfiles = runfiles.merge(ctx.attr._runfiles.default_runfiles)
261-
if ctx.executable.loader:
262-
runfiles = runfiles.merge(ctx.attr.loader.default_runfiles)
211+
212+
ctx.actions.expand_template(
213+
template = ctx.file._run_template,
214+
output = runnable_loader,
215+
substitutions = {
216+
"{{BASH_RLOCATION_FUNCTION}}": BASH_RLOCATION_FUNCTION,
217+
"{{tar}}": to_rlocation_path(ctx, bsdtar.tarinfo.binary),
218+
"{{mtree_path}}": to_rlocation_path(ctx, mtree_spec),
219+
"{{loader}}": to_rlocation_path(ctx, ctx.file.loader) if ctx.file.loader else "",
220+
"{{manifest_root}}": manifest_json.root.path,
221+
"{{image_root}}": image.root.path,
222+
"{{workspace_name}}": ctx.workspace_name,
223+
},
224+
is_executable = True,
225+
)
263226

264227
return [
265228
DefaultInfo(
@@ -277,7 +240,7 @@ oci_load = rule(
277240
"@bazel_tools//tools/sh:toolchain_type",
278241
"@aspect_bazel_lib//lib:coreutils_toolchain_type",
279242
"@aspect_bazel_lib//lib:jq_toolchain_type",
280-
"@tar.bzl//tar/toolchain:type",
243+
"@aspect_bazel_lib//lib:tar_toolchain_type",
281244
],
282245
executable = True,
283246
)

oci/private/load.sh.tpl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env bash
2-
set -o pipefail -o errexit -o nounset
2+
set -o pipefail -o errexit -o nounserun
33

44
{{BASH_RLOCATION_FUNCTION}}
55

@@ -24,13 +24,12 @@ else
2424
fi
2525

2626
# Strip manifest root and image root from mtree to make it compatible with runfiles layout.
27-
image_root="$(rlocation {{image_root}})/"
28-
manifest_root="$(rlocation {{manifest_root}})/"
27+
image_root="{{image_root}}/"
28+
manifest_root="{{manifest_root}}/"
2929
mtree_contents="$(cat $MTREE)"
3030
mtree_contents="${mtree_contents//"$image_root"/}"
3131
mtree_contents="${mtree_contents//"$manifest_root"/}"
3232

33-
echo "$TAR" --cd "$RUNFILES_DIR/{{workspace_name}}" --create --no-xattr --no-mac-metadata @-
3433
"$CONTAINER_CLI" load --input <(
3534
"$TAR" --cd "$RUNFILES_DIR/{{workspace_name}}" --create --no-xattr --no-mac-metadata @- <<< "$mtree_contents"
3635
)

oci/private/tarball.sh.tpl

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44

55
set -o pipefail -o errexit -o nounset
66

7-
{{BASH_RLOCATION_FUNCTION}}
8-
97
readonly FORMAT="{{format}}"
10-
readonly JQ="$(rlocation "{{jq_path}}")"
11-
readonly COREUTILS="$(rlocation "{{coreutils_path}}")"
12-
readonly TAR="$(rlocation "{{tar}}")"
13-
readonly IMAGE_DIR="$(rlocation "{{image_dir}}")"
8+
readonly JQ="{{jq_path}}"
9+
readonly COREUTILS="{{coreutils_path}}"
10+
readonly TAR="{{tar}}"
11+
readonly IMAGE_DIR="{{image_dir}}"
1412
readonly REPOTAGS=($("${COREUTILS}" cat "{{tags}}"))
1513
readonly INDEX_FILE="${IMAGE_DIR}/index.json"
1614

0 commit comments

Comments
 (0)