fix(snmp): let the snmpd.conf write report its exit status

The write ended in `exit $s`, which ends the shell inside the braces
_rc wraps around every command, so __PVE_RC was never printed and the
step always failed. The write now shares the apt file writer, which
ends in a test instead of exit.

Closes #30
This commit is contained in:
Christian Manivong committed 2026-11-01 21:44:23 +01:00
1 parent d142db73e5
commit 52d63eb689
3 files changed
+45 -8

No files matched your search

+8 -8
View File
@@ -45,6 +45,8 @@ _SUBSCRIPTION_STATUS = {
_PACKAGE_NAME = re.compile(r"^[a-zA-Z0-9][a-zA-Z0-9_.+:~-]*$")
#: What may stand in an snmpd.conf value: no whitespace, no quote, no comment.
_SNMP_WORD = re.compile(r'^[^\s"#]+$')
#: The net-snmp agent's configuration file.
_SNMPD_CONF = "/etc/snmp/snmpd.conf"
def _net(value: str) -> str:
@@ -527,8 +529,12 @@ class ProxmoxSystemMixin:
return self._step(command, done)
def _write_apt_file(self, path: str, content: str) -> dict:
return self._write_root_file(path, content)
def _write_root_file(self, path: str, content: str) -> dict:
"""Write *content* to *path* as root, mode 644, through a temporary file."""
encoded = base64.b64encode(content.encode()).decode()
# No ``exit`` here: ``_rc`` reads the status after the command's braces.
# No ``exit`` here: it would end the shell before ``_rc`` reads the status.
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)}; '
@@ -559,13 +565,7 @@ 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',
"wrote /etc/snmp/snmpd.conf",
)
return self._write_root_file(_SNMPD_CONF, render_snmpd_conf(settings))
def _allow_snmp_sources(self, settings: dict) -> dict:
sources = [_net(s) for s in settings.get("allowed_sources", [])]