sinapsis-infra 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sinapsis_infra/__init__.py +1 -0
- sinapsis_infra/__main__.py +6 -0
- sinapsis_infra/cli.py +361 -0
- sinapsis_infra/core/__init__.py +1 -0
- sinapsis_infra/core/commands.py +76 -0
- sinapsis_infra/core/constants.py +29 -0
- sinapsis_infra/core/errors.py +21 -0
- sinapsis_infra/core/models.py +195 -0
- sinapsis_infra/hardware/__init__.py +1 -0
- sinapsis_infra/hardware/cuda-policy.toml +38 -0
- sinapsis_infra/hardware/cuda.py +168 -0
- sinapsis_infra/hardware/detection.py +238 -0
- sinapsis_infra/projects/__init__.py +1 -0
- sinapsis_infra/projects/application.py +107 -0
- sinapsis_infra/projects/discovery.py +218 -0
- sinapsis_infra/projects/examples.py +272 -0
- sinapsis_infra/projects/metadata.py +126 -0
- sinapsis_infra/provisioning/__init__.py +1 -0
- sinapsis_infra/provisioning/backends/__init__.py +1 -0
- sinapsis_infra/provisioning/backends/dispatch.py +28 -0
- sinapsis_infra/provisioning/backends/docker.py +209 -0
- sinapsis_infra/provisioning/backends/docker_sdk.py +279 -0
- sinapsis_infra/provisioning/backends/venv.py +124 -0
- sinapsis_infra/provisioning/execution.py +52 -0
- sinapsis_infra/provisioning/manager.py +55 -0
- sinapsis_infra/provisioning/planner.py +664 -0
- sinapsis_infra/provisioning/recipes.py +220 -0
- sinapsis_infra/provisioning/resolution.py +294 -0
- sinapsis_infra/provisioning/storage.py +245 -0
- sinapsis_infra/provisioning/verification.py +342 -0
- sinapsis_infra/runtimes/__init__.py +1 -0
- sinapsis_infra/runtimes/environment.py +20 -0
- sinapsis_infra/runtimes/llama_cpp/__init__.py +1 -0
- sinapsis_infra/runtimes/llama_cpp/bindings.py +322 -0
- sinapsis_infra/runtimes/llama_cpp/cli.py +47 -0
- sinapsis_infra/runtimes/llama_cpp/native.py +279 -0
- sinapsis_infra/runtimes/llama_cpp/provider.py +61 -0
- sinapsis_infra/runtimes/llama_cpp/verification.py +33 -0
- sinapsis_infra/runtimes/media.py +32 -0
- sinapsis_infra/runtimes/providers.py +156 -0
- sinapsis_infra/runtimes/registry.py +50 -0
- sinapsis_infra/runtimes/spec.py +25 -0
- sinapsis_infra/runtimes/tensorrt.py +37 -0
- sinapsis_infra/runtimes/torch.py +32 -0
- sinapsis_infra-0.1.0.dist-info/METADATA +555 -0
- sinapsis_infra-0.1.0.dist-info/RECORD +48 -0
- sinapsis_infra-0.1.0.dist-info/WHEEL +4 -0
- sinapsis_infra-0.1.0.dist-info/entry_points.txt +2 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Local infrastructure provisioning for Sinapsis."""
|
sinapsis_infra/cli.py
ADDED
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
"""Command-line entry point; errors go to stderr and data to stdout."""
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import json
|
|
5
|
+
import logging
|
|
6
|
+
import sys
|
|
7
|
+
from dataclasses import asdict, replace
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
from sinapsis_infra.provisioning.backends.dispatch import verify_environment
|
|
11
|
+
from sinapsis_infra.core.commands import SubprocessRunner
|
|
12
|
+
from sinapsis_infra.runtimes.registry import execute_install, register_parsers
|
|
13
|
+
from sinapsis_infra.provisioning.backends.docker_sdk import DockerSDKRunner
|
|
14
|
+
from sinapsis_infra.core.constants import AUTO, DEFAULT_PYTHON, DEFAULT_ROOT
|
|
15
|
+
from sinapsis_infra.hardware.detection import detect_docker, detect_host, enforce_python_floor
|
|
16
|
+
from sinapsis_infra.core.errors import InfraError
|
|
17
|
+
from sinapsis_infra.provisioning.execution import run_environment
|
|
18
|
+
from sinapsis_infra.provisioning.manager import InfrastructureManager
|
|
19
|
+
from sinapsis_infra.projects.metadata import load_metadata
|
|
20
|
+
from sinapsis_infra.projects.examples import Example, load_examples, select_variant
|
|
21
|
+
from sinapsis_infra.projects.discovery import discover, local_projects, remote_catalog
|
|
22
|
+
from sinapsis_infra.projects.application import Application, application_request, application_result
|
|
23
|
+
from sinapsis_infra.provisioning.resolution import split_packages
|
|
24
|
+
from sinapsis_infra.core.models import Acceleration, Backend, Capability, ProvisionRequest
|
|
25
|
+
from sinapsis_infra.provisioning.planner import resolve
|
|
26
|
+
from sinapsis_infra.provisioning.storage import Store
|
|
27
|
+
|
|
28
|
+
LOGGER = logging.getLogger(__name__)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def add_request_options(parser: argparse.ArgumentParser, *, advanced: bool = False) -> None:
|
|
32
|
+
"""Add shared plan and provision options."""
|
|
33
|
+
parser.add_argument("--backend", choices=tuple(Backend), default=Backend.AUTO)
|
|
34
|
+
parser.add_argument(
|
|
35
|
+
"--acceleration", "--accelerator", choices=tuple(Acceleration), default=None
|
|
36
|
+
)
|
|
37
|
+
parser.add_argument("--python", default=DEFAULT_PYTHON)
|
|
38
|
+
parser.add_argument("--cuda-version", default=AUTO, help="CUDA major.minor or auto (default)")
|
|
39
|
+
parser.add_argument(
|
|
40
|
+
"--deps",
|
|
41
|
+
action="append",
|
|
42
|
+
default=[],
|
|
43
|
+
help="Comma-separated capabilities; auto discovery is additive",
|
|
44
|
+
)
|
|
45
|
+
parser.add_argument(
|
|
46
|
+
"--sinapsis-packages",
|
|
47
|
+
action="append",
|
|
48
|
+
default=[],
|
|
49
|
+
help="Published Sinapsis requirements; repeat or separate package names with commas",
|
|
50
|
+
)
|
|
51
|
+
parser.add_argument("--base-only", action="store_true")
|
|
52
|
+
parser.add_argument("--allow-source-build", action="store_true")
|
|
53
|
+
parser.add_argument("--allow-cpu-fallback", action="store_true")
|
|
54
|
+
parser.add_argument("--metadata", action="append", default=[], metavar="PYPROJECT")
|
|
55
|
+
parser.add_argument(
|
|
56
|
+
"--no-discovery",
|
|
57
|
+
action="store_true",
|
|
58
|
+
help="Use explicit infrastructure without requiring local package declarations",
|
|
59
|
+
)
|
|
60
|
+
parser.add_argument(
|
|
61
|
+
"--example-file",
|
|
62
|
+
"--source",
|
|
63
|
+
type=Path,
|
|
64
|
+
help="Local pyproject.toml containing example definitions",
|
|
65
|
+
)
|
|
66
|
+
parser.add_argument("--example", help="Example name in --example-file")
|
|
67
|
+
parser.add_argument("--variant")
|
|
68
|
+
parser.add_argument("--llama-version", default=None, help="Standalone llama.cpp release tag")
|
|
69
|
+
parser.add_argument("--name")
|
|
70
|
+
parser.add_argument(
|
|
71
|
+
"--service-accelerator", action="append", default=[], metavar="SERVICE=ACCELERATOR"
|
|
72
|
+
)
|
|
73
|
+
parser.add_argument("--no-build", action="store_true", help="Disallow custom Docker builds")
|
|
74
|
+
parser.add_argument("--dry-run", action="store_true", help="Resolve without installing")
|
|
75
|
+
parser.add_argument("example_name", nargs="?", metavar="EXAMPLE")
|
|
76
|
+
parser.add_argument("variant_name", nargs="?", metavar="VARIANT")
|
|
77
|
+
parser.add_argument("--ref", help="Official remote catalog branch, tag or commit")
|
|
78
|
+
parser.add_argument("--help-all", action="help", help="Show all options")
|
|
79
|
+
if not advanced:
|
|
80
|
+
hide_advanced(parser, {"help", "help_all", "dry_run", "example_name", "variant_name"})
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def hide_advanced(parser: argparse.ArgumentParser, visible: set[str]) -> None:
|
|
84
|
+
"""Suppress advanced help only; keep every argument accepted by argparse."""
|
|
85
|
+
# argparse exposes no public action iteration API.
|
|
86
|
+
for action in parser._actions:
|
|
87
|
+
if action.dest not in visible:
|
|
88
|
+
action.help = argparse.SUPPRESS
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def add_global_options(
|
|
92
|
+
parser: argparse.ArgumentParser, *, subcommand: bool = False, advanced: bool = False
|
|
93
|
+
) -> None:
|
|
94
|
+
"""Accept shared options before or after a subcommand."""
|
|
95
|
+
parser.add_argument(
|
|
96
|
+
"--root",
|
|
97
|
+
type=Path,
|
|
98
|
+
default=argparse.SUPPRESS if subcommand else DEFAULT_ROOT,
|
|
99
|
+
help="Local state directory" if advanced else argparse.SUPPRESS,
|
|
100
|
+
)
|
|
101
|
+
parser.add_argument(
|
|
102
|
+
"--verbose", action="store_true", default=argparse.SUPPRESS if subcommand else False
|
|
103
|
+
)
|
|
104
|
+
parser.add_argument(
|
|
105
|
+
"--format",
|
|
106
|
+
choices=("json", "text"),
|
|
107
|
+
default=argparse.SUPPRESS if subcommand else "text",
|
|
108
|
+
help="Output format" if advanced else argparse.SUPPRESS,
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def parse_args(argv: list[str] | None) -> argparse.Namespace:
|
|
113
|
+
"""Parse the local infrastructure CLI."""
|
|
114
|
+
arguments = list(sys.argv[1:] if argv is None else argv)
|
|
115
|
+
option_arguments = arguments[: arguments.index("--")] if "--" in arguments else arguments
|
|
116
|
+
advanced = "--help-all" in option_arguments
|
|
117
|
+
parser = argparse.ArgumentParser(prog="sinx-infra")
|
|
118
|
+
parser.add_argument("--help-all", action="help", help="Show advanced options")
|
|
119
|
+
add_global_options(parser, advanced=advanced)
|
|
120
|
+
commands = parser.add_subparsers(dest="command", required=True)
|
|
121
|
+
commands.add_parser("doctor", help="Inspect host and Docker daemon")
|
|
122
|
+
install = commands.add_parser("install", help="Install optional runtimes in an existing venv")
|
|
123
|
+
register_parsers(
|
|
124
|
+
install, lambda runtime: add_global_options(runtime, subcommand=True, advanced=advanced)
|
|
125
|
+
)
|
|
126
|
+
add_request_options(
|
|
127
|
+
commands.add_parser("plan", help="Resolve a concrete profile"), advanced=advanced
|
|
128
|
+
)
|
|
129
|
+
add_request_options(
|
|
130
|
+
commands.add_parser("provision", help="Create or reuse a local profile"), advanced=advanced
|
|
131
|
+
)
|
|
132
|
+
for command in ("inspect", "verify"):
|
|
133
|
+
commands.add_parser(command).add_argument("name")
|
|
134
|
+
commands.add_parser("metadata", help="Validate local pyproject metadata").add_argument(
|
|
135
|
+
"path", type=Path
|
|
136
|
+
)
|
|
137
|
+
commands.add_parser("examples", help="List local example declarations").add_argument(
|
|
138
|
+
"path", type=Path
|
|
139
|
+
)
|
|
140
|
+
listing = commands.add_parser("list", help="Discover applications and package examples")
|
|
141
|
+
listing.add_argument("--ref")
|
|
142
|
+
listing.add_argument("--example-file", "--source", type=Path)
|
|
143
|
+
info = commands.add_parser("info", help="Inspect an example and its variants")
|
|
144
|
+
info.add_argument("example_name")
|
|
145
|
+
info.add_argument("--example-file", "--source", type=Path)
|
|
146
|
+
info.add_argument("--ref")
|
|
147
|
+
run = commands.add_parser("run", help="Run a command in a verified local environment")
|
|
148
|
+
run.add_argument("--help-all", action="help", help="Show all options")
|
|
149
|
+
run.add_argument("--env", required=True)
|
|
150
|
+
run.add_argument("argv", nargs=argparse.REMAINDER)
|
|
151
|
+
for command_parser in commands.choices.values():
|
|
152
|
+
add_global_options(command_parser, subcommand=True, advanced=advanced)
|
|
153
|
+
return parser.parse_args(arguments)
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def make_request(args: argparse.Namespace) -> ProvisionRequest:
|
|
157
|
+
"""Convert CLI values, preserving Python extras and version constraints."""
|
|
158
|
+
try:
|
|
159
|
+
deps = tuple(
|
|
160
|
+
dict.fromkeys(
|
|
161
|
+
Capability(value.strip())
|
|
162
|
+
for group in args.deps
|
|
163
|
+
for value in group.split(",")
|
|
164
|
+
if value.strip() not in ("auto", "none", "")
|
|
165
|
+
)
|
|
166
|
+
)
|
|
167
|
+
except ValueError as exc:
|
|
168
|
+
raise InfraError(f"Unknown infrastructure capability: {exc}") from exc
|
|
169
|
+
packages = split_packages(tuple(args.sinapsis_packages))
|
|
170
|
+
acceleration = Acceleration(args.acceleration or Acceleration.AUTO)
|
|
171
|
+
return ProvisionRequest(
|
|
172
|
+
backend=Backend(args.backend),
|
|
173
|
+
acceleration=acceleration,
|
|
174
|
+
python=args.python,
|
|
175
|
+
dependencies=deps,
|
|
176
|
+
packages=packages,
|
|
177
|
+
name=args.name,
|
|
178
|
+
no_build=args.no_build,
|
|
179
|
+
base_only=args.base_only,
|
|
180
|
+
allow_source_build=args.allow_source_build,
|
|
181
|
+
allow_cpu_fallback=args.allow_cpu_fallback,
|
|
182
|
+
metadata_paths=tuple(args.metadata),
|
|
183
|
+
packages_auto=not args.no_discovery,
|
|
184
|
+
llama_version=args.llama_version or ProvisionRequest().llama_version,
|
|
185
|
+
cuda_version=args.cuda_version,
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def service_overrides(values: list[str]) -> tuple[tuple[str, Acceleration], ...]:
|
|
190
|
+
"""Validate service-specific accelerator overrides."""
|
|
191
|
+
result: dict[str, Acceleration] = {}
|
|
192
|
+
for value in values:
|
|
193
|
+
name, separator, accelerator = value.partition("=")
|
|
194
|
+
if not separator or not name or name in result:
|
|
195
|
+
raise InfraError(f"Expected one SERVICE=ACCELERATOR override per service: {value}")
|
|
196
|
+
try:
|
|
197
|
+
result[name] = Acceleration(accelerator)
|
|
198
|
+
except ValueError as exc:
|
|
199
|
+
raise InfraError(f"Invalid service accelerator: {accelerator}") from exc
|
|
200
|
+
return tuple(result.items())
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def example_data(example: Example) -> dict[str, object]:
|
|
204
|
+
"""Present schema-2 requirements using the public dependency vocabulary."""
|
|
205
|
+
data = asdict(example)
|
|
206
|
+
if example.schema_version == 2:
|
|
207
|
+
data["dependencies"] = data.pop("packages")
|
|
208
|
+
return data
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def selected_application(args: argparse.Namespace, request: ProvisionRequest) -> Application | None:
|
|
212
|
+
"""Resolve positional and legacy example flags without changing legacy bare provisioning."""
|
|
213
|
+
if args.example_name and args.example and args.example_name != args.example:
|
|
214
|
+
raise InfraError("Conflicting positional example and --example")
|
|
215
|
+
if args.variant_name and args.variant and args.variant_name != args.variant:
|
|
216
|
+
raise InfraError("Conflicting positional variant and --variant")
|
|
217
|
+
name = args.example_name or args.example
|
|
218
|
+
variant = args.variant_name or args.variant
|
|
219
|
+
# Preserve existing bare provision as a base-profile command. Infer only when
|
|
220
|
+
# the nearest project actually contains a single example and no explicit base intent.
|
|
221
|
+
if not name and not args.example_file and not args.ref:
|
|
222
|
+
if variant:
|
|
223
|
+
raise InfraError("Select an example before selecting a variant")
|
|
224
|
+
if (
|
|
225
|
+
args.base_only
|
|
226
|
+
or args.sinapsis_packages
|
|
227
|
+
or args.deps
|
|
228
|
+
or args.name
|
|
229
|
+
or args.metadata
|
|
230
|
+
or args.no_discovery
|
|
231
|
+
):
|
|
232
|
+
return None
|
|
233
|
+
nearest = next(
|
|
234
|
+
(
|
|
235
|
+
p
|
|
236
|
+
for p in local_projects(Path.cwd())
|
|
237
|
+
if Path.cwd().resolve().is_relative_to(Path(p.path).parent) and p.examples
|
|
238
|
+
),
|
|
239
|
+
None,
|
|
240
|
+
)
|
|
241
|
+
if nearest is None:
|
|
242
|
+
return None
|
|
243
|
+
names = {e.name for e in nearest.examples}
|
|
244
|
+
if len(names) != 1:
|
|
245
|
+
raise InfraError("Multiple examples in this project; select an example name")
|
|
246
|
+
name = next(iter(names))
|
|
247
|
+
project = discover(name, args.example_file, args.ref)
|
|
248
|
+
if name is None:
|
|
249
|
+
names = {e.name for e in project.examples}
|
|
250
|
+
if len(names) != 1:
|
|
251
|
+
raise InfraError("Select an example name")
|
|
252
|
+
name = next(iter(names))
|
|
253
|
+
example = select_variant(project.examples, name, variant)
|
|
254
|
+
# The example is a complete application declaration, not a package metadata catalog.
|
|
255
|
+
request = replace(request, packages_auto=not args.no_discovery and bool(args.metadata))
|
|
256
|
+
return application_request(
|
|
257
|
+
project,
|
|
258
|
+
example,
|
|
259
|
+
request,
|
|
260
|
+
explicit_acceleration=args.acceleration is not None,
|
|
261
|
+
service_accelerators=service_overrides(args.service_accelerator),
|
|
262
|
+
llama_version=args.llama_version,
|
|
263
|
+
)
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
def execute(args: argparse.Namespace) -> dict[str, object]:
|
|
267
|
+
"""Dispatch a command and return serializable results."""
|
|
268
|
+
if args.command == "install":
|
|
269
|
+
local = SubprocessRunner()
|
|
270
|
+
return execute_install(args, local, detect_host(local))
|
|
271
|
+
runner = DockerSDKRunner()
|
|
272
|
+
store = Store(args.root)
|
|
273
|
+
if args.command == "doctor":
|
|
274
|
+
host = detect_host(runner)
|
|
275
|
+
docker: object = None
|
|
276
|
+
if host.docker_cli:
|
|
277
|
+
try:
|
|
278
|
+
docker = asdict(detect_docker(runner))
|
|
279
|
+
except InfraError as exc:
|
|
280
|
+
LOGGER.warning("Docker unavailable: %s", exc)
|
|
281
|
+
docker = {"error": str(exc)}
|
|
282
|
+
return {
|
|
283
|
+
"host": asdict(host),
|
|
284
|
+
"docker": docker,
|
|
285
|
+
"supported_accelerators": ["cpu", "cuda", "apple"],
|
|
286
|
+
"capabilities": list(Capability),
|
|
287
|
+
}
|
|
288
|
+
if args.command == "list":
|
|
289
|
+
if args.ref and args.example_file:
|
|
290
|
+
raise InfraError("--ref and --example-file cannot be combined")
|
|
291
|
+
projects = () if args.ref else local_projects(Path.cwd(), args.example_file)
|
|
292
|
+
entries = [
|
|
293
|
+
{
|
|
294
|
+
"name": e.name,
|
|
295
|
+
"variant": e.variant,
|
|
296
|
+
"default": e.default,
|
|
297
|
+
"kind": p.kind,
|
|
298
|
+
"path": p.path,
|
|
299
|
+
}
|
|
300
|
+
for p in projects
|
|
301
|
+
for e in p.examples
|
|
302
|
+
]
|
|
303
|
+
if entries or args.example_file:
|
|
304
|
+
return {"examples": entries}
|
|
305
|
+
revision, remote = remote_catalog(args.ref or "main")
|
|
306
|
+
return {"revision": revision, "examples": remote}
|
|
307
|
+
if args.command == "info":
|
|
308
|
+
project = discover(args.example_name, args.example_file, args.ref)
|
|
309
|
+
return {
|
|
310
|
+
"kind": project.kind,
|
|
311
|
+
"path": project.path,
|
|
312
|
+
"revision": project.revision,
|
|
313
|
+
"variants": [example_data(e) for e in project.examples if e.name == args.example_name],
|
|
314
|
+
}
|
|
315
|
+
if args.command == "examples":
|
|
316
|
+
return {"examples": [example_data(example) for example in load_examples(args.path)]}
|
|
317
|
+
if args.command == "metadata":
|
|
318
|
+
return asdict(load_metadata(args.path))
|
|
319
|
+
if args.command == "inspect":
|
|
320
|
+
return asdict(store.inspect(args.name))
|
|
321
|
+
if args.command == "verify":
|
|
322
|
+
return asdict(verify_environment(runner, store, store.inspect(args.name)))
|
|
323
|
+
request = make_request(args)
|
|
324
|
+
application = selected_application(args, request)
|
|
325
|
+
if application is None and args.service_accelerator:
|
|
326
|
+
raise InfraError("--service-accelerator requires an example with a service")
|
|
327
|
+
if application is not None:
|
|
328
|
+
manager = (
|
|
329
|
+
None if args.command == "plan" or args.dry_run else InfrastructureManager(runner, store)
|
|
330
|
+
)
|
|
331
|
+
return application_result(application, runner, manager)
|
|
332
|
+
if args.command == "plan" or args.dry_run:
|
|
333
|
+
plan = resolve(runner, request)
|
|
334
|
+
return {"fingerprint": plan.fingerprint, "plan": asdict(plan)}
|
|
335
|
+
return asdict(InfrastructureManager(runner, store).provision(request))
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
def main(argv: list[str] | None = None) -> int:
|
|
339
|
+
"""Run the CLI with consistent error codes and optional diagnostic logging."""
|
|
340
|
+
args = parse_args(argv)
|
|
341
|
+
logging.basicConfig(
|
|
342
|
+
level=logging.DEBUG if args.verbose else logging.INFO,
|
|
343
|
+
format="%(levelname)s: %(message)s",
|
|
344
|
+
stream=sys.stderr,
|
|
345
|
+
)
|
|
346
|
+
try:
|
|
347
|
+
enforce_python_floor()
|
|
348
|
+
if args.command == "run":
|
|
349
|
+
store = Store(args.root)
|
|
350
|
+
argv = tuple(args.argv[1:] if args.argv[:1] == ["--"] else args.argv)
|
|
351
|
+
return run_environment(DockerSDKRunner(), store, store.inspect(args.env), argv)
|
|
352
|
+
result = execute(args)
|
|
353
|
+
except (InfraError, OSError) as exc:
|
|
354
|
+
LOGGER.error("%s", exc, exc_info=args.verbose)
|
|
355
|
+
return 1
|
|
356
|
+
except KeyboardInterrupt:
|
|
357
|
+
LOGGER.warning("Interrupted")
|
|
358
|
+
return 130
|
|
359
|
+
# Text currently uses pretty JSON for easy copy/paste; JSON is compact for automation.
|
|
360
|
+
print(json.dumps(result, indent=2 if args.format == "text" else None, sort_keys=True))
|
|
361
|
+
return 0
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Shared models, errors and command execution."""
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"""Typed subprocess boundary with no shell interpolation."""
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
import subprocess
|
|
5
|
+
import sys
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from typing import Protocol
|
|
8
|
+
|
|
9
|
+
from sinapsis_infra.core.constants import PROBE_TIMEOUT
|
|
10
|
+
from sinapsis_infra.core.errors import CommandError
|
|
11
|
+
|
|
12
|
+
LOGGER = logging.getLogger(__name__)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@dataclass(frozen=True, slots=True)
|
|
16
|
+
class CommandResult:
|
|
17
|
+
"""Captured subprocess result."""
|
|
18
|
+
|
|
19
|
+
returncode: int
|
|
20
|
+
stdout: str
|
|
21
|
+
stderr: str
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class Runner(Protocol):
|
|
25
|
+
"""Injectable command execution boundary."""
|
|
26
|
+
|
|
27
|
+
def run(
|
|
28
|
+
self, args: tuple[str, ...], *, timeout: int = PROBE_TIMEOUT, live: bool = False
|
|
29
|
+
) -> CommandResult:
|
|
30
|
+
"""Run a command, optionally forwarding output to stderr."""
|
|
31
|
+
...
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class SubprocessRunner:
|
|
35
|
+
"""Execute argv directly and forward long-running build output."""
|
|
36
|
+
|
|
37
|
+
def run(
|
|
38
|
+
self, args: tuple[str, ...], *, timeout: int = PROBE_TIMEOUT, live: bool = False
|
|
39
|
+
) -> CommandResult:
|
|
40
|
+
"""Execute an external command and translate process failures."""
|
|
41
|
+
LOGGER.debug("Executing %s", args)
|
|
42
|
+
try:
|
|
43
|
+
result = subprocess.run(
|
|
44
|
+
args,
|
|
45
|
+
check=False,
|
|
46
|
+
text=True,
|
|
47
|
+
encoding="utf-8",
|
|
48
|
+
errors="replace",
|
|
49
|
+
stdin=subprocess.DEVNULL,
|
|
50
|
+
stdout=sys.stderr if live else subprocess.PIPE,
|
|
51
|
+
stderr=sys.stderr if live else subprocess.PIPE,
|
|
52
|
+
timeout=timeout,
|
|
53
|
+
)
|
|
54
|
+
except (OSError, subprocess.TimeoutExpired) as exc:
|
|
55
|
+
raise CommandError(f"Could not execute {args[0]}: {exc}") from exc
|
|
56
|
+
return CommandResult(result.returncode, result.stdout or "", result.stderr or "")
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def checked(
|
|
60
|
+
runner: Runner, args: tuple[str, ...], *, timeout: int = PROBE_TIMEOUT, live: bool = False
|
|
61
|
+
) -> str:
|
|
62
|
+
"""Require success and return stdout, with failure diagnostics."""
|
|
63
|
+
result = runner.run(args, timeout=timeout, live=live)
|
|
64
|
+
if result.returncode:
|
|
65
|
+
detail = result.stderr.strip() or result.stdout.strip() or "See command output above."
|
|
66
|
+
raise CommandError(f"{args[0]} exited {result.returncode}: {detail[-3000:]}")
|
|
67
|
+
return result.stdout.strip()
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def probe(runner: Runner, args: tuple[str, ...]) -> str | None:
|
|
71
|
+
"""Run an optional probe; log diagnostic failures without hiding them."""
|
|
72
|
+
try:
|
|
73
|
+
return checked(runner, args)
|
|
74
|
+
except CommandError as exc:
|
|
75
|
+
LOGGER.debug("Optional probe %s failed: %s", args[0], exc)
|
|
76
|
+
return None
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"""Shared domain and operational constants."""
|
|
2
|
+
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
SCHEMA_VERSION = 2
|
|
6
|
+
STATE_SCHEMA_VERSION = 3
|
|
7
|
+
MIN_PYTHON = (3, 13)
|
|
8
|
+
AUTO = "auto"
|
|
9
|
+
NVIDIA_SMI = "nvidia-smi"
|
|
10
|
+
NVCC = "nvcc"
|
|
11
|
+
DEFAULT_PYTHON = "3.13"
|
|
12
|
+
DEFAULT_ROOT = Path.home() / ".sinx-infra"
|
|
13
|
+
UV = "uv"
|
|
14
|
+
DOCKER = "docker"
|
|
15
|
+
UV_IMAGE = "ghcr.io/astral-sh/uv:0.10.0"
|
|
16
|
+
PYTHON_IMAGE = "docker.io/library/python"
|
|
17
|
+
IMAGE_REPOSITORY = "sinapsis/base"
|
|
18
|
+
PROFILE_LABEL = "tech.sinapsis.infra.profile"
|
|
19
|
+
CONTAINER_ROOT = "/opt/sinx"
|
|
20
|
+
CONTAINER_VENV = f"{CONTAINER_ROOT}/venv"
|
|
21
|
+
MANIFEST_FILE = "infrastructure.json"
|
|
22
|
+
PROBE_TIMEOUT = 30
|
|
23
|
+
RESOLVE_TIMEOUT = 600
|
|
24
|
+
BUILD_TIMEOUT = 7200
|
|
25
|
+
MANAGED_PLATFORM_HINT = (
|
|
26
|
+
"Choose a supported local configuration or use the Sinapsis managed platform."
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
PYPI_INDEX = "https://pypi.org/simple"
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"""User-facing infrastructure failures."""
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class InfraError(Exception):
|
|
5
|
+
"""Base class for expected provisioning errors."""
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class CommandError(InfraError):
|
|
9
|
+
"""An external command could not complete successfully."""
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class CompatibilityError(InfraError):
|
|
13
|
+
"""Requested infrastructure is not supported."""
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class StateError(InfraError):
|
|
17
|
+
"""Managed state is missing, inconsistent or locked."""
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class MetadataError(InfraError):
|
|
21
|
+
"""A local infrastructure declaration is invalid."""
|