сонет вайб 4

This commit is contained in:
dv 2026-04-08 13:24:30 +03:00
parent 22b88fed1a
commit b59f0222f0

View file

@ -632,6 +632,7 @@ async def discover_via_ssh(
"config_path", config_path "config_path", config_path
) )
rack_hostname = external_id
try: try:
config_text = await _read_remote_config( config_text = await _read_remote_config(
host=rack_host, host=rack_host,
@ -640,13 +641,11 @@ async def discover_via_ssh(
password=ssh_password, password=ssh_password,
config_path=effective_config_path, config_path=effective_config_path,
) )
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}")
continue rack_ssh_ok = False
config_text = None
# Add the rack itself as a main_server device if it doesn't exist
# Use external_id (e.g., "lane-1-1") as the hostname
rack_hostname = external_id
logger.info(f"[ssh_discovery] Processing rack {rack_host} ({rack_hostname})") logger.info(f"[ssh_discovery] Processing rack {rack_host} ({rack_hostname})")
try: try:
existing_rack = (await db.execute( existing_rack = (await db.execute(
@ -665,27 +664,29 @@ async def discover_via_ssh(
role="other", role="other",
source="auto_ssh", source="auto_ssh",
rack_id=rack_id_mapping.get(external_id), rack_id=rack_id_mapping.get(external_id),
status="online", status="online" if rack_ssh_ok else "unknown",
last_seen=now, last_seen=now if rack_ssh_ok else None,
)) ))
logger.info(f"[ssh_discovery] {rack_host} ({rack_hostname}) - ADDED: category=embedded, role=other, hostname={rack_hostname}, status=online, source=auto_ssh") 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")
result.created += 1 result.created += 1
result.actions.append(DeviceAction( result.actions.append(DeviceAction(
ip=rack_host, ip=rack_host,
hostname=rack_hostname, hostname=rack_hostname,
action="ADDED", action="ADDED",
reason="rack PC discovered via SSH connection", reason="rack PC from racks table" + (" (SSH ok)" if rack_ssh_ok else " (SSH failed, added as unknown)"),
category="embedded", category="embedded",
role="other", role="other",
)) ))
else: else:
rack_changes = [] rack_changes = []
# SSH succeeded → always update status and last_seen if rack_ssh_ok:
if existing_rack.status != "online": # SSH succeeded → update status and last_seen
existing_rack.status = "online" if existing_rack.status != "online":
rack_changes.append("status: →online") existing_rack.status = "online"
existing_rack.last_seen = now rack_changes.append("status: →online")
rack_changes.append(f"last_seen: →{now.isoformat()}") 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: 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] existing_rack.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.rack_id}")
@ -722,6 +723,10 @@ async def discover_via_ssh(
category="embedded", category="embedded",
role="other", role="other",
)) ))
if not rack_ssh_ok:
await db.flush()
continue # no config to parse
# Get all rack IPs to exclude them from device discovery # Get all rack IPs to exclude them from device discovery
all_rack_ips = [host for _, host, _ in rack_hosts] all_rack_ips = [host for _, host, _ in rack_hosts]
devices_found = _parse_config(config_text, rack_host, all_rack_ips) devices_found = _parse_config(config_text, rack_host, all_rack_ips)