feat(scheduled-jobs): scheduled jobs through PosixScheduledJobsMixin (#67)
driver / standard (pull_request) Successful in 16s
driver / lint (pull_request) Successful in 13s
driver / test (3.12) (pull_request) Failing after 51s
driver / types (pull_request) Failing after 59s
driver / test (3.13) (pull_request) Failing after 47s
driver / lowest (pull_request) Failing after 29s
driver / build (pull_request) Successful in 30s
driver / security (pull_request) Failing after 1m14s
CI / driver (pull_request) Failing after 2m29s
driver / publish (pull_request) Skipped

get_scheduled_jobs reads /etc/crontab, /etc/cron.d, the user crontabs,
run-parts, anacron and every systemd timer in one round trip as root;
manage_scheduled_job enables, disables or runs one now; put_ and
delete_scheduled_job write and remove an application's own job.

get_cron_jobs goes: it read without root, missed most of what a host
schedules, and nothing called it.

Requires napalm-device-types 4.26.0.

Closes #67
This commit is contained in:
Christian Manivong committed 2026-11-03 05:32:01 +01:00
1 parent cb0e308a78
commit ca32b89686
7 files changed
+77 -110

No files matched your search

+2 -72
View File
@@ -42,6 +42,7 @@ from napalm_device_types import (
LldpNeighborDict,
OpenSshServerMixin,
OSDriver,
PosixScheduledJobsMixin,
ServiceClustersMixin,
ServicePackagesMixin,
SnmpAgentMixin,
@@ -60,7 +61,6 @@ from napalm_device_types import (
)
from napalm_device_types.models import (
ApplyUpdatesResultDict,
CronJobDict,
FixResultDict,
FixStepOutcomeDict,
PackageDict,
@@ -310,6 +310,7 @@ class LinuxDriver(
SystemIdentityMixin,
OpenSshServerMixin,
SystemdServicesMixin,
PosixScheduledJobsMixin,
HostStatusMixin,
ContainerEngineMixin,
HostnameMixin,
@@ -1754,77 +1755,6 @@ class LinuxDriver(
)
return processes
# ------------------------------------------------------------------
# OSDriver – cron jobs
# ------------------------------------------------------------------
def get_cron_jobs(self) -> list[CronJobDict]:
"""Return cron entries from user crontabs and /etc/cron.d."""
jobs: list[CronJobDict] = []
# /etc/cron.d/* — system-wide cron fragments (include user field)
cron_d_files = self._send("ls /etc/cron.d/ 2>/dev/null").splitlines()
for fname in cron_d_files:
fname = fname.strip()
if not fname:
continue
content = self._send(f"cat /etc/cron.d/{fname} 2>/dev/null")
for line in content.splitlines():
job = self._parse_cron_line(line, source_user="root", has_user_field=True)
if job:
jobs.append(job)
# Per-user crontabs from /var/spool/cron/crontabs (Debian) or /var/spool/cron (RHEL)
for spool_dir in ("/var/spool/cron/crontabs", "/var/spool/cron"):
ls_out = self._send(f"ls {spool_dir} 2>/dev/null")
for uname in ls_out.splitlines():
uname = uname.strip()
if not uname:
continue
content = self._send(f"cat {spool_dir}/{uname} 2>/dev/null")
for line in content.splitlines():
job = self._parse_cron_line(line, source_user=uname, has_user_field=False)
if job:
jobs.append(job)
return jobs
@staticmethod
def _parse_cron_line(line: str, source_user: str, has_user_field: bool) -> CronJobDict | None:
"""Parse a single crontab line; returns ``None`` for comments/blanks."""
stripped = line.strip()
# Remove trailing comment
comment = ""
if "#" in stripped:
idx = stripped.index("#")
comment = stripped[idx + 1 :].strip()
stripped = stripped[:idx].strip()
if not stripped or stripped.startswith("@") or stripped.startswith("MAILTO"):
return None
parts = stripped.split(None, 6 if has_user_field else 5)
expected = 6 if has_user_field else 5
if len(parts) < expected:
return None
schedule = " ".join(parts[:5])
if has_user_field:
user = parts[5]
command = parts[6] if len(parts) > 6 else ""
else:
user = source_user
command = parts[5] if len(parts) > 5 else ""
job: CronJobDict = {
"user": user,
"schedule": schedule,
"command": command,
}
if comment:
job["description"] = comment
return job
# ── Device actions ────────────────────────────────────────────────────────
def get_snmp_config(self) -> SNMPConfigDict | None: