diff --git a/backend/app/services/device_discovery.py b/backend/app/services/device_discovery.py index 8651513..3a285da 100644 --- a/backend/app/services/device_discovery.py +++ b/backend/app/services/device_discovery.py @@ -633,6 +633,8 @@ async def discover_via_ssh( ) rack_hostname = external_id + rack_ssh_ok = False + config_text = None try: config_text = await _read_remote_config( host=rack_host, @@ -644,13 +646,12 @@ async def discover_via_ssh( rack_ssh_ok = True except Exception as exc: result.errors.append(f"SSH to {rack_host} failed: {exc}") - rack_ssh_ok = False - config_text = None + logger.info(f"[ssh_discovery] Processing rack {rack_host} ({rack_hostname}), ssh_ok={rack_ssh_ok}, in_db={existing_rack_pc is not None}") - # Reuse existing_rack_pc (queried before SSH) — same identity map object - existing_rack = existing_rack_pc + try: - if existing_rack is None: + if existing_rack_pc is None: + # New device — insert with status based on SSH result db.add(Device( object_id=obj.id, ip=rack_host, @@ -663,7 +664,7 @@ async def discover_via_ssh( last_seen=now if rack_ssh_ok else None, )) status_str = "online" if rack_ssh_ok else "unknown" - logger.info(f"[ssh_discovery] {rack_host} ({rack_hostname}) - ADDED: category=embedded, role=other, hostname={rack_hostname}, status={status_str}, source=auto_ssh") + logger.info(f"[ssh_discovery] {rack_host} ({rack_hostname}) - ADDED: status={status_str}") result.created += 1 result.actions.append(DeviceAction( ip=rack_host, @@ -676,40 +677,40 @@ async def discover_via_ssh( else: rack_changes = [] if rack_ssh_ok: - # SSH succeeded → update status and last_seen - if existing_rack.status != "online": - existing_rack.status = "online" + if existing_rack_pc.status != "online": + existing_rack_pc.status = "online" rack_changes.append("status: →online") - existing_rack.last_seen = now - rack_changes.append(f"last_seen: →{now.isoformat()}") - if existing_rack.rack_id is None and rack_id_mapping.get(external_id) is not None: - existing_rack.rack_id = rack_id_mapping[external_id] - rack_changes.append(f"rack_id: None→{existing_rack.rack_id}") + existing_rack_pc.last_seen = now + rack_changes.append("last_seen: updated") + if existing_rack_pc.rack_id is None and rack_id_mapping.get(external_id) is not None: + existing_rack_pc.rack_id = rack_id_mapping[external_id] + rack_changes.append(f"rack_id: None→{existing_rack_pc.rack_id}") if rack_changes: - logger.info(f"[ssh_discovery] {rack_host} ({existing_rack.hostname}) - UPDATED: {', '.join(rack_changes)}") + logger.info(f"[ssh_discovery] {rack_host} ({existing_rack_pc.hostname}) - UPDATED: {', '.join(rack_changes)}") result.updated += 1 result.actions.append(DeviceAction( ip=rack_host, - hostname=existing_rack.hostname, + hostname=existing_rack_pc.hostname, action="UPDATED", reason="; ".join(rack_changes), - category=existing_rack.category, - role=existing_rack.role, + category=existing_rack_pc.category, + role=existing_rack_pc.role, )) else: - logger.info(f"[ssh_discovery] {rack_host} ({existing_rack.hostname}) - SKIPPED: rack already exists in devices") + reason = "SSH failed, rack already in DB" if not rack_ssh_ok else "rack already up to date" + logger.info(f"[ssh_discovery] {rack_host} ({existing_rack_pc.hostname}) - SKIPPED: {reason}") result.skipped += 1 result.actions.append(DeviceAction( ip=rack_host, - hostname=existing_rack.hostname, + hostname=existing_rack_pc.hostname, action="SKIPPED", - reason="rack already exists in devices", - category=existing_rack.category, - role=existing_rack.role, + reason=reason, + category=existing_rack_pc.category, + role=existing_rack_pc.role, )) except Exception as exc: - logger.error(f"[ssh_discovery] {rack_host} - ERROR adding rack: {exc}") - result.errors.append(f"Error adding rack {rack_host}: {exc}") + logger.error(f"[ssh_discovery] {rack_host} - ERROR: {exc}") + result.errors.append(f"Error processing rack {rack_host}: {exc}") result.actions.append(DeviceAction( ip=rack_host, hostname=rack_hostname, @@ -718,6 +719,7 @@ async def discover_via_ssh( category="embedded", role="other", )) + if not rack_ssh_ok: try: await db.flush()