askrobots / dbbasic-object-primitive
Public
Python functions as REST objects, with clustering, load balancing and state replication; no containers or orchestration.
cluster_config.py · 4.1 KB · 132 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | Cluster configuration loader |
| 4 | |
| 5 | Reads cluster.tsv to get station configuration for the distributed cluster. |
| 6 | Falls back to environment variables or defaults if config file doesn't exist. |
| 7 | """ |
| 8 | |
| 9 | import os |
| 10 | import csv |
| 11 | from pathlib import Path |
| 12 | from typing import List, Dict, Optional |
| 13 | |
| 14 | |
| 15 | class ClusterConfig: |
| 16 | """Load and manage cluster configuration""" |
| 17 | |
| 18 | def __init__(self, config_file: str = "cluster.tsv"): |
| 19 | self.config_file = Path(config_file) |
| 20 | self.stations = [] |
| 21 | self._load() |
| 22 | |
| 23 | def _load(self): |
| 24 | """Load configuration from TSV file""" |
| 25 | if not self.config_file.exists(): |
| 26 | # Fallback to single-station mode with environment variables |
| 27 | self.stations = [{ |
| 28 | 'station_id': os.environ.get('STATION_ID', 'station1'), |
| 29 | 'host': os.environ.get('STATION_HOST', 'localhost'), |
| 30 | 'port': int(os.environ.get('STATION_PORT', '8001')), |
| 31 | 'user': os.environ.get('STATION_USER', os.environ.get('USER', 'user')), |
| 32 | 'role': os.environ.get('STATION_ROLE', 'master') |
| 33 | }] |
| 34 | return |
| 35 | |
| 36 | # Read TSV file |
| 37 | with open(self.config_file, 'r', encoding='utf-8') as f: |
| 38 | reader = csv.DictReader( |
| 39 | (line for line in f if not line.startswith('#')), |
| 40 | delimiter='\t' |
| 41 | ) |
| 42 | |
| 43 | for row in reader: |
| 44 | # Skip empty rows |
| 45 | if not row.get('station_id'): |
| 46 | continue |
| 47 | |
| 48 | self.stations.append({ |
| 49 | 'station_id': row['station_id'].strip(), |
| 50 | 'host': row['host'].strip(), |
| 51 | 'port': int(row['port']), |
| 52 | 'user': row['user'].strip(), |
| 53 | 'role': row['role'].strip() |
| 54 | }) |
| 55 | |
| 56 | def get_station(self, station_id: str) -> Optional[Dict]: |
| 57 | """Get configuration for a specific station""" |
| 58 | for station in self.stations: |
| 59 | if station['station_id'] == station_id: |
| 60 | return station |
| 61 | return None |
| 62 | |
| 63 | def get_master(self) -> Optional[Dict]: |
| 64 | """Get the master station configuration""" |
| 65 | for station in self.stations: |
| 66 | if station['role'] == 'master': |
| 67 | return station |
| 68 | return None |
| 69 | |
| 70 | def get_workers(self) -> List[Dict]: |
| 71 | """Get all worker station configurations""" |
| 72 | return [s for s in self.stations if s['role'] == 'worker'] |
| 73 | |
| 74 | def get_all_stations(self) -> List[Dict]: |
| 75 | """Get all station configurations""" |
| 76 | return self.stations |
| 77 | |
| 78 | def get_ssh_target(self, station_id: str) -> Optional[str]: |
| 79 | """Get SSH target string (user@host) for a station""" |
| 80 | station = self.get_station(station_id) |
| 81 | if station: |
| 82 | return f"{station['user']}@{station['host']}" |
| 83 | return None |
| 84 | |
| 85 | def get_url(self, station_id: str, path: str = "") -> Optional[str]: |
| 86 | """Get full URL for a station""" |
| 87 | station = self.get_station(station_id) |
| 88 | if station: |
| 89 | return f"http://{station['host']}:{station['port']}{path}" |
| 90 | return None |
| 91 | |
| 92 | |
| 93 | # Global instance (lazy loaded) |
| 94 | _config = None |
| 95 | |
| 96 | |
| 97 | def get_config() -> ClusterConfig: |
| 98 | """Get the global cluster configuration""" |
| 99 | global _config |
| 100 | if _config is None: |
| 101 | _config = ClusterConfig() |
| 102 | return _config |
| 103 | |
| 104 | |
| 105 | def reload_config(): |
| 106 | """Reload configuration from file""" |
| 107 | global _config |
| 108 | _config = ClusterConfig() |
| 109 | |
| 110 | |
| 111 | if __name__ == "__main__": |
| 112 | # Test/debug the configuration |
| 113 | config = get_config() |
| 114 | |
| 115 | print("Cluster Configuration:") |
| 116 | print(f"Total stations: {len(config.stations)}") |
| 117 | print() |
| 118 | |
| 119 | master = config.get_master() |
| 120 | if master: |
| 121 | print(f"Master: {master['station_id']} at {master['host']}:{master['port']}") |
| 122 | |
| 123 | workers = config.get_workers() |
| 124 | if workers: |
| 125 | print(f"Workers: {len(workers)}") |
| 126 | for worker in workers: |
| 127 | print(f" - {worker['station_id']} at {worker['host']}:{worker['port']}") |
| 128 | |
| 129 | print() |
| 130 | print("All stations:") |
| 131 | for station in config.get_all_stations(): |
| 132 | print(f" {station['station_id']}: {station['user']}@{station['host']}:{station['port']} ({station['role']})") |