сонет вайб 6

This commit is contained in:
dv 2026-04-08 13:44:37 +03:00
parent 58c36da2ae
commit 862c6cbe18

View file

@ -633,6 +633,8 @@ async def discover_via_ssh(
) )
rack_hostname = external_id rack_hostname = external_id
rack_ssh_ok = False
config_text = None
try: try:
config_text = await _read_remote_config( config_text = await _read_remote_config(
host=rack_host, host=rack_host,
@ -644,13 +646,12 @@ async def discover_via_ssh(
rack_ssh_ok = True rack_ssh_ok = True
except Exception as exc: except Exception as exc:
result.errors.append(f"SSH to {rack_host} failed: {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}") 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: try:
if existing_rack is None: if existing_rack_pc is None:
# New device — insert with status based on SSH result
db.add(Device( db.add(Device(
object_id=obj.id, object_id=obj.id,
ip=rack_host, ip=rack_host,
@ -663,7 +664,7 @@ async def discover_via_ssh(
last_seen=now if rack_ssh_ok else None, last_seen=now if rack_ssh_ok else None,
)) ))
status_str = "online" if rack_ssh_ok else "unknown" 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.created += 1
result.actions.append(DeviceAction( result.actions.append(DeviceAction(
ip=rack_host, ip=rack_host,
@ -676,40 +677,40 @@ async def discover_via_ssh(
else: else:
rack_changes = [] rack_changes = []
if rack_ssh_ok: if rack_ssh_ok:
# SSH succeeded → update status and last_seen if existing_rack_pc.status != "online":
if existing_rack.status != "online": existing_rack_pc.status = "online"
existing_rack.status = "online"
rack_changes.append("status: →online") rack_changes.append("status: →online")
existing_rack.last_seen = now existing_rack_pc.last_seen = now
rack_changes.append(f"last_seen: →{now.isoformat()}") rack_changes.append("last_seen: updated")
if existing_rack.rack_id is None and rack_id_mapping.get(external_id) is not None: if existing_rack_pc.rack_id is None and rack_id_mapping.get(external_id) is not None:
existing_rack.rack_id = rack_id_mapping[external_id] existing_rack_pc.rack_id = rack_id_mapping[external_id]
rack_changes.append(f"rack_id: None→{existing_rack.rack_id}") rack_changes.append(f"rack_id: None→{existing_rack_pc.rack_id}")
if rack_changes: 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.updated += 1
result.actions.append(DeviceAction( result.actions.append(DeviceAction(
ip=rack_host, ip=rack_host,
hostname=existing_rack.hostname, hostname=existing_rack_pc.hostname,
action="UPDATED", action="UPDATED",
reason="; ".join(rack_changes), reason="; ".join(rack_changes),
category=existing_rack.category, category=existing_rack_pc.category,
role=existing_rack.role, role=existing_rack_pc.role,
)) ))
else: 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.skipped += 1
result.actions.append(DeviceAction( result.actions.append(DeviceAction(
ip=rack_host, ip=rack_host,
hostname=existing_rack.hostname, hostname=existing_rack_pc.hostname,
action="SKIPPED", action="SKIPPED",
reason="rack already exists in devices", reason=reason,
category=existing_rack.category, category=existing_rack_pc.category,
role=existing_rack.role, role=existing_rack_pc.role,
)) ))
except Exception as exc: except Exception as exc:
logger.error(f"[ssh_discovery] {rack_host} - ERROR adding rack: {exc}") logger.error(f"[ssh_discovery] {rack_host} - ERROR: {exc}")
result.errors.append(f"Error adding rack {rack_host}: {exc}") result.errors.append(f"Error processing rack {rack_host}: {exc}")
result.actions.append(DeviceAction( result.actions.append(DeviceAction(
ip=rack_host, ip=rack_host,
hostname=rack_hostname, hostname=rack_hostname,
@ -718,6 +719,7 @@ async def discover_via_ssh(
category="embedded", category="embedded",
role="other", role="other",
)) ))
if not rack_ssh_ok: if not rack_ssh_ok:
try: try:
await db.flush() await db.flush()