mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-21 23:12:25 +01:00
Some checks failed
dependency freshness / dependencies on newest stable (push) Has been cancelled
file length / file-length (push) Has been cancelled
md-naming / markdown naming convention (push) Has been cancelled
omniroute_offload pre-commit / pre-commit (push) Has been cancelled
palette drift / palette (push) Has been cancelled
repo-contract / repo contract (push) Has been cancelled
shared gate tests / gate unit tests (push) Has been cancelled
omniroute-gateway start|health|update|backup|combo keeps the local OmniRoute gateway up. systemd --user timers: health every 10 min (real free-best chat; restart <=1/30 min, reactivate providers whose cooldown ended, notify once per outage), update weekly Sun 12:00 (backup, newest stable -web tag, rollback of image + volume on a failed health check). Failure modes: health/update fail loudly (exit 1 + desktop + Signal); all providers cooling = degraded (exit 0, one notice/day); a missing signal-bot .env logs and exits non-zero on alert paths; health skips while update holds the lock. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WzxdWZSdJmNjGUvLnHJ4WG
101 lines
3.3 KiB
Python
101 lines
3.3 KiB
Python
"""One OmniRoute gateway: container, volume, port, lock and state, read at call time."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import fcntl
|
|
import json
|
|
import os
|
|
import re
|
|
from contextlib import contextmanager
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
from typing import TYPE_CHECKING, Any
|
|
|
|
if TYPE_CHECKING:
|
|
from collections.abc import Iterator
|
|
|
|
REPO = "diegosouzapw/omniroute"
|
|
DEFAULT_IMAGE = f"{REPO}:3.8.52-web"
|
|
STABLE_TAG = re.compile(r"^(\d+)\.(\d+)\.(\d+)-web$")
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Config:
|
|
"""One gateway instance (live: omniroute on 20128)."""
|
|
|
|
name: str
|
|
volume: str
|
|
port: int
|
|
base_url: str
|
|
conf_dir: Path
|
|
state_dir: Path
|
|
backup_dir: Path
|
|
signal_env: Path
|
|
|
|
@classmethod
|
|
def from_env(cls, env: dict[str, str] | None = None) -> Config:
|
|
"""Read OMNIROUTE_NAME / _VOLUME / _PORT / _SIGNAL_ENV and HOME."""
|
|
env = dict(os.environ) if env is None else env
|
|
home = Path(env.get("HOME", str(Path.home())))
|
|
name = env.get("OMNIROUTE_NAME", "omniroute")
|
|
port = int(env.get("OMNIROUTE_PORT", "20128"))
|
|
signal = env.get("OMNIROUTE_SIGNAL_ENV", str(home / "src/signal-bot/.env"))
|
|
return cls(
|
|
name=name,
|
|
volume=env.get("OMNIROUTE_VOLUME", "omniroute-data"),
|
|
port=port,
|
|
base_url=f"http://127.0.0.1:{port}",
|
|
conf_dir=home / ".config/omniroute",
|
|
state_dir=home / ".local/state/omniroute-gateway" / name,
|
|
backup_dir=home / "data/backups/omniroute" / name,
|
|
signal_env=Path(signal),
|
|
)
|
|
|
|
def image(self) -> str:
|
|
"""Return the pinned image, or the repo default before any update."""
|
|
try:
|
|
pinned = (self.state_dir / "image").read_text().strip()
|
|
except FileNotFoundError:
|
|
return DEFAULT_IMAGE
|
|
return pinned or DEFAULT_IMAGE
|
|
|
|
|
|
def version(tag: str) -> tuple[int, int, int] | None:
|
|
"""Return (major, minor, patch) of a stable '-web' tag or image ref, else None."""
|
|
match = STABLE_TAG.match(tag.rsplit(":", 1)[-1])
|
|
if not match:
|
|
return None
|
|
major, minor, patch = (int(g) for g in match.groups())
|
|
return major, minor, patch
|
|
|
|
|
|
@contextmanager
|
|
def locked(cfg: Config, *, blocking: bool) -> Iterator[bool]:
|
|
"""Hold the instance lock; yields False when non-blocking and already held."""
|
|
cfg.state_dir.mkdir(parents=True, exist_ok=True)
|
|
with (cfg.state_dir / "lock").open("w") as handle:
|
|
flags = fcntl.LOCK_EX | (0 if blocking else fcntl.LOCK_NB)
|
|
try:
|
|
fcntl.flock(handle, flags)
|
|
except BlockingIOError:
|
|
yield False
|
|
return
|
|
yield True
|
|
|
|
|
|
def load_state(cfg: Config) -> dict[str, Any]:
|
|
"""Return alert state; empty when missing or unreadable."""
|
|
try:
|
|
data = json.loads((cfg.state_dir / "alert.json").read_text())
|
|
except OSError, json.JSONDecodeError:
|
|
return {}
|
|
return data if isinstance(data, dict) else {}
|
|
|
|
|
|
def save_state(cfg: Config, data: dict[str, Any], image: str | None = None) -> None:
|
|
"""Persist alert state, and the newly running image when given."""
|
|
cfg.state_dir.mkdir(parents=True, exist_ok=True)
|
|
(cfg.state_dir / "alert.json").write_text(json.dumps(data, sort_keys=True))
|
|
if image:
|
|
(cfg.state_dir / "image").write_text(f"{image}\n")
|