-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.py
More file actions
98 lines (86 loc) · 3.29 KB
/
start.py
File metadata and controls
98 lines (86 loc) · 3.29 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
import os
import subprocess
import time
import sys
import argparse
from concurrent.futures import ThreadPoolExecutor
def check_dependencies():
print("🔍 Checking dependencies...")
try:
import fastapi
import textual
import httpx
import kubernetes
import groq
from dotenv import load_dotenv
load_dotenv() # Load local .env file if it exists
print("✅ All dependencies found and .env loaded.")
except ImportError as e:
missing = e.name if hasattr(e, 'name') else str(e)
print(f"❌ Missing dependency: {missing}")
print("\n💡 Please run this command in your terminal:")
print(" pip install -r requirements.txt")
sys.exit(1)
def check_k8s():
print("🔍 Checking Kubernetes connection...")
from kubernetes import client, config
try:
config.load_kube_config()
v1 = client.CoreV1Api()
v1.list_namespace(timeout_seconds=2)
print("✅ Kubernetes connection successful.")
except Exception as e:
print(f"⚠️ Kubernetes connection failed: {e}")
print("💡 OpsAgent will run, but cluster data will be empty.")
def build_frontend():
frontend_dir = os.path.join(os.getcwd(), "frontend")
dist_dir = os.path.join(frontend_dir, "dist")
if os.path.exists(dist_dir):
print("✅ Frontend already built.")
return
print("🏗️ Building frontend (this may take a minute)...")
try:
if os.name == 'nt': # Windows/WSL handling
subprocess.run(["npm", "install"], cwd=frontend_dir, check=True, shell=True)
subprocess.run(["npm", "run", "build"], cwd=frontend_dir, check=True, shell=True)
else:
subprocess.run(["npm", "install"], cwd=frontend_dir, check=True)
subprocess.run(["npm", "run", "build"], cwd=frontend_dir, check=True)
print("✅ Frontend built successfully.")
except Exception as e:
print(f"⚠️ Frontend build failed: {e}")
print("💡 OpsAgent will fall back to the standalone dashboard.html.")
def run_backend():
print("🚀 Starting FastAPI backend on http://127.0.0.1:8000...")
# Use 127.0.0.1 to match TUI expectations
return subprocess.Popen(
[sys.executable, "-m", "uvicorn", "main:app", "--host", "127.0.0.1", "--port", "8000", "--log-level", "error"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
def run_tui():
print("🚀 Launching OpsAgent TUI...")
subprocess.run([sys.executable, "tui.py"])
def main():
parser = argparse.ArgumentParser(description="OpsAgent Orchestrator")
parser.add_argument("--check-only", action="store_true", help="Only check dependencies and K8s")
parser.add_argument("--skip-build", action="store_true", help="Skip frontend build")
args = parser.parse_args()
check_dependencies()
check_k8s()
if args.check_only:
return
if not args.skip_build:
build_frontend()
backend_process = None
try:
backend_process = run_backend()
time.sleep(1) # Give it a second to bind
run_tui()
except KeyboardInterrupt:
print("\n👋 Shutting down OpsAgent...")
finally:
if backend_process:
backend_process.terminate()
if __name__ == "__main__":
main()