hegemony-step-sdk 0.43.1__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.
@@ -0,0 +1,132 @@
1
+ # SPDX-FileCopyrightText: 2025-2026 Rexonix s.r.o.
2
+ # SPDX-FileContributor: Jakub Trávník <jakub.travnik@gmail.com>
3
+ #
4
+ # SPDX-License-Identifier: AGPL-3.0-or-later
5
+
6
+ """Public SDK for Hegemony step-handler plugins.
7
+
8
+ Dependency-light (pydantic only). Out-of-tree plugin wheels depend on this package
9
+ and never import Hegemony app internals. A plugin exposes a ``register(registry)``
10
+ callable under the ``hegemony.step_handlers`` entry-point group and registers
11
+ :class:`BaseHandler` subclasses; handlers reach every platform facility through
12
+ ``ctx.services`` (:class:`HandlerServices`).
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ from ._version import SDK_ABI_VERSION, __version__
18
+ from .concurrency import (
19
+ MAX_PARALLEL_DEVICES_LIMIT,
20
+ for_each_target_device,
21
+ max_parallel_from_config,
22
+ )
23
+ from .contract import (
24
+ DEFAULT_DEVICE_PLATFORM,
25
+ DEFAULT_RUN_TIMEOUT_MARGIN_SECONDS,
26
+ MAX_CHAIN_OUTPUT_CHARS,
27
+ OUTPUT_EVENT_CHUNK_CHARS,
28
+ OUTPUT_EVENT_MAX_CHARS,
29
+ OUTPUT_MILESTONE,
30
+ OUTPUT_TRUNCATION_NOTICE,
31
+ BaseHandler,
32
+ HandlerContext,
33
+ HandlerResult,
34
+ HandlerTargeting,
35
+ InvalidConfigError,
36
+ command_label,
37
+ resolve_target_devices_for_roles,
38
+ )
39
+ from .enums import StepKind
40
+ from .probes import (
41
+ PROBE_ENTRY_POINT_GROUP,
42
+ BaseProbe,
43
+ ProbeRegistry,
44
+ ProbeResult,
45
+ )
46
+ from .registry import StepHandlerRegistry
47
+ from .services import (
48
+ BUILD_NETWORK_DEFAULT,
49
+ BUILD_NETWORK_NONE,
50
+ CommandResult,
51
+ ContainerRuntime,
52
+ FileDownload,
53
+ HandlerServices,
54
+ ImageBuild,
55
+ ImageCache,
56
+ SecretWriteResult,
57
+ ShellResult,
58
+ ShellTransport,
59
+ StateBackendAccess,
60
+ Transport,
61
+ )
62
+ from .transports import (
63
+ DEFAULT_DEVICE_MGMT_PORT,
64
+ TRANSPORT_ENTRY_POINT_GROUP,
65
+ ConnectionCancellationRegistry,
66
+ DeviceConnectionSpec,
67
+ DeviceCredentials,
68
+ HostKeyRejectedError,
69
+ HostKeyVerifier,
70
+ JumpHostSpec,
71
+ SshAlgorithmRecorder,
72
+ SshAlgorithms,
73
+ TransportOutputSink,
74
+ TransportRegistry,
75
+ )
76
+
77
+ #: The entry-point group out-of-tree step-handler plugins register under.
78
+ STEP_HANDLER_ENTRY_POINT_GROUP = "hegemony.step_handlers"
79
+
80
+ __all__ = [
81
+ "BUILD_NETWORK_DEFAULT",
82
+ "BUILD_NETWORK_NONE",
83
+ "DEFAULT_DEVICE_MGMT_PORT",
84
+ "DEFAULT_DEVICE_PLATFORM",
85
+ "DEFAULT_RUN_TIMEOUT_MARGIN_SECONDS",
86
+ "MAX_CHAIN_OUTPUT_CHARS",
87
+ "MAX_PARALLEL_DEVICES_LIMIT",
88
+ "OUTPUT_EVENT_CHUNK_CHARS",
89
+ "OUTPUT_EVENT_MAX_CHARS",
90
+ "OUTPUT_MILESTONE",
91
+ "OUTPUT_TRUNCATION_NOTICE",
92
+ "PROBE_ENTRY_POINT_GROUP",
93
+ "SDK_ABI_VERSION",
94
+ "STEP_HANDLER_ENTRY_POINT_GROUP",
95
+ "TRANSPORT_ENTRY_POINT_GROUP",
96
+ "BaseHandler",
97
+ "command_label",
98
+ "BaseProbe",
99
+ "CommandResult",
100
+ "ConnectionCancellationRegistry",
101
+ "ContainerRuntime",
102
+ "ImageBuild",
103
+ "ImageCache",
104
+ "DeviceConnectionSpec",
105
+ "DeviceCredentials",
106
+ "FileDownload",
107
+ "HandlerContext",
108
+ "HandlerResult",
109
+ "HandlerServices",
110
+ "HandlerTargeting",
111
+ "HostKeyRejectedError",
112
+ "HostKeyVerifier",
113
+ "InvalidConfigError",
114
+ "JumpHostSpec",
115
+ "ProbeRegistry",
116
+ "ProbeResult",
117
+ "SecretWriteResult",
118
+ "ShellResult",
119
+ "SshAlgorithmRecorder",
120
+ "SshAlgorithms",
121
+ "ShellTransport",
122
+ "StateBackendAccess",
123
+ "StepHandlerRegistry",
124
+ "StepKind",
125
+ "Transport",
126
+ "TransportOutputSink",
127
+ "TransportRegistry",
128
+ "__version__",
129
+ "for_each_target_device",
130
+ "max_parallel_from_config",
131
+ "resolve_target_devices_for_roles",
132
+ ]
@@ -0,0 +1,16 @@
1
+ # SPDX-FileCopyrightText: 2025-2026 Rexonix s.r.o.
2
+ # SPDX-FileContributor: Jakub Trávník <jakub.travnik@gmail.com>
3
+ #
4
+ # SPDX-License-Identifier: AGPL-3.0-or-later
5
+
6
+ """SDK version and ABI marker.
7
+
8
+ ``SDK_ABI_VERSION`` is bumped only when the plugin-facing registration contract changes
9
+ incompatibly. The core platform compares it against the version it was built for and can
10
+ refuse to load plugins from a newer, incompatible ABI.
11
+ """
12
+
13
+ from __future__ import annotations
14
+
15
+ __version__ = "0.43.1"
16
+ SDK_ABI_VERSION = 1
@@ -0,0 +1,85 @@
1
+ # SPDX-FileCopyrightText: 2025-2026 Rexonix s.r.o.
2
+ # SPDX-FileContributor: Jakub Trávník <jakub.travnik@gmail.com>
3
+ #
4
+ # SPDX-License-Identifier: AGPL-3.0-or-later
5
+
6
+ """Bounded per-device concurrency for multi-device handlers.
7
+
8
+ Handlers that iterate target devices can opt into parallel processing via a
9
+ ``max_parallel_devices`` config value (default 1 = strictly sequential, the
10
+ classic loop, unless the handler passes its own default to
11
+ :func:`max_parallel_from_config`). :func:`for_each_target_device` bounds
12
+ concurrency with a semaphore and returns results in device order, so
13
+ aggregation stays deterministic regardless of completion order. Live output from concurrent
14
+ devices interleaves in the Logs tab, where lines are already labelled with
15
+ ``attrs.device_name``.
16
+ """
17
+
18
+ from __future__ import annotations
19
+
20
+ import asyncio
21
+ from collections.abc import Awaitable, Callable, Mapping, Sequence
22
+ from typing import Any, TypeVar
23
+
24
+ R = TypeVar("R")
25
+
26
+ #: Upper bound for ``max_parallel_devices`` — matches the default executor
27
+ #: thread pool headroom the blocking (netmiko) transport runs in.
28
+ MAX_PARALLEL_DEVICES_LIMIT = 32
29
+
30
+
31
+ def max_parallel_from_config(config: Mapping[str, Any], *, default: int = 1) -> int:
32
+ """Read ``max_parallel_devices`` from a step config, clamped and safe.
33
+
34
+ A missing or empty value means ``default``: 1 (sequential) unless the
35
+ handler declares another default for its field. Anything unparsable
36
+ degrades to 1 (sequential) rather than failing the step over a
37
+ display-level knob.
38
+ """
39
+ value = config.get("max_parallel_devices")
40
+ if value is None or value == "":
41
+ value = default
42
+ try:
43
+ value = int(value)
44
+ except (TypeError, ValueError):
45
+ return 1
46
+ return max(1, min(value, MAX_PARALLEL_DEVICES_LIMIT))
47
+
48
+
49
+ async def for_each_target_device(
50
+ devices: Sequence[dict[str, Any]],
51
+ worker: Callable[[dict[str, Any]], Awaitable[R]],
52
+ *,
53
+ max_parallel: int = 1,
54
+ ) -> list[R]:
55
+ """Run ``await worker(device)`` for every device with bounded concurrency.
56
+
57
+ ``max_parallel <= 1`` runs strictly sequentially — identical semantics to
58
+ the classic per-device loop, including early visibility of each device's
59
+ progress before the next starts. Above 1, at most ``max_parallel``
60
+ workers run at once and results are returned in device order.
61
+
62
+ Workers are expected to handle their own per-device errors (the
63
+ sequential loops always did); an exception escaping a worker propagates
64
+ and cancels the remaining concurrent workers (``asyncio.gather`` alone
65
+ would leave them running, so cancellation is explicit here).
66
+ """
67
+ if max_parallel <= 1:
68
+ return [await worker(device) for device in devices]
69
+
70
+ semaphore = asyncio.Semaphore(max_parallel)
71
+
72
+ async def _bounded(device: dict[str, Any]) -> R:
73
+ async with semaphore:
74
+ return await worker(device)
75
+
76
+ tasks = [asyncio.ensure_future(_bounded(device)) for device in devices]
77
+ try:
78
+ return list(await asyncio.gather(*tasks))
79
+ except BaseException:
80
+ for task in tasks:
81
+ task.cancel()
82
+ # Reap every task so cancellations complete and nothing leaks into
83
+ # the event loop's unhandled-exception hook.
84
+ await asyncio.gather(*tasks, return_exceptions=True)
85
+ raise