|
| 1 | +"""Pure topology projection for Ulysses discovery observations.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +from src.sandwich_runtime import SandwichInstallation |
| 8 | +from src.ulysses_discovery import HostDiscoverySnapshot |
| 9 | +from src.ulysses_runtime import ( |
| 10 | + RuntimeAdapter, |
| 11 | + RuntimeDefinition, |
| 12 | + RuntimeRegistry, |
| 13 | + RuntimeStatus, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +def _path_matches(left: Path | None, right: Path | None) -> bool: |
| 18 | + if left is None or right is None: |
| 19 | + return False |
| 20 | + return left.resolve() == right.resolve() |
| 21 | + |
| 22 | + |
| 23 | +def _runtime_status( |
| 24 | + definition: RuntimeDefinition, |
| 25 | + snapshot: HostDiscoverySnapshot, |
| 26 | + sandwich: SandwichInstallation, |
| 27 | +) -> tuple[RuntimeStatus, tuple[int, ...]]: |
| 28 | + active_ports = tuple( |
| 29 | + sorted( |
| 30 | + { |
| 31 | + socket.port |
| 32 | + for socket in snapshot.listening_sockets |
| 33 | + if any(binding.port == socket.port for binding in definition.ports) |
| 34 | + } |
| 35 | + ) |
| 36 | + ) |
| 37 | + if definition.runtime_id == "sandwich.runtime": |
| 38 | + return ( |
| 39 | + RuntimeStatus.RUNNING if sandwich.installed else RuntimeStatus.STOPPED, |
| 40 | + (), |
| 41 | + ) |
| 42 | + if active_ports: |
| 43 | + return RuntimeStatus.RUNNING, active_ports |
| 44 | + |
| 45 | + if definition.adapter is RuntimeAdapter.SYSTEMD_USER: |
| 46 | + expected = f"{definition.runtime_id.replace('.', '-')}.service" |
| 47 | + unit = next( |
| 48 | + (item for item in snapshot.systemd_user_units if item.unit == expected), |
| 49 | + None, |
| 50 | + ) |
| 51 | + if unit is not None: |
| 52 | + if unit.active == "active": |
| 53 | + return RuntimeStatus.RUNNING, () |
| 54 | + if unit.active == "failed" or unit.sub == "failed": |
| 55 | + return RuntimeStatus.FAILED, () |
| 56 | + return RuntimeStatus.STOPPED, () |
| 57 | + |
| 58 | + if definition.adapter is RuntimeAdapter.DOCKER_COMPOSE: |
| 59 | + containers = tuple( |
| 60 | + item |
| 61 | + for item in snapshot.compose_containers |
| 62 | + if _path_matches(item.working_dir, definition.source_root) |
| 63 | + ) |
| 64 | + if containers: |
| 65 | + statuses = tuple(item.status.lower() for item in containers) |
| 66 | + if any("unhealthy" in status for status in statuses): |
| 67 | + return RuntimeStatus.DEGRADED, () |
| 68 | + if any(status.startswith("up") for status in statuses): |
| 69 | + return RuntimeStatus.RUNNING, () |
| 70 | + if all( |
| 71 | + status.startswith(("exited", "created", "dead")) |
| 72 | + for status in statuses |
| 73 | + ): |
| 74 | + return RuntimeStatus.STOPPED, () |
| 75 | + |
| 76 | + if definition.ports and not any( |
| 77 | + issue.source == "sockets" for issue in snapshot.issues |
| 78 | + ): |
| 79 | + return RuntimeStatus.STOPPED, () |
| 80 | + return RuntimeStatus.UNKNOWN, () |
| 81 | + |
| 82 | + |
| 83 | +def _runtime_payload( |
| 84 | + definition: RuntimeDefinition, |
| 85 | + snapshot: HostDiscoverySnapshot, |
| 86 | + sandwich: SandwichInstallation, |
| 87 | +) -> dict: |
| 88 | + status, active_ports = _runtime_status(definition, snapshot, sandwich) |
| 89 | + return { |
| 90 | + "runtime_id": definition.runtime_id, |
| 91 | + "label": definition.label, |
| 92 | + "adapter": definition.adapter.value, |
| 93 | + "ownership": definition.ownership.value, |
| 94 | + "scope": definition.scope.value, |
| 95 | + "status": status.value, |
| 96 | + "source_root": ( |
| 97 | + str(definition.source_root) if definition.source_root is not None else None |
| 98 | + ), |
| 99 | + "source_exists": ( |
| 100 | + definition.source_root.exists() |
| 101 | + if definition.source_root is not None |
| 102 | + else None |
| 103 | + ), |
| 104 | + "ports": [ |
| 105 | + { |
| 106 | + "port": binding.port, |
| 107 | + "protocol": binding.protocol, |
| 108 | + "host": binding.host, |
| 109 | + "active": binding.port in active_ports, |
| 110 | + } |
| 111 | + for binding in definition.ports |
| 112 | + ], |
| 113 | + "dependencies": list(definition.dependencies), |
| 114 | + "capabilities": list(definition.capabilities), |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | +def build_topology_report( |
| 119 | + registry: RuntimeRegistry, |
| 120 | + snapshot: HostDiscoverySnapshot, |
| 121 | + sandwich: SandwichInstallation, |
| 122 | +) -> dict: |
| 123 | + """Create the stable, secret-free response consumed by the Services UI.""" |
| 124 | + |
| 125 | + runtimes = [ |
| 126 | + _runtime_payload(definition, snapshot, sandwich) |
| 127 | + for definition in registry.definitions() |
| 128 | + ] |
| 129 | + counts = { |
| 130 | + status.value: sum(item["status"] == status.value for item in runtimes) |
| 131 | + for status in RuntimeStatus |
| 132 | + } |
| 133 | + return { |
| 134 | + "schema_version": "ulysses.topology.v1", |
| 135 | + "observed_at": snapshot.observed_at, |
| 136 | + "counts": counts, |
| 137 | + "runtimes": runtimes, |
| 138 | + "javascript_runtime": { |
| 139 | + "id": "sandwich", |
| 140 | + "installed": sandwich.installed, |
| 141 | + "source_root": ( |
| 142 | + str(sandwich.source_root) |
| 143 | + if sandwich.source_root is not None |
| 144 | + else None |
| 145 | + ), |
| 146 | + "commands": { |
| 147 | + name: str(path) |
| 148 | + for name, path in sorted(sandwich.command_paths.items()) |
| 149 | + }, |
| 150 | + "missing_commands": list(sandwich.missing_commands), |
| 151 | + }, |
| 152 | + "compose": [ |
| 153 | + { |
| 154 | + "container_id": item.container_id, |
| 155 | + "name": item.name, |
| 156 | + "status": item.status, |
| 157 | + "project": item.project, |
| 158 | + "working_dir": ( |
| 159 | + str(item.working_dir) if item.working_dir is not None else None |
| 160 | + ), |
| 161 | + "ports": item.ports, |
| 162 | + } |
| 163 | + for item in snapshot.compose_containers |
| 164 | + ], |
| 165 | + "tmux": [ |
| 166 | + { |
| 167 | + "name": item.name, |
| 168 | + "attached": item.attached, |
| 169 | + "windows": item.windows, |
| 170 | + "created_at": item.created_at, |
| 171 | + } |
| 172 | + for item in snapshot.tmux_sessions |
| 173 | + ], |
| 174 | + "systemd_user": [ |
| 175 | + { |
| 176 | + "unit": item.unit, |
| 177 | + "load": item.load, |
| 178 | + "active": item.active, |
| 179 | + "sub": item.sub, |
| 180 | + "description": item.description, |
| 181 | + } |
| 182 | + for item in snapshot.systemd_user_units |
| 183 | + ], |
| 184 | + "issues": [ |
| 185 | + {"source": issue.source, "detail": issue.detail} |
| 186 | + for issue in snapshot.issues |
| 187 | + ], |
| 188 | + } |
0 commit comments