feat(servers): distinguish rejected credentials from unreachable nodes

An offline server used to report a single "Failed to connect" state, but
"the node does not answer" and "the node answered 401" are fixed in
completely different ways — the first is power or network, the second is a
stale token, which is what happens when a node joins a cluster and its
user.cfg and priv/token.cfg are replaced by the cluster ones.

- add ProxmoxClient.check_connection() returning (ok, error_kind, error);
  token auth surfaces 401 as ResourceException rather than
  AuthenticationError, since proxmoxer validates the token on first use
- store the reason in proxmox_servers.last_error_kind (migration 40) and
  expose it through the server response
- surface an auth_error state in the UI with a hint explaining the usual
  cause

Storage listings now also report per-node availability: the cluster config
declares a storage for the whole cluster but says nothing about whether it
actually came up on a given node, so a missing volume group or pool stayed
invisible. Availability comes from a single /cluster/resources call.
This commit is contained in:
markmorado
2026-07-24 04:42:59 +05:00
parent db7411b1bf
commit 075b8cdaaa
15 changed files with 218 additions and 31 deletions

View File

@@ -140,6 +140,10 @@ class ProxmoxClient(VmMixin, LxcMixin, ClusterMixin, StorageMixin, NetworkMixin,
self.connection_key = f"{host}:{user}:{token_name or 'password'}"
self.proxmox = None
self.last_used = time.time()
# Причина неудачного подключения: 'auth' | 'connection' | None.
# Нужна, чтобы отличить протухший токен от недоступной ноды.
self.connect_error_kind = None
self.connect_error = None
# Check cache first
with connection_cache_lock:
@@ -198,6 +202,13 @@ class ProxmoxClient(VmMixin, LxcMixin, ClusterMixin, StorageMixin, NetworkMixin,
logger.debug(f"Cached connection for {self.connection_key}")
except Exception as e:
logger.error(f"Failed to test Proxmox connection for {host}: {e}")
# При токен-авторизации 401 прилетает не AuthenticationError,
# а ResourceException со status_code — proxmoxer не делает
# отдельный логин, токен проверяется на первом же запросе.
self.connect_error_kind = (
'auth' if getattr(e, 'status_code', None) == 401 else 'connection'
)
self.connect_error = str(e)
self.proxmox = None
# Удаляем неудачное подключение из кеша
with connection_cache_lock:
@@ -206,6 +217,8 @@ class ProxmoxClient(VmMixin, LxcMixin, ClusterMixin, StorageMixin, NetworkMixin,
except AuthenticationError as e:
logger.error(f"Ошибка аутентификации Proxmox {host}: {e}")
self.connect_error_kind = 'auth'
self.connect_error = str(e)
self.proxmox = None
# Удаляем из кеша при ошибке аутентификации
with connection_cache_lock:
@@ -213,6 +226,10 @@ class ProxmoxClient(VmMixin, LxcMixin, ClusterMixin, StorageMixin, NetworkMixin,
del connection_cache[self.connection_key]
except Exception as e:
logger.error(f"Ошибка подключения к Proxmox {host}: {e}")
self.connect_error_kind = (
'auth' if getattr(e, 'status_code', None) == 401 else 'connection'
)
self.connect_error = str(e)
self.proxmox = None
# Удаляем из кеша при ошибке подключения
if self.connection_key in connection_cache:
@@ -229,6 +246,30 @@ class ProxmoxClient(VmMixin, LxcMixin, ClusterMixin, StorageMixin, NetworkMixin,
except Exception:
return False
def check_connection(self) -> tuple:
"""
Проверить API, отличая отказ авторизации от недоступности ноды.
«Нода не отвечает» и «нода ответила 401» чинятся по-разному: первое —
сеть/питание, второе — протухший токен (например, после ввода ноды в
кластер, когда user.cfg и priv/token.cfg замещаются кластерными).
Returns:
(ok, error_kind, error) — error_kind: None | 'auth' | 'connection'
"""
if not self.proxmox:
return (False, self.connect_error_kind or 'connection',
self.connect_error or 'Failed to connect')
try:
self.proxmox.version.get()
self.last_used = time.time()
return (True, None, None)
except AuthenticationError as e:
return (False, 'auth', str(e))
except Exception as e:
kind = 'auth' if getattr(e, 'status_code', None) == 401 else 'connection'
return (False, kind, str(e))
@asynccontextmanager
async def ensure_connection(self):
"""Ensure connection is active before use"""

View File

@@ -152,6 +152,38 @@ class StorageMixin:
logger.error(f"Ошибка получения хранилищ ({content}) ноды {node}: {e}")
return result
def get_storage_node_status(self) -> Dict[str, List[Dict[str, Any]]]:
"""
Доступность каждого хранилища по нодам — одним запросом /cluster/resources.
Конфиг кластера (/storage) не знает, поднялось ли хранилище на конкретной
ноде: VG/пул может быть объявлен, но физически отсутствовать — типичный
случай после ввода новой ноды в кластер, где локальные хранилища ноды
заменяются кластерным конфигом.
Хранилища, отключённые в конфиге (disable), в /cluster/resources не
попадают, поэтому в карте их не будет.
Returns:
{storage_id: [{'node': 'prod', 'active': True}, ...]}
"""
if not self.proxmox:
return {}
status: Dict[str, List[Dict[str, Any]]] = {}
try:
for res in self.get_cluster_resources(type_='storage'):
storage_id = res.get('storage')
node = res.get('node')
if not storage_id or not node:
continue
status.setdefault(storage_id, []).append({
'node': node,
'active': res.get('status') == 'available',
})
except Exception as e:
logger.error(f"Ошибка получения статусов хранилищ с {self.host}: {e}")
return status
def get_node_storages(self, node: str, content: str = None) -> List[Dict]:
"""Get storages available on a specific node, optionally filtered by content type"""
if not self.proxmox: