Repository navigation
Expand file tree
/
Copy pathcheck_runtime.py
More file actions
174 lines (147 loc) · 6.57 KB
/
Copy pathcheck_runtime.py
File metadata and controls
174 lines (147 loc) · 6.57 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
"""Check the installed builder tools without network access or registry writes.
Run this with the task interpreter, /opt/lightwell/bin/python. The
Containerfile.cli runs it after assembly so missing tools, plugins, shared libraries,
or CLI options fail the image build before the image can be used by Tekton.
"""
from __future__ import annotations
import argparse
import importlib.metadata
import json
import os
import shutil
import subprocess
import tempfile
from pathlib import Path
CLI_COMMANDS = (
"lightwell-source-to-sdist",
"lightwell-build-wheels",
"lightwell-build-sdist",
)
def run(command: list[str], required_options: tuple[str, ...] = ()) -> None:
"""Execute a local probe and verify options used by the task package.
Args:
command: Argument vector for a help or version probe.
required_options: CLI flags that must occur in its output.
Raises:
RuntimeError: An executable or required option is missing, or a probe fails.
subprocess.TimeoutExpired: The command does not finish within one minute.
"""
if shutil.which(command[0]) is None:
raise RuntimeError(f"Required executable is missing from PATH: {command[0]}")
try:
result = subprocess.run(
command,
check=True,
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
timeout=60,
)
except subprocess.CalledProcessError as error:
raise RuntimeError(f"Probe failed: {' '.join(command)}\n{error.stdout}") from error
for option in required_options:
if option not in result.stdout:
raise RuntimeError(f"{' '.join(command)} does not expose required option {option}")
print(f"OK: {' '.join(command)}", flush=True)
def main() -> None:
"""Verify task dependencies and native builder tools."""
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--profile", choices=("builder", "plugins"), required=True)
args = parser.parse_args()
if args.profile == "plugins":
check_fromager_plugins()
return
for distribution in ("lightwell-python", "packaging", "PyYAML"):
print(f"{distribution}=={importlib.metadata.version(distribution)}", flush=True)
for command in CLI_COMMANDS:
run([command, "--help"])
common_probes = (
(["git", "--version"], ()),
(["oras", "push", "--help"], ("--export-manifest", "--registry-config")),
(["oras", "tag", "--help"], ("--registry-config",)),
)
for command, options in common_probes:
run(command, options)
check_registry_auth()
check_builder()
for parent in (Path.cwd(), Path.home()):
with tempfile.TemporaryDirectory(dir=parent, prefix="builder-smoke-") as directory:
Path(directory, "write-check").write_text("ok\n")
print(f"{args.profile} runtime checks passed as UID {os.getuid()}", flush=True)
def check_registry_auth() -> None:
"""Exercise select-oci-auth with synthetic credentials without contacting a registry."""
with tempfile.TemporaryDirectory(prefix="builder-auth-smoke-") as directory:
auth = Path(directory) / ".docker/config.json"
auth.parent.mkdir()
auth.write_text(json.dumps({"auths": {"registry.test/team": {"auth": "smoke-only"}}}))
# The pinned helper reads ~/.docker/config.json. Isolate the child process
# from developer credentials, including versions that support env overrides.
environment = dict(os.environ) | {
"HOME": directory,
"AUTHFILE": str(auth),
"DOCKER_CONFIG": str(auth.parent),
}
try:
result = subprocess.run(
["select-oci-auth", "registry.test/team/image:tag"],
env=environment,
capture_output=True,
text=True,
timeout=60,
check=True,
)
except subprocess.CalledProcessError as error:
# This probe uses only synthetic credentials, so stderr is safe to show.
raise RuntimeError(
f"select-oci-auth smoke check failed (exit {error.returncode}): {error.stderr}"
) from error
expected = {"auths": {"registry.test": {"auth": "smoke-only"}}}
if json.loads(result.stdout) != expected:
raise RuntimeError("select-oci-auth did not select the repository credential")
print("OK: select-oci-auth credential selection", flush=True)
def check_builder() -> None:
"""Verify the native build tools and load the Fromager plugin entrypoints."""
probes = (
(["fromager", "--help"], ("--settings-file", "--settings-dir", "--patches-dir")),
(["fromager", "bootstrap", "--help"], ("--cache-wheel-server-url",)),
(["fromager", "build-sequence", "--help"], ()),
(["fromager", "step", "prepare-source", "--help"], ()),
(["fromager", "step", "build-sdist", "--help"], ()),
(["auditwheel", "repair", "--help"], ()),
(["patchelf", "--version"], ()),
(["cosign", "attach", "sbom", "--help"], ("--sbom", "--type")),
(["gcc", "--version"], ()),
(["g++", "--version"], ()),
(["gfortran", "--version"], ()),
(["make", "--version"], ()),
(["cmake", "--version"], ()),
(["pkg-config", "--version"], ()),
(["cargo", "--version"], ()),
(["rustc", "--version"], ()),
)
for command, options in probes:
run(command, options)
overrides = Path("/usr/local/share/fromager/overrides")
if not (overrides / "settings.yaml").is_file() or not (overrides / "patches").is_dir():
raise RuntimeError("Fromager settings or patches are missing")
# The plugin belongs to Fromager's environment, not the isolated task venv.
run(
[
"/src/venv/bin/python",
str(Path(__file__).resolve()),
"--profile",
"plugins",
]
)
def check_fromager_plugins() -> None:
"""Load the package overrides using Fromager's own Python environment."""
print(f"fromager=={importlib.metadata.version('fromager')}", flush=True)
plugins = importlib.metadata.entry_points(group="fromager.project_overrides")
if not any(plugin.name == "faiss_cpu" for plugin in plugins):
raise RuntimeError("Fromager package override is missing: faiss_cpu")
# Load every installed override, including any supplied by a newer base.
for plugin in sorted(plugins, key=lambda entry: entry.name):
plugin.load()
print(f"OK: Fromager override {plugin.name}", flush=True)
if __name__ == "__main__":
main()