fix: name the VM a failed create_vm_from_cloud_init leaves behind
driver / standard (pull_request) Successful in 19s
driver / lint (pull_request) Successful in 15s
driver / types (pull_request) Successful in 1m5s
driver / test (3.12) (pull_request) Failing after 1m6s
driver / lowest (pull_request) Failing after 5s
driver / test (3.13) (pull_request) Failing after 58s
driver / build (pull_request) Successful in 31s
driver / security (pull_request) Successful in 1m9s
CI / driver (pull_request) Failing after 2m49s
driver / publish (pull_request) Skipped

A failure after the VM shell existed (disk import, NIC or snippet config,
start) raised without the VMID, so the caller could not remove the
half-built VM and it stayed on the node. It now raises napalm-device-types'
VMProvisionIncomplete with vmid and node, the failure as its cause. A
failure before the shell exists raises as before.

The NIC config strings are built by a helper of their own, which keeps
create_vm_from_cloud_init within its quality baseline.

Closes #28
This commit is contained in:
Christian Manivong committed 2026-11-01 22:11:13 +01:00
1 parent cb29a42c31
commit 61821cd607
5 files changed
+179 -32

No files matched your search

+40 -28
View File
@@ -21,6 +21,7 @@ from napalm_device_types.models import (
from napalm_device_types.provisioning import (
QEMU_GUEST_AGENTS,
GuestAgents,
VMProvisionIncomplete,
network_config,
split_compression,
)
@@ -130,6 +131,29 @@ def _import_volume_name(image_url: str) -> str | None:
return f"netork-{_url_key(image_url)}-{safe_stem}.{extension}"
def _nic_config_args(nics: list[dict[str, Any]]) -> dict[str, str]:
"""``{"net<i>": "virtio[=<mac>],bridge=<bridge>[,tag=|,trunks=]"}`` per NIC."""
config_args: dict[str, str] = {}
for i, nic in enumerate(nics):
bridge = nic.get("bridge")
if not bridge:
raise ValueError(f"NIC {i}: bridge is required")
# Build base config: model[=mac] + bridge
mac = nic.get("mac")
net_config = f"virtio={mac},bridge={bridge}" if mac else f"virtio,bridge={bridge}"
# Add VLAN configuration (access vs trunk)
if "trunk_vlan_tags" in nic and nic["trunk_vlan_tags"]:
vlan_list = ";".join(str(v) for v in nic["trunk_vlan_tags"])
net_config += f",trunks={vlan_list}"
elif "vlan_tag" in nic and nic["vlan_tag"] is not None:
net_config += f",tag={nic['vlan_tag']}"
config_args[f"net{i}"] = net_config
return config_args
class ProxmoxVMProvisionMixin:
"""Mixin to add VM provisioning to ProxmoxDriver."""
@@ -609,12 +633,18 @@ class ProxmoxVMProvisionMixin:
{"vmid": str, "name": str, "node": str}
Raises:
RuntimeError: provisioning failure (download, import, config, timeout, etc.)
ValueError: invalid storage or configuration, or a cpu_type that is
unknown or that this node's CPU cannot run, or another node
(raised before anything is created)
VMProvisionIncomplete: any failure once the VM shell exists (download,
import, storage, NIC or snippet config, start, timeout, ...); a
RuntimeError that carries the half-built VM's vmid and node, with
the failure as its cause
RuntimeError: a failure taking the VMID or creating the shell (no VM
is left behind)
ValueError: a cpu_type that is unknown or that this node's CPU cannot
run, a guest_os it cannot provision, or another node (raised
before anything is created)
"""
self._check_placement(guest_os, node)
created: int | None = None # the VMID once the shell exists on the node
try:
_logger.info(f"Creating {guest_os} VM '{name}' from image {image_url}")
cpu_model = self._resolve_cpu_type(cpu_type)
@@ -638,6 +668,7 @@ class ProxmoxVMProvisionMixin:
# queries (below) would have nothing to talk to.
**_GUEST_VM_SETTINGS[guest_os],
)
created = vmid
# Step 3: Download cloud image (cached) and import as root disk
image_storage = storage or self._find_default_image_storage()
@@ -647,29 +678,7 @@ class ProxmoxVMProvisionMixin:
# Step 4: Configure network interfaces (CPU/memory already set at shell creation)
_logger.info(f"Configuring {len(nics)} NIC(s) for VM {vmid}")
config_args: dict[str, Any] = {}
# Build NIC config strings generically
for i, nic in enumerate(nics):
bridge = nic.get("bridge")
if not bridge:
raise ValueError(f"NIC {i}: bridge is required")
# Build base config: model[=mac] + bridge
mac = nic.get("mac")
net_config = f"virtio={mac},bridge={bridge}" if mac else f"virtio,bridge={bridge}"
# Add VLAN configuration (access vs trunk)
if "trunk_vlan_tags" in nic and nic["trunk_vlan_tags"]:
vlan_list = ";".join(str(v) for v in nic["trunk_vlan_tags"])
net_config += f",trunks={vlan_list}"
elif "vlan_tag" in nic and nic["vlan_tag"] is not None:
net_config += f",tag={nic['vlan_tag']}"
config_args[f"net{i}"] = net_config
self._node_api().qemu(vmid).config.post(**config_args)
self._node_api().qemu(vmid).config.post(**_nic_config_args(nics))
# Step 5: Verify snippet storage exists
# (enabled is absent-not-falsy, and node-scoping matters — see
@@ -777,7 +786,10 @@ class ProxmoxVMProvisionMixin:
except Exception as e:
_logger.exception(f"Failed to create VM '{name}': {e}")
raise
if created is None:
raise
# The VM stays behind half built; the caller removes it by its id.
raise VMProvisionIncomplete(str(e), vmid=str(created), node=self._node_name) from e
def destroy_vm(
self,