-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.py
More file actions
169 lines (136 loc) · 5.57 KB
/
Copy pathmain.py
File metadata and controls
169 lines (136 loc) · 5.57 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
"""
Agentic-IAM: Main Application Entry Point
Central orchestrator that manages the Agentic-IAM platform, integrating all
components including the FastAPI backend, Streamlit dashboard, and core
Agent Identity Framework modules.
"""
import asyncio
import signal
import sys
import logging
from pathlib import Path
from typing import Optional
import multiprocessing as mp
import uvicorn
import subprocess
# Add core modules to path
sys.path.append(str(Path(__file__).parent / "core"))
sys.path.append(str(Path(__file__).parent.parent))
from core.agentic_iam import AgenticIAM
from config.settings import Settings
from utils.logger import setup_logging
class AgenticIAMPlatform:
"""Main platform orchestrator"""
def __init__(self):
self.settings = Settings()
self.logger = setup_logging(
log_level=self.settings.log_level,
log_file=self.settings.log_file,
enable_console=True
)
self.iam: Optional[AgenticIAM] = None
self.api_process: Optional[mp.Process] = None
self.dashboard_process: Optional[mp.Process] = None
self.running = False
async def initialize(self):
"""Initialize the platform"""
self.logger.info("Initializing Agentic-IAM Platform...")
# Initialize core IAM system
self.iam = AgenticIAM(self.settings)
await self.iam.initialize()
self.logger.info("Platform initialization complete")
def start_api_server(self):
"""Start the FastAPI server"""
try:
uvicorn.run(
"api.main:app",
host=self.settings.api_host,
port=self.settings.api_port,
log_level=self.settings.log_level.lower(),
access_log=True
)
except Exception as e:
self.logger.error(f"API server error: {e}")
def start_dashboard_server(self):
"""Start the Streamlit dashboard"""
try:
import subprocess
cmd = [
sys.executable, "-m", "streamlit", "run",
"dashboard/main.py",
"--server.port", str(self.settings.dashboard_port),
"--server.address", self.settings.dashboard_host,
"--browser.gatherUsageStats", "false"
]
subprocess.run(cmd, cwd=Path(__file__).parent)
except Exception as e:
self.logger.error(f"Dashboard server error: {e}")
async def start(self):
"""Start the complete platform"""
await self.initialize()
self.running = True
self.logger.info("Starting Agentic-IAM Platform...")
# Start API server in separate process
if self.settings.enable_api:
self.api_process = mp.Process(target=self.start_api_server)
self.api_process.start()
self.logger.info(f"API server started on {self.settings.api_host}:{self.settings.api_port}")
# Start dashboard in separate process
if self.settings.enable_dashboard:
self.dashboard_process = mp.Process(target=self.start_dashboard_server)
self.dashboard_process.start()
self.logger.info(f"Dashboard started on {self.settings.dashboard_host}:{self.settings.dashboard_port}")
self.logger.info("Platform started successfully")
# Wait for processes
try:
while self.running:
await asyncio.sleep(1)
# Check if processes are still alive
if self.api_process and not self.api_process.is_alive():
self.logger.error("API server process died")
self.running = False
if self.dashboard_process and not self.dashboard_process.is_alive():
self.logger.error("Dashboard process died")
self.running = False
except KeyboardInterrupt:
self.logger.info("Shutdown signal received")
await self.shutdown()
async def shutdown(self):
"""Graceful shutdown"""
self.logger.info("Shutting down Agentic-IAM Platform...")
self.running = False
# Terminate processes
if self.api_process:
self.api_process.terminate()
self.api_process.join(timeout=5)
if self.api_process.is_alive():
self.api_process.kill()
if self.dashboard_process:
self.dashboard_process.terminate()
self.dashboard_process.join(timeout=5)
if self.dashboard_process.is_alive():
self.dashboard_process.kill()
# Shutdown IAM system
if self.iam:
await self.iam.shutdown()
self.logger.info("Platform shutdown complete")
async def main():
"""Main entry point"""
platform = AgenticIAMPlatform()
# Setup signal handlers
def signal_handler(sig, frame):
platform.logger.info(f"Received signal {sig}")
asyncio.create_task(platform.shutdown())
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
try:
await platform.start()
except Exception as e:
platform.logger.error(f"Platform error: {e}")
await platform.shutdown()
sys.exit(1)
if __name__ == "__main__":
# Set multiprocessing start method
if sys.platform.startswith('darwin'): # macOS
mp.set_start_method('spawn', force=True)
asyncio.run(main())