feat(scheduled-jobs): ScheduledJobsMixin and its POSIX implementation (#86)
driver / standard (pull_request) Successful in 20s
driver / lint (pull_request) Successful in 23s
driver / types (pull_request) Successful in 1m16s
driver / test (3.13) (pull_request) Successful in 1m18s
driver / test (3.12) (pull_request) Successful in 1m27s
driver / lowest (pull_request) Successful in 46s
driver / build (pull_request) Successful in 43s
driver / security (pull_request) Successful in 1m27s
CI / driver (pull_request) Successful in 3m11s
driver / publish (pull_request) Skipped

What a host runs on its own schedule as one shape: cron lines, run-parts
scripts, anacron, systemd timers, periodic, a platform's own jobs, Windows
tasks. Every job says who owns its definition and what the driver can do
to it; every write names the digest the caller saw and is refused when the
host's definition changed since.

PosixScheduledJobsMixin reads everything in one framed report as root,
switches a cron line behind #napalm-disabled:, a timer by systemctl and a
run-parts script by its execute bit, runs a job now bounded by timeout, and
writes an application's job as a timer where systemd runs, else in
/etc/cron.d or the user's crontab. napalm_device_types.cron parses cron
expressions, gives their next runs and writes them as OnCalendar lines.

get_cron_jobs and CronJobDict go: nothing called them.

Version 4.26.0, so drivers can require it before the release is tagged.

Closes #86
This commit is contained in:
christianmanivong committed 2026-11-03 05:29:27 +01:00
1 parent fdf2f89115
commit f573fa53d2
16 files changed
+2440 -44

No files matched your search

+22
View File
@@ -58,6 +58,8 @@ instead of being restated on every role that happens to need it:
* :class:`~napalm_device_types.service_clusters.ServiceClustersMixin`
* :class:`~napalm_device_types.service_packages.ServicePackagesMixin`
* :class:`~napalm_device_types.sudo_history.SudoHistoryMixin`
* :class:`~napalm_device_types.scheduled_jobs.ScheduledJobsMixin`
* :class:`~napalm_device_types.scheduled_jobs_posix.PosixScheduledJobsMixin`
* :class:`~napalm_device_types.services.ServiceControlMixin`
* :class:`~napalm_device_types.systemd.SystemdServicesMixin`
* :class:`~napalm_device_types.updates.UpdateMixin`
@@ -205,6 +207,17 @@ from napalm_device_types.ping_sweep import PingSweepMixin, driver_supports_ping
from napalm_device_types.provisioning import VMProvisionIncomplete
from napalm_device_types.residential_gateway import ResidentialGatewayDriver
from napalm_device_types.roles import primary_role_of, role_keys_of, roles_of
from napalm_device_types.scheduled_jobs import (
ScheduledJobChanged,
ScheduledJobDict,
ScheduledJobRefused,
ScheduledJobResultDict,
ScheduledJobsMixin,
ScheduledJobSpecDict,
ScheduledJobsReadingDict,
job_digest,
)
from napalm_device_types.scheduled_jobs_posix import PosixScheduledJobsMixin
from napalm_device_types.service_clusters import (
SERVICE_CLUSTERS_COMMAND,
ServiceClustersMixin,
@@ -400,6 +413,15 @@ __all__ = [
"PortSpec",
"ResidentialGatewayDriver",
"ServiceControlMixin",
"PosixScheduledJobsMixin",
"ScheduledJobChanged",
"ScheduledJobDict",
"ScheduledJobRefused",
"ScheduledJobResultDict",
"ScheduledJobSpecDict",
"ScheduledJobsMixin",
"ScheduledJobsReadingDict",
"job_digest",
"StorageDriver",
"SwitchDriver",
"SYSTEMD_SERVICES_COMMAND",
+222
View File
@@ -0,0 +1,222 @@
"""Crontabs and anacrontabs as text: the jobs in them, and changing one line of them.
Pure: no I/O. :mod:`napalm_device_types.scheduled_jobs_posix` reads the files and
writes them back; this module says what is in them and what the new text is.
**A line's key survives switching it off.** A job switched off is the same line
behind the marker :data:`DISABLED_MARKER`; the key is built from the line without
it, so the job keeps its key and the marker is all that changes. Two equal lines
in one file get ``:2``, ``:3`` after the first.
**A job an application wrote** is the line after ``# napalm-scheduled-job: <name>``
and is keyed ``job:<name>``, whatever its schedule.
**Everything else is left as it was**: comments, blank lines, variables, lines
cron would not take.
"""
from __future__ import annotations
import hashlib
import re
from dataclasses import dataclass, field
from shlex import quote
DISABLED_MARKER = "#napalm-disabled: "
JOB_MARKER = "# napalm-scheduled-job: "
_VARIABLE = re.compile(r"^([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)$")
_ANACRON_PERIOD = re.compile(r"^(\d+|@(daily|weekly|monthly|yearly|annually))$")
@dataclass(frozen=True)
class CronLine:
"""One job in a crontab: where it is, what it says, and what was set before it."""
index: int # the line's number in the text, from 0
key: str
schedule: str
run_as: str
command: str
enabled: bool
env: dict[str, str] = field(default_factory=dict)
name: str = "" # the name an application gave it
description: str = ""
def _unquote(value: str) -> str:
value = value.strip()
if len(value) >= 2 and value[0] == value[-1] and value[0] in "'\"":
return value[1:-1]
return value
def _digest(text: str) -> str:
return hashlib.sha256(" ".join(text.split()).encode()).hexdigest()[:8]
def _split(content: str, *, system: bool, user: str) -> tuple[str, str, str] | None:
"""(schedule, run_as, command) of a cron line; None for one cron would not take."""
fields = 1 if content.startswith("@") else 5
owner = 1 if system else 0
parts = content.split(None, fields + owner)
if len(parts) < fields + owner + 1:
return None
schedule = " ".join(parts[:fields])
run_as = parts[fields] if system else user
return schedule, run_as, parts[-1]
def _anacron_split(content: str) -> tuple[str, str, str] | None:
"""(period, job identifier, command) of an anacrontab line."""
parts = content.split(None, 3)
if len(parts) < 4 or not _ANACRON_PERIOD.match(parts[0]) or not parts[1].isdigit():
return None
return parts[0], parts[2], parts[3]
@dataclass
class _Scan:
"""What is set while a text is read line by line."""
env: dict[str, str] = field(default_factory=dict)
seen: dict[str, int] = field(default_factory=dict)
name: str = ""
description: str = ""
def key(self, base: str) -> str:
n = self.seen.get(base, 0) + 1
self.seen[base] = n
return base if n == 1 else f"{base}:{n}"
def take_name(self) -> tuple[str, str]:
named = self.name, self.description
self.name, self.description = "", ""
return named
def _content(raw: str) -> tuple[str, bool] | None:
"""The job text of *raw* and whether it is on; None for a comment or a blank."""
line = raw.strip()
if line.startswith(DISABLED_MARKER.strip()):
return line[len(DISABLED_MARKER.strip()) :].strip(), False
if not line or line.startswith("#"):
return None
return line, True
def _remember(scan: _Scan, raw: str) -> bool:
"""Take a variable or an application's marker from *raw*; whether it was one."""
line = raw.strip()
if line.startswith(JOB_MARKER.strip()):
name, _, description = line[len(JOB_MARKER.strip()) :].strip().partition(" ")
scan.name, scan.description = name, description.strip()
return True
variable = _VARIABLE.match(line)
if variable:
scan.env[variable[1]] = _unquote(variable[2])
return True
return False
def parse_crontab(path: str, text: str, *, system: bool, user: str = "") -> list[CronLine]:
"""The jobs of the crontab *text* at *path*.
*system*: a sixth field names the user (``/etc/crontab``, ``/etc/cron.d``);
otherwise every job runs as *user*.
"""
scan, jobs = _Scan(), []
for index, raw in enumerate(text.splitlines()):
if _remember(scan, raw):
continue
found = _content(raw)
split = _split(found[0], system=system, user=user) if found else None
if found is None or split is None:
continue
content, enabled = found
name, description = scan.take_name()
key = f"job:{name}" if name else scan.key(f"cron:{path}:{_digest(content)}")
schedule, run_as, command = split
jobs.append(
CronLine(
index, key, schedule, run_as, command, enabled, dict(scan.env), name, description
)
)
return jobs
def parse_anacrontab(text: str) -> list[CronLine]:
"""The jobs of an anacrontab: period, delay, identifier, command; all run as root."""
scan, jobs = _Scan(), []
for index, raw in enumerate(text.splitlines()):
if _remember(scan, raw):
continue
found = _content(raw)
split = _anacron_split(found[0]) if found else None
if found is None or split is None:
continue
period, identifier, command = split
key = scan.key(f"anacron:{identifier}")
jobs.append(CronLine(index, key, period, "root", command, found[1], dict(scan.env)))
return jobs
def _replace(text: str, index: int, lines: list[str]) -> str:
rows = text.splitlines()
rows[index : index + 1] = lines
return "\n".join(rows) + "\n"
def switched(text: str, line: CronLine, *, enabled: bool) -> str:
"""*text* with *line* switched on or off: the marker added or taken away, nothing else."""
raw = text.splitlines()[line.index]
found = _content(raw)
if found is None: # pragma: no cover - a CronLine always points at a job
raise ValueError(f"line {line.index} is no job")
content = found[0]
return _replace(text, line.index, [content if enabled else DISABLED_MARKER + content])
def marked_lines(name: str, description: str, line: str, *, enabled: bool) -> list[str]:
"""An application's job as crontab lines: its marker, then the job, maybe switched off."""
marker = f"{JOB_MARKER}{name} {description}".rstrip()
return [marker, line if enabled else DISABLED_MARKER + line]
def with_job(text: str, jobs: list[CronLine], name: str, lines: list[str]) -> str:
"""*text* with the application's job *name* as *lines*: replaced, or added at the end."""
current = next((j for j in jobs if j.name == name), None)
if current is None:
body = text if not text or text.endswith("\n") else text + "\n"
return body + "\n".join(lines) + "\n"
rows = text.splitlines()
rows[current.index - 1 : current.index + 1] = lines
return "\n".join(rows) + "\n"
def without_job(text: str, jobs: list[CronLine], name: str) -> str:
"""*text* without the application's job *name* and its marker."""
current = next(j for j in jobs if j.name == name)
rows = text.splitlines()
del rows[current.index - 1 : current.index + 1]
return "\n".join(rows) + ("\n" if rows else "")
def _unescape(part: str) -> str:
return part.replace("\\%", "%")
def run_script(command: str, env: dict[str, str]) -> str:
"""*command* as ``sh -c`` takes it, the way cron would run it.
The crontab's variables are exported first. The first ``%`` that is not
escaped ends the command; what follows is its stdin, with every further
``%`` a newline, and ``\\%`` is a ``%``.
"""
parts = re.split(r"(?<!\\)%", command)
exports = "".join(f"export {k}={quote(v)}; " for k, v in env.items() if k != "MAILTO")
head = _unescape(parts[0])
if len(parts) == 1:
return exports + head
stdin = "\n".join(_unescape(p) for p in parts[1:])
return f"{exports}printf '%s\\n' {quote(stdin)} | {{ {head}\n}}"
+404
View File
@@ -0,0 +1,404 @@
"""The scheduled-jobs report of a POSIX host: the command that prints it, and its parse.
:mod:`napalm_device_types.scheduled_jobs_posix` sends :func:`reading_command` and
hands what came back to :func:`parse_reading`. Pure: no I/O.
**One framed report, as root.** Every crontab, cron.d file, anacrontab and job
script is printed behind a header line and with ``|`` before each of its lines,
so no line of a file can pass for a header; run-parts scripts by their execute
bit and checksum; systemd's timers and the units they start by ``systemctl show``;
then the time zone and which package owns each definition file. A report whose
end marker is missing raises: a list cut short must never read as jobs that went
away.
"""
from __future__ import annotations
import re
from dataclasses import dataclass, field
from datetime import UTC, datetime
from shlex import quote
from napalm_device_types._crontab import JOB_MARKER, CronLine, parse_anacrontab, parse_crontab
from napalm_device_types.scheduled_jobs import DELETE, EDIT, job_digest
from napalm_device_types.terminal import strip_terminal_codes
_BEGIN = "SJ_BEGIN"
_END = "SJ_END"
SCHEDULE_HEADER = "# schedule: "
TIMER_PROPERTIES = (
"Id,Unit,Description,TimersCalendar,TimersMonotonic,NextElapseUSecRealtime,"
"LastTriggerUSec,UnitFileState,ActiveState,FragmentPath"
)
UNIT_PROPERTIES = "Id,ExecStart,User,Result,FragmentPath"
#: The names run-parts runs (Debian's ``--lsbsysinit``-less default).
_RUN_PARTS_NAME = re.compile(r"^[A-Za-z0-9_-]+$")
_SECTION = re.compile(r"^\[([a-z-]+)(?: (.*))?\]$")
_OWNERS_SH = (
"if command -v dpkg >/dev/null 2>&1; then xargs dpkg -S 2>/dev/null; "
"elif command -v rpm >/dev/null 2>&1; then while read -r p; do "
'o=$(rpm -qf --qf \'%{NAME}\' -- "$p" 2>/dev/null) && printf \'%s: %s\\n\' "$o" "$p"; done; '
"elif command -v apk >/dev/null 2>&1; then while read -r p; do "
'o=$(apk info -qW -- "$p" 2>/dev/null) && [ -n "$o" ] && printf \'%s: %s\\n\' "$o" "$p"; done; '
"else cat >/dev/null; fi"
)
@dataclass(frozen=True)
class Paths:
"""Where a host keeps its scheduled jobs."""
system_files: tuple[str, ...]
cron_d_dirs: tuple[str, ...]
spool_dirs: tuple[str, ...]
run_parts: tuple[tuple[str, str], ...] # (period, directory)
anacrontab: str
script_dir: str
systemd_marker: str = "/run/systemd/system"
owners_sh: str = _OWNERS_SH
@dataclass
class Reading:
"""What one report said, beyond the jobs: what writes need to know."""
jobs: list[dict[str, object]] = field(default_factory=list)
time_zone: str | None = None
systemd: bool = False
#: per key: the unit a timer starts, the crontab line a cron job is
units: dict[str, str] = field(default_factory=dict)
lines: dict[str, CronLine] = field(default_factory=dict)
def _q(path: str) -> str:
return quote(path)
def _files(section: str, pattern: str) -> str:
return (
f'for f in {pattern}; do [ -f "$f" ] || continue; printf \'[%s %s]\\n\' {section} "$f"; '
'sed \'s/^/|/\' "$f"; P="$P\n$f"; done; '
)
def _run_parts(period: str, directory: str) -> str:
return (
f'for f in {_q(directory)}/*; do [ -f "$f" ] || continue; '
'[ -x "$f" ] && x=1 || x=0; c=$(cksum < "$f" | cut -d" " -f1); '
f'printf \'[run-parts %s %s %s %s]\\n\' {period} "$x" "$c" "$f"; P="$P\n$f"; done; '
)
def _timers(paths: Paths) -> str:
show = f"systemctl show --no-pager -p {TIMER_PROPERTIES}"
units = f"systemctl show --no-pager -p {UNIT_PROPERTIES}"
return (
f"if [ -d {_q(paths.systemd_marker)} ]; then echo '[systemd]'; "
"t=$(systemctl list-unit-files --type=timer --no-legend --no-pager 2>/dev/null "
"| awk '$1 !~ /@\\./ {print $1}'); "
'if [ -n "$t" ]; then '
f"s=$({show} --timestamp=unix $t 2>/dev/null || {show} $t 2>/dev/null); "
"echo '[timers]'; printf '%s\\n' \"$s\" | sed 's/^/|/'; "
"u=$(printf '%s\\n' \"$s\" | sed -n 's/^Unit=//p'); "
f"echo '[units]'; {units} $u 2>/dev/null | sed 's/^/|/'; "
"P=\"$P\n$(printf '%s\\n' \"$s\" | sed -n 's/^FragmentPath=//p')\"; fi; fi; "
)
def reading_command(paths: Paths) -> str:
"""One POSIX ``sh`` script that prints every scheduled job of the host; run it as root."""
parts = ["printf '%s%s\\n' SJ_ BEGIN; P=''; "]
parts += [_files("system", _q(f)) for f in paths.system_files]
parts += [_files("system", f"{_q(d)}/*") for d in paths.cron_d_dirs]
parts += [_files("user", f"{_q(d)}/*") for d in paths.spool_dirs]
parts.append(_files("anacron", _q(paths.anacrontab)))
parts += [_run_parts(period, d) for period, d in paths.run_parts]
parts.append(_files("script", f"{_q(paths.script_dir)}/*"))
parts.append(_timers(paths))
parts.append(
"echo '[tz]'; cat /etc/timezone 2>/dev/null || readlink /etc/localtime 2>/dev/null; "
f"echo '[owners]'; printf '%s\\n' \"$P\" | sed '/^$/d' | {{ {paths.owners_sh}; }}; "
"printf '%s%s\\n' SJ_ END"
)
return "".join(parts)
# -- parse ------------------------------------------------------------------------------------
@dataclass
class _Section:
name: str
args: str
lines: list[str] = field(default_factory=list)
def _sections(output: str) -> list[_Section]:
text = strip_terminal_codes(output)
begin, end = text.rfind(_BEGIN), text.rfind(_END)
if begin < 0 or end < begin:
raise ValueError("the scheduled-jobs report is cut short (no end marker)")
sections: list[_Section] = []
for line in text[begin + len(_BEGIN) : end].splitlines():
header = _SECTION.match(line)
if header:
sections.append(_Section(header[1], header[2] or ""))
elif sections:
sections[-1].lines.append(line[1:] if line.startswith("|") else line)
return sections
def _owned(lines: list[str]) -> set[str]:
owned: set[str] = set()
for line in lines:
package, sep, path = line.partition(": ")
if sep and path.startswith("/") and not package.startswith("diversion"):
owned.add(path.strip())
return owned
def _time_zone(lines: list[str]) -> str | None:
value = next((ln.strip() for ln in lines if ln.strip()), "")
if "zoneinfo/" in value:
value = value.split("zoneinfo/", 1)[1]
return value or None
def _blocks(lines: list[str]) -> list[dict[str, list[str]]]:
blocks: list[dict[str, list[str]]] = [{}]
for line in lines:
if not line.strip():
blocks.append({})
continue
key, sep, value = line.partition("=")
if sep:
blocks[-1].setdefault(key, []).append(value)
return [b for b in blocks if b.get("Id")]
def _one(block: dict[str, list[str]], key: str) -> str:
return (block.get(key) or [""])[0].strip()
def _timestamp(value: str) -> str | None:
"""systemd's ``@<seconds>`` (or a UTC time) as ISO 8601; None when it never was."""
value = value.strip()
if value.startswith("@") and value[1:].isdigit() and int(value[1:]) > 0:
return datetime.fromtimestamp(int(value[1:]), UTC).isoformat()
match = re.search(r"(\d{4}-\d\d-\d\d \d\d:\d\d:\d\d) UTC$", value)
if match:
return datetime.strptime(match[1], "%Y-%m-%d %H:%M:%S").replace(tzinfo=UTC).isoformat()
return None
def _timer_entries(block: dict[str, list[str]], key: str) -> list[str]:
"""``{ OnCalendar=daily ; next_elapse=… }`` → ``OnCalendar=daily``, one per entry."""
return [v.strip(" {}").split(" ; ")[0].strip() for v in block.get(key, []) if v.strip()]
def _argv(exec_start: list[str]) -> str:
commands = []
for value in exec_start:
match = re.search(r"argv\[\]=(.*?) ; ", value)
if match:
commands.append(match[1].strip())
return "; ".join(commands)
@dataclass
class _Script:
name: str
description: str
schedule: str
command: str
def _script(path: str, lines: list[str]) -> _Script | None:
"""An application's job script: its marker, schedule and command; None for any other file."""
marker = next((ln for ln in lines if ln.startswith(JOB_MARKER)), None)
schedule = next((ln for ln in lines if ln.startswith(SCHEDULE_HEADER)), None)
if marker is None or schedule is None:
return None
name, _, description = marker[len(JOB_MARKER) :].partition(" ")
body = [ln for ln in lines if ln not in (marker, schedule) and not ln.startswith("#!")]
return _Script(
name, description.strip(), schedule[len(SCHEDULE_HEADER) :], "\n".join(body).strip()
)
def _job(
*,
key: str,
kind: str,
source: str,
run_as: str,
schedule: str,
schedule_kind: str,
command: str,
enabled: bool,
managed_by: str,
actions: list[str],
content: str = "",
**extra: object,
) -> dict[str, object]:
job: dict[str, object] = {
"key": key,
"kind": kind,
"source": source,
"run_as": run_as,
"schedule": schedule,
"schedule_kind": schedule_kind,
"command": command,
"enabled": enabled,
"managed_by": managed_by,
"actions": actions,
}
job["digest"] = job_digest(job, content=content)
job.update({k: v for k, v in extra.items() if v not in (None, "")})
return job
_SWITCH = ["enable", "disable", "run"]
_OWN = [*_SWITCH, EDIT, DELETE]
def _cron_jobs(section: _Section, owned: set[str], reading: Reading) -> None:
path = section.args
text = "\n".join(section.lines)
if section.name == "anacron":
found = parse_anacrontab(text)
else:
user = path.rsplit("/", 1)[-1]
found = parse_crontab(path, text, system=section.name == "system", user=user)
kind = "anacron" if section.name == "anacron" else "cron"
for line in found:
own = bool(line.name)
reading.lines[line.key] = line
reading.jobs.append(
_job(
key=line.key,
kind=kind,
source=path,
run_as=line.run_as,
schedule=line.schedule,
schedule_kind="period" if kind == "anacron" else "cron",
command=line.command,
enabled=line.enabled,
managed_by="application" if own else ("package" if path in owned else "admin"),
actions=_OWN if own else _SWITCH,
name=line.name,
description=line.description,
)
)
def _run_parts_job(section: _Section, owned: set[str], reading: Reading) -> None:
period, executable, checksum, path = section.args.split(" ", 3)
if not _RUN_PARTS_NAME.match(path.rsplit("/", 1)[-1]):
return
reading.jobs.append(
_job(
key=f"run-parts:{path}",
kind="run_parts",
source=path,
run_as="root",
schedule=period,
schedule_kind="period",
command=path,
enabled=executable == "1",
managed_by="package" if path in owned else "admin",
actions=_SWITCH,
content=checksum,
)
)
def _timer_job(
timer: dict[str, list[str]],
unit: dict[str, list[str]],
scripts: dict[str, _Script],
owned: set[str],
) -> dict[str, object]:
fragment = _one(timer, "FragmentPath")
calendar = _timer_entries(timer, "TimersCalendar")
schedule = "; ".join(calendar + _timer_entries(timer, "TimersMonotonic"))
command = _argv(unit.get("ExecStart", []))
script = scripts.get(command.split(" ", 1)[0])
last_run = _timestamp(_one(timer, "LastTriggerUSec"))
switchable = _one(timer, "UnitFileState") not in ("static", "generated", "transient")
common = {
"kind": "systemd_timer",
"source": fragment,
"run_as": _one(unit, "User") or "root",
"enabled": _one(timer, "ActiveState") == "active",
"last_run": last_run,
"last_result": _one(unit, "Result") or None if last_run else None,
"next_run": _timestamp(_one(timer, "NextElapseUSecRealtime")),
}
if script is not None:
return _job(
key=f"job:{script.name}",
schedule=script.schedule,
schedule_kind="cron",
command=script.command,
managed_by="application",
actions=_OWN,
name=script.name,
description=script.description,
**common, # type: ignore[arg-type]
)
packaged = fragment in owned or fragment.startswith(("/usr/lib/", "/lib/"))
return _job(
key=f"timer:{_one(timer, 'Id')}",
schedule=schedule,
schedule_kind="calendar" if calendar else "monotonic",
command=command,
managed_by="package" if packaged else "admin",
actions=_SWITCH if switchable else ["run"],
description=_one(timer, "Description"),
**common, # type: ignore[arg-type]
)
def _timer_jobs(
sections: dict[str, _Section], scripts: dict[str, _Script], owned: set[str], reading: Reading
) -> None:
timers = _blocks(sections["timers"].lines) if "timers" in sections else []
units = (
{_one(b, "Id"): b for b in _blocks(sections["units"].lines)} if "units" in sections else {}
)
for timer in timers:
unit = units.get(_one(timer, "Unit"), {})
job = _timer_job(timer, unit, scripts, owned)
reading.units[str(job["key"])] = _one(timer, "Unit")
reading.jobs.append(job)
def parse_reading(output: str) -> Reading:
"""The jobs, time zone and write context of a report from :func:`reading_command`.
:raises ValueError: when the report is cut short.
"""
sections = _sections(output)
named = {s.name: s for s in sections if s.name in ("timers", "units", "tz", "owners")}
owned = _owned(named["owners"].lines) if "owners" in named else set()
reading = Reading(
time_zone=_time_zone(named["tz"].lines) if "tz" in named else None,
systemd=any(s.name == "systemd" for s in sections),
)
for section in sections:
if section.name in ("system", "user", "anacron"):
_cron_jobs(section, owned, reading)
elif section.name == "run-parts":
_run_parts_job(section, owned, reading)
_timer_jobs(named, _scripts(sections), owned, reading)
return reading
def _scripts(sections: list[_Section]) -> dict[str, _Script]:
"""An application's job scripts, by path."""
scripts = ((s.args, _script(s.args, s.lines)) for s in sections if s.name == "script")
return {path: script for path, script in scripts if script is not None}
+203
View File
@@ -0,0 +1,203 @@
"""Cron expressions: what they mean, when they fire next, and as systemd ``OnCalendar``.
A scheduled job is given as a cron expression on every host, and a host with
systemd runs it as a timer. So the expression has to mean the same in three
places -- on the host's cron, in a preview of the next runs, and in the
``OnCalendar`` lines a timer is written with -- and the meaning lives here once.
**What cron means is Vixie cron's.** Five fields (minute, hour, day of month,
month, day of week), each ``*``, a value, a range ``a-b``, a step ``*/n`` or
``a-b/n``, or a list of those; month and weekday names in any case; ``7`` is
Sunday as well as ``0``; the ``@hourly`` … ``@yearly`` aliases. When both day
fields are restricted -- neither starts with ``*`` -- a day matches if *either*
does; otherwise both must (``*/2`` counts as starting with ``*``).
**``OnCalendar`` by expansion.** Every field is written as its values, so a range
with a step needs no systemd syntax of its own, and a timer fires on exactly the
minutes cron would. The *either* rule is two ``OnCalendar`` lines, which systemd
ORs. ``@reboot`` has no calendar; a timer takes it as ``OnBootSec``.
**Next runs are in the host's time zone.** A local time the clock skips when
summer time starts is left out; cron implementations disagree on it, and a
preview that promised it would promise too much.
"""
from __future__ import annotations
from datetime import date, datetime, timedelta
from typing import NamedTuple
from zoneinfo import ZoneInfo
REBOOT = "@reboot"
ALIASES = {
"@yearly": "0 0 1 1 *",
"@annually": "0 0 1 1 *",
"@monthly": "0 0 1 * *",
"@weekly": "0 0 * * 0",
"@daily": "0 0 * * *",
"@midnight": "0 0 * * *",
"@hourly": "0 * * * *",
}
_MONTHS = ("jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec")
_WEEKDAYS = ("sun", "mon", "tue", "wed", "thu", "fri", "sat")
_SYSTEMD_WEEKDAYS = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")
#: (lowest, highest, names) per field; a name stands for its index plus the lowest.
_FIELDS = (
(0, 59, ()),
(0, 23, ()),
(1, 31, ()),
(1, 12, _MONTHS),
(0, 7, _WEEKDAYS),
)
#: How far ahead a next run is looked for: a 29 February comes within eight years.
_HORIZON_DAYS = 8 * 366
class CronSchedule(NamedTuple):
"""An expression's values per field, and whether a day matches on either day field."""
minutes: frozenset[int]
hours: frozenset[int]
days: frozenset[int]
months: frozenset[int]
weekdays: frozenset[int] # 0 = Sunday
either_day: bool
def is_reboot(expr: str) -> bool:
"""Whether *expr* is ``@reboot``: once at boot, no calendar."""
return expr.strip().lower() == REBOOT
def _value(token: str, low: int, names: tuple[str, ...]) -> int:
if token.isdigit():
return int(token)
if token.lower() in names:
return names.index(token.lower()) + low
raise ValueError(f"not a value: {token!r}")
def _part(part: str, low: int, high: int, names: tuple[str, ...]) -> set[int]:
span, _, step_text = part.partition("/")
step = 1
if step_text:
if not step_text.isdigit() or int(step_text) == 0:
raise ValueError(f"not a step: {part!r}")
step = int(step_text)
if span == "*":
first, last = low, high
elif "-" in span:
a, _, b = span.partition("-")
first, last = _value(a, low, names), _value(b, low, names)
else:
first = _value(span, low, names)
last = high if step_text else first
if not low <= first <= last <= high:
raise ValueError(f"out of range {low}-{high}: {part!r}")
return set(range(first, last + 1, step))
def _field(text: str, low: int, high: int, names: tuple[str, ...]) -> frozenset[int]:
values: set[int] = set()
for part in text.split(","):
if not part:
raise ValueError(f"empty list item in {text!r}")
values |= _part(part, low, high, names)
return frozenset(values)
def parse_cron(expr: str) -> CronSchedule:
"""The values *expr* stands for.
:raises ValueError: for anything Vixie cron would not take, and for
``@reboot``, which names no time.
"""
text = expr.strip()
if is_reboot(text):
raise ValueError("@reboot names no time; it runs once at boot")
if text.startswith("@"):
if text.lower() not in ALIASES:
raise ValueError(f"unknown alias {text!r}")
text = ALIASES[text.lower()]
fields = text.split()
if len(fields) != len(_FIELDS):
raise ValueError(f"a cron expression has five fields, not {len(fields)}: {expr!r}")
minutes, hours, days, months, weekdays = (
_field(f, low, high, names) for f, (low, high, names) in zip(fields, _FIELDS, strict=True)
)
if 7 in weekdays:
weekdays = (weekdays - {7}) | {0}
either = not fields[2].startswith("*") and not fields[4].startswith("*")
return CronSchedule(minutes, hours, days, months, frozenset(weekdays), either)
def _day_matches(s: CronSchedule, day: date) -> bool:
if day.month not in s.months:
return False
on_day = day.day in s.days
on_weekday = (day.isoweekday() % 7) in s.weekdays
return (on_day or on_weekday) if s.either_day else (on_day and on_weekday)
def _local(day: date, hour: int, minute: int, tz: ZoneInfo) -> datetime | None:
"""*day* at *hour*:*minute* in *tz*; None when the clock skips that time."""
naive = datetime(day.year, day.month, day.day, hour, minute)
aware = naive.replace(tzinfo=tz)
if aware.astimezone(ZoneInfo("UTC")).astimezone(tz).replace(tzinfo=None) != naive:
return None
return aware
def next_runs(expr: str, after: datetime, tz: ZoneInfo, count: int = 5) -> list[datetime]:
"""The next *count* times *expr* fires after *after*, in *tz*.
``@reboot`` and a day that never comes (31 February) give none.
:raises ValueError: for an expression :func:`parse_cron` refuses.
"""
if is_reboot(expr):
return []
s = parse_cron(expr)
start = after.astimezone(tz)
runs: list[datetime] = []
for offset in range(_HORIZON_DAYS):
day = start.date() + timedelta(days=offset)
if not _day_matches(s, day):
continue
for hour in sorted(s.hours):
for minute in sorted(s.minutes):
at = _local(day, hour, minute, tz)
if at is None or at <= after:
continue
runs.append(at)
if len(runs) == count:
return runs
return runs
def _values(values: frozenset[int], low: int, high: int) -> str:
if values == frozenset(range(low, high + 1)):
return "*"
return ",".join(f"{v:02d}" for v in sorted(values))
def to_on_calendar(expr: str) -> list[str]:
"""``OnCalendar`` lines that fire exactly when *expr* does.
:raises ValueError: for an expression :func:`parse_cron` refuses, ``@reboot``
among them -- a timer takes that as ``OnBootSec``.
"""
s = parse_cron(expr)
time = f"{_values(s.hours, 0, 23)}:{_values(s.minutes, 0, 59)}:00"
months = _values(s.months, 1, 12)
days = _values(s.days, 1, 31)
weekdays = ",".join(_SYSTEMD_WEEKDAYS[d] for d in (1, 2, 3, 4, 5, 6, 0) if d in s.weekdays)
every_weekday = s.weekdays == frozenset(range(7))
if s.either_day:
return [f"*-{months}-{days} {time}", f"{weekdays} *-{months}-* {time}"]
prefix = "" if every_weekday else f"{weekdays} "
return [f"{prefix}*-{months}-{days} {time}"]
-9
View File
@@ -1038,15 +1038,6 @@ class ProcessDict(TypedDict):
command: str # full command line
class CronJobDict(TypedDict):
"""A scheduled cron task."""
user: str # owner of the crontab entry
schedule: str # five-field cron expression, e.g. "0 * * * *"
command: str # shell command to execute
description: NotRequired[str] # optional inline comment
class ApplyUpdatesResultDict(TypedDict):
"""Result of an ``apply_updates()`` call."""
-29
View File
@@ -15,7 +15,6 @@ from typing import TYPE_CHECKING
from napalm_device_types.base import DeviceTypeDriver
from napalm_device_types.health_metrics import HealthMetricsMixin
from napalm_device_types.models import (
CronJobDict,
DeviceActionResultDict,
ProcessDict,
SNMPConfigDict,
@@ -131,34 +130,6 @@ class OSDriver(
"""
...
# ------------------------------------------------------------------
# Cron jobs
# ------------------------------------------------------------------
def get_cron_jobs(self) -> list[CronJobDict]:
"""
Returns scheduled cron tasks from all user crontabs and ``/etc/cron.d``.
Each entry contains:
* user (string) - owner of the crontab entry
* schedule (string) - five-field cron expression (e.g. ``"0 * * * *"``)
* command (string) - shell command to execute
* description (string, optional) - inline comment text if present
Example::
[
{
"user": "root",
"schedule": "0 4 * * *",
"command": "/usr/local/bin/backup.sh",
"description": "nightly backup",
},
]
"""
...
# ------------------------------------------------------------------
# SNMP
# ------------------------------------------------------------------
+196
View File
@@ -0,0 +1,196 @@
"""Scheduled jobs: what a host runs on its own schedule, and switching it on, off and now.
A host runs work on its own: cron lines, run-parts scripts, anacron, systemd
timers, FreeBSD's ``periodic``, a firewall's or a NAS's own scheduled jobs, the
Windows Task Scheduler. :class:`ScheduledJobsMixin` reads all of them as one
shape, :class:`~napalm_device_types.models.ScheduledJobDict`, and acts on them.
Declared under ``if TYPE_CHECKING`` like a role: nothing exists at runtime until
a driver implements it. :class:`~napalm_device_types.scheduled_jobs_posix.PosixScheduledJobsMixin`
is the implementation for a host reached through the command channel.
**Who owns a definition** is on every job (``managed_by``): ``package`` (installed
by one), ``admin`` (written on the host), ``platform`` (generated by the device's
own configuration -- OPNsense, OpenMediaVault, QTS, Windows), ``application`` (written
through :meth:`put_scheduled_job`, under the ``name`` the application gave it).
What the application makes of that is the application's.
**What may be done is on every job too** (``actions``): every job a driver can
switch takes ``enable``, ``disable`` and ``run``; only an ``application`` job takes
``edit`` and ``delete``. A driver never edits or removes a job it did not write.
**Never over a change it did not see.** Every write names the ``digest`` the caller
saw. The driver reads the job again right before it writes and raises
:class:`ScheduledJobChanged` when the definition differs.
"""
from __future__ import annotations
import hashlib
import json
import re
from typing import TYPE_CHECKING, Any, NotRequired, TypedDict
from napalm_device_types.cron import is_reboot, parse_cron
#: What :meth:`ScheduledJobsMixin.manage_scheduled_job` takes.
ACTIONS = ("enable", "disable", "run")
#: What a job's ``actions`` may list beyond :data:`ACTIONS`.
EDIT = "edit"
DELETE = "delete"
KINDS = ("cron", "run_parts", "anacron", "systemd_timer", "periodic", "task")
MANAGED_BY = ("package", "admin", "platform", "application")
#: Seconds a run now may take on the host before ``timeout`` stops it.
RUN_TIMEOUT = 300
#: A job's name becomes a file and a unit name: lower case, digits, dashes.
_NAME = re.compile(r"[a-z0-9][a-z0-9-]{0,62}")
#: A user name as useradd and the BSDs take it.
_USER = re.compile(r"[A-Za-z_][A-Za-z0-9_.-]{0,31}\$?")
#: The parts of a job that are its definition; what the host says of its runs is not.
_DEFINITION = ("kind", "source", "run_as", "schedule", "command", "enabled")
class ScheduledJobDict(TypedDict):
"""A job the host runs on its own schedule (``get_scheduled_jobs``).
A cron line, a run-parts script, an anacron job, a systemd timer, a FreeBSD
``periodic`` script, a platform's own job, a Windows task -- one shape for all
of them (``napalm_device_types.scheduled_jobs``).
"""
key: str # stable name on this host: "timer:apt-daily.timer", "cron:/etc/crontab:1a2b3c4d"
kind: str # cron | run_parts | anacron | systemd_timer | periodic | task
source: str # the file, unit file or configuration it is defined in
run_as: str # the user it runs as
schedule: str # as the host has it: "*/5 * * * *", "OnCalendar=daily", "daily"
schedule_kind: str # cron | calendar | monotonic | period | triggers
command: str # as the host has it
enabled: bool
managed_by: str # package | admin | platform | application
digest: str # job_digest(): the definition as it is now
actions: list[str] # what the driver can do to this job: enable, disable, run, edit, delete
name: NotRequired[str] # the name an application gave a job it wrote (put_scheduled_job)
description: NotRequired[str]
last_run: NotRequired[str | None] # ISO 8601, UTC; where the host knows it
last_result: NotRequired[str | None] # "success", or what the host says went wrong
next_run: NotRequired[str | None] # ISO 8601, UTC; where the host knows it
class ScheduledJobsReadingDict(TypedDict):
"""Every scheduled job of a host, and the time zone its schedules are in."""
jobs: list[ScheduledJobDict]
time_zone: str | None # IANA name ("Europe/Berlin"); None when the host does not say
class ScheduledJobSpecDict(TypedDict):
"""A job an application wants on the host (``put_scheduled_job``)."""
name: str # the job's name on the host, chosen by the application: [a-z0-9][a-z0-9-]*
schedule: str # a cron expression (napalm_device_types.cron), "@reboot" included
run_as: str
command: str # one or more lines of POSIX sh; a platform may take its own (OPNsense: configd)
enabled: bool
description: NotRequired[str]
class ScheduledJobResultDict(TypedDict):
"""What an action on a scheduled job did."""
success: bool
output: str # what the host said; for a run, what the job printed
class ScheduledJobChanged(Exception): # noqa: N818 - a state, as the callers word it
"""The job's definition differs from the digest the caller saw: nothing was written."""
class ScheduledJobRefused(ValueError):
"""The job cannot take what was asked of it, or the request itself is not valid."""
def job_digest(job: dict[str, Any], *, content: str = "") -> str:
"""The definition of *job* in 16 hex characters.
Over kind, source, run_as, schedule, command and enabled -- so enabling a job
changes it -- and *content*: what the definition points at that the job's
fields do not show, such as a run-parts script's checksum.
"""
parts = [job.get(field) for field in _DEFINITION] + [content]
return hashlib.sha256(json.dumps(parts).encode()).hexdigest()[:16]
def check_job_name(name: str) -> None:
""":raises ScheduledJobRefused: for a name a file or a unit could not carry safely."""
if not _NAME.fullmatch(name or ""):
raise ScheduledJobRefused(f"not a job name: {name!r} (a-z, 0-9 and -, at most 63)")
def check_spec(spec: ScheduledJobSpecDict) -> None:
""":raises ScheduledJobRefused: for a spec no host could be given as it is."""
check_job_name(spec["name"])
if not _USER.fullmatch(spec.get("run_as") or ""):
raise ScheduledJobRefused(f"not a user name: {spec.get('run_as')!r}")
if not (spec.get("command") or "").strip():
raise ScheduledJobRefused("a job needs a command")
if "\n" in (spec.get("description") or ""):
raise ScheduledJobRefused("a description is one line")
if not is_reboot(spec["schedule"]):
try:
parse_cron(spec["schedule"])
except ValueError as exc:
raise ScheduledJobRefused(str(exc)) from exc
class ScheduledJobsMixin:
"""Declares the scheduled-jobs contract; a driver that can implements it.
A verb a driver cannot do at all is ``None`` on the driver, as ``manage_service``
is; a verb one job cannot take is missing from that job's ``actions``.
"""
if TYPE_CHECKING: # pragma: no cover - declared for type checkers only
def get_scheduled_jobs(self) -> ScheduledJobsReadingDict:
"""Every scheduled job of the host.
:raises ValueError: when the reading came back cut short -- never a
shorter list.
"""
...
def manage_scheduled_job(
self, key: str, action: str, *, digest: str
) -> ScheduledJobResultDict:
"""Enable, disable or run the job *key* now.
:raises ScheduledJobChanged: when the job is no longer as *digest* says.
:raises ScheduledJobRefused: when the job does not take *action*.
:raises LookupError: when the host has no job *key*.
"""
...
def put_scheduled_job(
self, spec: ScheduledJobSpecDict, *, digest: str | None = None
) -> ScheduledJobResultDict:
"""Create the job ``spec["name"]`` (*digest* None) or change it.
:raises ScheduledJobChanged: when the job is no longer as *digest*
says, or when a job of that name is there although *digest* is None.
:raises ScheduledJobRefused: for a spec :func:`check_spec` refuses, or
one the host cannot take.
"""
...
def delete_scheduled_job(self, key: str, *, digest: str) -> ScheduledJobResultDict:
"""Delete the job *key*, which an application wrote.
:raises ScheduledJobChanged: when the job is no longer as *digest* says.
:raises ScheduledJobRefused: when the job is not one an application wrote.
:raises LookupError: when the host has no job *key*.
"""
...
+461
View File
@@ -0,0 +1,461 @@
"""Scheduled jobs on a POSIX host, over the command channel: cron and systemd timers.
:class:`PosixScheduledJobsMixin` implements
:class:`~napalm_device_types.scheduled_jobs.ScheduledJobsMixin` for a driver that has
:meth:`~napalm_device_types.channel.CommandChannelMixin.run_command`. The driver
mixes it in and sets the paths its host keeps jobs in; the defaults are Debian's.
**Reading** is one framed report as root (:mod:`napalm_device_types._scheduled_reading`).
**Switching** a cron or anacron line comments it out behind ``#napalm-disabled: ``
and takes exactly that marker away again; a run-parts script loses or gets its
execute bit; a timer is ``systemctl disable --now`` / ``enable --now``. A timer that
cannot be switched (``static``) only runs.
**Writing** stages the new text as the login user and installs it as root with one
fixed command: ``crontab -u <user> <file>`` for a user's crontab, ``install`` with
the file's own mode and owner for a system file. Root never reads stdin. Before any
write the job is read again and compared with the digest the caller saw.
**An application's own job** (:meth:`put_scheduled_job`) is a timer on a host with
systemd -- ``<name>.timer`` and ``<name>.service`` in :attr:`UNIT_DIR`, the command
in :attr:`JOB_SCRIPT_DIR` ``/<name>`` behind a marker and its cron expression --
else a file in :attr:`OWN_CRON_D_DIR`, else marked lines in the user's crontab.
**Running now** is bounded by :data:`~napalm_device_types.scheduled_jobs.RUN_TIMEOUT`:
a timer's unit by ``systemctl start`` and its journal since then, a cron line's
command as its user the way cron runs it (``_crontab.run_script``).
"""
from __future__ import annotations
import shlex
from collections.abc import Sequence
from typing import TYPE_CHECKING, ClassVar
from napalm_device_types._crontab import (
JOB_MARKER,
CronLine,
marked_lines,
parse_anacrontab,
parse_crontab,
run_script,
switched,
with_job,
without_job,
)
from napalm_device_types._scheduled_reading import (
SCHEDULE_HEADER,
Paths,
Reading,
parse_reading,
reading_command,
)
from napalm_device_types.cron import is_reboot, to_on_calendar
from napalm_device_types.scheduled_jobs import (
ACTIONS,
RUN_TIMEOUT,
ScheduledJobChanged,
ScheduledJobRefused,
ScheduledJobResultDict,
ScheduledJobsMixin,
ScheduledJobSpecDict,
ScheduledJobsReadingDict,
check_spec,
)
if TYPE_CHECKING:
from napalm_device_types.channel import CommandResult
_STAGE = 't=$(mktemp) && chmod 644 "$t" && cat > "$t" && printf %s "$t"'
#: What a run keeps of the job's output.
_OUTPUT_LIMIT = 64 * 1024
_READ_TIMEOUT = 120
def _command(argv: Sequence[str]) -> str:
return shlex.join(argv)
def _said(result: CommandResult) -> str:
return "\n".join(p for p in (result.stdout.strip(), result.stderr.strip()) if p)[
-_OUTPUT_LIMIT:
]
def _result(result: CommandResult, done: str) -> ScheduledJobResultDict:
if result.exit_code != 0:
return {"success": False, "output": _said(result) or f"exit {result.exit_code}"}
return {"success": True, "output": _said(result) or done}
class PosixScheduledJobsMixin(ScheduledJobsMixin):
"""Scheduled jobs of a host reached through ``run_command``; the paths are the driver's."""
CRON_SYSTEM_FILES: ClassVar[tuple[str, ...]] = ("/etc/crontab",)
CRON_D_DIRS: ClassVar[tuple[str, ...]] = ("/etc/cron.d",)
CRON_SPOOL_DIRS: ClassVar[tuple[str, ...]] = (
"/var/spool/cron/crontabs",
"/var/spool/cron",
"/etc/crontabs",
)
RUN_PARTS_DIRS: ClassVar[tuple[tuple[str, str], ...]] = (
("hourly", "/etc/cron.hourly"),
("daily", "/etc/cron.daily"),
("weekly", "/etc/cron.weekly"),
("monthly", "/etc/cron.monthly"),
)
ANACRONTAB: ClassVar[str] = "/etc/anacrontab"
JOB_SCRIPT_DIR: ClassVar[str] = "/usr/local/libexec/scheduled-jobs"
UNIT_DIR: ClassVar[str] = "/etc/systemd/system"
#: Where a host without systemd takes an application's job as a file; None: its crontab.
OWN_CRON_D_DIR: ClassVar[str | None] = "/etc/cron.d"
ROOT_GROUP: ClassVar[str] = "root"
#: Prints "<octal mode> <owner> <group>" of the path appended.
STAT_ARGV: ClassVar[tuple[str, ...]] = ("stat", "-c", "%a %U %G", "--")
#: Run as root after a user's crontab was written; for a host that keeps it elsewhere too.
CRONTAB_AFTER_WRITE: ClassVar[tuple[str, ...] | None] = None
if TYPE_CHECKING: # pragma: no cover - declared for type checkers only
def run_command(
self,
command: str,
*,
privileged: bool = False,
timeout: float = 60,
stdin: bytes | None = None,
) -> CommandResult: ...
# -- reading ---------------------------------------------------------------------------------
def _sj_paths(self) -> Paths:
return Paths(
system_files=self.CRON_SYSTEM_FILES,
cron_d_dirs=self.CRON_D_DIRS,
spool_dirs=self.CRON_SPOOL_DIRS,
run_parts=self.RUN_PARTS_DIRS,
anacrontab=self.ANACRONTAB,
script_dir=self.JOB_SCRIPT_DIR,
)
def _sj_read(self) -> Reading:
result = self.run_command(
reading_command(self._sj_paths()), privileged=True, timeout=_READ_TIMEOUT
)
return parse_reading(result.stdout)
def get_scheduled_jobs(self) -> ScheduledJobsReadingDict:
"""Every scheduled job of the host, in one round trip.
:raises ValueError: when the report came back cut short.
"""
reading = self._sj_read()
return {"jobs": reading.jobs, "time_zone": reading.time_zone} # type: ignore[typeddict-item]
def _sj_job(self, reading: Reading, key: str, digest: str) -> dict[str, object]:
job = next((j for j in reading.jobs if j["key"] == key), None)
if job is None:
raise LookupError(f"no scheduled job {key}")
if job["digest"] != digest:
raise ScheduledJobChanged(f"{key} changed on the host since it was read")
return job
# -- commands --------------------------------------------------------------------------------
def _sj_root(self, argv: Sequence[str], timeout: float = 60) -> CommandResult:
return self.run_command(_command(argv), privileged=True, timeout=timeout)
def _sj_checked(self, argv: Sequence[str]) -> CommandResult:
result = self._sj_root(argv)
if result.exit_code != 0:
raise RuntimeError(f"{_command(argv)}: {_said(result) or result.exit_code}")
return result
def _sj_stage(self, content: str) -> str:
result = self.run_command(_STAGE, timeout=30, stdin=content.encode())
path = result.stdout.strip().splitlines()[-1:] or [""]
if result.exit_code != 0 or not path[0].startswith("/"):
raise RuntimeError(f"cannot stage a file: {_said(result)}")
return path[0]
def _sj_install(self, content: str, path: str, mode: str, owner: str, group: str) -> None:
staged = self._sj_stage(content)
try:
self._sj_checked(
["install", "-D", "-m", mode, "-o", owner, "-g", group, "--", staged, path]
)
finally:
self.run_command(_command(["rm", "-f", "--", staged]), timeout=30)
# -- crontab files ---------------------------------------------------------------------------
def _sj_spool_user(self, path: str) -> str | None:
"""The user whose crontab *path* is; None for a system file."""
directory, _, user = path.rpartition("/")
return user if directory in self.CRON_SPOOL_DIRS else None
def _sj_crontab(self, user: str) -> str:
result = self._sj_root(["crontab", "-u", user, "-l"])
if result.exit_code != 0:
if "no crontab" in (result.stderr + result.stdout).lower():
return ""
raise RuntimeError(f"cannot read the crontab of {user}: {_said(result)}")
return result.stdout
def _sj_text(self, path: str) -> str:
user = self._sj_spool_user(path)
if user is not None:
return self._sj_crontab(user)
return self._sj_checked(["cat", "--", path]).stdout
def _sj_write_crontab(self, user: str, text: str) -> None:
staged = self._sj_stage(text)
try:
self._sj_checked(["crontab", "-u", user, staged])
finally:
self.run_command(_command(["rm", "-f", "--", staged]), timeout=30)
if self.CRONTAB_AFTER_WRITE:
self._sj_checked(self.CRONTAB_AFTER_WRITE)
def _sj_write(self, path: str, text: str) -> None:
user = self._sj_spool_user(path)
if user is not None:
self._sj_write_crontab(user, text)
return
mode, owner, group = self._sj_checked([*self.STAT_ARGV, path]).stdout.split()[:3]
self._sj_install(text, path, mode, owner, group)
def _sj_line(self, job: dict[str, object], text: str) -> CronLine:
"""The job's line in *text* as it is now, read again for the write."""
path = str(job["source"])
if job["kind"] == "anacron":
found = parse_anacrontab(text)
else:
user = self._sj_spool_user(path)
found = parse_crontab(path, text, system=user is None, user=user or "")
line = next((ln for ln in found if ln.key == job["key"]), None)
if line is None:
raise ScheduledJobChanged(f"{job['key']} is no longer in {path}")
return line
# -- switching and running -------------------------------------------------------------------
def _sj_timer_unit(self, job: dict[str, object]) -> str:
key = str(job["key"])
return f"{job['name']}.timer" if key.startswith("job:") else key.removeprefix("timer:")
def _sj_switch(self, job: dict[str, object], enabled: bool) -> ScheduledJobResultDict:
word = "enabled" if enabled else "disabled"
if job["kind"] == "systemd_timer":
verb = "enable" if enabled else "disable"
return _result(
self._sj_root(["systemctl", verb, "--now", "--", self._sj_timer_unit(job)]), word
)
if job["kind"] == "run_parts":
return _result(
self._sj_root(["chmod", "+x" if enabled else "-x", "--", str(job["source"])]), word
)
path = str(job["source"])
text = self._sj_text(path)
self._sj_write(path, switched(text, self._sj_line(job, text), enabled=enabled))
return {"success": True, "output": f"{job['key']} {word}"}
def _sj_as(self, user: str, script: str) -> list[str]:
"""*script* run by ``sh`` as *user*, from root."""
if user == "root":
return ["sh", "-c", script]
return ["su", "-s", "/bin/sh", user, "-c", script]
def _sj_run(self, job: dict[str, object], reading: Reading) -> ScheduledJobResultDict:
bound = ["timeout", str(RUN_TIMEOUT)]
wait = RUN_TIMEOUT + 30
if job["kind"] == "systemd_timer":
unit = reading.units[str(job["key"])]
since = self.run_command("date +%s", timeout=30).stdout.strip()
started = self._sj_root([*bound, "systemctl", "start", "--", unit], timeout=wait)
journal = self._sj_root(
[
"journalctl",
"-u",
unit,
"--since",
f"@{since}",
"--no-pager",
"-o",
"cat",
"-n",
"200",
]
)
output = "\n".join(p for p in (_said(started), journal.stdout.strip()) if p)
return {"success": started.exit_code == 0, "output": output or f"{unit} ran"}
if job["kind"] == "run_parts":
return _result(self._sj_root([*bound, str(job["source"])], timeout=wait), "ran")
path = str(job["source"])
line = self._sj_line(job, self._sj_text(path))
script = run_script(line.command, line.env)
return _result(
self._sj_root([*bound, *self._sj_as(line.run_as, script)], timeout=wait), "ran"
)
def manage_scheduled_job(self, key: str, action: str, *, digest: str) -> ScheduledJobResultDict:
"""Enable, disable or run the job *key* now; see :class:`ScheduledJobsMixin`."""
if action not in ACTIONS:
raise ScheduledJobRefused(f"unknown action {action!r}; allowed: {list(ACTIONS)}")
reading = self._sj_read()
job = self._sj_job(reading, key, digest)
if action not in job["actions"]: # type: ignore[operator]
raise ScheduledJobRefused(f"{key} cannot be told to {action} on this host")
if action == "run":
return self._sj_run(job, reading)
return self._sj_switch(job, action == "enable")
# -- an application's own jobs ---------------------------------------------------------------
def _sj_own(
self, reading: Reading, spec: ScheduledJobSpecDict, digest: str | None
) -> dict[str, object] | None:
current = next((j for j in reading.jobs if j["key"] == f"job:{spec['name']}"), None)
if digest is None:
if current is not None:
raise ScheduledJobChanged(f"a job {spec['name']} is there already")
return None
if current is None:
raise LookupError(f"no scheduled job job:{spec['name']}")
if current["digest"] != digest:
raise ScheduledJobChanged(f"job:{spec['name']} changed on the host since it was read")
return current
def _sj_group(self, user: str) -> str:
result = self.run_command(_command(["id", "-gn", "--", user]), timeout=30)
if result.exit_code != 0:
raise ScheduledJobRefused(f"the host has no user {user}")
return result.stdout.strip()
def _sj_calendar(self, schedule: str) -> list[str]:
if is_reboot(schedule):
return ["OnBootSec=1min"]
lines = to_on_calendar(schedule)
for line in lines:
checked = self.run_command(
_command(["systemd-analyze", "calendar", "--", line]), timeout=30
)
if checked.exit_code != 0:
raise ScheduledJobRefused(f"systemd does not take {line!r}: {_said(checked)}")
return [f"OnCalendar={line}" for line in lines]
def _sj_put_timer(self, spec: ScheduledJobSpecDict) -> None:
name, description = spec["name"], spec.get("description") or spec["name"]
script = f"{self.JOB_SCRIPT_DIR}/{name}"
unit_description = description.replace("%", "%%")
timer = "\n".join(
[
"[Unit]",
f"Description={unit_description}",
"",
"[Timer]",
*self._sj_calendar(spec["schedule"]),
f"Unit={name}.service",
"",
"[Install]",
"WantedBy=timers.target",
"",
]
)
service = "\n".join(
[
"[Unit]",
f"Description={unit_description}",
"",
"[Service]",
"Type=oneshot",
f"User={spec['run_as']}",
f"ExecStart={script}",
"",
]
)
body = f"#!/bin/sh\n{JOB_MARKER}{name} {spec.get('description') or ''}".rstrip()
body += f"\n{SCHEDULE_HEADER}{spec['schedule']}\n{spec['command'].strip()}\n"
self._sj_install(body, script, "750", "root", self._sj_group(spec["run_as"]))
self._sj_install(service, f"{self.UNIT_DIR}/{name}.service", "644", "root", self.ROOT_GROUP)
self._sj_install(timer, f"{self.UNIT_DIR}/{name}.timer", "644", "root", self.ROOT_GROUP)
self._sj_checked(["systemctl", "daemon-reload"])
verb = "enable" if spec["enabled"] else "disable"
self._sj_checked(["systemctl", verb, "--now", "--", f"{name}.timer"])
def _sj_cron_text(self, spec: ScheduledJobSpecDict, *, system: bool) -> list[str]:
if "\n" in spec["command"].strip():
raise ScheduledJobRefused("a cron line holds one line of command")
owner = f" {spec['run_as']}" if system else ""
line = f"{spec['schedule']}{owner} {spec['command'].strip()}"
return marked_lines(
spec["name"], spec.get("description") or "", line, enabled=spec["enabled"]
)
def _sj_put_cron(self, spec: ScheduledJobSpecDict, current: dict[str, object] | None) -> None:
if self.OWN_CRON_D_DIR is not None:
self._sj_group(spec["run_as"])
text = "\n".join(self._sj_cron_text(spec, system=True)) + "\n"
self._sj_install(
text, f"{self.OWN_CRON_D_DIR}/{spec['name']}", "644", "root", self.ROOT_GROUP
)
return
if current is not None and current["run_as"] != spec["run_as"]:
self._sj_delete_cron(current)
user = spec["run_as"]
text = self._sj_crontab(user)
jobs = parse_crontab("", text, system=False, user=user)
self._sj_write_crontab(
user, with_job(text, jobs, spec["name"], self._sj_cron_text(spec, system=False))
)
def put_scheduled_job(
self, spec: ScheduledJobSpecDict, *, digest: str | None = None
) -> ScheduledJobResultDict:
"""Create or change the application's own job; see :class:`ScheduledJobsMixin`."""
check_spec(spec)
reading = self._sj_read()
current = self._sj_own(reading, spec, digest)
if reading.systemd:
self._sj_put_timer(spec)
else:
self._sj_put_cron(spec, current)
return {
"success": True,
"output": f"job:{spec['name']} {'changed' if current else 'created'}",
}
def _sj_delete_cron(self, job: dict[str, object]) -> None:
path = str(job["source"])
if self._sj_spool_user(path) is None:
self._sj_checked(["rm", "-f", "--", path])
return
text = self._sj_text(path)
jobs = parse_crontab(path, text, system=False, user=str(job["run_as"]))
self._sj_write(path, without_job(text, jobs, str(job["name"])))
def delete_scheduled_job(self, key: str, *, digest: str) -> ScheduledJobResultDict:
"""Delete the application's job *key*; see :class:`ScheduledJobsMixin`."""
job = self._sj_job(self._sj_read(), key, digest)
if job["managed_by"] != "application":
raise ScheduledJobRefused(
f"{key} was not written by an application; it is only switched"
)
name = str(job["name"])
if job["kind"] != "systemd_timer":
self._sj_delete_cron(job)
return {"success": True, "output": f"{key} deleted"}
self._sj_root(["systemctl", "disable", "--now", "--", f"{name}.timer"])
self._sj_checked(
[
"rm",
"-f",
"--",
f"{self.UNIT_DIR}/{name}.timer",
f"{self.UNIT_DIR}/{name}.service",
f"{self.JOB_SCRIPT_DIR}/{name}",
]
)
self._sj_checked(["systemctl", "daemon-reload"])
return {"success": True, "output": f"{key} deleted"}