|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import json |
| 5 | +import sys |
| 6 | +import os |
| 7 | +import logging |
| 8 | + |
| 9 | +from urllib import request |
| 10 | +from typing import Dict, List, Optional, Tuple |
| 11 | + |
| 12 | + |
| 13 | +def print_config_options(args): |
| 14 | + logging.info("Configuration options:") |
| 15 | + logging.info(f" Fleet name: {args.fleet_name}") |
| 16 | + logging.info(f" Cluster ID: {args.cluster_id}") |
| 17 | + logging.info(f" Static nodes: {args.static_nodes}") |
| 18 | + logging.info(f" Bootstrap nodes: {args.bootstrap_nodes}") |
| 19 | + logging.info(f" Store nodes: {args.store_nodes}") |
| 20 | + logging.info(f" Output path: {args.output}") |
| 21 | + |
| 22 | + |
| 23 | +def parse_args(): |
| 24 | + parser = argparse.ArgumentParser(description="Scan Waku nodes and build wakufleetconfig.json using DNS-based ENRs") |
| 25 | + parser.add_argument("--fleet-name", required=True, help="Fleet name, e.g. status-go.test") |
| 26 | + parser.add_argument("--cluster-id", required=True, type=int, help="Cluster ID, e.g. 16") |
| 27 | + parser.add_argument( |
| 28 | + "--static-nodes", |
| 29 | + default="", |
| 30 | + help="Comma-separated list of static node hostnames (Docker DNS names), e.g. 'node-1,node-2'", |
| 31 | + ) |
| 32 | + parser.add_argument( |
| 33 | + "--bootstrap-nodes", |
| 34 | + default="", |
| 35 | + help="Comma-separated list of bootstrap node hostnames, e.g. 'boot-1,boot-2'", |
| 36 | + ) |
| 37 | + parser.add_argument( |
| 38 | + "--store-nodes", |
| 39 | + default="", |
| 40 | + help="Comma-separated list of store node hostnames, e.g. 'store-1,store-2'", |
| 41 | + ) |
| 42 | + parser.add_argument("--output", required=True, help="Output path for wakufleetconfig.json inside the container") |
| 43 | + return parser.parse_args() |
| 44 | + |
| 45 | + |
| 46 | +def _fetch_debug_info(host: str) -> Optional[Dict]: |
| 47 | + url = f"http://{host}:8645/debug/v1/info" |
| 48 | + logging.info(f"Fetching debug info from Waku node {url}") |
| 49 | + with request.urlopen(url, timeout=5.0) as resp: |
| 50 | + assert resp.status == 200 |
| 51 | + data = resp.read() |
| 52 | + return json.loads(data.decode("utf-8")) |
| 53 | + |
| 54 | + |
| 55 | +def _first_dns_addr(listen_addrs: List[str]) -> Optional[str]: |
| 56 | + for addr in listen_addrs: |
| 57 | + if addr.startswith("/dns4/") or addr.startswith("/dns6/"): |
| 58 | + return addr |
| 59 | + return listen_addrs[0] if listen_addrs else None |
| 60 | + |
| 61 | + |
| 62 | +def _scan_hosts(hosts: List[str]) -> Dict[str, Tuple[str, Optional[str]]]: |
| 63 | + """ |
| 64 | + Returns mapping host -> (enrUri, addr) |
| 65 | + """ |
| 66 | + results: Dict[str, Tuple[str, Optional[str]]] = {} |
| 67 | + for host in hosts: |
| 68 | + host = host.strip() |
| 69 | + if not host: |
| 70 | + continue |
| 71 | + info = _fetch_debug_info(host) |
| 72 | + if not info: |
| 73 | + raise RuntimeError(f"Unable to gather ENR for host '{host}'") |
| 74 | + enr = info.get("enrUri") |
| 75 | + addrs = info.get("listenAddresses", []) or [] |
| 76 | + addr = _first_dns_addr(addrs) |
| 77 | + if not enr: |
| 78 | + raise RuntimeError(f"No ENR in debug info for host '{host}'") |
| 79 | + results[host] = (enr, addr) |
| 80 | + logging.info(f"Host {host}: enr={enr}, addr={addr}") |
| 81 | + return results |
| 82 | + |
| 83 | + |
| 84 | +def main(): |
| 85 | + logging.basicConfig(level=logging.INFO, format="[scan_waku_fleet] %(message)s") |
| 86 | + args = parse_args() |
| 87 | + print_config_options(args) |
| 88 | + |
| 89 | + def split_list(s: str) -> List[str]: |
| 90 | + return [x.strip() for x in s.split(",") if x.strip()] if s else [] |
| 91 | + |
| 92 | + static_hosts = split_list(args.static_nodes) |
| 93 | + bootstrap_hosts = split_list(args.bootstrap_nodes) |
| 94 | + store_hosts = split_list(args.store_nodes) |
| 95 | + |
| 96 | + all_hosts = list(dict.fromkeys(static_hosts + bootstrap_hosts + store_hosts)) # preserve order, unique |
| 97 | + |
| 98 | + if not all_hosts: |
| 99 | + logging.info("No hosts provided. Nothing to do.") |
| 100 | + return 0 |
| 101 | + |
| 102 | + try: |
| 103 | + scanned = _scan_hosts(all_hosts) |
| 104 | + except Exception as e: |
| 105 | + logging.error(str(e)) |
| 106 | + return 2 |
| 107 | + |
| 108 | + # Assemble the config structure |
| 109 | + fleet_name = args.fleet_name |
| 110 | + cluster_id = int(args.cluster_id) |
| 111 | + |
| 112 | + # wakuNodes: use static hosts ENRs |
| 113 | + waku_nodes: List[str] = [scanned[h][0] for h in static_hosts if h in scanned] |
| 114 | + |
| 115 | + # discV5BootstrapNodes: from bootstrap hosts ENRs |
| 116 | + bootstrap_enrs: List[str] = [scanned[h][0] for h in bootstrap_hosts if h in scanned] |
| 117 | + |
| 118 | + # storeNodes: list of {id, enr, addr, fleet} |
| 119 | + store_nodes: List[Dict[str, str]] = [] |
| 120 | + for h in store_hosts: |
| 121 | + if h not in scanned: |
| 122 | + continue |
| 123 | + enr, addr = scanned[h] |
| 124 | + node = { |
| 125 | + "id": h, |
| 126 | + "enr": enr, |
| 127 | + "fleet": fleet_name, |
| 128 | + } |
| 129 | + if addr: |
| 130 | + node["addr"] = addr |
| 131 | + store_nodes.append(node) |
| 132 | + |
| 133 | + output = { |
| 134 | + fleet_name: { |
| 135 | + "clusterId": cluster_id, |
| 136 | + "wakuNodes": waku_nodes, |
| 137 | + "discV5BootstrapNodes": bootstrap_enrs, |
| 138 | + "storeNodes": store_nodes, |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + # Ensure destination directory exists |
| 143 | + os.makedirs(os.path.dirname(args.output or ".") or ".", exist_ok=True) |
| 144 | + |
| 145 | + with open(args.output, "w", encoding="utf-8") as f: |
| 146 | + json.dump(output, f, indent=2) |
| 147 | + f.write("\n") |
| 148 | + |
| 149 | + logging.info(f"Written fleet config for '{fleet_name}' to {args.output}") |
| 150 | + return 0 |
| 151 | + |
| 152 | + |
| 153 | +if __name__ == "__main__": |
| 154 | + sys.exit(main()) |
0 commit comments