diff --git a/CHANGELOG.md b/CHANGELOG.md index 7157099..f3d0be9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.11.0] - 2026-11-25 + ### Added +- `resume_vm(name)` (napalm-device-types' `HypervisorDriver`, NetOrk/netork#2658): a paused + VM continues where `suspend_vm` stopped it, through `status/resume`, and the driver waits + for its task. A VM that is not paused is refused (PVE answers a running one with success + and does nothing), and so is a container: the driver does not suspend one, PVE's + container suspend is experimental, and its listing does not tell a suspended container + from another. - A native Zabbix agent on both drivers (napalm-device-types `ZabbixAgentMixin`, NetOrk/netork#2571): a node runs no container engine. `configure_zabbix_agent(settings)` installs Debian's `zabbix-agent`, and where Debian has none and the caller names the @@ -53,6 +61,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 in its own daemons; `get_clusters()` and `get_backup_jobs()` keep them. ### Changed +- `get_vms()` reports a paused VM as `"paused"`, no longer as `"running"` (NetOrk/netork#2658). + PVE says `status: running` for a guest that `status/suspend` paused, or that sleeps in + S3; only the full listing (`GET /nodes/{node}/qemu?full=1`) has its `qmpstatus`, so the + driver asks for it. - Needs napalm-device-types 4.40.0 or later (`ZabbixAgentMixin`). - The SNMP agent's ufw step is the shared `_allow_sources(allowed, port, proto)`; the rule it writes for udp/161 is the same. diff --git a/napalm_proxmox/vm_contract_mixin.py b/napalm_proxmox/vm_contract_mixin.py index bad5d83..af4e07b 100644 --- a/napalm_proxmox/vm_contract_mixin.py +++ b/napalm_proxmox/vm_contract_mixin.py @@ -19,6 +19,8 @@ from napalm_device_types.models import ( VMPassthroughDict, ) +from napalm_proxmox.vm_mixin import qemu_status + _JsonDict = dict[str, Any] _POWER_TIMEOUT = 120 @@ -188,6 +190,25 @@ class ProxmoxVMContractMixin: raise RuntimeError(f"Proxmox cannot suspend container {vmid}") self._run_power(vmid, vm_type, "suspend") + def resume_vm(self, name: str) -> None: + """``status/resume`` for a paused QEMU guest (NetOrk/netork#2658). + + A container is refused: this driver does not suspend one, PVE's container + suspend is experimental (CRIU), and its listing does not tell a suspended + container from another. A guest that is not paused is refused too: PVE would + answer a running one with success and do nothing. + """ + vmid, vm_type = self._resolve_vm(name) + if vm_type != "vm": + raise RuntimeError( + f"Proxmox cannot resume container {vmid}: only a paused VM can be resumed" + ) + current = self._guest_api(vmid, vm_type).status.current.get() or {} + state = qemu_status(current) + if state != "paused": + raise RuntimeError(f"Cannot resume VM {vmid}: it is not paused ({state})") + self._run_power(vmid, vm_type, "resume") + def get_vm_config(self, name: str) -> VMConfigDict: vmid, vm_type = self._resolve_vm(name) cfg = self._guest_api(vmid, vm_type).config.get() or {} diff --git a/napalm_proxmox/vm_mixin.py b/napalm_proxmox/vm_mixin.py index b4379f9..229f9e2 100644 --- a/napalm_proxmox/vm_mixin.py +++ b/napalm_proxmox/vm_mixin.py @@ -24,6 +24,20 @@ logger = logging.getLogger(__name__) _JsonDict = dict[str, Any] +#: The QMP states of a guest whose QEMU process holds its memory but does not run it: +#: paused (``status/suspend``, the GUI's Pause) and asleep in S3. ``status/resume`` +#: brings either back. PVE's ``status`` says ``running`` for both (NetOrk/netork#2658). +PAUSED_QMP_STATES = ("paused", "suspended") + + +def qemu_status(vm: _JsonDict) -> str: + """A QEMU guest's status as the contract names it: ``paused`` where PVE's full + status (``qmpstatus``) says the running process does not execute the guest.""" + status = str(vm.get("status", "unknown")) + if status == "running" and vm.get("qmpstatus") in PAUSED_QMP_STATES: + return "paused" + return status + class ProxmoxVMMixin: """Mixin providing VM and container NAPALM methods.""" @@ -215,7 +229,7 @@ class ProxmoxVMMixin: * vmid (str) - Proxmox VM/container ID, e.g. ``"100"`` * name (str) - display name * type (str) - ``"vm"`` or ``"container"`` - * status (str) - ``"running"``, ``"stopped"``, etc. + * status (str) - ``"running"``, ``"stopped"``, ``"paused"`` (``qemu_status``) * vcpus (int) - allocated vCPUs * memory (int) - configured RAM in megabytes * cpu_usage (float) - current CPU utilisation 0.0–1.0 (from last stats cycle) @@ -227,12 +241,12 @@ class ProxmoxVMMixin: """ result: list[_JsonDict] = [] - # QEMU VMs + # QEMU VMs; only the full status has ``qmpstatus``, which tells paused from running. try: - for vm in self._node_api().qemu.get() or []: + for vm in self._node_api().qemu.get(full=1) or []: vmid = int(vm.get("vmid", 0)) name = vm.get("name", f"vm-{vmid}") - status = vm.get("status", "unknown") + status = qemu_status(vm) cpu_usage = float(vm.get("cpu", 0.0) or 0.0) uptime = int(vm.get("uptime", 0) or 0) diff --git a/pyproject.toml b/pyproject.toml index bf73104..9c3b62e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "napalm-proxmox" -version = "0.10.0" +version = "0.11.0" description = "NAPALM drivers for Proxmox VE (classic Linux networking, SDN, OVS) and Proxmox Backup Server" readme = "README.md" license = "Apache-2.0" diff --git a/tests/test_vm_contract.py b/tests/test_vm_contract.py index eae97ba..fb23493 100644 --- a/tests/test_vm_contract.py +++ b/tests/test_vm_contract.py @@ -41,6 +41,43 @@ class TestGetVmsReportsStringIds: assert [vm["vmid"] for vm in driver.get_vms()] == ["99", "1000"] +class TestGetVmsTellsPausedFromRunning: + """PVE reports a paused guest ``status: running``; only ``qmpstatus`` tells, and only + when the listing is asked for the full status (NetOrk/netork#2658).""" + + @pytest.fixture + def listed(self, driver, api): + driver.get_vm_interfaces = MagicMock(return_value=({}, False, False)) + driver._get_vm_disk_and_boot = MagicMock(return_value=([], False)) + api.lxc.get.return_value = [] + + def statuses(*qemu): + api.qemu.get.return_value = [ + {"vmid": 100 + i, "name": f"vm{i}", **row} for i, row in enumerate(qemu) + ] + return {vm["vmid"]: vm["status"] for vm in driver.get_vms()} + + return statuses + + def test_the_listing_asks_for_the_full_status(self, listed, api): + listed({"status": "running", "qmpstatus": "running"}) + api.qemu.get.assert_called_once_with(full=1) + + def test_a_paused_guest_is_paused(self, listed): + assert listed({"status": "running", "qmpstatus": "paused"}) == {"100": "paused"} + + def test_a_guest_asleep_in_s3_is_paused(self, listed): + """Its QEMU process still holds its memory; ``status/resume`` wakes it.""" + assert listed({"status": "running", "qmpstatus": "suspended"}) == {"100": "paused"} + + def test_running_and_stopped_stay_as_they_are(self, listed): + assert listed( + {"status": "running", "qmpstatus": "running"}, + {"status": "stopped", "qmpstatus": "stopped"}, + {"status": "running"}, + ) == {"100": "running", "101": "stopped", "102": "running"} + + class TestResolveVm: def test_by_vmid_string(self, driver, api): assert driver._resolve_vm("100") == (100, "vm") @@ -84,6 +121,29 @@ class TestPowerActions: with pytest.raises(RuntimeError, match="container"): driver.suspend_vm("200") + @pytest.mark.parametrize("qmpstatus", ["paused", "suspended"]) + def test_resume_posts_status_resume_and_waits(self, driver, api, qmpstatus): + guest = api.qemu.return_value + guest.status.current.get.return_value = {"status": "running", "qmpstatus": qmpstatus} + guest.status.resume.post.return_value = "UPID:resume" + driver.resume_vm("web01") + guest.status.resume.post.assert_called_once() + driver._wait_for_task.assert_called_once_with("UPID:resume", timeout=120) + + @pytest.mark.parametrize( + "current", [{"status": "running", "qmpstatus": "running"}, {"status": "stopped"}] + ) + def test_resume_of_a_guest_that_is_not_paused_is_refused(self, driver, api, current): + api.qemu.return_value.status.current.get.return_value = current + with pytest.raises(RuntimeError, match="not paused"): + driver.resume_vm("100") + api.qemu.return_value.status.resume.post.assert_not_called() + + def test_resume_of_a_container_is_refused(self, driver, api): + with pytest.raises(RuntimeError, match="container 200"): + driver.resume_vm("200") + api.lxc.return_value.status.resume.post.assert_not_called() + def test_api_error_becomes_runtime_error(self, driver, api): api.qemu.return_value.status.start.post.side_effect = Exception("locked") with pytest.raises(RuntimeError, match="locked"):