forked from SimpleOpenSoftware/chronicle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservices.py
More file actions
executable file
·412 lines (347 loc) · 15.6 KB
/
Copy pathservices.py
File metadata and controls
executable file
·412 lines (347 loc) · 15.6 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#!/usr/bin/env python3
"""
Chronicle Service Management
Start, stop, and manage configured services
"""
import argparse
import subprocess
from pathlib import Path
import yaml
from rich.console import Console
from rich.table import Table
from dotenv import dotenv_values
console = Console()
def load_config_yml():
"""Load config.yml from repository root"""
config_path = Path(__file__).parent / 'config' / 'config.yml'
if not config_path.exists():
return None
try:
with open(config_path, 'r') as f:
return yaml.safe_load(f)
except Exception as e:
console.print(f"[yellow]⚠️ Warning: Could not load config/config.yml: {e}[/yellow]")
return None
SERVICES = {
'backend': {
'path': 'backends/advanced',
'compose_file': 'docker-compose.yml',
'description': 'Advanced Backend + WebUI',
'ports': ['8000', '5173']
},
'speaker-recognition': {
'path': 'extras/speaker-recognition',
'compose_file': 'docker-compose.yml',
'description': 'Speaker Recognition Service',
'ports': ['8085', '5174/8444']
},
'asr-services': {
'path': 'extras/asr-services',
'compose_file': 'docker-compose.yml',
'description': 'Parakeet ASR Service',
'ports': ['8767']
},
'openmemory-mcp': {
'path': 'extras/openmemory-mcp',
'compose_file': 'docker-compose.yml',
'description': 'OpenMemory MCP Server',
'ports': ['8765']
}
}
def check_service_configured(service_name):
"""Check if service is configured (has .env file)"""
service = SERVICES[service_name]
service_path = Path(service['path'])
# Backend uses advanced init, others use .env
if service_name == 'backend':
return (service_path / '.env').exists()
else:
return (service_path / '.env').exists()
def run_compose_command(service_name, command, build=False):
"""Run docker compose command for a service"""
service = SERVICES[service_name]
service_path = Path(service['path'])
if not service_path.exists():
console.print(f"[red]❌ Service directory not found: {service_path}[/red]")
return False
compose_file = service_path / service['compose_file']
if not compose_file.exists():
console.print(f"[red]❌ Docker compose file not found: {compose_file}[/red]")
return False
cmd = ['docker', 'compose']
# For backend service, check if HTTPS is configured (Caddyfile exists)
if service_name == 'backend':
caddyfile_path = service_path / 'Caddyfile'
if caddyfile_path.exists() and caddyfile_path.is_file():
# Enable HTTPS profile to start Caddy service
cmd.extend(['--profile', 'https'])
# Check if Obsidian/Neo4j is enabled
obsidian_enabled = False
# Method 1: Check config.yml (preferred)
config_data = load_config_yml()
if config_data:
memory_config = config_data.get('memory', {})
obsidian_config = memory_config.get('obsidian', {})
if obsidian_config.get('enabled', False):
obsidian_enabled = True
# Method 2: Fallback to .env for backward compatibility
if not obsidian_enabled:
env_file = service_path / '.env'
if env_file.exists():
env_values = dotenv_values(env_file)
if env_values.get('OBSIDIAN_ENABLED', 'false').lower() == 'true':
obsidian_enabled = True
if obsidian_enabled:
cmd.extend(['--profile', 'obsidian'])
console.print("[blue]ℹ️ Starting with Obsidian/Neo4j support[/blue]")
# Handle speaker-recognition service specially
if service_name == 'speaker-recognition' and command in ['up', 'down']:
# Read configuration to determine profile
env_file = service_path / '.env'
if env_file.exists():
env_values = dotenv_values(env_file)
compute_mode = env_values.get('COMPUTE_MODE', 'cpu')
# Add profile flag for both up and down commands
if compute_mode == 'gpu':
cmd.extend(['--profile', 'gpu'])
else:
cmd.extend(['--profile', 'cpu'])
if command == 'up':
https_enabled = env_values.get('REACT_UI_HTTPS', 'false')
if https_enabled.lower() == 'true':
# HTTPS mode: start with profile for all services (includes nginx)
cmd.extend(['up', '-d'])
else:
# HTTP mode: start specific services with profile (no nginx)
cmd.extend(['up', '-d', 'speaker-service-gpu' if compute_mode == 'gpu' else 'speaker-service-cpu', 'web-ui'])
elif command == 'down':
cmd.extend(['down'])
else:
# Fallback: no profile
if command == 'up':
cmd.extend(['up', '-d'])
elif command == 'down':
cmd.extend(['down'])
else:
# Standard compose commands for other services
if command == 'up':
cmd.extend(['up', '-d'])
elif command == 'down':
cmd.extend(['down'])
elif command == 'restart':
cmd.extend(['restart'])
elif command == 'status':
cmd.extend(['ps'])
if command == 'up' and build:
cmd.append('--build')
try:
# For commands that need real-time output (build), stream to console
if build and command == 'up':
console.print(f"[dim]Building {service_name} containers...[/dim]")
process = subprocess.Popen(
cmd,
cwd=service_path,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
bufsize=1
)
# Simply stream all output with coloring
all_output = []
if process.stdout is None:
raise RuntimeError("Process stdout is None - unable to read command output")
for line in process.stdout:
line = line.rstrip()
if not line:
continue
# Store for error context
all_output.append(line)
# Print with appropriate coloring
if 'error' in line.lower() or 'failed' in line.lower():
console.print(f" [red]{line}[/red]")
elif 'Successfully' in line or 'Started' in line or 'Created' in line:
console.print(f" [green]{line}[/green]")
elif 'Building' in line or 'Creating' in line:
console.print(f" [cyan]{line}[/cyan]")
elif 'warning' in line.lower():
console.print(f" [yellow]{line}[/yellow]")
else:
console.print(f" [dim]{line}[/dim]")
# Wait for process to complete
process.wait()
# If build failed, show error summary
if process.returncode != 0:
console.print(f"\n[red]❌ Build failed for {service_name}[/red]")
return False
return True
else:
# For non-build commands, run silently unless there's an error
result = subprocess.run(
cmd,
cwd=service_path,
capture_output=True,
text=True,
check=False,
timeout=120 # 2 minute timeout for service status checks
)
if result.returncode == 0:
return True
else:
console.print(f"[red]❌ Command failed[/red]")
if result.stderr:
console.print("[red]Error output:[/red]")
# Show all error output
for line in result.stderr.splitlines():
console.print(f" [dim]{line}[/dim]")
return False
except subprocess.TimeoutExpired:
console.print(f"[red]❌ Command timed out after 2 minutes for {service_name}[/red]")
return False
except Exception as e:
console.print(f"[red]❌ Error running command: {e}[/red]")
return False
def start_services(services, build=False):
"""Start specified services"""
console.print(f"🚀 [bold]Starting {len(services)} services...[/bold]")
success_count = 0
for service_name in services:
if service_name not in SERVICES:
console.print(f"[red]❌ Unknown service: {service_name}[/red]")
continue
if not check_service_configured(service_name):
console.print(f"[yellow]⚠️ {service_name} not configured, skipping[/yellow]")
continue
console.print(f"\n🔧 Starting {service_name}...")
if run_compose_command(service_name, 'up', build):
console.print(f"[green]✅ {service_name} started[/green]")
success_count += 1
else:
console.print(f"[red]❌ Failed to start {service_name}[/red]")
console.print(f"\n[green]🎉 {success_count}/{len(services)} services started successfully[/green]")
def stop_services(services):
"""Stop specified services"""
console.print(f"🛑 [bold]Stopping {len(services)} services...[/bold]")
success_count = 0
for service_name in services:
if service_name not in SERVICES:
console.print(f"[red]❌ Unknown service: {service_name}[/red]")
continue
console.print(f"\n🔧 Stopping {service_name}...")
if run_compose_command(service_name, 'down'):
console.print(f"[green]✅ {service_name} stopped[/green]")
success_count += 1
else:
console.print(f"[red]❌ Failed to stop {service_name}[/red]")
console.print(f"\n[green]🎉 {success_count}/{len(services)} services stopped successfully[/green]")
def restart_services(services):
"""Restart specified services"""
console.print(f"🔄 [bold]Restarting {len(services)} services...[/bold]")
success_count = 0
for service_name in services:
if service_name not in SERVICES:
console.print(f"[red]❌ Unknown service: {service_name}[/red]")
continue
if not check_service_configured(service_name):
console.print(f"[yellow]⚠️ {service_name} not configured, skipping[/yellow]")
continue
console.print(f"\n🔧 Restarting {service_name}...")
if run_compose_command(service_name, 'restart'):
console.print(f"[green]✅ {service_name} restarted[/green]")
success_count += 1
else:
console.print(f"[red]❌ Failed to restart {service_name}[/red]")
console.print(f"\n[green]🎉 {success_count}/{len(services)} services restarted successfully[/green]")
def show_status():
"""Show status of all services"""
console.print("📊 [bold]Service Status:[/bold]\n")
table = Table()
table.add_column("Service", style="cyan")
table.add_column("Configured", justify="center")
table.add_column("Description", style="dim")
table.add_column("Ports", style="green")
for service_name, service_info in SERVICES.items():
configured = "✅" if check_service_configured(service_name) else "❌"
ports = ", ".join(service_info['ports'])
table.add_row(
service_name,
configured,
service_info['description'],
ports
)
console.print(table)
console.print("\n💡 [dim]Use 'python services.py start --all' to start all configured services[/dim]")
def main():
parser = argparse.ArgumentParser(description="Chronicle Service Management")
subparsers = parser.add_subparsers(dest='command', help='Available commands')
# Start command
start_parser = subparsers.add_parser('start', help='Start services')
start_parser.add_argument('services', nargs='*',
help='Services to start: backend, speaker-recognition, asr-services, openmemory-mcp (or use --all)')
start_parser.add_argument('--all', action='store_true', help='Start all configured services')
start_parser.add_argument('--build', action='store_true', help='Build images before starting')
# Stop command
stop_parser = subparsers.add_parser('stop', help='Stop services')
stop_parser.add_argument('services', nargs='*',
help='Services to stop: backend, speaker-recognition, asr-services, openmemory-mcp (or use --all)')
stop_parser.add_argument('--all', action='store_true', help='Stop all services')
# Restart command
restart_parser = subparsers.add_parser('restart', help='Restart services')
restart_parser.add_argument('services', nargs='*',
help='Services to restart: backend, speaker-recognition, asr-services, openmemory-mcp (or use --all)')
restart_parser.add_argument('--all', action='store_true', help='Restart all services')
# Status command
subparsers.add_parser('status', help='Show service status')
args = parser.parse_args()
if not args.command:
show_status()
return
if args.command == 'status':
show_status()
elif args.command == 'start':
if args.all:
services = [s for s in SERVICES.keys() if check_service_configured(s)]
elif args.services:
# Validate service names
invalid_services = [s for s in args.services if s not in SERVICES]
if invalid_services:
console.print(f"[red]❌ Invalid service names: {', '.join(invalid_services)}[/red]")
console.print(f"Available services: {', '.join(SERVICES.keys())}")
return
services = args.services
else:
console.print("[red]❌ No services specified. Use --all or specify service names.[/red]")
return
start_services(services, args.build)
elif args.command == 'stop':
if args.all:
# Only stop configured services (like start --all does)
services = [s for s in SERVICES.keys() if check_service_configured(s)]
elif args.services:
# Validate service names
invalid_services = [s for s in args.services if s not in SERVICES]
if invalid_services:
console.print(f"[red]❌ Invalid service names: {', '.join(invalid_services)}[/red]")
console.print(f"Available services: {', '.join(SERVICES.keys())}")
return
services = args.services
else:
console.print("[red]❌ No services specified. Use --all or specify service names.[/red]")
return
stop_services(services)
elif args.command == 'restart':
if args.all:
services = [s for s in SERVICES.keys() if check_service_configured(s)]
elif args.services:
# Validate service names
invalid_services = [s for s in args.services if s not in SERVICES]
if invalid_services:
console.print(f"[red]❌ Invalid service names: {', '.join(invalid_services)}[/red]")
console.print(f"Available services: {', '.join(SERVICES.keys())}")
return
services = args.services
else:
console.print("[red]❌ No services specified. Use --all or specify service names.[/red]")
return
restart_services(services)
if __name__ == "__main__":
main()