Files
OSIT-AE-API-FastAPI/app/routers/agent_bridge.py

60 lines
2.2 KiB
Python

from fastapi import APIRouter, Depends, HTTPException
import os
import platform
import json
from typing import Dict, Any
from app.lib_general_v3 import AccountContext, get_account_context
from app.models.response_models import Resp_Body_Base, mk_resp
router = APIRouter()
@router.get("/status", response_model=Resp_Body_Base, tags=["Agent Bridge"])
async def get_container_status(
account: AccountContext = Depends(get_account_context)
):
"""
Returns diagnostic information about the container environment.
Only accessible to administrators/managers via existing hierarchy.
"""
# Simple check for administrative access
if not getattr(account, "administrator", False) and not getattr(account, "manager", False):
raise HTTPException(status_code=403, detail="Administrative access required.")
status_data = {
"os": platform.system(),
"release": platform.release(),
"python_version": platform.python_version(),
"hostname": platform.node(),
"cpu_count": os.cpu_count(),
"environment_vars": {k: v for k, v in os.environ.items() if "PASSWORD" not in k.upper() and "KEY" not in k.upper() and "SECRET" not in k.upper()},
"cwd": os.getcwd(),
"container": os.path.exists('/.dockerenv')
}
return mk_resp(data=status_data)
@router.get("/logs", response_model=Resp_Body_Base, tags=["Agent Bridge"])
async def get_latest_logs(
lines: int = 50,
account: AccountContext = Depends(get_account_context)
):
"""
Returns the last N lines of the application log.
"""
if not getattr(account, "administrator", False) and not getattr(account, "manager", False):
raise HTTPException(status_code=403, detail="Administrative access required.")
from app.config import settings
log_path = settings.LOG_PATH.get('app', '/logs/aether_api.log')
if not os.path.exists(log_path):
return mk_resp(data=False, status_message=f"Log file not found at {log_path}")
try:
with open(log_path, 'r') as f:
log_lines = f.readlines()
latest = log_lines[-lines:] if len(log_lines) > lines else log_lines
return mk_resp(data="".join(latest))
except Exception as e:
return mk_resp(data=False, status_message=str(e))