Python functions as REST objects, with clustering, load balancing and state replication; no containers or orchestration.

0 downloads

dbbasic-object-primitive/cluster_config.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-primitive.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-primitive.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-primitive.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

Dan Q Consolidate duplicate QUICKSTARTs on the top-level file 503f830 7 months ago History
cluster_config.py · 4.1 KB · 132 lines
1#!/usr/bin/env python3
2"""
3Cluster configuration loader
4
5Reads cluster.tsv to get station configuration for the distributed cluster.
6Falls back to environment variables or defaults if config file doesn't exist.
7"""
8
9import os
10import csv
11from pathlib import Path
12from typing import List, Dict, Optional
13
14
15class 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
97def 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
105def reload_config():
106 """Reload configuration from file"""
107 global _config
108 _config = ClusterConfig()
109
110
111if __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']})")