style: ruff format and the lint rules of the driver standard
Mechanical: ruff format, and ruff check --fix with the rule set every NAPALM driver uses (E, F, I, UP, B at line length 100, Python 3.10). What ruff cannot fix comes in the next commit.
This commit is contained in:
1 parent
1f6859de08
commit
3b873d91fe
27 files changed
+195
-225
No files matched your search
@@ -25,8 +25,6 @@ from typing import Any
|
||||
|
||||
from napalm_device_types import APT_UPGRADABLE_COMMAND, parse_apt_upgradable
|
||||
|
||||
from napalm_proxmox import utils
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_JsonDict = dict[str, Any]
|
||||
@@ -177,9 +175,7 @@ class ProxmoxSystemMixin:
|
||||
Proxmox does not expose SNMP configuration via the REST API.
|
||||
We read /etc/snmp/snmpd.conf via exec if available.
|
||||
"""
|
||||
raw = self._exec_ssh_command(
|
||||
"cat /etc/snmp/snmpd.conf 2>/dev/null || true"
|
||||
)
|
||||
raw = self._exec_ssh_command("cat /etc/snmp/snmpd.conf 2>/dev/null || true")
|
||||
communities: dict[str, _JsonDict] = {}
|
||||
location = ""
|
||||
contact = ""
|
||||
@@ -344,17 +340,19 @@ class ProxmoxSystemMixin:
|
||||
if not name or status != "installed":
|
||||
continue
|
||||
size_bytes = int(size_kb) * 1024 if size_kb.isdigit() else 0
|
||||
result.append({
|
||||
"name": name,
|
||||
"version": version,
|
||||
"installed": True,
|
||||
"description": "",
|
||||
"size": size_bytes,
|
||||
"source": "pve",
|
||||
"source_package": source_package,
|
||||
"source_version": source_version,
|
||||
"upgrade_version": upgradable.get(name, ""),
|
||||
})
|
||||
result.append(
|
||||
{
|
||||
"name": name,
|
||||
"version": version,
|
||||
"installed": True,
|
||||
"description": "",
|
||||
"size": size_bytes,
|
||||
"source": "pve",
|
||||
"source_package": source_package,
|
||||
"source_version": source_version,
|
||||
"upgrade_version": upgradable.get(name, ""),
|
||||
}
|
||||
)
|
||||
return result
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
@@ -443,7 +441,7 @@ class ProxmoxSystemMixin:
|
||||
def apply_updates(self, packages: list[str]) -> _JsonDict:
|
||||
"""Upgrade the given packages via ``apt-get install`` over SSH."""
|
||||
for pkg in packages:
|
||||
if not re.match(r'^[a-zA-Z0-9_\-\+\.]+$', pkg):
|
||||
if not re.match(r"^[a-zA-Z0-9_\-\+\.]+$", pkg):
|
||||
raise ValueError(f"Invalid package name: {pkg!r}")
|
||||
pkg_args = " ".join(packages)
|
||||
output = self._exec_ssh_command(
|
||||
@@ -526,9 +524,9 @@ class ProxmoxSystemMixin:
|
||||
encoded = base64.b64encode(content.encode()).decode()
|
||||
# No ``exit`` here: ``_rc`` reads the status after the command's braces.
|
||||
return self._step(
|
||||
f"umask 077 && t=$(mktemp) && echo {encoded} | base64 -d > \"$t\" && "
|
||||
f"install -m 644 -o root -g root \"$t\" {shlex.quote(path)}; "
|
||||
"s=$?; rm -f \"$t\"; [ \"$s\" -eq 0 ]",
|
||||
f'umask 077 && t=$(mktemp) && echo {encoded} | base64 -d > "$t" && '
|
||||
f'install -m 644 -o root -g root "$t" {shlex.quote(path)}; '
|
||||
's=$?; rm -f "$t"; [ "$s" -eq 0 ]',
|
||||
f"wrote {path}",
|
||||
)
|
||||
|
||||
@@ -557,9 +555,9 @@ class ProxmoxSystemMixin:
|
||||
def _write_snmp_agent_config(self, settings: dict) -> dict:
|
||||
encoded = base64.b64encode(render_snmpd_conf(settings).encode()).decode()
|
||||
return self._step(
|
||||
f"umask 077 && t=$(mktemp) && echo {encoded} | base64 -d > \"$t\" && "
|
||||
f"install -m 644 -o root -g root \"$t\" /etc/snmp/snmpd.conf; "
|
||||
"s=$?; rm -f \"$t\"; exit $s",
|
||||
f'umask 077 && t=$(mktemp) && echo {encoded} | base64 -d > "$t" && '
|
||||
f'install -m 644 -o root -g root "$t" /etc/snmp/snmpd.conf; '
|
||||
's=$?; rm -f "$t"; exit $s',
|
||||
"wrote /etc/snmp/snmpd.conf",
|
||||
)
|
||||
|
||||
@@ -614,9 +612,11 @@ class ProxmoxSystemMixin:
|
||||
fqdn = new_hostname
|
||||
|
||||
# Derive old short hostname for substitution in /etc/hosts
|
||||
old_short = self._exec_ssh_command(
|
||||
"cat /etc/hostname 2>/dev/null || hostname -s 2>/dev/null"
|
||||
).strip().split(".")[0]
|
||||
old_short = (
|
||||
self._exec_ssh_command("cat /etc/hostname 2>/dev/null || hostname -s 2>/dev/null")
|
||||
.strip()
|
||||
.split(".")[0]
|
||||
)
|
||||
|
||||
# ── 1. /etc/hostname ─────────────────────────────────────────────
|
||||
hostname_b64 = _b64.b64encode(f"{short_hostname}\n".encode()).decode()
|
||||
@@ -631,6 +631,7 @@ class ProxmoxSystemMixin:
|
||||
# Both "hostname" and "hostname.domain" forms are covered because
|
||||
# the new value is the full fqdn when a domain was given.
|
||||
import re as _re
|
||||
|
||||
new_replace = fqdn if domain else short_hostname
|
||||
hosts_new = _re.sub(
|
||||
r"\b" + _re.escape(old_short) + r"\b",
|
||||
@@ -647,16 +648,12 @@ class ProxmoxSystemMixin:
|
||||
# ── 3. /etc/mailname ─────────────────────────────────────────────
|
||||
mailname_b64 = _b64.b64encode(f"{fqdn}\n".encode()).decode()
|
||||
self._exec_ssh_command(
|
||||
f"[ -f /etc/mailname ]"
|
||||
f" && echo {mailname_b64} | base64 -d > /etc/mailname"
|
||||
f" || true"
|
||||
f"[ -f /etc/mailname ] && echo {mailname_b64} | base64 -d > /etc/mailname || true"
|
||||
)
|
||||
|
||||
# ── 4. Postfix myhostname ─────────────────────────────────────────
|
||||
self._exec_ssh_command(
|
||||
f"command -v postconf >/dev/null 2>&1"
|
||||
f" && postconf -e 'myhostname = {fqdn}'"
|
||||
f" || true"
|
||||
f"command -v postconf >/dev/null 2>&1 && postconf -e 'myhostname = {fqdn}' || true"
|
||||
)
|
||||
|
||||
# ── 5. Apply hostname at runtime ─────────────────────────────────
|
||||
@@ -699,7 +696,9 @@ class ProxmoxSystemMixin:
|
||||
logger.info(
|
||||
"Proxmox set_hostname: %s → %s (fqdn: %s). "
|
||||
"Reboot required for node name to update in web UI / cluster.",
|
||||
old_short, short_hostname, fqdn,
|
||||
old_short,
|
||||
short_hostname,
|
||||
fqdn,
|
||||
)
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
@@ -736,12 +735,12 @@ class ProxmoxSystemMixin:
|
||||
if not dev:
|
||||
continue
|
||||
entry: dict = {
|
||||
"model": disk.get("model", ""),
|
||||
"serial": disk.get("serial", ""),
|
||||
"type": disk.get("type", ""),
|
||||
"size": disk.get("size", 0),
|
||||
"health": disk.get("health", "unknown").lower(),
|
||||
"wearout": disk.get("wearout"), # NVMe wear indicator 0-100
|
||||
"model": disk.get("model", ""),
|
||||
"serial": disk.get("serial", ""),
|
||||
"type": disk.get("type", ""),
|
||||
"size": disk.get("size", 0),
|
||||
"health": disk.get("health", "unknown").lower(),
|
||||
"wearout": disk.get("wearout"), # NVMe wear indicator 0-100
|
||||
"temperature": None,
|
||||
"percentage_used": None,
|
||||
"available_spare": None,
|
||||
@@ -772,7 +771,7 @@ class ProxmoxSystemMixin:
|
||||
# HDD-specific SMART attributes
|
||||
for attr in smart.get("attributes", []):
|
||||
name = attr.get("name", "").lower()
|
||||
raw = attr.get("raw", "")
|
||||
raw = attr.get("raw", "")
|
||||
try:
|
||||
raw_int = int(str(raw).split()[0])
|
||||
except (ValueError, TypeError):
|
||||
|
||||
Reference in new issue
Block a user