Archived
41 lines
1.3 KiB
Python
41 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."""
|
|
client = _client()
|
|
node = settings.pve_node
|
|
endpoint = client.nodes(node).qemu(vmid) if guest_type == "vm" else client.nodes(node).lxc(vmid)
|
|
|
|
try:
|
|
current = endpoint.status.current.get()
|
|
except Exception as error:
|
|
if _is_not_found(error):
|
|
return
|
|
raise
|
|
|
|
# Proxmox не удаляет запущенную VM/LXC, поэтому сначала останавливаем её.
|
|
if current.get("status") == "running":
|
|
stop_upid = endpoint.status.stop.post()
|
|
if stop_upid:
|
|
_wait_task(client, node, stop_upid)
|
|
|
|
try:
|
|
delete_upid = endpoint.delete()
|
|
except Exception as error:
|
|
if _is_not_found(error):
|
|
return
|
|
raise
|
|
|
|
if delete_upid:
|
|
_wait_task(client, node, delete_upid)
|