From 0c9ee8b51b954ff2792e7a71e7b0733114eb97ab Mon Sep 17 00:00:00 2001 From: dv Date: Wed, 8 Apr 2026 13:03:41 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=BE=D0=BD=D0=B5=D1=82=20=D0=B2=D0=B0?= =?UTF-8?q?=D0=B9=D0=B1=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/services/device_discovery.py | 45 +++++++++++++++--------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/backend/app/services/device_discovery.py b/backend/app/services/device_discovery.py index 266b6f1..75ab508 100644 --- a/backend/app/services/device_discovery.py +++ b/backend/app/services/device_discovery.py @@ -542,8 +542,8 @@ async def discover_via_ssh( infra_devices = [] if obj.server_ip: infra_devices.append((obj.server_ip, "main_server", "caps_server")) - if obj.db_host and obj.db_host != obj.server_ip: - infra_devices.append((obj.db_host, "vm", "db_server")) + if db_host and db_host != obj.server_ip: + infra_devices.append((db_host, "vm", "db_server")) for infra_ip, infra_category, infra_hostname in infra_devices: try: @@ -694,9 +694,17 @@ async def discover_via_ssh( logger.info(f"[ssh_discovery] Found {len(devices_found)} devices from plugins in {rack_host}") for found in devices_found: - # Hostname: "{external_id}-{plugin_name}", e.g. "lane-1-1-camera_1" - hostname = f"{external_id}-{found.plugin_name}" - rack_id = rack_id_mapping.get(external_id) + # vm devices (RTSP, Asterisk, etc.) are shared infrastructure — place in server_room, + # not tied to any specific rack. Other devices belong to the current rack. + is_shared = found.category == "vm" + if is_shared: + hostname = found.plugin_name # e.g. "asterisk", "camera_1_rtsp" + rack_id = None + device_location = "server_room" + else: + hostname = f"{external_id}-{found.plugin_name}" # e.g. "lane-1-1-camera_1" + rack_id = rack_id_mapping.get(external_id) + device_location = None try: existing = (await db.execute( @@ -730,22 +738,24 @@ async def discover_via_ssh( if existing.role == "other" and found.role != "other": changes.append(f"role: other→{found.role}") existing.role = found.role - # Update hostname if it was never set or still auto-generated - if not existing.hostname or existing.hostname.startswith("rack") or (existing.hostname.startswith("lane") and not existing.hostname.startswith(f"{external_id}")): - if existing.hostname != hostname: - changes.append(f"hostname: '{existing.hostname}'→'{hostname}'") - existing.hostname = hostname - # Update rack_id if not set - if existing.rack_id is None and rack_id is not None: - existing.rack_id = rack_id - changes.append(f"rack_id: None→{rack_id}") + if not is_shared: + # Rack-specific device: update hostname if stale + if not existing.hostname or existing.hostname.startswith("rack"): + if existing.hostname != hostname: + changes.append(f"hostname: '{existing.hostname}'→'{hostname}'") + existing.hostname = hostname + # Update rack_id if not set + if existing.rack_id is None and rack_id is not None: + existing.rack_id = rack_id + changes.append(f"rack_id: None→{rack_id}") + # Shared vm devices: never update hostname/rack_id — they're already in server_room if changes: logger.info(f"[ssh_discovery] {found.ip} ({found.plugin_name}) - UPDATED: {', '.join(changes)}") result.updated += 1 result.actions.append(DeviceAction( ip=found.ip, - hostname=hostname, + hostname=existing.hostname, action="UPDATED", reason="; ".join(changes), category=found.category, @@ -756,7 +766,7 @@ async def discover_via_ssh( result.skipped += 1 result.actions.append(DeviceAction( ip=found.ip, - hostname=hostname, + hostname=existing.hostname, action="SKIPPED", reason=f"already exists with same values (source={existing.source}, category={existing.category}, role={existing.role}, hostname={existing.hostname})", category=existing.category, @@ -771,9 +781,10 @@ async def discover_via_ssh( role=found.role, source="auto_ssh", rack_id=rack_id, + location=device_location, device_meta={"plugin": found.plugin_name, "cls": found.cls}, )) - logger.info(f"[ssh_discovery] {found.ip} ({found.plugin_name}) - ADDED: category={found.category}, role={found.role}, hostname={hostname}, rack_id={rack_id}, source=auto_ssh") + logger.info(f"[ssh_discovery] {found.ip} ({found.plugin_name}) - ADDED: category={found.category}, role={found.role}, hostname={hostname}, rack_id={rack_id}, location={device_location}, source=auto_ssh") result.created += 1 result.actions.append(DeviceAction( ip=found.ip,