From 8ef880b73b8781b0e0c371c1604314d5bfa098a1 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Sun, 1 Nov 2026 22:08:54 +0100 Subject: [PATCH] fix(hostname): refuse to rename a clustered node The rename moves /etc/pve/nodes/ while the node still runs as , which Proxmox does not support for a cluster member. set_hostname now checks for a corosync.conf first and raises before touching anything on a member, or when the check cannot be read; a single node renames as before. The check is the file, not `pvecm status`: pvecm decides membership by /etc/pve/corosync.conf itself, and its status also fails on a member whose corosync is down. /etc/corosync/corosync.conf covers a member whose /etc/pve is not mounted. Closes #21 --- CHANGELOG.md | 5 +++++ napalm_proxmox/system_mixin.py | 30 ++++++++++++++++++++------ tests/test_set_hostname.py | 39 ++++++++++++++++++++++++++++++++-- 3 files changed, 66 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d48777c..a0f4b61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -54,6 +54,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `set_hostname()` renames only the node's own names in `/etc/hosts`: other hosts whose names start with the old one (`pve-backup`) and comments stay as they are, and the old FQDN becomes the new one instead of a mix of both (#21). +- `set_hostname()` refuses a node that is part of a cluster (a + `/etc/pve/corosync.conf` or `/etc/corosync/corosync.conf` exists) with a + `CommandErrorException`, before changing anything: it moved + `/etc/pve/nodes/` while the cluster still knew the node as ``. Also + refused when the check cannot be read. A single node renames as before (#21). - `apply_updates()` reports a failed upgrade as `success: False`, with apt-get's exit status in `error`; it reported success whatever apt-get did (#20). - `get_users()` lists the node's root and regular login accounts (uid 1000 and diff --git a/napalm_proxmox/system_mixin.py b/napalm_proxmox/system_mixin.py index 669f078..2a013bf 100644 --- a/napalm_proxmox/system_mixin.py +++ b/napalm_proxmox/system_mixin.py @@ -26,6 +26,7 @@ import re import shlex from typing import Any +from napalm.base.exceptions import CommandErrorException from napalm_device_types import APT_UPGRADABLE_COMMAND, parse_apt_upgradable from napalm_proxmox.ntp import NTP_STATS_COMMAND, parse_ntp_stats @@ -49,6 +50,11 @@ _PACKAGE_NAME = re.compile(r"^[a-zA-Z0-9][a-zA-Z0-9_.+:~-]*$") _SNMP_WORD = re.compile(r'^[^\s"#]+$') #: The net-snmp agent's configuration file. _SNMPD_CONF = "/etc/snmp/snmpd.conf" +#: Exit status 0 when the node is a cluster member. pvecm itself decides +#: membership by /etc/pve/corosync.conf; /etc/corosync/corosync.conf is the +#: local copy every member keeps, there even when /etc/pve is not mounted. +#: Not ``pvecm status``: it also fails on a member whose corosync is down. +_CLUSTER_MEMBER_CHECK = "[ -e /etc/pve/corosync.conf ] || [ -e /etc/corosync/corosync.conf ]" def _rename_in_hosts(hosts: str, old_short: str, new_short: str, new_domain: str | None) -> str: @@ -617,6 +623,18 @@ class ProxmoxSystemMixin: "snmpd enabled and running", ) + def _refuse_clustered_node(self) -> None: + _, status = self._rc(_CLUSTER_MEMBER_CHECK) + if status == 0: + raise CommandErrorException( + "set_hostname refused: the node is part of a cluster; rename a cluster" + " member by Proxmox's own procedure, not in place" + ) + if status != 1: + raise CommandErrorException( + "set_hostname refused: could not tell whether the node is part of a cluster" + ) + def set_hostname(self, new_hostname: str) -> None: """Set the system hostname on the Proxmox node. @@ -634,16 +652,16 @@ class ProxmoxSystemMixin: 7. Proxmox node TLS certificate — regenerated via ``pvenode cert create --overwrite``. - **Important**: the Proxmox *node name* (shown in the web UI and stored - in ``/etc/pve/nodes//``) only changes after a full reboot. - Until then the web UI still shows the old node name, but the OS-level - hostname and the SSL certificate already reflect the new value. + The Proxmox *node name* (web UI, ``/etc/pve/nodes//``) only + changes after a reboot. Accepts a bare hostname or an FQDN. - Accepts a bare hostname (``pve-see``) or an FQDN - (``pve-see.see.local``). + Raises CommandErrorException, before changing anything, on a cluster + member (a corosync.conf exists): its rename is the cluster's procedure. """ import base64 as _b64 + self._refuse_clustered_node() + if "." in new_hostname: short_hostname, domain = new_hostname.split(".", 1) fqdn = new_hostname diff --git a/tests/test_set_hostname.py b/tests/test_set_hostname.py index cc59e31..b5f86ab 100644 --- a/tests/test_set_hostname.py +++ b/tests/test_set_hostname.py @@ -1,4 +1,5 @@ -"""set_hostname: what it writes to /etc/hosts (napalm-proxmox#21).""" +"""set_hostname: what it writes to /etc/hosts, and that it leaves a clustered node alone +(napalm-proxmox#21).""" from __future__ import annotations @@ -6,6 +7,7 @@ import base64 import re import pytest +from napalm.base.exceptions import CommandErrorException from napalm_proxmox import ProxmoxDriver @@ -18,13 +20,20 @@ HOSTS = """\ class _Node: - """The node's shell: /etc/hostname reads "pve", /etc/hosts reads HOSTS.""" + """The node's shell: /etc/hostname reads "pve", /etc/hosts reads HOSTS. + + *cluster_check* is what the test for a corosync.conf prints: exit status 1 + (no corosync.conf, a single node) unless a test says otherwise. + """ def __init__(self) -> None: self.ran: list[str] = [] + self.cluster_check = "__PVE_RC=1" def __call__(self, command: str) -> str: self.ran.append(command) + if "corosync.conf" in command: + return self.cluster_check if command.startswith("cat /etc/hostname"): return "pve\n" if command.startswith("cat /etc/hosts"): @@ -70,3 +79,29 @@ def test_the_same_name_leaves_hosts_alone(driver, node): driver.set_hostname("pve") assert not any("/etc/hosts" in c and "base64 -d" in c for c in node.ran) + + +def test_a_clustered_node_is_refused_before_anything_changes(driver, node): + node.cluster_check = "__PVE_RC=0" + + with pytest.raises(CommandErrorException, match="part of a cluster"): + driver.set_hostname("new") + + assert [c for c in node.ran if "corosync.conf" not in c] == [] + + +def test_the_cluster_check_looks_at_both_corosync_confs(driver, node): + driver.set_hostname("new") + + check = next(c for c in node.ran if "corosync.conf" in c) + assert "/etc/pve/corosync.conf" in check + assert "/etc/corosync/corosync.conf" in check + + +def test_an_unreadable_cluster_check_refuses_too(driver, node): + node.cluster_check = "" + + with pytest.raises(CommandErrorException, match="could not tell"): + driver.set_hostname("new") + + assert [c for c in node.ran if "corosync.conf" not in c] == []