Fix vm_destroy: Use vm/list endpoint for reliable VM info + IP extraction

This commit is contained in:
sascha 2026-04-22 21:32:47 +02:00
parent 3b37c639a2
commit 6f165ccb2c

33
app.py
View file

@ -374,32 +374,33 @@ async def vm_destroy_full(vmid: int, _=Depends(_verify), dry_run: bool = Query(F
results = {"vmid": vmid, "dry_run": dry_run, "steps": {}} results = {"vmid": vmid, "dry_run": dry_run, "steps": {}}
async with httpx.AsyncClient(verify=False, timeout=30) as c: async with httpx.AsyncClient(verify=False, timeout=30) as c:
# Step 1: Find VM and get details # Step 1: Find VM and get details from vm/list (more reliable than config endpoint)
vm_info = None vm_info = None
node_name = None node_name = None
nodes = await c.get("https://10.5.85.11:8006/api2/json/nodes", headers={"Authorization": auth}) vms = await c.get("https://10.5.85.11:8006/api2/json/nodes", headers={"Authorization": auth})
for n in nodes.json().get("data", []): for n in vms.json().get("data", []):
r = await c.get(f"https://10.5.85.11:8006/api2/json/nodes/{n['node']}/qemu/{vmid}", headers={"Authorization": auth}) r = await c.get(f"https://10.5.85.11:8006/api2/json/nodes/{n['node']}/qemu", headers={"Authorization": auth})
if r.status_code == 200: for vm in r.json().get("data", []):
data = r.json().get("data", {}) if vm.get("vmid") == vmid:
# Proxmox returns config as dict, but status endpoint returns list vm_info = vm
if isinstance(data, list) and len(data) > 0:
vm_info = data[0]
elif isinstance(data, dict):
vm_info = data
node_name = n["node"] node_name = n["node"]
break break
if vm_info:
break
if not vm_info: if not vm_info:
return JSONResponse({"error": f"VM {vmid} not found"}, status_code=404) return JSONResponse({"error": f"VM {vmid} not found"}, status_code=404)
hostname = vm_info.get("name", "") or vm_info.get("vmid", "") hostname = vm_info.get("name", "") or str(vmid)
# Extract IP from net0 config (format: "virtio=AA:BB:CC:DD:EE:FF,bridge=vmbr0,ip=10.X.Y.Z") # Extract IP from vm list or config
net0 = vm_info.get("net0", "") ip = vm_info.get("ip", "")
ip = "" if not ip:
# Try to get from net0 config
cfg = await c.get(f"https://10.5.85.11:8006/api2/json/nodes/{node_name}/qemu/{vmid}/config", headers={"Authorization": auth})
net0 = cfg.json().get("data", {}).get("net0", "")
if "ip=" in net0: if "ip=" in net0:
ip = net0.split("ip=")[-1].split(",")[0] ip = net0.split("ip=")[-1].split(",")[0]
results["vm_info"] = {"hostname": hostname, "ip": ip, "node": node_name} results["vm_info"] = {"hostname": hostname, "ip": ip, "node": node_name, "status": vm_info.get("status", "unknown")}
# Step 2: Stop VM (if running) # Step 2: Stop VM (if running)
if vm_info.get("status") == "running": if vm_info.get("status") == "running":