|
| 1 | +import collections |
| 2 | +import logging |
| 3 | +import re |
| 4 | +import subprocess |
| 5 | +import threading |
| 6 | +import time |
| 7 | +import uuid |
| 8 | +from typing import Callable |
| 9 | + |
| 10 | +_ANSI_RE = re.compile(r"\x1b\[[0-9;?]*[a-zA-Z]") |
| 11 | +_ERROR_RE = re.compile(r"\b(error|traceback|fatal|exception|failed|segmentation fault|critical)\b", re.IGNORECASE) |
| 12 | +_WARN_RE = re.compile(r"\b(warn|warning|deprecated|retry|retrying)\b", re.IGNORECASE) |
| 13 | +_MAX_LINE = 600 |
| 14 | + |
| 15 | + |
| 16 | +_URL_USERINFO_RE = re.compile(r"(\b[a-zA-Z][a-zA-Z0-9+.\-]*://)([^/\s:@]+):([^@\s/]+)@") |
| 17 | +_QUERY_SECRET_RE = re.compile( |
| 18 | + r"([?&](?:token|key|api_key|apikey|access_token|password|passwd|psk|secret)=)[^&\s\"']+", re.IGNORECASE |
| 19 | +) |
| 20 | +_KV_SECRET_RE = re.compile( |
| 21 | + r"\b(password|passwd|psk|secret|token|api_key|apikey)\b(\s*[=:]\s*)(\"?)[^\s\"',;]+", re.IGNORECASE |
| 22 | +) |
| 23 | +_BEARER_RE = re.compile(r"(\bBearer\s+)[A-Za-z0-9._~+/=\-]+", re.IGNORECASE) |
| 24 | +_MASK = "••••" |
| 25 | + |
| 26 | + |
| 27 | +def strip_ansi(text: str) -> str: |
| 28 | + return _ANSI_RE.sub("", text) |
| 29 | + |
| 30 | + |
| 31 | +def redact(text: str, known_secrets: tuple[str, ...] = ()) -> str: |
| 32 | + """Hide credentials before a line can reach a screen or a network client.""" |
| 33 | + for secret in known_secrets: |
| 34 | + if secret in text: |
| 35 | + text = text.replace(secret, _MASK) |
| 36 | + text = _URL_USERINFO_RE.sub(r"\1***:***@", text) |
| 37 | + text = _QUERY_SECRET_RE.sub(rf"\1{_MASK}", text) |
| 38 | + text = _KV_SECRET_RE.sub(rf"\1\2\3{_MASK}", text) |
| 39 | + return _BEARER_RE.sub(rf"\1{_MASK}", text) |
| 40 | + |
| 41 | + |
| 42 | +def classify_level(text: str, default: str = "info") -> str: |
| 43 | + if _ERROR_RE.search(text): |
| 44 | + return "error" |
| 45 | + if _WARN_RE.search(text): |
| 46 | + return "warn" |
| 47 | + return default |
| 48 | + |
| 49 | + |
| 50 | +class BootEventLog: |
| 51 | + """Thread-safe ring buffer of boot events that any number of consumers can follow live.""" |
| 52 | + |
| 53 | + def __init__(self, max_events: int = 4000): |
| 54 | + self.epoch = uuid.uuid4().hex[:12] |
| 55 | + self._events: collections.deque[dict] = collections.deque(maxlen=max_events) |
| 56 | + self._seq = 0 |
| 57 | + self._lock = threading.Lock() |
| 58 | + self._subscribers: list[Callable[[dict], None]] = [] |
| 59 | + self._known_secrets: tuple[str, ...] = () |
| 60 | + |
| 61 | + def set_known_secrets(self, values) -> None: |
| 62 | + cleaned = {str(v) for v in values if v and len(str(v)) >= 6} |
| 63 | + self._known_secrets = tuple(sorted(cleaned, key=len, reverse=True)) |
| 64 | + |
| 65 | + def emit(self, source: str, text: str, level: str = "info") -> dict | None: |
| 66 | + text = redact(strip_ansi(text).rstrip(), self._known_secrets) |
| 67 | + if not text: |
| 68 | + return None |
| 69 | + if len(text) > _MAX_LINE: |
| 70 | + text = text[:_MAX_LINE] + "…" |
| 71 | + with self._lock: |
| 72 | + self._seq += 1 |
| 73 | + event = {"seq": self._seq, "ts": time.time(), "source": source, "level": level, "text": text} |
| 74 | + self._events.append(event) |
| 75 | + subscribers = list(self._subscribers) |
| 76 | + for callback in subscribers: |
| 77 | + try: |
| 78 | + callback(event) |
| 79 | + except Exception: |
| 80 | + pass |
| 81 | + return event |
| 82 | + |
| 83 | + def tail(self, after_seq: int = 0, limit: int = 500) -> list[dict]: |
| 84 | + with self._lock: |
| 85 | + events = [e for e in self._events if e["seq"] > after_seq] |
| 86 | + return events[-limit:] |
| 87 | + |
| 88 | + def subscribe(self, callback: Callable[[dict], None]) -> None: |
| 89 | + with self._lock: |
| 90 | + self._subscribers.append(callback) |
| 91 | + |
| 92 | + def unsubscribe(self, callback: Callable[[dict], None]) -> None: |
| 93 | + with self._lock: |
| 94 | + if callback in self._subscribers: |
| 95 | + self._subscribers.remove(callback) |
| 96 | + |
| 97 | + @property |
| 98 | + def last_seq(self) -> int: |
| 99 | + with self._lock: |
| 100 | + return self._seq |
| 101 | + |
| 102 | + |
| 103 | +BOOT_LOG = BootEventLog() |
| 104 | + |
| 105 | + |
| 106 | +def forward_service_line(name: str, raw: str, log: BootEventLog | None = None) -> None: |
| 107 | + """Mirror a LogSink line ('<ts> <stream> <text>') into the boot event log.""" |
| 108 | + from robot_orchestrator.supervisor.logsink import parse_line |
| 109 | + |
| 110 | + parsed = parse_line(name, raw) |
| 111 | + (log or BOOT_LOG).emit(name, parsed["text"], classify_level(parsed["text"])) |
| 112 | + |
| 113 | + |
| 114 | +class BootLogHandler(logging.Handler): |
| 115 | + """Forwards orchestrator log records (not uvicorn/asyncio chatter) to the boot event log.""" |
| 116 | + |
| 117 | + def __init__(self, log: BootEventLog | None = None): |
| 118 | + super().__init__(level=logging.INFO) |
| 119 | + self._log = log or BOOT_LOG |
| 120 | + |
| 121 | + def emit(self, record: logging.LogRecord) -> None: |
| 122 | + if record.name.startswith(("uvicorn", "asyncio", "http")): |
| 123 | + return |
| 124 | + try: |
| 125 | + level = {"WARNING": "warn", "ERROR": "error", "CRITICAL": "error"}.get(record.levelname, "info") |
| 126 | + self._log.emit("orchestrator", record.getMessage(), level) |
| 127 | + except Exception: |
| 128 | + pass |
| 129 | + |
| 130 | + |
| 131 | +def _mask_values(text: str, mask: tuple[str, ...]) -> str: |
| 132 | + for value in mask: |
| 133 | + if value: |
| 134 | + text = text.replace(value, _MASK) |
| 135 | + return text |
| 136 | + |
| 137 | + |
| 138 | +def _pump(stream, source: str, sink: list[str], log: BootEventLog, mask: tuple[str, ...]) -> None: |
| 139 | + last = None |
| 140 | + try: |
| 141 | + for raw in stream: |
| 142 | + sink.append(raw) |
| 143 | + line = _mask_values(strip_ansi(raw).rstrip(), mask) |
| 144 | + if not line or line == last: |
| 145 | + continue |
| 146 | + last = line |
| 147 | + log.emit(source, line, classify_level(line)) |
| 148 | + except (ValueError, OSError): |
| 149 | + pass |
| 150 | + |
| 151 | + |
| 152 | +def _describe(argv, mask: tuple[str, ...] = ()) -> str: |
| 153 | + text = argv if isinstance(argv, str) else " ".join(str(part) for part in argv) |
| 154 | + text = _mask_values(text, mask) |
| 155 | + return text if len(text) <= 240 else text[:237] + "..." |
| 156 | + |
| 157 | + |
| 158 | +def logged_run( |
| 159 | + argv, |
| 160 | + *, |
| 161 | + source: str, |
| 162 | + cwd=None, |
| 163 | + env=None, |
| 164 | + timeout: float | None = None, |
| 165 | + check: bool = False, |
| 166 | + shell: bool = False, |
| 167 | + announce: bool = True, |
| 168 | + mask: tuple[str, ...] = (), |
| 169 | + log: BootEventLog | None = None, |
| 170 | +) -> subprocess.CompletedProcess: |
| 171 | + """Drop-in for subprocess.run(capture_output=True, text=True) that also streams every output line live.""" |
| 172 | + log = log or BOOT_LOG |
| 173 | + if announce: |
| 174 | + log.emit(source, f"$ {_describe(argv, mask)}", "cmd") |
| 175 | + |
| 176 | + process = subprocess.Popen( |
| 177 | + argv, cwd=cwd, env=env, shell=shell, |
| 178 | + stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 179 | + text=True, encoding="utf-8", errors="replace", bufsize=1, |
| 180 | + ) |
| 181 | + out_lines: list[str] = [] |
| 182 | + err_lines: list[str] = [] |
| 183 | + threads = [ |
| 184 | + threading.Thread(target=_pump, args=(process.stdout, source, out_lines, log, mask), daemon=True), |
| 185 | + threading.Thread(target=_pump, args=(process.stderr, source, err_lines, log, mask), daemon=True), |
| 186 | + ] |
| 187 | + for thread in threads: |
| 188 | + thread.start() |
| 189 | + |
| 190 | + try: |
| 191 | + process.wait(timeout=timeout) |
| 192 | + except subprocess.TimeoutExpired: |
| 193 | + process.kill() |
| 194 | + process.wait() |
| 195 | + for thread in threads: |
| 196 | + thread.join(timeout=2.0) |
| 197 | + log.emit(source, f"таймаут {timeout}s: процесс убит", "error") |
| 198 | + raise subprocess.TimeoutExpired(argv, timeout, output="".join(out_lines), stderr="".join(err_lines)) |
| 199 | + except BaseException: |
| 200 | + process.kill() |
| 201 | + process.wait() |
| 202 | + raise |
| 203 | + |
| 204 | + for thread in threads: |
| 205 | + thread.join(timeout=5.0) |
| 206 | + |
| 207 | + stdout, stderr = "".join(out_lines), "".join(err_lines) |
| 208 | + if announce and process.returncode != 0: |
| 209 | + log.emit(source, f"завершилось с кодом {process.returncode}", "error") |
| 210 | + if check and process.returncode != 0: |
| 211 | + raise subprocess.CalledProcessError(process.returncode, argv, output=stdout, stderr=stderr) |
| 212 | + return subprocess.CompletedProcess(argv, process.returncode, stdout, stderr) |
0 commit comments