File size: 3,375 Bytes
503b0e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python3
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
    # auto-detect: if qdrant.dbops.local resolves, assume central
    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":
            # qdrant.host -> localhost
            line = re.sub(r"^(\s*host:\s*)qdrant\.dbops\.local\b", r"\1localhost", line)
            # gremlin_host -> localhost
            line = re.sub(r"^(\s*gremlin_host:\s*)gremlin\.dbops\.local\b", r"\1localhost", line)

            # Track when inside nodes lists
            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):
                # We will toggle based on previous section header by context later
                pass

            # Cheap context by remembering last seen section
            # Determine section by scanning previous non-empty lines
            # Simpler: switch on indentation depth
            # Replace host within redis/dfly nodes to localhost
            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 a new top-level key begins, reset flags
            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:
                # Replace any 'host: something' with localhost
                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()