feat: add agent zones
This commit is contained in:
1 parent
d8cd7ad058
commit
82309db79a
29 files changed
+2242
-255
No files matched your search
@@ -0,0 +1 @@
|
||||
"""Agent Zone routing domain."""
|
||||
@@ -0,0 +1,8 @@
|
||||
"""Agent Zone dependency wiring."""
|
||||
|
||||
from app.agent_zones.repository import AgentZoneRepository
|
||||
from app.agent_zones.service import AgentZoneService
|
||||
|
||||
|
||||
def get_agent_zone_service() -> AgentZoneService:
|
||||
return AgentZoneService(AgentZoneRepository())
|
||||
@@ -0,0 +1,17 @@
|
||||
"""Agent Zone domain exceptions."""
|
||||
|
||||
|
||||
class AgentZoneError(Exception):
|
||||
"""Base Agent Zone error."""
|
||||
|
||||
|
||||
class AgentZoneNotFound(AgentZoneError):
|
||||
"""A Site or Agent Zone does not exist."""
|
||||
|
||||
|
||||
class AgentZoneValidationError(AgentZoneError):
|
||||
"""Agent Zone input or assignment is invalid."""
|
||||
|
||||
|
||||
class AgentZoneConflict(AgentZoneError):
|
||||
"""An Agent Zone uniqueness or assignment invariant conflicts."""
|
||||
@@ -0,0 +1,114 @@
|
||||
"""Typed Agent Zone and check-in contracts."""
|
||||
|
||||
from ipaddress import ip_network
|
||||
from typing import Any
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator
|
||||
|
||||
|
||||
class AgentZoneCreateRequest(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
site_id: UUID
|
||||
name: str = Field(min_length=1, max_length=120)
|
||||
description: str | None = Field(default=None, max_length=1000)
|
||||
networks: list[str] = Field(default_factory=list, max_length=64)
|
||||
|
||||
@field_validator("name")
|
||||
@classmethod
|
||||
def normalize_name(cls, value: str) -> str:
|
||||
normalized = value.strip()
|
||||
if not normalized:
|
||||
raise ValueError("name cannot be blank")
|
||||
return normalized
|
||||
|
||||
@field_validator("description")
|
||||
@classmethod
|
||||
def normalize_description(cls, value: str | None) -> str | None:
|
||||
if value is None:
|
||||
return None
|
||||
return value.strip() or None
|
||||
|
||||
@field_validator("networks")
|
||||
@classmethod
|
||||
def normalize_networks(cls, values: list[str]) -> list[str]:
|
||||
normalized: list[str] = []
|
||||
for value in values:
|
||||
try:
|
||||
network = str(ip_network(value.strip(), strict=True))
|
||||
except ValueError as exc:
|
||||
raise ValueError(f"invalid CIDR network: {value}") from exc
|
||||
if network in normalized:
|
||||
raise ValueError(f"duplicate CIDR network: {network}")
|
||||
normalized.append(network)
|
||||
return normalized
|
||||
|
||||
|
||||
class AgentZoneNetwork(BaseModel):
|
||||
id: UUID
|
||||
network: str
|
||||
|
||||
|
||||
class AgentZoneSummary(BaseModel):
|
||||
id: UUID
|
||||
site_id: UUID
|
||||
name: str
|
||||
description: str | None = None
|
||||
is_enabled: bool
|
||||
assigned_agent_id: str | None = None
|
||||
networks: list[AgentZoneNetwork] = Field(default_factory=list)
|
||||
|
||||
|
||||
class AgentZoneListResponse(BaseModel):
|
||||
data: list[AgentZoneSummary]
|
||||
meta: dict[str, object] = Field(default_factory=dict)
|
||||
error: None = None
|
||||
|
||||
|
||||
class AgentZoneCreateResponse(BaseModel):
|
||||
data: AgentZoneSummary
|
||||
meta: dict[str, object] = Field(default_factory=dict)
|
||||
error: None = None
|
||||
|
||||
|
||||
class AgentCheckinRequest(BaseModel):
|
||||
model_config = ConfigDict(extra="allow")
|
||||
|
||||
agent_id: str | None = Field(default=None, max_length=255)
|
||||
site_id: UUID | None = None
|
||||
agent_zone_id: UUID | None = None
|
||||
site_name: str | None = Field(default=None, max_length=255)
|
||||
hostname: str | None = Field(default=None, max_length=255)
|
||||
version: str | None = Field(default=None, max_length=120)
|
||||
ip_address: str | None = Field(default=None, max_length=255)
|
||||
|
||||
@model_validator(mode="after")
|
||||
def require_site_for_explicit_zone(self) -> "AgentCheckinRequest":
|
||||
if self.agent_zone_id is not None and self.site_id is None:
|
||||
raise ValueError("site_id is required when agent_zone_id is supplied")
|
||||
return self
|
||||
|
||||
|
||||
class AgentCheckinResponse(BaseModel):
|
||||
ok: bool = True
|
||||
hub_mode: bool = True
|
||||
agent_id: str
|
||||
site_id: UUID | None = None
|
||||
agent_zone_id: UUID | None = None
|
||||
routing_available: bool
|
||||
routing_reason: str | None = None
|
||||
site_name: str
|
||||
message: str = "Agent check-in received."
|
||||
|
||||
|
||||
class AgentZoneRoutingContext(BaseModel):
|
||||
site_id: UUID | None = None
|
||||
agent_zone_id: UUID | None = None
|
||||
routing_reason: str | None = None
|
||||
|
||||
|
||||
class MinerZoneAssociationResult(BaseModel):
|
||||
agent_zone_id: UUID | None = None
|
||||
routing_reason: str | None = None
|
||||
outcomes: dict[UUID, str] = Field(default_factory=dict)
|
||||
@@ -0,0 +1,300 @@
|
||||
"""PostgreSQL persistence for Agent Zones and ownership assignments."""
|
||||
|
||||
from collections.abc import Callable, Iterable
|
||||
from typing import Any
|
||||
from uuid import UUID
|
||||
|
||||
import psycopg
|
||||
from psycopg.errors import ForeignKeyViolation, UniqueViolation
|
||||
from psycopg.rows import dict_row
|
||||
from psycopg.types.json import Jsonb
|
||||
|
||||
from app.agent_zones.exceptions import (
|
||||
AgentZoneConflict,
|
||||
AgentZoneNotFound,
|
||||
)
|
||||
from app.config import settings
|
||||
|
||||
|
||||
ConnectionFactory = Callable[[], psycopg.Connection]
|
||||
|
||||
|
||||
def default_connection_factory() -> psycopg.Connection:
|
||||
return psycopg.connect(
|
||||
host=settings.postgres_host,
|
||||
port=settings.postgres_port,
|
||||
dbname=settings.postgres_db,
|
||||
user=settings.postgres_user,
|
||||
password=settings.postgres_password,
|
||||
connect_timeout=5,
|
||||
)
|
||||
|
||||
|
||||
class AgentZoneRepository:
|
||||
def __init__(self, connection_factory: ConnectionFactory | None = None):
|
||||
self._connection_factory = connection_factory or default_connection_factory
|
||||
|
||||
def get_agent(self, agent_id: str) -> dict | None:
|
||||
connection = self._connection_factory()
|
||||
try:
|
||||
with connection.cursor(row_factory=dict_row) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT agent_id, site_id, agent_zone_id, site_name, hostname,
|
||||
version, ip_address, last_checkin_at, last_payload
|
||||
FROM minerops_site_agents
|
||||
WHERE agent_id = %s
|
||||
""",
|
||||
(agent_id,),
|
||||
)
|
||||
row = cur.fetchone()
|
||||
return dict(row) if row else None
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
def get_zone(self, zone_id: UUID) -> dict | None:
|
||||
connection = self._connection_factory()
|
||||
try:
|
||||
with connection.cursor(row_factory=dict_row) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT id, site_id, name, description, is_enabled,
|
||||
created_at, updated_at
|
||||
FROM agent_zones
|
||||
WHERE id = %s
|
||||
""",
|
||||
(zone_id,),
|
||||
)
|
||||
row = cur.fetchone()
|
||||
return dict(row) if row else None
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
def list_usable_zones(self, site_id: UUID) -> list[dict]:
|
||||
connection = self._connection_factory()
|
||||
try:
|
||||
with connection.cursor(row_factory=dict_row) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT id, site_id, name, description, is_enabled
|
||||
FROM agent_zones
|
||||
WHERE site_id = %s AND is_enabled
|
||||
ORDER BY lower(name), id
|
||||
""",
|
||||
(site_id,),
|
||||
)
|
||||
return [dict(row) for row in cur.fetchall()]
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
def list_zones(self, site_id: UUID | None = None) -> list[dict]:
|
||||
connection = self._connection_factory()
|
||||
try:
|
||||
with connection.cursor(row_factory=dict_row) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT zone.id, zone.site_id, zone.name, zone.description,
|
||||
zone.is_enabled, agent.agent_id AS assigned_agent_id,
|
||||
range.id AS range_id, range.network::text AS network
|
||||
FROM agent_zones AS zone
|
||||
LEFT JOIN minerops_site_agents AS agent
|
||||
ON agent.agent_zone_id = zone.id
|
||||
LEFT JOIN agent_zone_network_ranges AS range
|
||||
ON range.agent_zone_id = zone.id
|
||||
WHERE (%s::uuid IS NULL OR zone.site_id = %s::uuid)
|
||||
ORDER BY lower(zone.name), zone.id, range.network, range.id
|
||||
""",
|
||||
(site_id, site_id),
|
||||
)
|
||||
rows = cur.fetchall()
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
zones: dict[UUID, dict] = {}
|
||||
for row in rows:
|
||||
zone = zones.setdefault(
|
||||
row["id"],
|
||||
{
|
||||
"id": row["id"],
|
||||
"site_id": row["site_id"],
|
||||
"name": row["name"],
|
||||
"description": row["description"],
|
||||
"is_enabled": row["is_enabled"],
|
||||
"assigned_agent_id": row["assigned_agent_id"],
|
||||
"networks": [],
|
||||
},
|
||||
)
|
||||
if row["range_id"] is not None:
|
||||
zone["networks"].append(
|
||||
{"id": row["range_id"], "network": row["network"]}
|
||||
)
|
||||
return list(zones.values())
|
||||
|
||||
def create_zone(
|
||||
self,
|
||||
*,
|
||||
site_id: UUID,
|
||||
name: str,
|
||||
description: str | None,
|
||||
networks: list[str],
|
||||
) -> UUID:
|
||||
connection = self._connection_factory()
|
||||
try:
|
||||
with connection.cursor(row_factory=dict_row) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO agent_zones (site_id, name, description)
|
||||
VALUES (%s, %s, %s)
|
||||
RETURNING id
|
||||
""",
|
||||
(site_id, name, description),
|
||||
)
|
||||
zone_id = cur.fetchone()["id"]
|
||||
for network in networks:
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO agent_zone_network_ranges (agent_zone_id, network)
|
||||
VALUES (%s, %s::cidr)
|
||||
""",
|
||||
(zone_id, network),
|
||||
)
|
||||
connection.commit()
|
||||
return zone_id
|
||||
except ForeignKeyViolation as exc:
|
||||
connection.rollback()
|
||||
raise AgentZoneNotFound("Site not found") from exc
|
||||
except UniqueViolation as exc:
|
||||
connection.rollback()
|
||||
raise AgentZoneConflict(
|
||||
"An enabled Agent Zone with this name already exists at the Site"
|
||||
) from exc
|
||||
except Exception:
|
||||
connection.rollback()
|
||||
raise
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
def save_checkin(
|
||||
self,
|
||||
*,
|
||||
agent_id: str,
|
||||
site_id: UUID | None,
|
||||
agent_zone_id: UUID | None,
|
||||
site_name: str,
|
||||
hostname: str | None,
|
||||
version: str | None,
|
||||
ip_address: str | None,
|
||||
payload: dict[str, Any],
|
||||
) -> None:
|
||||
connection = self._connection_factory()
|
||||
try:
|
||||
with connection.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO minerops_site_agents (
|
||||
agent_id, site_id, agent_zone_id, site_name, hostname,
|
||||
version, ip_address, last_checkin_at, last_payload
|
||||
) VALUES (%s, %s, %s, %s, %s, %s, %s, now(), %s)
|
||||
ON CONFLICT (agent_id) DO UPDATE SET
|
||||
site_id = EXCLUDED.site_id,
|
||||
agent_zone_id = EXCLUDED.agent_zone_id,
|
||||
site_name = EXCLUDED.site_name,
|
||||
hostname = EXCLUDED.hostname,
|
||||
version = EXCLUDED.version,
|
||||
ip_address = EXCLUDED.ip_address,
|
||||
last_checkin_at = now(),
|
||||
last_payload = EXCLUDED.last_payload
|
||||
""",
|
||||
(
|
||||
agent_id,
|
||||
site_id,
|
||||
agent_zone_id,
|
||||
site_name,
|
||||
hostname,
|
||||
version,
|
||||
ip_address,
|
||||
Jsonb(payload),
|
||||
),
|
||||
)
|
||||
connection.commit()
|
||||
except UniqueViolation as exc:
|
||||
connection.rollback()
|
||||
raise AgentZoneConflict(
|
||||
"The Agent Zone already has an assigned Agent"
|
||||
) from exc
|
||||
except (ForeignKeyViolation, psycopg.errors.CheckViolation) as exc:
|
||||
connection.rollback()
|
||||
raise AgentZoneConflict("Agent Zone and Site assignment conflict") from exc
|
||||
except Exception:
|
||||
connection.rollback()
|
||||
raise
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
def get_agent_assignment(self, agent_id: str) -> dict | None:
|
||||
connection = self._connection_factory()
|
||||
try:
|
||||
with connection.cursor(row_factory=dict_row) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT agent.agent_id, agent.site_id, agent.agent_zone_id,
|
||||
zone.is_enabled AS zone_enabled
|
||||
FROM minerops_site_agents AS agent
|
||||
LEFT JOIN agent_zones AS zone ON zone.id = agent.agent_zone_id
|
||||
WHERE agent.agent_id = %s
|
||||
""",
|
||||
(agent_id,),
|
||||
)
|
||||
row = cur.fetchone()
|
||||
return dict(row) if row else None
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
def assign_miners_to_zone(
|
||||
self, miner_ids: Iterable[UUID], agent_zone_id: UUID
|
||||
) -> dict[UUID, str]:
|
||||
unique_ids = list(dict.fromkeys(miner_ids))
|
||||
if not unique_ids:
|
||||
return {}
|
||||
connection = self._connection_factory()
|
||||
outcomes: dict[UUID, str] = {}
|
||||
try:
|
||||
with connection.cursor(row_factory=dict_row) as cur:
|
||||
cur.execute(
|
||||
"SELECT id, site_id FROM agent_zones WHERE id = %s",
|
||||
(agent_zone_id,),
|
||||
)
|
||||
zone = cur.fetchone()
|
||||
if not zone:
|
||||
raise AgentZoneNotFound("Agent Zone not found")
|
||||
for miner_id in unique_ids:
|
||||
cur.execute(
|
||||
"SELECT id, site_id, agent_zone_id FROM miners WHERE id = %s FOR UPDATE",
|
||||
(miner_id,),
|
||||
)
|
||||
miner = cur.fetchone()
|
||||
if not miner:
|
||||
outcomes[miner_id] = "miner_not_found"
|
||||
elif miner["agent_zone_id"] == agent_zone_id:
|
||||
outcomes[miner_id] = "preserved"
|
||||
elif miner["agent_zone_id"] is not None:
|
||||
outcomes[miner_id] = "agent_zone_conflict"
|
||||
elif miner["site_id"] not in {None, zone["site_id"]}:
|
||||
outcomes[miner_id] = "site_conflict"
|
||||
else:
|
||||
cur.execute(
|
||||
"""
|
||||
UPDATE miners
|
||||
SET site_id = %s, agent_zone_id = %s, updated_at = now()
|
||||
WHERE id = %s AND agent_zone_id IS NULL
|
||||
""",
|
||||
(zone["site_id"], agent_zone_id, miner_id),
|
||||
)
|
||||
outcomes[miner_id] = "assigned"
|
||||
connection.commit()
|
||||
return outcomes
|
||||
except Exception:
|
||||
connection.rollback()
|
||||
raise
|
||||
finally:
|
||||
connection.close()
|
||||
@@ -0,0 +1,75 @@
|
||||
"""HTTP mapping for Agent Zones and Agent check-in."""
|
||||
|
||||
from typing import Annotated
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
||||
|
||||
from app.agent_commands.dependencies import require_agent_identity
|
||||
from app.agent_zones.dependencies import get_agent_zone_service
|
||||
from app.agent_zones.exceptions import (
|
||||
AgentZoneConflict,
|
||||
AgentZoneNotFound,
|
||||
AgentZoneValidationError,
|
||||
)
|
||||
from app.agent_zones.models import (
|
||||
AgentCheckinRequest,
|
||||
AgentCheckinResponse,
|
||||
AgentZoneCreateRequest,
|
||||
AgentZoneCreateResponse,
|
||||
AgentZoneListResponse,
|
||||
)
|
||||
from app.agent_zones.service import AgentZoneService
|
||||
from app.identity import require_permission
|
||||
|
||||
|
||||
router = APIRouter(tags=["agent-zones"])
|
||||
ZoneService = Annotated[AgentZoneService, Depends(get_agent_zone_service)]
|
||||
AgentIdentity = Annotated[str, Depends(require_agent_identity)]
|
||||
ZoneViewer = Annotated[dict, Depends(require_permission("agents.view"))]
|
||||
ZoneManager = Annotated[dict, Depends(require_permission("agents.manage"))]
|
||||
|
||||
|
||||
@router.get("/api/agent-zones", response_model=AgentZoneListResponse)
|
||||
def list_agent_zones(
|
||||
current_user: ZoneViewer,
|
||||
service: ZoneService,
|
||||
site_id: UUID | None = Query(default=None),
|
||||
) -> AgentZoneListResponse:
|
||||
del current_user
|
||||
return AgentZoneListResponse(data=service.list_zones(site_id))
|
||||
|
||||
|
||||
@router.post(
|
||||
"/api/agent-zones",
|
||||
response_model=AgentZoneCreateResponse,
|
||||
status_code=status.HTTP_201_CREATED,
|
||||
)
|
||||
def create_agent_zone(
|
||||
request: AgentZoneCreateRequest,
|
||||
current_user: ZoneManager,
|
||||
service: ZoneService,
|
||||
) -> AgentZoneCreateResponse:
|
||||
del current_user
|
||||
try:
|
||||
return AgentZoneCreateResponse(data=service.create_zone(request))
|
||||
except AgentZoneNotFound as exc:
|
||||
raise HTTPException(status_code=404, detail=str(exc)) from exc
|
||||
except AgentZoneConflict as exc:
|
||||
raise HTTPException(status_code=409, detail=str(exc)) from exc
|
||||
|
||||
|
||||
@router.post("/api/agent/checkin", response_model=AgentCheckinResponse)
|
||||
def agent_checkin(
|
||||
request: AgentCheckinRequest,
|
||||
agent_id: AgentIdentity,
|
||||
service: ZoneService,
|
||||
) -> AgentCheckinResponse:
|
||||
try:
|
||||
return service.checkin(agent_id, request)
|
||||
except AgentZoneNotFound as exc:
|
||||
raise HTTPException(status_code=404, detail=str(exc)) from exc
|
||||
except AgentZoneConflict as exc:
|
||||
raise HTTPException(status_code=409, detail=str(exc)) from exc
|
||||
except AgentZoneValidationError as exc:
|
||||
raise HTTPException(status_code=422, detail=str(exc)) from exc
|
||||
@@ -0,0 +1,194 @@
|
||||
"""Business rules for Agent Zone routing and transitional check-in."""
|
||||
|
||||
from typing import Any
|
||||
from uuid import UUID
|
||||
|
||||
from app.agent_zones.exceptions import (
|
||||
AgentZoneNotFound,
|
||||
AgentZoneValidationError,
|
||||
)
|
||||
from app.agent_zones.models import (
|
||||
AgentCheckinRequest,
|
||||
AgentCheckinResponse,
|
||||
AgentZoneRoutingContext,
|
||||
AgentZoneCreateRequest,
|
||||
AgentZoneSummary,
|
||||
MinerZoneAssociationResult,
|
||||
)
|
||||
from app.agent_zones.repository import AgentZoneRepository
|
||||
|
||||
|
||||
SENSITIVE_KEYS = {
|
||||
"agent_secret",
|
||||
"api3_account",
|
||||
"api3_password",
|
||||
"password",
|
||||
"salt",
|
||||
"token",
|
||||
}
|
||||
|
||||
|
||||
def _sanitize(value: Any) -> Any:
|
||||
if isinstance(value, dict):
|
||||
return {
|
||||
key: _sanitize(item)
|
||||
for key, item in value.items()
|
||||
if key.lower() not in SENSITIVE_KEYS
|
||||
and not any(
|
||||
marker in key.lower()
|
||||
for marker in ("secret", "password", "token", "salt")
|
||||
)
|
||||
}
|
||||
if isinstance(value, list):
|
||||
return [_sanitize(item) for item in value]
|
||||
return value
|
||||
|
||||
|
||||
class AgentZoneService:
|
||||
@staticmethod
|
||||
def validate_agent_identity(
|
||||
authenticated_agent_id: str, claimed_agent_id: str | None
|
||||
) -> None:
|
||||
if claimed_agent_id and claimed_agent_id.strip() != authenticated_agent_id:
|
||||
raise AgentZoneValidationError(
|
||||
"Payload agent_id does not match authenticated Agent identity"
|
||||
)
|
||||
|
||||
def __init__(self, repository: AgentZoneRepository):
|
||||
self.repository = repository
|
||||
|
||||
def list_zones(self, site_id: UUID | None = None) -> list[AgentZoneSummary]:
|
||||
return [
|
||||
AgentZoneSummary.model_validate(row)
|
||||
for row in self.repository.list_zones(site_id)
|
||||
]
|
||||
|
||||
def create_zone(self, request: AgentZoneCreateRequest) -> AgentZoneSummary:
|
||||
zone_id = self.repository.create_zone(
|
||||
site_id=request.site_id,
|
||||
name=request.name,
|
||||
description=request.description,
|
||||
networks=request.networks,
|
||||
)
|
||||
for zone in self.list_zones(request.site_id):
|
||||
if zone.id == zone_id:
|
||||
return zone
|
||||
raise AgentZoneNotFound("Created Agent Zone was not found")
|
||||
|
||||
def checkin(
|
||||
self, authenticated_agent_id: str, request: AgentCheckinRequest
|
||||
) -> AgentCheckinResponse:
|
||||
self.validate_agent_identity(authenticated_agent_id, request.agent_id)
|
||||
|
||||
existing = self.repository.get_agent(authenticated_agent_id)
|
||||
requested_site = request.site_id
|
||||
requested_zone = request.agent_zone_id
|
||||
final_site = requested_site or (existing or {}).get("site_id")
|
||||
final_zone: UUID | None = None
|
||||
reason: str | None = None
|
||||
|
||||
established_zone = None
|
||||
established_zone_id = (existing or {}).get("agent_zone_id")
|
||||
if established_zone_id is not None:
|
||||
established_zone = self.repository.get_zone(established_zone_id)
|
||||
if not established_zone:
|
||||
raise AgentZoneValidationError(
|
||||
"Existing Agent Zone assignment is invalid"
|
||||
)
|
||||
if (
|
||||
requested_site is not None
|
||||
and established_zone["site_id"] != requested_site
|
||||
):
|
||||
raise AgentZoneValidationError(
|
||||
"Agent cannot change Sites while assigned to an Agent Zone"
|
||||
)
|
||||
|
||||
if requested_zone is not None:
|
||||
zone = self.repository.get_zone(requested_zone)
|
||||
if not zone:
|
||||
raise AgentZoneNotFound("Agent Zone not found")
|
||||
if zone["site_id"] != requested_site:
|
||||
raise AgentZoneValidationError(
|
||||
"Agent Zone does not belong to the supplied Site"
|
||||
)
|
||||
if not zone["is_enabled"]:
|
||||
raise AgentZoneValidationError("Agent Zone is disabled")
|
||||
if established_zone_id is not None and established_zone_id != requested_zone:
|
||||
raise AgentZoneValidationError(
|
||||
"Agent is already assigned to a different Agent Zone"
|
||||
)
|
||||
final_zone = requested_zone
|
||||
elif established_zone is not None:
|
||||
final_site = established_zone["site_id"]
|
||||
final_zone = established_zone["id"]
|
||||
if not established_zone["is_enabled"]:
|
||||
reason = "agent_zone_disabled"
|
||||
elif final_site is not None:
|
||||
usable = self.repository.list_usable_zones(final_site)
|
||||
if len(usable) == 1:
|
||||
final_zone = usable[0]["id"]
|
||||
elif not usable:
|
||||
reason = "agent_zone_unavailable"
|
||||
else:
|
||||
reason = "agent_zone_ambiguous"
|
||||
else:
|
||||
reason = "site_unassigned"
|
||||
|
||||
site_name = (request.site_name or "").strip()
|
||||
if not site_name:
|
||||
site_name = str((existing or {}).get("site_name") or "Unassigned")
|
||||
payload = _sanitize(request.model_dump(mode="json"))
|
||||
self.repository.save_checkin(
|
||||
agent_id=authenticated_agent_id,
|
||||
site_id=final_site,
|
||||
agent_zone_id=final_zone,
|
||||
site_name=site_name,
|
||||
hostname=(request.hostname or "").strip() or None,
|
||||
version=(request.version or "").strip() or None,
|
||||
ip_address=(request.ip_address or "").strip() or None,
|
||||
payload=payload,
|
||||
)
|
||||
routing_available = final_zone is not None and reason is None
|
||||
return AgentCheckinResponse(
|
||||
agent_id=authenticated_agent_id,
|
||||
site_id=final_site,
|
||||
agent_zone_id=final_zone,
|
||||
routing_available=routing_available,
|
||||
routing_reason=reason,
|
||||
site_name=site_name,
|
||||
)
|
||||
|
||||
def routing_context(
|
||||
self, agent_id: str, recorded_zone_id: UUID | None = None
|
||||
) -> AgentZoneRoutingContext:
|
||||
assignment = self.repository.get_agent_assignment(agent_id)
|
||||
if not assignment or assignment.get("agent_zone_id") is None:
|
||||
return AgentZoneRoutingContext(routing_reason="agent_zone_unassigned")
|
||||
zone_id = assignment["agent_zone_id"]
|
||||
if recorded_zone_id is not None and recorded_zone_id != zone_id:
|
||||
return AgentZoneRoutingContext(routing_reason="agent_zone_conflict")
|
||||
if assignment.get("zone_enabled") is not True:
|
||||
return AgentZoneRoutingContext(
|
||||
site_id=assignment.get("site_id"),
|
||||
agent_zone_id=zone_id,
|
||||
routing_reason="agent_zone_disabled",
|
||||
)
|
||||
return AgentZoneRoutingContext(
|
||||
site_id=assignment.get("site_id"), agent_zone_id=zone_id
|
||||
)
|
||||
|
||||
def associate_miners_from_agent(
|
||||
self, agent_id: str, miner_ids: list[UUID]
|
||||
) -> MinerZoneAssociationResult:
|
||||
routing = self.routing_context(agent_id)
|
||||
if routing.routing_reason:
|
||||
return MinerZoneAssociationResult(
|
||||
agent_zone_id=routing.agent_zone_id,
|
||||
routing_reason=routing.routing_reason,
|
||||
)
|
||||
return MinerZoneAssociationResult(
|
||||
agent_zone_id=routing.agent_zone_id,
|
||||
outcomes=self.repository.assign_miners_to_zone(
|
||||
miner_ids, routing.agent_zone_id
|
||||
),
|
||||
)
|
||||
@@ -777,6 +777,8 @@ def required_api_permission(method: str, path: str) -> str | None:
|
||||
return "repairs.read"
|
||||
if component_request(path):
|
||||
return "components.create" if method == "POST" else "components.read"
|
||||
if path == "/api/agent-zones":
|
||||
return "agents.view" if method == "GET" else "agents.manage"
|
||||
if repair_parts_request(path):
|
||||
return "repairs.read" if method == "GET" else "repairs.parts.manage"
|
||||
if path.startswith("/api/admin/"):
|
||||
|
||||
+154
-235
@@ -11,7 +11,12 @@ from fastapi import Request, Depends, FastAPI, Header, HTTPException, Query, sta
|
||||
from pydantic import BaseModel, ConfigDict, EmailStr, Field, ValidationError
|
||||
|
||||
from app.config import settings
|
||||
from app.agent_commands.dependencies import require_agent_identity
|
||||
from app.agent_commands.router import router as agent_commands_router
|
||||
from app.agent_zones.dependencies import get_agent_zone_service
|
||||
from app.agent_zones.models import MinerZoneAssociationResult
|
||||
from app.agent_zones.service import AgentZoneService
|
||||
from app.agent_zones.router import router as agent_zones_router
|
||||
from app.miner_control.router import router as miner_control_router
|
||||
from app.dashboard.router import router as dashboard_router
|
||||
from app.identity import (
|
||||
@@ -67,6 +72,7 @@ app.include_router(frontend_router)
|
||||
app.include_router(inventory_router)
|
||||
app.include_router(miner_control_router)
|
||||
app.include_router(agent_commands_router)
|
||||
app.include_router(agent_zones_router)
|
||||
app.include_router(components_router)
|
||||
app.include_router(locations_router)
|
||||
app.include_router(dashboard_router)
|
||||
@@ -25081,70 +25087,6 @@ async def startup_ensure_minerops_agent_tables():
|
||||
await ensure_minerops_agent_tables()
|
||||
|
||||
|
||||
@app.post("/api/agent/checkin")
|
||||
async def minerops_agent_checkin(request: Request):
|
||||
import json
|
||||
|
||||
minerops_require_agent_secret(request)
|
||||
await ensure_minerops_agent_tables()
|
||||
|
||||
try:
|
||||
body = await request.json()
|
||||
except Exception:
|
||||
body = {}
|
||||
|
||||
agent_id = str(body.get("agent_id") or "plexus-windows-agent").strip()
|
||||
site_id = str(body.get("site_id") or "").strip()
|
||||
site_name = str(body.get("site_name") or "Plexus").strip()
|
||||
hostname = str(body.get("hostname") or "").strip()
|
||||
version = str(body.get("version") or "").strip()
|
||||
ip_address = str(body.get("ip_address") or "").strip()
|
||||
|
||||
conn = await minerops_command_db_connect()
|
||||
|
||||
try:
|
||||
await conn.execute(
|
||||
"""
|
||||
INSERT INTO minerops_site_agents (
|
||||
agent_id,
|
||||
site_id,
|
||||
site_name,
|
||||
hostname,
|
||||
version,
|
||||
ip_address,
|
||||
last_checkin_at,
|
||||
last_payload
|
||||
)
|
||||
VALUES ($1, NULLIF($2, '')::uuid, $3, $4, $5, $6, now(), $7::jsonb)
|
||||
ON CONFLICT (agent_id)
|
||||
DO UPDATE SET
|
||||
site_id = COALESCE(EXCLUDED.site_id, minerops_site_agents.site_id),
|
||||
site_name = EXCLUDED.site_name,
|
||||
hostname = EXCLUDED.hostname,
|
||||
version = EXCLUDED.version,
|
||||
ip_address = EXCLUDED.ip_address,
|
||||
last_checkin_at = now(),
|
||||
last_payload = EXCLUDED.last_payload
|
||||
""",
|
||||
agent_id,
|
||||
site_id,
|
||||
site_name,
|
||||
hostname,
|
||||
version,
|
||||
ip_address,
|
||||
json.dumps(body),
|
||||
)
|
||||
finally:
|
||||
await conn.close()
|
||||
|
||||
return {
|
||||
"ok": True,
|
||||
"hub_mode": True,
|
||||
"agent_id": agent_id,
|
||||
"site_name": site_name,
|
||||
"message": "Agent check-in received.",
|
||||
}
|
||||
|
||||
|
||||
@app.get("/api/agent/config")
|
||||
async def minerops_agent_config(request: Request):
|
||||
@@ -25183,10 +25125,13 @@ async def minerops_agent_config(request: Request):
|
||||
|
||||
|
||||
@app.post("/api/agent/miner-snapshots")
|
||||
async def minerops_agent_miner_snapshots(request: Request):
|
||||
async def minerops_agent_miner_snapshots(
|
||||
request: Request,
|
||||
authenticated_agent_id: str = Depends(require_agent_identity),
|
||||
zone_service: AgentZoneService = Depends(get_agent_zone_service),
|
||||
):
|
||||
import json
|
||||
|
||||
minerops_require_agent_secret(request)
|
||||
await ensure_minerops_agent_tables()
|
||||
|
||||
try:
|
||||
@@ -25194,30 +25139,39 @@ async def minerops_agent_miner_snapshots(request: Request):
|
||||
except Exception:
|
||||
body = {}
|
||||
|
||||
agent_id = str(body.get("agent_id") or "plexus-windows-agent").strip()
|
||||
site_name = str(body.get("site_name") or "Plexus").strip()
|
||||
|
||||
zone_service.validate_agent_identity(
|
||||
authenticated_agent_id, str(body.get("agent_id") or "").strip() or None
|
||||
)
|
||||
site_name = str(body.get("site_name") or "Unassigned").strip()
|
||||
miners = body.get("miners")
|
||||
|
||||
if not isinstance(miners, list):
|
||||
miners = []
|
||||
|
||||
conn = await minerops_command_db_connect()
|
||||
miner_ids = []
|
||||
for item in miners:
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
try:
|
||||
miner_ids.append(UUID(str(item.get("miner_id") or "")))
|
||||
except ValueError:
|
||||
continue
|
||||
ownership = zone_service.associate_miners_from_agent(
|
||||
authenticated_agent_id, miner_ids
|
||||
)
|
||||
|
||||
conn = await minerops_command_db_connect()
|
||||
try:
|
||||
row = await conn.fetchrow(
|
||||
"""
|
||||
INSERT INTO minerops_agent_snapshots (
|
||||
agent_id,
|
||||
site_name,
|
||||
snapshot_kind,
|
||||
miner_count,
|
||||
payload
|
||||
agent_id, agent_zone_id, site_name, snapshot_kind,
|
||||
miner_count, payload
|
||||
)
|
||||
VALUES ($1, $2, 'miner_poll', $3, $4::jsonb)
|
||||
VALUES ($1, $2::uuid, $3, 'miner_poll', $4, $5::jsonb)
|
||||
RETURNING id, received_at
|
||||
""",
|
||||
agent_id,
|
||||
authenticated_agent_id,
|
||||
str(ownership.agent_zone_id) if ownership.agent_zone_id else None,
|
||||
site_name,
|
||||
len(miners),
|
||||
json.dumps(body),
|
||||
@@ -25225,13 +25179,16 @@ async def minerops_agent_miner_snapshots(request: Request):
|
||||
finally:
|
||||
await conn.close()
|
||||
|
||||
apply_result = await minerops_apply_agent_snapshot_to_live_tables(body)
|
||||
|
||||
apply_result = await minerops_apply_agent_snapshot_to_live_tables(
|
||||
body, ownership
|
||||
)
|
||||
return {
|
||||
"ok": True,
|
||||
"snapshot_id": row["id"] if row else None,
|
||||
"received_at": row["received_at"].isoformat() if row and row["received_at"] else None,
|
||||
"miner_count": len(miners),
|
||||
"agent_zone_id": str(ownership.agent_zone_id) if ownership.agent_zone_id else None,
|
||||
"routing_reason": ownership.routing_reason,
|
||||
"apply_result": apply_result,
|
||||
"message": "Miner snapshot received, staged, and applied to live tables.",
|
||||
}
|
||||
@@ -25570,23 +25527,22 @@ def minerops_agent_parse_summary(miner_payload: dict) -> dict:
|
||||
}
|
||||
|
||||
|
||||
async def minerops_apply_agent_snapshot_to_live_tables(snapshot_body: dict) -> dict:
|
||||
async def minerops_apply_agent_snapshot_to_live_tables(
|
||||
snapshot_body: dict, ownership: MinerZoneAssociationResult
|
||||
) -> dict:
|
||||
import json
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
|
||||
miners = snapshot_body.get("miners")
|
||||
|
||||
if not isinstance(miners, list):
|
||||
miners = []
|
||||
|
||||
now = datetime.now(timezone.utc)
|
||||
|
||||
applied = 0
|
||||
online_count = 0
|
||||
offline_count = 0
|
||||
errors = []
|
||||
|
||||
conn = await minerops_command_db_connect()
|
||||
|
||||
try:
|
||||
@@ -25597,29 +25553,38 @@ async def minerops_apply_agent_snapshot_to_live_tables(snapshot_body: dict) -> d
|
||||
miner_id = str(item.get("miner_id") or "").strip()
|
||||
ip_address = str(item.get("ip_address") or "").strip()
|
||||
online = bool(item.get("online"))
|
||||
|
||||
if not miner_id:
|
||||
continue
|
||||
try:
|
||||
miner_uuid = UUID(miner_id)
|
||||
except ValueError:
|
||||
errors.append({"miner_id": miner_id, "error": "invalid_miner_id"})
|
||||
continue
|
||||
|
||||
if ownership.routing_reason:
|
||||
errors.append(
|
||||
{"miner_id": miner_id, "error": ownership.routing_reason}
|
||||
)
|
||||
continue
|
||||
zone_outcome = ownership.outcomes.get(miner_uuid, "miner_not_found")
|
||||
if zone_outcome not in {"assigned", "preserved"}:
|
||||
errors.append({"miner_id": miner_id, "error": zone_outcome})
|
||||
continue
|
||||
|
||||
parsed = minerops_agent_parse_summary(item)
|
||||
raw_summary = parsed["raw"]
|
||||
msg = parsed["msg"]
|
||||
|
||||
status = "online" if online else "offline"
|
||||
|
||||
mhs_av = minerops_agent_to_float(msg.get("MHS av"))
|
||||
mhs_1m = minerops_agent_to_float(msg.get("MHS 1m"))
|
||||
mhs_15m = minerops_agent_to_float(msg.get("MHS 15m"))
|
||||
hs_rt = minerops_agent_to_float(msg.get("HS RT"))
|
||||
|
||||
power_w = minerops_agent_to_float(msg.get("Power"))
|
||||
power_rate = minerops_agent_to_float(msg.get("Power Rate"))
|
||||
|
||||
env_temp = minerops_agent_to_float(msg.get("Env Temp"))
|
||||
chip_temp_min = minerops_agent_to_float(msg.get("Chip Temp Min"))
|
||||
chip_temp_max = minerops_agent_to_float(msg.get("Chip Temp Max"))
|
||||
chip_temp_avg = minerops_agent_to_float(msg.get("Chip Temp Avg"))
|
||||
|
||||
fan_in = minerops_agent_to_int(msg.get("Fan Speed In"))
|
||||
fan_out = minerops_agent_to_int(msg.get("Fan Speed Out"))
|
||||
uptime = minerops_agent_to_int(msg.get("Uptime"))
|
||||
@@ -25629,12 +25594,10 @@ async def minerops_apply_agent_snapshot_to_live_tables(snapshot_body: dict) -> d
|
||||
try:
|
||||
if online:
|
||||
online_count += 1
|
||||
|
||||
await conn.execute(
|
||||
"""
|
||||
UPDATE miners
|
||||
SET
|
||||
status = $2,
|
||||
SET status = $2,
|
||||
current_ip = COALESCE(NULLIF($3, '')::inet, current_ip),
|
||||
last_seen_at = $4,
|
||||
updated_at = now()
|
||||
@@ -25645,54 +25608,20 @@ async def minerops_apply_agent_snapshot_to_live_tables(snapshot_body: dict) -> d
|
||||
ip_address,
|
||||
now,
|
||||
)
|
||||
|
||||
await conn.execute(
|
||||
"""
|
||||
INSERT INTO miner_status_snapshots (
|
||||
id,
|
||||
miner_id,
|
||||
observed_ip,
|
||||
observed_at,
|
||||
mhs_av,
|
||||
mhs_1m,
|
||||
mhs_15m,
|
||||
hs_rt,
|
||||
power_w,
|
||||
power_rate_w_per_th,
|
||||
env_temp_c,
|
||||
chip_temp_min_c,
|
||||
chip_temp_max_c,
|
||||
chip_temp_avg_c,
|
||||
fan_speed_in_rpm,
|
||||
fan_speed_out_rpm,
|
||||
uptime_seconds,
|
||||
hash_stable,
|
||||
pool_rejected_percent,
|
||||
raw_summary_json,
|
||||
created_at
|
||||
id, miner_id, observed_ip, observed_at, mhs_av,
|
||||
mhs_1m, mhs_15m, hs_rt, power_w,
|
||||
power_rate_w_per_th, env_temp_c, chip_temp_min_c,
|
||||
chip_temp_max_c, chip_temp_avg_c, fan_speed_in_rpm,
|
||||
fan_speed_out_rpm, uptime_seconds, hash_stable,
|
||||
pool_rejected_percent, raw_summary_json, created_at
|
||||
)
|
||||
VALUES (
|
||||
$1::uuid,
|
||||
$2::uuid,
|
||||
NULLIF($3, '')::inet,
|
||||
$4,
|
||||
$5,
|
||||
$6,
|
||||
$7,
|
||||
$8,
|
||||
$9,
|
||||
$10,
|
||||
$11,
|
||||
$12,
|
||||
$13,
|
||||
$14,
|
||||
$15,
|
||||
$16,
|
||||
$17,
|
||||
$18,
|
||||
$19,
|
||||
$20::jsonb,
|
||||
now()
|
||||
$1::uuid, $2::uuid, NULLIF($3, '')::inet, $4,
|
||||
$5, $6, $7, $8, $9, $10, $11, $12, $13, $14,
|
||||
$15, $16, $17, $18, $19, $20::jsonb, now()
|
||||
)
|
||||
""",
|
||||
str(uuid.uuid4()),
|
||||
@@ -25718,12 +25647,10 @@ async def minerops_apply_agent_snapshot_to_live_tables(snapshot_body: dict) -> d
|
||||
)
|
||||
else:
|
||||
offline_count += 1
|
||||
|
||||
await conn.execute(
|
||||
"""
|
||||
UPDATE miners
|
||||
SET
|
||||
status = $2,
|
||||
SET status = $2,
|
||||
current_ip = COALESCE(NULLIF($3, '')::inet, current_ip),
|
||||
updated_at = now()
|
||||
WHERE id = $1::uuid
|
||||
@@ -25732,19 +25659,12 @@ async def minerops_apply_agent_snapshot_to_live_tables(snapshot_body: dict) -> d
|
||||
status,
|
||||
ip_address,
|
||||
)
|
||||
|
||||
applied += 1
|
||||
|
||||
except Exception as exc:
|
||||
if len(errors) < 20:
|
||||
errors.append(
|
||||
{
|
||||
"miner_id": miner_id,
|
||||
"ip_address": ip_address,
|
||||
"error": str(exc),
|
||||
}
|
||||
{"miner_id": miner_id, "ip_address": ip_address, "error": str(exc)}
|
||||
)
|
||||
|
||||
finally:
|
||||
await conn.close()
|
||||
|
||||
@@ -25756,7 +25676,6 @@ async def minerops_apply_agent_snapshot_to_live_tables(snapshot_body: dict) -> d
|
||||
}
|
||||
|
||||
|
||||
|
||||
# -----------------------------
|
||||
# Phase 5K: Agent command queue
|
||||
# -----------------------------
|
||||
@@ -26639,24 +26558,28 @@ async def minerops_queue_agent_scan_new_miners(current_user: dict = Depends(get_
|
||||
|
||||
|
||||
@app.post("/api/agent/discoveries")
|
||||
async def minerops_agent_post_discoveries(request: Request):
|
||||
async def minerops_agent_post_discoveries(
|
||||
request: Request,
|
||||
authenticated_agent_id: str = Depends(require_agent_identity),
|
||||
zone_service: AgentZoneService = Depends(get_agent_zone_service),
|
||||
):
|
||||
import json
|
||||
import uuid
|
||||
|
||||
minerops_require_agent_secret(request)
|
||||
await minerops_ensure_agent_discovery_table()
|
||||
|
||||
body = await request.json()
|
||||
|
||||
agent_id = str(body.get("agent_id") or "plexus-windows-agent").strip()
|
||||
site_name = str(body.get("site_name") or "Plexus").strip()
|
||||
zone_service.validate_agent_identity(
|
||||
authenticated_agent_id, str(body.get("agent_id") or "").strip() or None
|
||||
)
|
||||
site_name = str(body.get("site_name") or "Unassigned").strip()
|
||||
discoveries = body.get("discoveries") or []
|
||||
|
||||
if not isinstance(discoveries, list):
|
||||
discoveries = []
|
||||
|
||||
ownership_context = zone_service.associate_miners_from_agent(
|
||||
authenticated_agent_id, []
|
||||
)
|
||||
conn = await minerops_command_db_connect()
|
||||
|
||||
inserted = 0
|
||||
matched_existing = 0
|
||||
staged_new = 0
|
||||
@@ -26666,42 +26589,38 @@ async def minerops_agent_post_discoveries(request: Request):
|
||||
for item in discoveries:
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
|
||||
ip_address = str(item.get("ip_address") or "").strip()
|
||||
mac_address = str(item.get("mac_address") or "").strip().upper()
|
||||
room_name = str(item.get("room_name") or "").strip()
|
||||
model = str(item.get("model") or "").strip()
|
||||
hostname = str(item.get("hostname") or "").strip()
|
||||
miner_sn = str(item.get("miner_sn") or "").strip()
|
||||
|
||||
matched = None
|
||||
|
||||
if mac_address:
|
||||
matched = await conn.fetchrow(
|
||||
"""
|
||||
SELECT id
|
||||
FROM miners
|
||||
WHERE upper(current_mac) = upper($1)
|
||||
LIMIT 1
|
||||
""",
|
||||
"SELECT id FROM miners WHERE upper(current_mac) = upper($1) LIMIT 1",
|
||||
mac_address,
|
||||
)
|
||||
|
||||
if not matched and ip_address:
|
||||
matched = await conn.fetchrow(
|
||||
"""
|
||||
SELECT id
|
||||
FROM miners
|
||||
WHERE current_ip = NULLIF($1, '')::inet
|
||||
LIMIT 1
|
||||
""",
|
||||
"SELECT id FROM miners WHERE current_ip = NULLIF($1, '')::inet LIMIT 1",
|
||||
ip_address,
|
||||
)
|
||||
|
||||
status = "known" if matched else "new"
|
||||
|
||||
if matched:
|
||||
matched_existing += 1
|
||||
association = zone_service.associate_miners_from_agent(
|
||||
authenticated_agent_id, [matched["id"]]
|
||||
)
|
||||
outcome = association.outcomes.get(matched["id"])
|
||||
if outcome not in {"assigned", "preserved"}:
|
||||
status = "zone_conflict" if outcome else "known"
|
||||
if outcome:
|
||||
errors.append(
|
||||
{"miner_id": str(matched["id"]), "error": outcome}
|
||||
)
|
||||
else:
|
||||
staged_new += 1
|
||||
|
||||
@@ -26709,45 +26628,23 @@ async def minerops_agent_post_discoveries(request: Request):
|
||||
await conn.execute(
|
||||
"""
|
||||
INSERT INTO minerops_agent_discoveries (
|
||||
id,
|
||||
agent_id,
|
||||
site_name,
|
||||
room_name,
|
||||
ip_address,
|
||||
mac_address,
|
||||
model,
|
||||
hostname,
|
||||
miner_sn,
|
||||
raw_payload,
|
||||
discovery_status,
|
||||
matched_miner_id,
|
||||
first_seen_at,
|
||||
last_seen_at,
|
||||
created_at,
|
||||
updated_at
|
||||
)
|
||||
VALUES (
|
||||
$1,
|
||||
$2,
|
||||
$3,
|
||||
NULLIF($4, ''),
|
||||
NULLIF($5, '')::inet,
|
||||
NULLIF($6, ''),
|
||||
NULLIF($7, ''),
|
||||
NULLIF($8, ''),
|
||||
NULLIF($9, ''),
|
||||
$10::jsonb,
|
||||
$11,
|
||||
$12::uuid,
|
||||
now(),
|
||||
now(),
|
||||
now(),
|
||||
now()
|
||||
id, agent_id, agent_zone_id, site_name, room_name,
|
||||
ip_address, mac_address, model, hostname, miner_sn,
|
||||
raw_payload, discovery_status, matched_miner_id,
|
||||
first_seen_at, last_seen_at, created_at, updated_at
|
||||
) VALUES (
|
||||
$1, $2, $3::uuid, $4, NULLIF($5, ''),
|
||||
NULLIF($6, '')::inet, NULLIF($7, ''), NULLIF($8, ''),
|
||||
NULLIF($9, ''), NULLIF($10, ''), $11::jsonb, $12,
|
||||
$13::uuid, now(), now(), now(), now()
|
||||
)
|
||||
ON CONFLICT (id) DO NOTHING
|
||||
""",
|
||||
str(uuid.uuid4()),
|
||||
agent_id,
|
||||
authenticated_agent_id,
|
||||
str(ownership_context.agent_zone_id)
|
||||
if ownership_context.agent_zone_id
|
||||
else None,
|
||||
site_name,
|
||||
room_name,
|
||||
ip_address,
|
||||
@@ -26759,12 +26656,12 @@ async def minerops_agent_post_discoveries(request: Request):
|
||||
status,
|
||||
str(matched["id"]) if matched else None,
|
||||
)
|
||||
|
||||
inserted += 1
|
||||
except Exception as exc:
|
||||
if len(errors) < 20:
|
||||
errors.append({"ip_address": ip_address, "mac_address": mac_address, "error": str(exc)})
|
||||
|
||||
errors.append(
|
||||
{"ip_address": ip_address, "mac_address": mac_address, "error": str(exc)}
|
||||
)
|
||||
finally:
|
||||
await conn.close()
|
||||
|
||||
@@ -26774,6 +26671,10 @@ async def minerops_agent_post_discoveries(request: Request):
|
||||
"inserted": inserted,
|
||||
"matched_existing": matched_existing,
|
||||
"staged_new": staged_new,
|
||||
"agent_zone_id": str(ownership_context.agent_zone_id)
|
||||
if ownership_context.agent_zone_id
|
||||
else None,
|
||||
"routing_reason": ownership_context.routing_reason,
|
||||
"errors": errors,
|
||||
"message": "Discovery results received.",
|
||||
}
|
||||
@@ -26841,9 +26742,12 @@ async def minerops_agent_discoveries(current_user: dict = Depends(get_current_us
|
||||
|
||||
|
||||
@app.post("/api/agent/discoveries/review")
|
||||
async def minerops_agent_discovery_review(request: Request, current_user: dict = Depends(get_current_user)):
|
||||
import asyncpg
|
||||
|
||||
async def minerops_agent_discovery_review(
|
||||
request: Request,
|
||||
current_user: dict = Depends(get_current_user),
|
||||
zone_service: AgentZoneService = Depends(get_agent_zone_service),
|
||||
):
|
||||
del current_user
|
||||
payload = await request.json()
|
||||
|
||||
ip_address = str(payload.get("ip_address") or "").strip()
|
||||
@@ -26864,6 +26768,7 @@ async def minerops_agent_discovery_review(request: Request, current_user: dict =
|
||||
SELECT
|
||||
id,
|
||||
agent_id,
|
||||
agent_zone_id,
|
||||
site_name,
|
||||
room_name,
|
||||
ip_address::text AS ip_address,
|
||||
@@ -26936,6 +26841,12 @@ async def minerops_agent_discovery_review(request: Request, current_user: dict =
|
||||
""", ip_address)
|
||||
|
||||
if existing:
|
||||
association = zone_service.associate_miners_from_agent(
|
||||
discovery["agent_id"], [UUID(existing["id"])]
|
||||
)
|
||||
ownership_status = association.outcomes.get(
|
||||
UUID(existing["id"]), association.routing_reason or "preserved"
|
||||
)
|
||||
await conn.execute("""
|
||||
UPDATE minerops_agent_discoveries
|
||||
SET
|
||||
@@ -26950,29 +26861,34 @@ async def minerops_agent_discovery_review(request: Request, current_user: dict =
|
||||
"action": action,
|
||||
"ip_address": ip_address,
|
||||
"miner_id": existing["id"],
|
||||
"ownership_status": ownership_status,
|
||||
"message": "Discovery already matched an existing inventory miner."
|
||||
}
|
||||
|
||||
room_id = None
|
||||
site_id = None
|
||||
|
||||
routing = zone_service.routing_context(
|
||||
discovery["agent_id"], discovery["agent_zone_id"]
|
||||
)
|
||||
site_id = routing.site_id if routing.routing_reason is None else None
|
||||
agent_zone_id = (
|
||||
routing.agent_zone_id if routing.routing_reason is None else None
|
||||
)
|
||||
room_name = discovery["room_name"]
|
||||
|
||||
if room_name:
|
||||
try:
|
||||
room = await conn.fetchrow("""
|
||||
SELECT id::text AS id, site_id::text AS site_id
|
||||
FROM rooms
|
||||
WHERE lower(name) = lower($1)
|
||||
LIMIT 1
|
||||
""", str(room_name))
|
||||
|
||||
if room:
|
||||
room_id = room["id"]
|
||||
site_id = room["site_id"]
|
||||
except Exception:
|
||||
room_id = None
|
||||
site_id = None
|
||||
if room_name and site_id:
|
||||
room = await conn.fetchrow(
|
||||
"""
|
||||
SELECT id::text AS id
|
||||
FROM rooms
|
||||
WHERE site_id = $1::uuid AND lower(name) = lower($2)
|
||||
ORDER BY id
|
||||
LIMIT 1
|
||||
""",
|
||||
str(site_id),
|
||||
str(room_name),
|
||||
)
|
||||
if room:
|
||||
room_id = room["id"]
|
||||
|
||||
model = discovery["model"] or "WhatsMiner"
|
||||
hostname = discovery["hostname"]
|
||||
@@ -26981,6 +26897,7 @@ async def minerops_agent_discovery_review(request: Request, current_user: dict =
|
||||
new_miner = await conn.fetchrow("""
|
||||
INSERT INTO miners (
|
||||
site_id,
|
||||
agent_zone_id,
|
||||
room_id,
|
||||
current_mac,
|
||||
current_ip,
|
||||
@@ -26998,14 +26915,15 @@ async def minerops_agent_discovery_review(request: Request, current_user: dict =
|
||||
VALUES (
|
||||
$1::uuid,
|
||||
$2::uuid,
|
||||
$3,
|
||||
$4::inet,
|
||||
$5,
|
||||
$3::uuid,
|
||||
$4,
|
||||
$5::inet,
|
||||
$6,
|
||||
$7,
|
||||
$7,
|
||||
'discovered',
|
||||
$8,
|
||||
$8,
|
||||
'discovered',
|
||||
$9,
|
||||
now(),
|
||||
now(),
|
||||
now(),
|
||||
@@ -27014,6 +26932,7 @@ async def minerops_agent_discovery_review(request: Request, current_user: dict =
|
||||
RETURNING id::text AS id
|
||||
""",
|
||||
site_id,
|
||||
agent_zone_id,
|
||||
room_id,
|
||||
mac,
|
||||
ip_address,
|
||||
|
||||
@@ -40,15 +40,20 @@ class MinerControlRepository:
|
||||
with connection.cursor(row_factory=dict_row) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT miners.id, miners.site_id, host(miners.current_ip) AS current_ip,
|
||||
SELECT miners.id, miners.site_id, miners.agent_zone_id,
|
||||
zones.id IS NOT NULL AS agent_zone_exists,
|
||||
zones.is_enabled AS agent_zone_enabled,
|
||||
host(miners.current_ip) AS current_ip,
|
||||
miners.status AS runtime_state,
|
||||
miners.configured_performance_mode,
|
||||
miners.api3_supported, miners.api3_api_switch,
|
||||
agents.agent_id, agents.last_checkin_at,
|
||||
agents.version AS agent_version, agents.last_payload
|
||||
FROM miners
|
||||
LEFT JOIN agent_zones AS zones
|
||||
ON zones.id = miners.agent_zone_id
|
||||
LEFT JOIN minerops_site_agents AS agents
|
||||
ON agents.site_id = miners.site_id
|
||||
ON agents.agent_zone_id = miners.agent_zone_id
|
||||
WHERE miners.id = %s
|
||||
""",
|
||||
(miner_id,),
|
||||
|
||||
@@ -29,10 +29,14 @@ class MinerControlService:
|
||||
|
||||
@staticmethod
|
||||
def _capability(context: dict) -> ControlCapability:
|
||||
if not context.get("site_id"):
|
||||
return ControlCapability(state="unavailable", reason="site_unassigned")
|
||||
if not context.get("agent_zone_id"):
|
||||
return ControlCapability(state="unavailable", reason="agent_zone_unassigned")
|
||||
if context.get("agent_zone_exists") is not True:
|
||||
return ControlCapability(state="unavailable", reason="agent_zone_missing")
|
||||
if context.get("agent_zone_enabled") is not True:
|
||||
return ControlCapability(state="unavailable", reason="agent_zone_disabled")
|
||||
if not context.get("agent_id"):
|
||||
return ControlCapability(state="unavailable", reason="agent_unassigned")
|
||||
return ControlCapability(state="unavailable", reason="agent_unavailable")
|
||||
if not context.get("current_ip"):
|
||||
return ControlCapability(state="unavailable", reason="current_ip_missing")
|
||||
if context.get("api3_supported") is False:
|
||||
|
||||
Reference in new issue
Block a user