Archived
39 lines
1.3 KiB
Python
39 lines
1.3 KiB
Python
from .config import settings
|
|
from .proxmox_client import _client, _wait_task
|
|
|
|
|
|
def _is_not_found(error: Exception) -> bool:
|
|
"""Определяет ответ Proxmox об отсутствующем госте."""
|
|
if getattr(error, "status_code", None) == 404:
|
|
return True
|
|
response = getattr(error, "response", None)
|
|
return getattr(response, "status_code", None) == 404
|
|
|
|
|
|
def delete_guest(guest_type: str, vmid: int) -> None:
|
|
"""Удаляет VM или LXC и ожидает завершения задачи Proxmox.
|
|
|
|
Если объект уже отсутствует в Proxmox, операция считается успешной:
|
|
это позволяет очищать осиротевшие записи панели после неудачного создания.
|
|
"""
|
|
client = _client()
|
|
node = settings.pve_node
|
|
endpoint = client.nodes(node).qemu(vmid) if guest_type == "vm" else client.nodes(node).lxc(vmid)
|
|
|
|
try:
|
|
endpoint.status.current.get()
|
|
except Exception as error:
|
|
if _is_not_found(error):
|
|
return
|
|
raise
|
|
|
|
try:
|
|
upid = endpoint.delete()
|
|
except Exception as error:
|
|
if _is_not_found(error):
|
|
return
|
|
raise
|
|
|
|
if upid:
|
|
_wait_task(client, node, upid)
|