Checks node-local prerequisites that break Hermes after relocation: direct TLS egress to api.telegram.org and real NFS read/write against the Hermes agent export. Removes the tester self-probe that caused every round to crash. Tests: compileall, unittest, git diff --check. Reviewed-on: #50 Co-authored-by: Alex Rudenk <rudolfss13+hermes@gmail.com>
72 lines
2.1 KiB
Python
72 lines
2.1 KiB
Python
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import socket
|
|
from dataclasses import dataclass
|
|
from typing import Iterable, List, Tuple
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class HostCheck:
|
|
name: str
|
|
address: str
|
|
protocol: str
|
|
port: int
|
|
|
|
|
|
def parse_peers(value: str) -> List[Tuple[str, str]]:
|
|
peers = []
|
|
for item in value.split(","):
|
|
item = item.strip()
|
|
if "=" not in item:
|
|
continue
|
|
name, address = (part.strip() for part in item.split("=", 1))
|
|
if name and address:
|
|
peers.append((name, address))
|
|
return peers
|
|
|
|
|
|
def build_host_checks(
|
|
peers: Iterable[Tuple[str, str]],
|
|
tcp_ports: Iterable[int],
|
|
udp_ports: Iterable[int],
|
|
) -> List[Tuple[str, str, str, int]]:
|
|
checks = []
|
|
for name, address in peers:
|
|
checks.extend((name, address, "tcp", port) for port in tcp_ports)
|
|
checks.extend((name, address, "udp", port) for port in udp_ports)
|
|
return checks
|
|
|
|
|
|
async def tcp_probe(address: str, port: int, timeout: float) -> Tuple[bool, str]:
|
|
try:
|
|
_, writer = await asyncio.wait_for(
|
|
asyncio.open_connection(address, port), timeout=timeout
|
|
)
|
|
writer.close()
|
|
try:
|
|
await writer.wait_closed()
|
|
except Exception:
|
|
pass
|
|
return True, "tcp connect ok"
|
|
except asyncio.TimeoutError:
|
|
return False, "timeout"
|
|
except OSError as exc:
|
|
return False, f"{type(exc).__name__}: {exc}"
|
|
|
|
|
|
async def udp_probe(address: str, port: int, timeout: float) -> Tuple[bool, str]:
|
|
loop = asyncio.get_running_loop()
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
sock.setblocking(False)
|
|
try:
|
|
await asyncio.wait_for(loop.sock_connect(sock, (address, port)), timeout)
|
|
await asyncio.wait_for(loop.sock_sendall(sock, b"connectivity-tester\n"), timeout)
|
|
return True, "udp datagram sent; no handshake available"
|
|
except asyncio.TimeoutError:
|
|
return False, "timeout"
|
|
except OSError as exc:
|
|
return False, f"{type(exc).__name__}: {exc}"
|
|
finally:
|
|
sock.close()
|