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

+6
View File
@@ -40,6 +40,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `get_ipv6_neighbors_table()` reports an unknown age as `-1.0`, as `get_arp_table()`
already did (napalm-device-types' `ARP_AGE_UNKNOWN`), instead of `0.0`, which
means "confirmed just now" (NetOrk/netork#1327).
- A `create_vm_from_cloud_init()` that fails once the VM shell exists (disk import,
storage, NIC or snippet configuration, start) raises `VMProvisionIncomplete`, a
`RuntimeError` with the VMID and node of the VM it left behind and the failure as
its cause, so the caller can remove it with `destroy_vm()`. Failures there that
were a `ValueError` (no snippet storage, a NIC without a bridge) are its cause now.
Needs `VMProvisionIncomplete` from napalm-device-types (#28).
## [0.8.0] - 2026-10-23
+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,
+2 -2
View File
@@ -22,7 +22,7 @@
"napalm_proxmox/vm_mixin.py::ProxmoxVMMixin._get_vm_disk_and_boot": 16,
"napalm_proxmox/vm_mixin.py::ProxmoxVMMixin.get_vm_interfaces": 37,
"napalm_proxmox/vm_mixin.py::ProxmoxVMMixin.get_vms": 21,
"napalm_proxmox/vm_provision_mixin.py::ProxmoxVMProvisionMixin.create_vm_from_cloud_init": 25,
"napalm_proxmox/vm_provision_mixin.py::ProxmoxVMProvisionMixin.create_vm_from_cloud_init": 18,
"napalm_proxmox/vm_provision_mixin.py::ProxmoxVMProvisionMixin.get_vm_status": 16
},
"function_lines": {
@@ -40,7 +40,7 @@
"napalm_proxmox/vm_mixin.py::ProxmoxVMMixin.get_vm_interfaces": 179,
"napalm_proxmox/vm_mixin.py::ProxmoxVMMixin.get_vms": 114,
"napalm_proxmox/vm_provision_mixin.py::ProxmoxVMProvisionMixin._download_cloud_image": 61,
"napalm_proxmox/vm_provision_mixin.py::ProxmoxVMProvisionMixin.create_vm_from_cloud_init": 232,
"napalm_proxmox/vm_provision_mixin.py::ProxmoxVMProvisionMixin.create_vm_from_cloud_init": 220,
"napalm_proxmox/vm_provision_mixin.py::ProxmoxVMProvisionMixin.destroy_vm": 65,
"napalm_proxmox/vm_provision_mixin.py::ProxmoxVMProvisionMixin.get_vm_status": 92
},
+125
View File
@@ -0,0 +1,125 @@
"""A creation that fails once the VM exists names that VM (#28).
Without its id the caller cannot remove the half-built VM, and it stays on the
node. A failure before the VM shell exists leaves nothing behind and raises
as it did.
"""
from __future__ import annotations
from unittest.mock import MagicMock, patch
import pytest
from napalm_device_types.provisioning import VMProvisionIncomplete
from napalm_proxmox.vm_provision_mixin import ProxmoxVMProvisionMixin
_DEBIAN_URL = (
"https://cloud.debian.org/images/cloud/trixie/latest/debian-13-genericcloud-amd64.qcow2"
)
def _mixin() -> tuple:
"""A mixin whose node answers like Proxmox and hands out VMID 101."""
mixin = ProxmoxVMProvisionMixin()
mixin._node_name = "pve1"
api = MagicMock()
api.cluster.nextid.get.return_value = 101
api.storage.return_value.get.return_value = {"path": "/var/lib/vz"}
node = MagicMock()
node.storage.get.return_value = [
{"storage": "local-lvm", "type": "lvmthin", "content": "images,rootdir", "enabled": 1},
{"storage": "local", "type": "dir", "content": "snippets,iso", "enabled": 1},
]
vm = MagicMock()
node.qemu.return_value = vm
vm.config.get.return_value = {"unused0": "local-lvm:vm-101-disk-0"}
vm.status.start.post.return_value = "UPID:pve1:start"
node.tasks.return_value.status.get.return_value = {"status": "stopped", "exitstatus": "OK"}
mixin._api = api
mixin._node_api = MagicMock(return_value=node)
mixin._download_cloud_image = MagicMock(
return_value="/var/lib/vz/template/netork-images/x.qcow2"
)
mixin._run_node_command = MagicMock(return_value="")
return mixin, node, vm
def _create(mixin):
with patch("time.sleep"):
return mixin.create_vm_from_cloud_init(
name="half-built",
image_url=_DEBIAN_URL,
cpu=2,
memory=2048,
nics=[{"bridge": "vmbr0"}],
cloud_init_config={"hostname": "half-built"},
)
def test_a_failed_disk_import_names_the_vm_it_left_behind():
mixin, _node, _vm = _mixin()
failure = RuntimeError("qm importdisk 101: storage 'local-lvm' is full")
mixin._download_cloud_image.side_effect = failure
with pytest.raises(VMProvisionIncomplete) as caught:
_create(mixin)
assert (caught.value.vmid, caught.value.node) == ("101", "pve1")
assert caught.value.__cause__ is failure
assert str(caught.value) == str(failure)
def test_a_failed_start_names_the_vm_it_left_behind():
mixin, node, _vm = _mixin()
node.tasks.return_value.status.get.return_value = {
"status": "stopped",
"exitstatus": "start failed: QEMU exited with code 1",
}
with pytest.raises(VMProvisionIncomplete, match="QEMU exited") as caught:
_create(mixin)
assert (caught.value.vmid, caught.value.node) == ("101", "pve1")
def test_an_invalid_nic_after_the_shell_exists_names_the_vm_too():
mixin, _node, _vm = _mixin()
with pytest.raises(VMProvisionIncomplete, match="bridge is required") as caught:
with patch("time.sleep"):
mixin.create_vm_from_cloud_init(
name="half-built",
image_url=_DEBIAN_URL,
cpu=2,
memory=2048,
nics=[{"vlan_tag": 10}],
cloud_init_config={},
)
assert isinstance(caught.value.__cause__, ValueError)
assert caught.value.vmid == "101"
def test_a_failure_creating_the_shell_leaves_no_vm_to_name():
mixin, node, _vm = _mixin()
failure = RuntimeError("VM 101 already exists")
node.qemu.post.side_effect = failure
with pytest.raises(RuntimeError) as caught:
_create(mixin)
assert caught.value is failure
assert not isinstance(caught.value, VMProvisionIncomplete)
def test_a_failure_taking_the_vmid_leaves_no_vm_to_name():
mixin, node, _vm = _mixin()
failure = RuntimeError("cluster not ready - no quorum?")
mixin._api.cluster.nextid.get.side_effect = failure
with pytest.raises(RuntimeError) as caught:
_create(mixin)
assert caught.value is failure
node.qemu.post.assert_not_called()
+6 -2
View File
@@ -6,6 +6,7 @@ import base64
from unittest.mock import MagicMock, patch
import pytest
from napalm_device_types.provisioning import VMProvisionIncomplete
from napalm_proxmox.vm_provision_mixin import ProxmoxVMProvisionMixin
@@ -220,7 +221,8 @@ def test_create_vm_from_cloud_init_dual_nic_trunk():
def test_create_vm_missing_snippet_storage():
"""create_vm_from_cloud_init raises ValueError if snippet storage unavailable."""
"""No snippet storage fails the creation; the VM shell exists by then, so the
ValueError arrives as the cause of VMProvisionIncomplete naming it (#28)."""
mixin = ProxmoxVMProvisionMixin()
mixin._node_name = "pve1"
@@ -250,7 +252,7 @@ def test_create_vm_missing_snippet_storage():
mock_task.status.get.return_value = {"status": "stopped", "exitstatus": "OK"}
mock_node.tasks.return_value = mock_task
with pytest.raises(ValueError, match="snippet"):
with pytest.raises(VMProvisionIncomplete, match="snippet") as caught:
with patch("napalm_proxmox.vm_provision_mixin.time.sleep"):
mixin.create_vm_from_cloud_init(
name="test-vm",
@@ -260,6 +262,8 @@ def test_create_vm_missing_snippet_storage():
nics=[{"bridge": "vmbr0"}],
cloud_init_config={},
)
assert isinstance(caught.value.__cause__, ValueError)
assert caught.value.vmid == "101"
def test_create_vm_with_disk_resize():