-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcortex_launcher.py
More file actions
391 lines (319 loc) · 12.2 KB
/
cortex_launcher.py
File metadata and controls
391 lines (319 loc) · 12.2 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
#!/usr/bin/env python3
"""
Cortex Knowledge Assistant - Launcher Interactivo
Este es el punto de entrada principal para ejecutar Cortex KA.
Proporciona un menú interactivo para:
- Iniciar/detener servicios
- Verificar estado del sistema
- Configuración inicial
- Administración básica
Uso:
python cortex_launcher.py # Menú interactivo
python cortex_launcher.py start # Arranque directo
python cortex_launcher.py stop # Detención directa
python cortex_launcher.py status # Ver estado
Requisitos:
- Python 3.10+
- Docker y docker compose
- Node.js 18+ (para UI)
Autor: Cortex Engineering Team
Fecha: 2025-12-02
"""
import os
import sys
import subprocess
import time
from pathlib import Path
from typing import Optional
import json
# Colores ANSI
class Colors:
RED = "\033[0;31m"
GREEN = "\033[0;32m"
YELLOW = "\033[1;33m"
BLUE = "\033[0;34m"
CYAN = "\033[0;36m"
MAGENTA = "\033[0;35m"
BOLD = "\033[1m"
NC = "\033[0m" # No color
def clear_screen():
os.system("clear" if os.name != "nt" else "cls")
def print_banner():
print(f"""
{Colors.CYAN}╔══════════════════════════════════════════════════════════════╗
║ ║
║ {Colors.BOLD}CORTEX KNOWLEDGE ASSISTANT{Colors.NC}{Colors.CYAN} ║
║ ───────────────────────────────────── ║
║ Sistema de Asistencia Inteligente ║
║ ║
╚══════════════════════════════════════════════════════════════╝{Colors.NC}
""")
def print_status_line(service: str, status: bool, url: Optional[str] = None):
"""Imprime una línea de estado de servicio."""
icon = f"{Colors.GREEN}●{Colors.NC}" if status else f"{Colors.RED}○{Colors.NC}"
status_text = (
f"{Colors.GREEN}ACTIVO{Colors.NC}"
if status
else f"{Colors.RED}INACTIVO{Colors.NC}"
)
url_text = f" → {Colors.BLUE}{url}{Colors.NC}" if url and status else ""
print(f" {icon} {service:<20} [{status_text}]{url_text}")
def check_port(port: int) -> bool:
"""Verifica si un puerto está escuchando (IPv4 o IPv6)."""
import socket
# Intentar IPv4 primero
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.settimeout(1)
if s.connect_ex(("127.0.0.1", port)) == 0:
return True
# Intentar IPv6
try:
with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as s:
s.settimeout(1)
if s.connect_ex(("::1", port)) == 0:
return True
except Exception:
pass
return False
def check_docker_container(name: str) -> bool:
"""Verifica si un contenedor Docker está corriendo."""
try:
result = subprocess.run(
["docker", "inspect", "-f", "{{.State.Running}}", name],
capture_output=True,
text=True,
timeout=5,
)
return result.stdout.strip() == "true"
except Exception:
return False
def get_system_status() -> dict:
"""Obtiene el estado completo del sistema."""
return {
"api": check_port(8088),
"ui": check_port(3000),
"qdrant": check_port(6333),
"redis": check_port(6379),
"ollama": check_port(11434),
"docker_qdrant": check_docker_container("cortex_qdrant"),
"docker_redis": check_docker_container("cortex_redis"),
"docker_ollama": check_docker_container("cortex_ollama"),
}
def print_system_status():
"""Muestra el estado actual del sistema."""
print(f"\n{Colors.BOLD}Estado del Sistema:{Colors.NC}\n")
status = get_system_status()
print(f" {Colors.CYAN}Servicios Principales:{Colors.NC}")
print_status_line("API (FastAPI)", status["api"], "http://localhost:8088")
print_status_line("UI (React/Vite)", status["ui"], "http://localhost:3000")
print(f"\n {Colors.CYAN}Infraestructura Docker:{Colors.NC}")
print_status_line("Qdrant (vectores)", status["qdrant"], "http://localhost:6333")
print_status_line("Redis (cache)", status["redis"], "localhost:6379")
print_status_line("Ollama (LLM local)", status["ollama"], "http://localhost:11434")
# Estado general
all_core = status["api"] and status["ui"] and status["qdrant"]
if all_core:
print(f"\n {Colors.GREEN}✓ Sistema operativo{Colors.NC}")
elif status["api"] or status["ui"]:
print(f"\n {Colors.YELLOW}⚠ Sistema parcialmente operativo{Colors.NC}")
else:
print(f"\n {Colors.RED}✗ Sistema detenido{Colors.NC}")
return status
def run_script(script_name: str, args: list = None) -> int:
"""Ejecuta un script bash del directorio scripts/."""
root = Path(__file__).parent
script_path = root / "scripts" / script_name
if not script_path.exists():
print(f"{Colors.RED}Error: Script {script_name} no encontrado{Colors.NC}")
return 1
cmd = ["bash", str(script_path)]
if args:
cmd.extend(args)
return subprocess.call(cmd)
def start_services():
"""Inicia todos los servicios."""
print(f"\n{Colors.BOLD}Iniciando Cortex...{Colors.NC}\n")
return run_script("start_cortex_full.sh")
def stop_services(stop_docker: bool = False):
"""Detiene los servicios."""
print(f"\n{Colors.BOLD}Deteniendo Cortex...{Colors.NC}\n")
args = ["--all"] if stop_docker else []
return run_script("stop_cortex.sh", args)
def check_health_api():
"""Verifica la salud de la API con detalle."""
import urllib.request
import urllib.error
print(f"\n{Colors.BOLD}Diagnóstico de API:{Colors.NC}\n")
endpoints = [
("Health básico", "http://localhost:8088/health"),
("Estado del sistema", "http://localhost:8088/api/system/status"),
]
for name, url in endpoints:
try:
with urllib.request.urlopen(url, timeout=5) as response:
data = json.loads(response.read().decode())
print(f" {Colors.GREEN}✓{Colors.NC} {name}")
if isinstance(data, dict):
for key, value in data.items():
if isinstance(value, dict):
print(f" {key}:")
for k, v in value.items():
print(f" {k}: {v}")
else:
print(f" {key}: {value}")
except urllib.error.URLError as e:
print(f" {Colors.RED}✗{Colors.NC} {name}: {e.reason}")
except Exception as e:
print(f" {Colors.RED}✗{Colors.NC} {name}: {e}")
print()
def show_logs(service: str):
"""Muestra los últimos logs de un servicio."""
root = Path(__file__).parent
log_file = root / "logs" / f"{service}.log"
if not log_file.exists():
print(f"{Colors.YELLOW}No hay logs para {service}{Colors.NC}")
return
print(f"\n{Colors.BOLD}Últimos logs de {service}:{Colors.NC}\n")
subprocess.call(["tail", "-30", str(log_file)])
print()
def show_credentials():
"""Muestra las credenciales de acceso."""
print(f"""
{Colors.BOLD}Credenciales de Acceso:{Colors.NC}
{Colors.CYAN}Administrador:{Colors.NC}
Usuario: gguerra.admin
Password: Admin@123
{Colors.CYAN}Usuario de Soporte:{Colors.NC}
Usuario: llucci.support
Password: (contactar admin)
{Colors.CYAN}Cliente Demo:{Colors.NC}
Usuario: cliente_cli-81093
Password: Demo!CLI-81093
{Colors.YELLOW}Nota: Cambie las contraseñas después del primer acceso.{Colors.NC}
""")
def show_urls():
"""Muestra las URLs de acceso."""
print(f"""
{Colors.BOLD}URLs de Acceso:{Colors.NC}
{Colors.CYAN}Aplicación Principal:{Colors.NC}
UI Web: http://localhost:3000
API REST: http://localhost:8088
API Docs: http://localhost:8088/docs
{Colors.CYAN}Infraestructura:{Colors.NC}
Qdrant: http://localhost:6333/dashboard
Ollama: http://localhost:11434
{Colors.CYAN}Endpoints Útiles:{Colors.NC}
Health: http://localhost:8088/health
Status: http://localhost:8088/api/system/status
""")
def interactive_menu():
"""Menú interactivo principal."""
while True:
clear_screen()
print_banner()
print_system_status()
print(f"""
{Colors.BOLD}Opciones:{Colors.NC}
{Colors.CYAN}Servicios:{Colors.NC}
1) Iniciar Cortex completo
2) Detener Cortex (mantener Docker)
3) Detener todo (incluyendo Docker)
4) Reiniciar servicios
{Colors.CYAN}Diagnóstico:{Colors.NC}
5) Ver estado detallado
6) Ver logs de API
7) Ver logs de UI
8) Diagnóstico de API
{Colors.CYAN}Información:{Colors.NC}
9) Ver credenciales
0) Ver URLs de acceso
{Colors.CYAN}Salir:{Colors.NC}
q) Salir del launcher
""")
choice = (
input(f"{Colors.BOLD}Seleccione una opción: {Colors.NC}").strip().lower()
)
if choice == "1":
start_services()
input("\nPresione Enter para continuar...")
elif choice == "2":
stop_services(stop_docker=False)
input("\nPresione Enter para continuar...")
elif choice == "3":
stop_services(stop_docker=True)
input("\nPresione Enter para continuar...")
elif choice == "4":
stop_services(stop_docker=False)
time.sleep(2)
start_services()
input("\nPresione Enter para continuar...")
elif choice == "5":
clear_screen()
print_banner()
print_system_status()
input("\nPresione Enter para continuar...")
elif choice == "6":
clear_screen()
show_logs("api")
input("\nPresione Enter para continuar...")
elif choice == "7":
clear_screen()
show_logs("ui")
input("\nPresione Enter para continuar...")
elif choice == "8":
clear_screen()
check_health_api()
input("\nPresione Enter para continuar...")
elif choice == "9":
clear_screen()
show_credentials()
input("\nPresione Enter para continuar...")
elif choice == "0":
clear_screen()
show_urls()
input("\nPresione Enter para continuar...")
elif choice in ("q", "exit", "quit"):
print(f"\n{Colors.GREEN}¡Hasta luego!{Colors.NC}\n")
break
else:
print(f"{Colors.YELLOW}Opción no válida{Colors.NC}")
time.sleep(1)
def main():
"""Punto de entrada principal."""
# Asegurarse de estar en el directorio correcto
os.chdir(Path(__file__).parent)
# Procesar argumentos de línea de comandos
if len(sys.argv) > 1:
command = sys.argv[1].lower()
if command == "start":
start_services()
elif command == "stop":
stop_all = "--all" in sys.argv
stop_services(stop_docker=stop_all)
elif command == "restart":
stop_services(stop_docker=False)
time.sleep(2)
start_services()
elif command == "status":
print_banner()
print_system_status()
elif command == "health":
check_health_api()
elif command == "logs":
service = sys.argv[2] if len(sys.argv) > 2 else "api"
show_logs(service)
elif command in ("help", "-h", "--help"):
print(__doc__)
else:
print(f"Comando desconocido: {command}")
print("Use 'python cortex_launcher.py help' para ver opciones.")
sys.exit(1)
else:
# Modo interactivo
try:
interactive_menu()
except KeyboardInterrupt:
print(f"\n\n{Colors.YELLOW}Interrumpido por el usuario{Colors.NC}\n")
sys.exit(0)
if __name__ == "__main__":
main()