| | |
| | import os |
| | import re |
| | import socket |
| | from datetime import datetime |
| |
|
| | SECRETS_DIR = os.environ.get("SECRETS_DIR", "/data/adaptai/secrets/dataops") |
| | BASE_PATH = os.path.join(SECRETS_DIR, "connections.yaml") |
| | EFFECTIVE_PATH = os.path.join(SECRETS_DIR, "effective_connections.yaml") |
| |
|
| |
|
| | def _can_resolve(host: str) -> bool: |
| | try: |
| | socket.gethostbyname(host) |
| | return True |
| | except Exception: |
| | return False |
| |
|
| |
|
| | def detect_mode(default="auto") -> str: |
| | mode = os.environ.get("DBOPS_MODE", default).lower() |
| | if mode in ("central", "local"): |
| | return mode |
| | |
| | return "central" if _can_resolve("qdrant.dbops.local") else "local" |
| |
|
| |
|
| | def render(base_text: str, mode: str) -> str: |
| | lines = base_text.splitlines() |
| | out = [] |
| | in_redis_nodes = False |
| | in_dfly_nodes = False |
| | for ln in lines: |
| | line = ln |
| | if mode == "local": |
| | |
| | line = re.sub(r"^(\s*host:\s*)qdrant\.dbops\.local\b", r"\1localhost", line) |
| | |
| | line = re.sub(r"^(\s*gremlin_host:\s*)gremlin\.dbops\.local\b", r"\1localhost", line) |
| |
|
| | |
| | if re.match(r"^\s*redis_cluster:\s*$", line): |
| | in_redis_nodes = False |
| | if re.match(r"^\s*dragonfly_cluster:\s*$", line): |
| | in_dfly_nodes = False |
| | if re.match(r"^\s*nodes:\s*$", line): |
| | |
| | pass |
| |
|
| | |
| | |
| | |
| | |
| | if re.search(r"^\s*redis_cluster:\s*$", line): |
| | in_redis_nodes = True |
| | in_dfly_nodes = False |
| | elif re.search(r"^\s*dragonfly_cluster:\s*$", line): |
| | in_dfly_nodes = True |
| | in_redis_nodes = False |
| |
|
| | |
| | if re.match(r"^[a-zA-Z0-9_]+:\s*$", line) and not line.startswith(" "): |
| | if not re.match(r"^(redis_cluster|dragonfly_cluster):\s*$", line): |
| | in_redis_nodes = False |
| | in_dfly_nodes = False |
| |
|
| | if in_redis_nodes or in_dfly_nodes: |
| | |
| | line = re.sub(r"^(\s*-\s*host:\s*)[^\s#]+", r"\1localhost", line) |
| |
|
| | out.append(line) |
| | hdr = [ |
| | "# Effective connections file (generated)", |
| | f"# Mode: {mode}", |
| | f"# Generated: {datetime.utcnow().isoformat()}Z", |
| | "", |
| | ] |
| | return "\n".join(hdr + out) + ("\n" if not out or not out[-1].endswith("\n") else "") |
| |
|
| |
|
| | def main(): |
| | if not os.path.exists(BASE_PATH): |
| | raise SystemExit(f"Base connections file not found: {BASE_PATH}") |
| | with open(BASE_PATH, "r") as f: |
| | base_text = f.read() |
| | mode = detect_mode() |
| | eff = render(base_text, mode) |
| | os.makedirs(SECRETS_DIR, exist_ok=True) |
| | with open(EFFECTIVE_PATH, "w") as f: |
| | f.write(eff) |
| | print(f"Rendered {EFFECTIVE_PATH} in {mode} mode") |
| |
|
| |
|
| | if __name__ == "__main__": |
| | main() |
| |
|
| |
|