-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplates.py
More file actions
288 lines (259 loc) · 8.94 KB
/
templates.py
File metadata and controls
288 lines (259 loc) · 8.94 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
"""
Config Templates for Fleet Config Manager.
Pre-built configuration templates for different deployment scenarios.
Each template returns a complete fleet configuration dict that can be
further customized via overrides.
"""
from __future__ import annotations
import copy
from typing import Any
def _deep_merge(base: dict, override: dict) -> dict:
"""Deep-merge override into base, returning a new dict."""
result = copy.deepcopy(base)
for key, val in override.items():
if key in result and isinstance(result[key], dict) and isinstance(val, dict):
result[key] = _deep_merge(result[key], val)
else:
result[key] = copy.deepcopy(val)
return result
class ConfigTemplates:
"""Pre-built configuration templates for the Pelagic fleet.
Usage::
tmpl = ConfigTemplates()
dev = tmpl.development()
custom = tmpl.generate("development", overrides={"keeper": {"port": 9000}})
"""
# -- Base skeleton --------------------------------------------------------
@staticmethod
def _base() -> dict[str, Any]:
return {
"fleet_name": "pelagic-fleet",
"version": "1.0.0",
"environment": "development",
"keeper": {
"host": "127.0.0.1",
"port": 8000,
"workers": 4,
"heartbeat_interval": 30,
"max_agents": 50,
},
"agents": {},
"network": {
"default_host": "127.0.0.1",
"base_port": 9000,
"port_step": 100,
"allowed_hosts": [],
"tls_enabled": False,
},
"logging": {
"level": "INFO",
"format": "json",
"file": None,
"max_size_mb": 100,
},
"secrets": {},
}
# -- Template generators --------------------------------------------------
def development(self) -> dict[str, Any]:
"""Local development setup — all agents on localhost with debug logging."""
config = self._base()
config["environment"] = "development"
config["logging"]["level"] = "DEBUG"
config["logging"]["format"] = "text"
config["agents"] = {
"git-agent": {
"name": "git-agent",
"type": "git",
"host": "127.0.0.1",
"port": 9100,
"enabled": True,
"env": {"GIT_REPO_PATH": "/tmp/fleet-repo"},
},
"chat-agent": {
"name": "chat-agent",
"type": "chat",
"host": "127.0.0.1",
"port": 9200,
"enabled": True,
},
"lighthouse": {
"name": "lighthouse",
"type": "lighthouse",
"host": "127.0.0.1",
"port": 9300,
"enabled": True,
},
}
return config
def production(self) -> dict[str, Any]:
"""Production setup — separate hosts, TLS, structured logging."""
config = self._base()
config["environment"] = "production"
config["logging"]["level"] = "WARNING"
config["logging"]["format"] = "json"
config["network"]["tls_enabled"] = True
config["network"]["default_host"] = "0.0.0.0"
config["keeper"]["host"] = "0.0.0.0"
config["keeper"]["workers"] = 8
config["agents"] = {
"git-agent": {
"name": "git-agent",
"type": "git",
"host": "0.0.0.0",
"port": 9100,
"enabled": True,
"env": {"GIT_REPO_PATH": "/data/fleet-repo"},
},
"chat-agent": {
"name": "chat-agent",
"type": "chat",
"host": "0.0.0.0",
"port": 9200,
"enabled": True,
},
"lighthouse": {
"name": "lighthouse",
"type": "lighthouse",
"host": "0.0.0.0",
"port": 9300,
"enabled": True,
},
"world-agent": {
"name": "world-agent",
"type": "world",
"host": "0.0.0.0",
"port": 9400,
"enabled": True,
},
}
return config
def minimal(self) -> dict[str, Any]:
"""Minimal setup — just Keeper + Git Agent."""
config = self._base()
config["environment"] = "development"
config["keeper"]["max_agents"] = 5
config["agents"] = {
"git-agent": {
"name": "git-agent",
"type": "git",
"host": "127.0.0.1",
"port": 9100,
"enabled": True,
"env": {"GIT_REPO_PATH": "/tmp/fleet-repo"},
},
}
return config
def full(self) -> dict[str, Any]:
"""Full setup — everything enabled."""
config = self._base()
config["environment"] = "staging"
config["network"]["tls_enabled"] = True
config["keeper"]["workers"] = 6
config["keeper"]["max_agents"] = 100
agent_defs = [
("git-agent", "git", 9100),
("chat-agent", "chat", 9200),
("lighthouse", "lighthouse", 9300),
("world-agent", "world", 9400),
("room-agent", "room", 9500),
("scheduler", "scheduler", 9600),
]
config["agents"] = {
name: {
"name": name,
"type": atype,
"host": "127.0.0.1",
"port": port,
"enabled": True,
}
for name, atype, port in agent_defs
}
return config
def docker(self) -> dict[str, Any]:
"""Docker-specific settings — internal networking."""
config = self._base()
config["environment"] = "production"
config["keeper"]["host"] = "0.0.0.0"
config["network"]["default_host"] = "0.0.0.0"
config["network"]["tls_enabled"] = True
config["logging"]["level"] = "INFO"
agent_defs = [
("git-agent", "git", 9100),
("chat-agent", "chat", 9200),
("lighthouse", "lighthouse", 9300),
]
config["agents"] = {
name: {
"name": name,
"type": atype,
"host": host,
"port": port,
"enabled": True,
"env": {
"DOCKER_NETWORK": "fleet-internal",
"IMAGE": f"pelagic/{name}:latest",
},
}
for name, atype, port in agent_defs
for host in [f"{name}"]
}
return config
# -- Generic generator ----------------------------------------------------
def generate(
self,
template_name: str,
overrides: dict[str, Any] | None = None,
) -> dict[str, Any]:
"""Generate a fleet config from a named template + optional overrides.
Args:
template_name: One of ``development``, ``production``, ``minimal``,
``full``, ``docker``.
overrides: Optional dict deep-merged on top of the template.
Returns:
Complete fleet configuration dict.
Raises:
ValueError: If *template_name* is unknown.
"""
generators = {
"development": self.development,
"production": self.production,
"minimal": self.minimal,
"full": self.full,
"docker": self.docker,
}
gen = generators.get(template_name)
if gen is None:
available = ", ".join(sorted(generators))
raise ValueError(
f"Unknown template '{template_name}'. Available: {available}"
)
config = gen()
if overrides:
config = _deep_merge(config, overrides)
return config
# -- List templates -------------------------------------------------------
@staticmethod
def list_templates() -> list[dict[str, str]]:
"""Return metadata about every available template."""
return [
{
"name": "development",
"description": "Local dev — all on localhost, debug logging",
},
{
"name": "production",
"description": "Production — separate hosts, TLS, JSON logs",
},
{
"name": "minimal",
"description": "Minimal — Keeper + Git Agent only",
},
{
"name": "full",
"description": "Full — all agents enabled",
},
{
"name": "docker",
"description": "Docker — internal container networking",
},
]