fix(hostname): refuse to rename a clustered node
driver / standard (pull_request) Successful in 18s
driver / lint (pull_request) Successful in 15s
driver / types (pull_request) Successful in 1m20s
driver / test (3.12) (pull_request) Successful in 1m26s
driver / lowest (pull_request) Successful in 43s
driver / test (3.13) (pull_request) Successful in 1m21s
driver / build (pull_request) Successful in 38s
driver / security (pull_request) Successful in 1m20s
CI / driver (pull_request) Successful in 3m12s
driver / publish (pull_request) Skipped
driver / standard (pull_request) Successful in 18s
driver / lint (pull_request) Successful in 15s
driver / types (pull_request) Successful in 1m20s
driver / test (3.12) (pull_request) Successful in 1m26s
driver / lowest (pull_request) Successful in 43s
driver / test (3.13) (pull_request) Successful in 1m21s
driver / build (pull_request) Successful in 38s
driver / security (pull_request) Successful in 1m20s
CI / driver (pull_request) Successful in 3m12s
driver / publish (pull_request) Skipped
The rename moves /etc/pve/nodes/<old> while the node still runs as <old>, 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
This commit is contained in:
1 parent
9c7b64ba64
commit
8ef880b73b
3 files changed
+66
-8
No files matched your search
@@ -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
|
- `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
|
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).
|
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/<old>` while the cluster still knew the node as `<old>`. 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
|
- `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).
|
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
|
- `get_users()` lists the node's root and regular login accounts (uid 1000 and
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ import re
|
|||||||
import shlex
|
import shlex
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
from napalm.base.exceptions import CommandErrorException
|
||||||
from napalm_device_types import APT_UPGRADABLE_COMMAND, parse_apt_upgradable
|
from napalm_device_types import APT_UPGRADABLE_COMMAND, parse_apt_upgradable
|
||||||
|
|
||||||
from napalm_proxmox.ntp import NTP_STATS_COMMAND, parse_ntp_stats
|
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"#]+$')
|
_SNMP_WORD = re.compile(r'^[^\s"#]+$')
|
||||||
#: The net-snmp agent's configuration file.
|
#: The net-snmp agent's configuration file.
|
||||||
_SNMPD_CONF = "/etc/snmp/snmpd.conf"
|
_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:
|
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",
|
"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:
|
def set_hostname(self, new_hostname: str) -> None:
|
||||||
"""Set the system hostname on the Proxmox node.
|
"""Set the system hostname on the Proxmox node.
|
||||||
|
|
||||||
@@ -634,16 +652,16 @@ class ProxmoxSystemMixin:
|
|||||||
7. Proxmox node TLS certificate — regenerated via
|
7. Proxmox node TLS certificate — regenerated via
|
||||||
``pvenode cert create --overwrite``.
|
``pvenode cert create --overwrite``.
|
||||||
|
|
||||||
**Important**: the Proxmox *node name* (shown in the web UI and stored
|
The Proxmox *node name* (web UI, ``/etc/pve/nodes/<name>/``) only
|
||||||
in ``/etc/pve/nodes/<name>/``) only changes after a full reboot.
|
changes after a reboot. Accepts a bare hostname or an FQDN.
|
||||||
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.
|
|
||||||
|
|
||||||
Accepts a bare hostname (``pve-see``) or an FQDN
|
Raises CommandErrorException, before changing anything, on a cluster
|
||||||
(``pve-see.see.local``).
|
member (a corosync.conf exists): its rename is the cluster's procedure.
|
||||||
"""
|
"""
|
||||||
import base64 as _b64
|
import base64 as _b64
|
||||||
|
|
||||||
|
self._refuse_clustered_node()
|
||||||
|
|
||||||
if "." in new_hostname:
|
if "." in new_hostname:
|
||||||
short_hostname, domain = new_hostname.split(".", 1)
|
short_hostname, domain = new_hostname.split(".", 1)
|
||||||
fqdn = new_hostname
|
fqdn = new_hostname
|
||||||
|
|||||||
@@ -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
|
from __future__ import annotations
|
||||||
|
|
||||||
@@ -6,6 +7,7 @@ import base64
|
|||||||
import re
|
import re
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from napalm.base.exceptions import CommandErrorException
|
||||||
|
|
||||||
from napalm_proxmox import ProxmoxDriver
|
from napalm_proxmox import ProxmoxDriver
|
||||||
|
|
||||||
@@ -18,13 +20,20 @@ HOSTS = """\
|
|||||||
|
|
||||||
|
|
||||||
class _Node:
|
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:
|
def __init__(self) -> None:
|
||||||
self.ran: list[str] = []
|
self.ran: list[str] = []
|
||||||
|
self.cluster_check = "__PVE_RC=1"
|
||||||
|
|
||||||
def __call__(self, command: str) -> str:
|
def __call__(self, command: str) -> str:
|
||||||
self.ran.append(command)
|
self.ran.append(command)
|
||||||
|
if "corosync.conf" in command:
|
||||||
|
return self.cluster_check
|
||||||
if command.startswith("cat /etc/hostname"):
|
if command.startswith("cat /etc/hostname"):
|
||||||
return "pve\n"
|
return "pve\n"
|
||||||
if command.startswith("cat /etc/hosts"):
|
if command.startswith("cat /etc/hosts"):
|
||||||
@@ -70,3 +79,29 @@ def test_the_same_name_leaves_hosts_alone(driver, node):
|
|||||||
driver.set_hostname("pve")
|
driver.set_hostname("pve")
|
||||||
|
|
||||||
assert not any("/etc/hosts" in c and "base64 -d" in c for c in node.ran)
|
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] == []
|
||||||
Reference in new issue
Block a user