Files
napalm-bsd/napalm_bsd/openbsd.py
T
Christian Manivong 018e7669ad feat(updates): apply_updates on FreeBSD and OpenBSD, each line as it comes (#13)
The upgrade commands move here from the application (NetOrk/netork#1112):
pkg upgrade or pkg_add -u for the chosen packages or every one, then the
base system's patches when base-system is chosen or nothing is. A classic
FreeBSD base installs what freebsd-update fetched (nothing fetched is no
failure; pkgbase needs no extra step); OpenBSD runs syspatch again when it
updated itself, and a second exit 2 is "nothing left".

The whole upgrade runs as root as one sh script on an exec channel, stderr
merged, the sudo password on stdin only and the script's stdin closed. The
log keyword napalm-device-types declares on apply_updates receives each line
as the host prints it. A host silent for 300 s, or a dropped connection, is
a failure with what was printed until then.

CI pre-installs napalm-device-types' branch commit with that declaration
until 4.54.0 is on PyPI.
2026-11-29 20:22:04 +01:00

126 lines
5.1 KiB
Python

"""OpenBSD: hw.* sysctls for the hardware, uname for release and kernel."""
from __future__ import annotations
import re
from dataclasses import replace
from typing import ClassVar
from napalm_device_types import FingerprintRule, SnmpHealthProfile
from napalm_device_types._crontab import CronLine
from napalm_bsd import parse
from napalm_bsd.base import BASE_SYSTEM, BsdDriver
#: OpenBSD cron's flags before a command: -n (no mail), -q (no log), -s (single instance).
_CRON_FLAGS = re.compile(r"^(?:-[nqs]+\s+)+")
class OpenBSDDriver(BsdDriver):
"""NAPALM driver for OpenBSD hosts, over SSH."""
TYPE_LABEL = "OpenBSD"
VENDOR = "OpenBSD"
DRIVER_NAME = "openbsd"
# OpenBSD's SSH banner names no OS; its sysDescr does ("OpenBSD host 7.9 GENERIC.MP#449").
SNMP_FINGERPRINT = [FingerprintRule("openbsd", weight=5.0)]
# net-snmp maps OpenBSD's CPU states wrongly: ssCpuIdle is 0 on an idle host, and
# hrProcessorLoad 100. No figure beats one that says the host is always busy (#7).
SNMP_HEALTH_PROFILE: ClassVar[SnmpHealthProfile] = {"cpu": {"unknown": True}}
OS_VERSION_COMMAND = "uname -sr"
# The kernel's build, e.g. GENERIC.MP#449; the release is in uname -r.
KERNEL_COMMAND = "uname -v"
# A sysctl node the machine lacks fails on its own and prints nothing.
PLATFORM_COMMAND = (
"for k in vendor product serialno; do "
'printf "%s=%s\\n" "$k" "$(sysctl -n hw.$k 2>/dev/null)"; done'
)
# fstat shows a user only their own processes; netstat shows every socket, unnamed.
LISTENING_COMMAND = "fstat -n"
LISTENING_FALLBACK_COMMAND = "netstat -an -f inet; netstat -an -f inet6"
# Scheduled jobs: no cron.d; root's crontab runs /etc/daily, weekly and monthly
# (and their .local), so they are jobs once, as its lines.
PLATFORM_COMMANDS = (
"/usr/bin/newsyslog",
"/bin/sh /etc/daily",
"/bin/sh /etc/weekly",
"/bin/sh /etc/monthly",
)
def _sj_line(self, job: dict[str, object], text: str) -> CronLine:
"""The line to switch or run: run without cron's own flags."""
line = super()._sj_line(job, text)
return replace(line, command=_CRON_FLAGS.sub("", line.command))
def _parse_listening(self, output: str, *, attributed: bool) -> list[dict]:
return parse.fstat_sockets(output) if attributed else parse.netstat_listening(output)
INSTALL_COMMAND = "pkg_add -I {name}"
UNINSTALL_COMMAND = "pkg_delete {name}"
# apply_updates; -I: never stop at a question.
UPGRADE_PACKAGES = "pkg_add -u -I {packages}"
UPGRADE_ALL = "pkg_add -u -I"
# syspatch exits 2 both when it updated itself (run it again) and when it
# had nothing to install; a second 2 means the latter. A failure is 1.
BASE_SYSTEM_UPGRADE = (
"syspatch; r=$?; if [ $r -eq 2 ]; then syspatch; r=$?; fi; "
"if [ $r -eq 2 ]; then r=0; fi; exit $r"
)
# rcctl check needs no root; "rcctl ls started" does.
SERVICE_STATUS_COMMAND = (
"for s in $(rcctl ls on); do "
"if rcctl check $s >/dev/null 2>&1; then r=1; else r=0; fi; "
'printf "%s\\t%s\\n" "$s" "$r"; done'
)
SERVICE_ACTION_COMMAND = "rcctl {action} {name}"
SNMPD_CONF = "/etc/snmp/snmpd.conf"
# net-snmp's rc script; "snmpd" is OpenBSD's own daemon.
SNMPD_START = "rcctl enable netsnmpd && rcctl restart netsnmpd"
def _parse_services(self, output: str) -> list[dict]:
return parse.rcctl_check(output)
def get_services(self) -> list[dict]:
return self._parse_services(self._out(self.SERVICE_STATUS_COMMAND, timeout=120))
def get_packages(self) -> list[dict]:
return parse.pkg_info(self._out("pkg_info"))
def get_available_updates(self) -> list[dict]:
"""Package updates from ``pkg_add -u -n -v``, and the base system's syspatches.
syspatch applies its patches together and in order, so they are one
entry (#799). Neither tool says which update is a security fix.
"""
updates: list[dict] = [
{**candidate, "origin": None, "security": None}
for candidate in parse.pkg_add_candidates(
self._read("pkg_add -u -n -v", privileged=True, timeout=300).stdout
)
]
patches = parse.syspatch(self._read("syspatch -c", privileged=True, timeout=120).stdout)
if patches:
installed = parse.syspatch(self._out("syspatch -l", privileged=True))
summary = (
", ".join(patches)
if len(patches) <= 3
else f"{len(patches)} patches: {patches[0]} … {patches[-1]}"
)
updates.append(
{
"name": BASE_SYSTEM,
"current_version": installed[-1] if installed else self._out("uname -r"),
"new_version": summary,
"origin": "syspatch",
"security": None,
}
)
return updates
def _hardware(self) -> tuple[str, str, str]:
hw = self._platform()
return hw.get("vendor", ""), hw.get("product", ""), hw.get("serialno", "")