feat(scheduled-jobs): OMV's own jobs through Cron RPC, generated files platform (#18) #19
No files matched your search
@@ -7,6 +7,22 @@ All notable changes to this driver. The format follows
|
||||
## [Unreleased]
|
||||
|
||||
### Added
|
||||
- Scheduled jobs (NetOrk/netork#1933, #18), on top of napalm-linux's reading and writers.
|
||||
The web UI's "Scheduled Jobs" and the power-management schedule are read through
|
||||
`Cron.getList` (key `omv:<uuid>`, disabled ones included) instead of the lines OMV generates
|
||||
for them in `/etc/cron.d/openmediavault-userdefined` and `-powermngmt`. They are switched
|
||||
with `Cron.set` (the job unchanged but `enable`), run with `Cron.execute` and followed
|
||||
through `Exec.getOutput` (stopped after 300 s), and an application's job is made with
|
||||
`Cron.set` and removed with `Cron.delete` -- an OMV job like any other, its comment
|
||||
`napalm-scheduled-job: <name> <description>`. Each write applies the `cron` module alone,
|
||||
so changes pending in the web UI for other modules stay pending; while the `cron` module
|
||||
itself has changes pending, every write is refused, or the apply would put them live too.
|
||||
The cron files OMV
|
||||
generates (`/etc/cron.d/openmediavault-*`, `/etc/cron.hourly/openmediavault-flushmailq`,
|
||||
`/etc/cron.{daily,weekly}/openmediavault-apticron`,
|
||||
`/etc/cron.daily/openmediavault-samba-recycle`) are `platform` and only run: the Linux path
|
||||
refuses to write them, as OMV would undo the edit on its next apply. Every other job keeps
|
||||
napalm-linux's behaviour. Requires napalm-device-types 4.26.0 and napalm-linux 0.16.0.
|
||||
- netOrk's own account with a fixed uid is not made on OMV yet (NetOrk/netork#1817):
|
||||
`_account_create_with_uid` raises `NotImplementedError`, as the keys go through
|
||||
`UserMgmt`, which knows only the users it made.
|
||||
|
||||
@@ -5,7 +5,7 @@ NAPALM driver for [OpenMediaVault](https://www.openmediavault.org/) NAS systems,
|
||||
[napalm-linux](https://git.netork.io/NAPALM/napalm-linux) (`LinuxDriver`).
|
||||
|
||||
OpenMediaVault is Debian underneath, so the driver inherits everything napalm-linux does for a
|
||||
Linux host (packages, updates, services, users, processes, cron, Docker) and adds the NAS side:
|
||||
Linux host (packages, updates, services, users, processes, scheduled jobs, Docker) and adds the NAS side:
|
||||
disks, md RAID arrays, filesystems, shares and the NAS services.
|
||||
|
||||
## Tested against
|
||||
@@ -40,7 +40,14 @@ with driver("192.0.2.10", "admin", "secret", optional_args={"sudo_password": "se
|
||||
| `get_volumes` | mounted block-device filesystems from `df`, read-only flag from `/proc/mounts` |
|
||||
| `get_shares` | SMB, NFS and FTP shares from OMV's configuration database (`omv-confdbadm`) |
|
||||
| `get_storage_services` | whether `smbd`, `nfs-server`, `proftpd`, `ssh` and `rsync` are enabled and running |
|
||||
| everything of napalm-linux | packages, updates, services, users, processes, cron, Docker, … |
|
||||
| `get_scheduled_jobs` | napalm-linux's reading of the host, but the web UI's "Scheduled Jobs" and the power-management schedule come from `Cron.getList` (`omv:<uuid>`, disabled ones too) instead of the lines OMV generates for them; the cron files OMV generates (`/etc/cron.d/openmediavault-*`, `openmediavault-flushmailq`, `-apticron`, `-samba-recycle`) are `platform` and only run |
|
||||
| `manage_scheduled_job` | an OMV job: `Cron.set` with only `enable` changed, or `Cron.execute` followed through `Exec.getOutput` (stopped after 300 s); a power-management job is not run; anything else as napalm-linux, never a generated file |
|
||||
| `put_scheduled_job` | an OMV job through `Cron.set`, in the web UI like any other, its comment `napalm-scheduled-job: <name> <description>`; a multi-line command is fine |
|
||||
| `delete_scheduled_job` | an application's OMV job through `Cron.delete`; an application's Linux job as napalm-linux |
|
||||
| everything of napalm-linux | packages, updates, services, users, processes, Docker, … |
|
||||
|
||||
Every scheduled-job write through RPC deploys the `cron` module alone (`Config.applyChanges`):
|
||||
changes left pending in the web UI for other modules stay pending.
|
||||
|
||||
"Upgrade everything" is a full upgrade (`FULL_UPGRADE = True`): a plain `apt-get upgrade` holds
|
||||
back OpenMediaVault's own packages and the kernel.
|
||||
|
||||
@@ -0,0 +1,425 @@
|
||||
"""Scheduled jobs on OpenMediaVault: the web UI's through RPC, the rest as on Linux (#18).
|
||||
|
||||
napalm-linux reads and writes every cron line, run-parts script and timer of the
|
||||
host (``PosixScheduledJobsMixin``). OMV writes some of those files itself, from
|
||||
its own database, and writes them again on every apply of the module they belong
|
||||
to -- an edit there would be taken back without a word:
|
||||
|
||||
* ``/etc/cron.d/openmediavault-userdefined``: the web UI's "Scheduled Jobs", and
|
||||
``/etc/cron.d/openmediavault-powermngmt``: the power-management schedule, both
|
||||
from ``conf.system.cron.job`` (module ``cron``);
|
||||
* ``/etc/cron.d/openmediavault-rsync`` (rsync jobs), ``openmediavault-mkrrdgraph``
|
||||
(the performance graphs) and the ``openmediavault-*`` files OMV's plugins write
|
||||
there the same way;
|
||||
* ``/etc/cron.hourly/openmediavault-flushmailq``, ``/etc/cron.{daily,weekly}/
|
||||
openmediavault-apticron`` and ``/etc/cron.daily/openmediavault-samba-recycle``.
|
||||
|
||||
Those are ``platform`` (:data:`GENERATED`) and the Linux path never writes them:
|
||||
such a job only runs. The lines of the two files from ``conf.system.cron.job``
|
||||
are left out of the reading altogether; the jobs ``Cron.getList`` returns stand
|
||||
in for them, keyed ``omv:<uuid>``, with the disabled ones the files leave out.
|
||||
They are switched with ``Cron.set`` (the job as it is, but ``enable``), run with
|
||||
``Cron.execute`` (followed through ``Exec.getOutput``), made and changed with
|
||||
``Cron.set`` and removed with ``Cron.delete`` -- so an application's job on OMV
|
||||
is an OMV job, in the web UI like any other. Each write applies the ``cron``
|
||||
module and nothing else (``Config.applyChanges``): changes left pending in the
|
||||
web UI for other modules stay pending. While the ``cron`` module itself has changes
|
||||
pending, every write is refused: an apply would put someone's half-finished edit
|
||||
live along with it (decided 2026-11-03, NetOrk/netork#1933).
|
||||
|
||||
The scripts OMV's package ships to the run-parts directories
|
||||
(``/etc/cron.daily/openmediavault-check_locked_users`` and the like) are not
|
||||
generated; they stay ``package`` jobs, switched as on Linux.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import fnmatch
|
||||
import json
|
||||
import time
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from napalm_device_types import (
|
||||
PosixScheduledJobsMixin,
|
||||
ScheduledJobChanged,
|
||||
ScheduledJobRefused,
|
||||
)
|
||||
from napalm_device_types.cron import parse_cron
|
||||
from napalm_device_types.scheduled_jobs import (
|
||||
ACTIONS,
|
||||
DELETE,
|
||||
EDIT,
|
||||
RUN_TIMEOUT,
|
||||
check_spec,
|
||||
job_digest,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from napalm_device_types import ScheduledJobResultDict, ScheduledJobSpecDict
|
||||
from napalm_device_types._scheduled_reading import Reading
|
||||
from napalm_device_types.scheduled_jobs import ScheduledJobsReadingDict
|
||||
|
||||
#: The cron files OMV generates (``fnmatch`` patterns): ``platform``, never written here.
|
||||
GENERATED = (
|
||||
"/etc/cron.d/openmediavault-*",
|
||||
"/etc/cron.hourly/openmediavault-flushmailq",
|
||||
"/etc/cron.daily/openmediavault-apticron",
|
||||
"/etc/cron.weekly/openmediavault-apticron",
|
||||
"/etc/cron.daily/openmediavault-samba-recycle",
|
||||
)
|
||||
#: The files OMV writes from ``conf.system.cron.job``: their jobs are read through RPC.
|
||||
_FROM_RPC = ("/etc/cron.d/openmediavault-userdefined", "/etc/cron.d/openmediavault-powermngmt")
|
||||
#: Every type of ``conf.system.cron.job``: the web UI's jobs and the power schedule.
|
||||
_TYPES = ["userdefined", "reboot", "shutdown", "standby"]
|
||||
#: The UUID that makes ``Cron.set`` create an object (``OMV_CONFIGOBJECT_NEW_UUID``).
|
||||
NEW_UUID = "fa4b1c66-ef79-11e5-87a0-0002b3a176b4"
|
||||
#: napalm-device-types' job marker, as the comment of an OMV job (``# `` is cron's).
|
||||
COMMENT_MARKER = "napalm-scheduled-job: "
|
||||
_KEY = "omv:"
|
||||
#: The module the scheduled jobs are, in OMV's configuration.
|
||||
_MODULE = "cron"
|
||||
_APPLY = {"modules": [_MODULE], "force": False}
|
||||
#: Salt restarts cron when it deploys: give it time.
|
||||
_APPLY_TIMEOUT = 600
|
||||
_SHORTCUTS = {
|
||||
"@reboot": "reboot",
|
||||
"@hourly": "hourly",
|
||||
"@daily": "daily",
|
||||
"@midnight": "daily",
|
||||
"@weekly": "weekly",
|
||||
"@monthly": "monthly",
|
||||
"@yearly": "yearly",
|
||||
"@annually": "yearly",
|
||||
}
|
||||
#: The five time fields in cron's order, with the flag that makes one ``*/<value>``.
|
||||
_TIME_FIELDS = (
|
||||
("minute", "everynminute"),
|
||||
("hour", "everynhour"),
|
||||
("dayofmonth", "everyndayofmonth"),
|
||||
("month", None),
|
||||
("dayofweek", None),
|
||||
)
|
||||
_LISTS = tuple(name for name, _every in _TIME_FIELDS)
|
||||
_FLAGS = ("enable", "sendemail", "everynminute", "everynhour", "everyndayofmonth")
|
||||
_TEXTS = ("uuid", "execution", "comment", "type", "username", "command")
|
||||
_SWITCH = ["enable", "disable"]
|
||||
#: Seconds between two reads of a run's output; what a run keeps of it.
|
||||
_POLL = 1.0
|
||||
_OUTPUT_LIMIT = 64 * 1024
|
||||
_sleep = time.sleep
|
||||
_monotonic = time.monotonic
|
||||
|
||||
|
||||
def generated(path: str) -> bool:
|
||||
"""Whether OMV generates the cron file *path* (:data:`GENERATED`)."""
|
||||
return any(fnmatch.fnmatchcase(path, pattern) for pattern in GENERATED)
|
||||
|
||||
|
||||
def _flag(value: object) -> bool:
|
||||
return value is True or str(value).lower() in ("1", "true", "yes", "on")
|
||||
|
||||
|
||||
def cron_schedule(job: dict[str, Any]) -> str:
|
||||
"""The cron expression of an OMV job, as OMV writes it to its cron file."""
|
||||
execution = str(job.get("execution") or "exactly")
|
||||
if execution != "exactly":
|
||||
return f"@{execution}"
|
||||
words = []
|
||||
for name, every in _TIME_FIELDS:
|
||||
value = str(job.get(name, "*"))
|
||||
words.append(f"*/{value}" if every and _flag(job.get(every)) else value)
|
||||
return " ".join(words)
|
||||
|
||||
|
||||
def omv_schedule(schedule: str) -> dict[str, Any]:
|
||||
"""The fields of ``Cron.set`` that make OMV run a job on cron's *schedule*.
|
||||
|
||||
An alias is OMV's shortcut. Otherwise each field is ``*``, a ``*/n`` where OMV
|
||||
has one (minute, hour, day of month), or the list of its values: OMV takes no
|
||||
ranges, no names and no step on month or weekday; Sunday is ``7``.
|
||||
|
||||
:raises ScheduledJobRefused: for an expression cron would not take, and for a
|
||||
weekday step beside a day of month -- its values would make cron run on
|
||||
either day instead of on both.
|
||||
"""
|
||||
text = schedule.strip()
|
||||
fields: dict[str, Any] = {"execution": "exactly", "minute": ["0"], "hour": ["0"]}
|
||||
fields.update({"dayofmonth": ["*"], "month": ["*"], "dayofweek": ["*"]})
|
||||
fields.update({every: False for _name, every in _TIME_FIELDS if every})
|
||||
if text.lower() in _SHORTCUTS:
|
||||
return {**fields, "execution": _SHORTCUTS[text.lower()]}
|
||||
return {**fields, **_exactly(text)}
|
||||
|
||||
|
||||
def _exactly(text: str) -> dict[str, Any]:
|
||||
"""The time fields of ``execution: exactly`` for the five-field *text*."""
|
||||
try:
|
||||
parsed = parse_cron(text)
|
||||
except ValueError as exc:
|
||||
raise ScheduledJobRefused(str(exc)) from exc
|
||||
words = text.split()
|
||||
if words[4].startswith("*/") and not words[2].startswith("*"):
|
||||
raise ScheduledJobRefused(f"OpenMediaVault cannot step weekdays beside a day: {text!r}")
|
||||
weekdays = frozenset(7 if day == 0 else day for day in parsed.weekdays)
|
||||
values = (parsed.minutes, parsed.hours, parsed.days, parsed.months, weekdays)
|
||||
fields: dict[str, Any] = {}
|
||||
for (name, every), word, field in zip(_TIME_FIELDS, words, values, strict=True):
|
||||
step = every is not None and word.startswith("*/")
|
||||
fields[name] = _omv_field(word, field, step)
|
||||
if every:
|
||||
fields[every] = step
|
||||
return fields
|
||||
|
||||
|
||||
def _omv_field(word: str, values: frozenset[int], step: bool) -> list[str]:
|
||||
if word == "*":
|
||||
return ["*"]
|
||||
if step:
|
||||
return [str(int(word[2:]))]
|
||||
return [str(v) for v in sorted(values)]
|
||||
|
||||
|
||||
def _actions(userdefined: bool, own: bool) -> list[str]:
|
||||
if not userdefined:
|
||||
return list(_SWITCH) # no script to run: Cron.execute runs a user-defined job's
|
||||
return [*_SWITCH, "run", EDIT, DELETE] if own else [*_SWITCH, "run"]
|
||||
|
||||
|
||||
def rpc_job(raw: dict[str, Any]) -> dict[str, object]:
|
||||
"""A job of ``Cron.getList`` as a scheduled job, keyed ``omv:<uuid>``."""
|
||||
userdefined = raw.get("type", "userdefined") == "userdefined"
|
||||
comment = str(raw.get("comment") or "")
|
||||
own = userdefined and comment.startswith(COMMENT_MARKER)
|
||||
name, _, description = (
|
||||
comment[len(COMMENT_MARKER) :].partition(" ") if own else ("", "", comment)
|
||||
)
|
||||
job: dict[str, object] = {
|
||||
"key": f"{_KEY}{raw['uuid']}",
|
||||
"kind": "cron",
|
||||
"source": "omv:cron" if userdefined else "omv:powermgmt",
|
||||
"run_as": str(raw.get("username") or "root"),
|
||||
"schedule": cron_schedule(raw),
|
||||
"schedule_kind": "cron",
|
||||
"command": str(raw.get("command") or ""),
|
||||
"enabled": _flag(raw.get("enable")),
|
||||
"managed_by": "application" if own else "admin",
|
||||
"actions": _actions(userdefined, own),
|
||||
}
|
||||
extra = [_flag(raw.get("sendemail")), comment, raw.get("type")]
|
||||
job["digest"] = job_digest(job, content=json.dumps(extra))
|
||||
job.update({k: v for k, v in (("name", name), ("description", description.strip())) if v})
|
||||
return job
|
||||
|
||||
|
||||
def _set_params(raw: dict[str, Any]) -> dict[str, Any]:
|
||||
"""What ``Cron.set`` takes of a job: the time fields as lists, as ``Cron.get`` has them."""
|
||||
params: dict[str, Any] = {k: str(raw.get(k) or "") for k in _TEXTS}
|
||||
params.update({k: _flag(raw.get(k)) for k in _FLAGS})
|
||||
for k in _LISTS:
|
||||
value = raw.get(k, "*")
|
||||
params[k] = value if isinstance(value, list) else str(value).split(",")
|
||||
return params
|
||||
|
||||
|
||||
def _tail(text: str) -> str:
|
||||
return text[-_OUTPUT_LIMIT:]
|
||||
|
||||
|
||||
class OmvScheduledJobsMixin(PosixScheduledJobsMixin):
|
||||
"""napalm-linux's scheduled jobs, with OMV's own through its RPC (module docstring)."""
|
||||
|
||||
PLATFORM_SOURCES = GENERATED
|
||||
|
||||
if TYPE_CHECKING:
|
||||
|
||||
def _omv_rpc(
|
||||
self,
|
||||
service: str,
|
||||
method: str,
|
||||
params: dict[str, Any] | None = None,
|
||||
timeout: float = 60,
|
||||
) -> Any: ...
|
||||
|
||||
def _omv_dirty_modules(self) -> list[str]: ...
|
||||
|
||||
# -- reading ---------------------------------------------------------------------------------
|
||||
|
||||
def _sj_read(self) -> Reading:
|
||||
"""The Linux reading without the lines RPC stands for; a generated job only runs."""
|
||||
reading = super()._sj_read()
|
||||
reading.jobs = [j for j in reading.jobs if j["source"] not in _FROM_RPC]
|
||||
for job in reading.jobs:
|
||||
if job["managed_by"] == "platform":
|
||||
job["actions"] = [a for a in job["actions"] if a == "run"] # type: ignore[attr-defined]
|
||||
return reading
|
||||
|
||||
def _omv_jobs(self) -> list[tuple[dict[str, object], dict[str, Any]]]:
|
||||
"""Every job of ``conf.system.cron.job``, with what ``Cron.getList`` said of it.
|
||||
|
||||
:raises ValueError: when the list came back shorter than its total.
|
||||
"""
|
||||
answer = self._omv_rpc("Cron", "getList", {"start": 0, "limit": None, "type": _TYPES})
|
||||
data = answer.get("data") if isinstance(answer, dict) else None
|
||||
if not isinstance(data, list) or len(data) != answer.get("total"):
|
||||
raise ValueError("Cron.getList came back cut short")
|
||||
return [(rpc_job(raw), raw) for raw in data]
|
||||
|
||||
def get_scheduled_jobs(self) -> ScheduledJobsReadingDict:
|
||||
"""Every scheduled job of the NAS: the Linux reading, OMV's own jobs through RPC.
|
||||
|
||||
:raises ValueError: when either came back cut short.
|
||||
:raises RuntimeError: when ``Cron.getList`` failed.
|
||||
"""
|
||||
reading = self._sj_read()
|
||||
jobs = reading.jobs + [job for job, _raw in self._omv_jobs()]
|
||||
return {"jobs": jobs, "time_zone": reading.time_zone} # type: ignore[typeddict-item]
|
||||
|
||||
def _omv_job(self, key: str, digest: str) -> tuple[dict[str, object], dict[str, Any]]:
|
||||
found = next(((j, r) for j, r in self._omv_jobs() if j["key"] == key), None)
|
||||
if found is None:
|
||||
raise LookupError(f"no scheduled job {key}")
|
||||
if found[0]["digest"] != digest:
|
||||
raise ScheduledJobChanged(f"{key} changed on the NAS since it was read")
|
||||
return found
|
||||
|
||||
# -- writing ---------------------------------------------------------------------------------
|
||||
|
||||
def _omv_clean(self) -> None:
|
||||
""":raises ScheduledJobRefused: while the ``cron`` module has changes pending."""
|
||||
if _MODULE in self._omv_dirty_modules():
|
||||
raise ScheduledJobRefused(
|
||||
"OpenMediaVault has unapplied changes to its scheduled jobs; apply or "
|
||||
"revert them in its web UI first"
|
||||
)
|
||||
|
||||
def _omv_set(self, raw: dict[str, Any]) -> dict[str, Any]:
|
||||
"""``Cron.set`` *raw*, then deploy the ``cron`` module alone."""
|
||||
self._omv_clean()
|
||||
stored: dict[str, Any] = self._omv_rpc("Cron", "set", _set_params(raw))
|
||||
self._omv_rpc("Config", "applyChanges", dict(_APPLY), _APPLY_TIMEOUT)
|
||||
return stored
|
||||
|
||||
def _omv_guard(self, path: str) -> None:
|
||||
if generated(path):
|
||||
raise ScheduledJobRefused(f"OpenMediaVault generates {path}; it would undo the edit")
|
||||
|
||||
def _sj_write(self, path: str, text: str) -> None:
|
||||
self._omv_guard(path)
|
||||
super()._sj_write(path, text)
|
||||
|
||||
def _sj_switch(self, job: dict[str, object], enabled: bool) -> ScheduledJobResultDict:
|
||||
self._omv_guard(str(job["source"]))
|
||||
return super()._sj_switch(job, enabled)
|
||||
|
||||
def _sj_delete_cron(self, job: dict[str, object]) -> None:
|
||||
self._omv_guard(str(job["source"]))
|
||||
super()._sj_delete_cron(job)
|
||||
|
||||
# -- switching and running -------------------------------------------------------------------
|
||||
|
||||
def manage_scheduled_job(self, key: str, action: str, *, digest: str) -> ScheduledJobResultDict:
|
||||
"""Enable, disable or run the job *key*; an OMV job through ``Cron.*``.
|
||||
|
||||
:raises ScheduledJobChanged: when the job is no longer as *digest* says.
|
||||
:raises ScheduledJobRefused: when the job does not take *action*.
|
||||
:raises LookupError: when the NAS has no job *key*.
|
||||
"""
|
||||
if not key.startswith(_KEY):
|
||||
return super().manage_scheduled_job(key, action, digest=digest)
|
||||
if action not in ACTIONS:
|
||||
raise ScheduledJobRefused(f"unknown action {action!r}; allowed: {list(ACTIONS)}")
|
||||
job, raw = self._omv_job(key, digest)
|
||||
if action not in job["actions"]: # type: ignore[operator]
|
||||
raise ScheduledJobRefused(f"{key} cannot be told to {action} on OpenMediaVault")
|
||||
if action == "run":
|
||||
return self._omv_run(key, str(raw["uuid"]))
|
||||
self._omv_set({**raw, "enable": action == "enable"})
|
||||
return {"success": True, "output": f"{key} {action}d"}
|
||||
|
||||
def _omv_run(self, key: str, uuid: str) -> ScheduledJobResultDict:
|
||||
"""``Cron.execute``, followed to its end or stopped after ``RUN_TIMEOUT``."""
|
||||
filename = self._omv_rpc("Cron", "execute", {"uuid": uuid})
|
||||
deadline = _monotonic() + RUN_TIMEOUT
|
||||
pos, said = 0, ""
|
||||
while True:
|
||||
try:
|
||||
got = self._omv_rpc("Exec", "getOutput", {"filename": filename, "pos": pos})
|
||||
except RuntimeError as exc: # the job failed: OMV raises with its exit status
|
||||
return {"success": False, "output": _tail(f"{said}{exc}")}
|
||||
said += str(got.get("output") or "")
|
||||
pos = int(got.get("pos") or pos)
|
||||
if not got.get("running"):
|
||||
return {"success": True, "output": _tail(said.strip()) or f"{key} ran"}
|
||||
if _monotonic() >= deadline:
|
||||
self._omv_rpc("Exec", "stop", {"filename": filename})
|
||||
return {"success": False, "output": _tail(f"{said}stopped after {RUN_TIMEOUT} s")}
|
||||
_sleep(_POLL)
|
||||
|
||||
# -- an application's own jobs ---------------------------------------------------------------
|
||||
|
||||
def _omv_own(self, name: str, digest: str | None) -> dict[str, Any] | None:
|
||||
own = (
|
||||
(j, r)
|
||||
for j, r in self._omv_jobs()
|
||||
if j["managed_by"] == "application" and j.get("name") == name
|
||||
)
|
||||
found = next(own, None)
|
||||
if digest is None:
|
||||
if found is not None:
|
||||
raise ScheduledJobChanged(f"a job {name} is there already")
|
||||
return None
|
||||
if found is None:
|
||||
raise LookupError(f"no scheduled job named {name}")
|
||||
if found[0]["digest"] != digest:
|
||||
raise ScheduledJobChanged(f"{found[0]['key']} changed on the NAS since it was read")
|
||||
return found[1]
|
||||
|
||||
def put_scheduled_job(
|
||||
self, spec: ScheduledJobSpecDict, *, digest: str | None = None
|
||||
) -> ScheduledJobResultDict:
|
||||
"""Create or change the application's job ``spec["name"]`` as an OMV job.
|
||||
|
||||
:raises ScheduledJobChanged: when the job is no longer as *digest* says, or
|
||||
one of that name is there although *digest* is None.
|
||||
:raises ScheduledJobRefused: for a spec ``check_spec`` refuses, a schedule
|
||||
OMV cannot hold, or a user the NAS does not have.
|
||||
:raises LookupError: for a *digest* and no job of that name.
|
||||
"""
|
||||
check_spec(spec)
|
||||
fields = omv_schedule(spec["schedule"])
|
||||
self._sj_group(spec["run_as"])
|
||||
current = self._omv_own(spec["name"], digest)
|
||||
comment = f"{COMMENT_MARKER}{spec['name']} {spec.get('description') or ''}".rstrip()
|
||||
stored = self._omv_set(
|
||||
{
|
||||
"uuid": current["uuid"] if current else NEW_UUID,
|
||||
"sendemail": current.get("sendemail") if current else False,
|
||||
"enable": spec["enabled"],
|
||||
"type": "userdefined",
|
||||
"comment": comment,
|
||||
"username": spec["run_as"],
|
||||
"command": spec["command"].strip(),
|
||||
**fields,
|
||||
}
|
||||
)
|
||||
done = "changed" if current else "created"
|
||||
return {"success": True, "output": f"{_KEY}{stored['uuid']} {done}"}
|
||||
|
||||
def delete_scheduled_job(self, key: str, *, digest: str) -> ScheduledJobResultDict:
|
||||
"""Delete the application's job *key*; an OMV job through ``Cron.delete``.
|
||||
|
||||
:raises ScheduledJobChanged: when the job is no longer as *digest* says.
|
||||
:raises ScheduledJobRefused: when an application did not write it.
|
||||
:raises LookupError: when the NAS has no job *key*.
|
||||
"""
|
||||
if not key.startswith(_KEY):
|
||||
return super().delete_scheduled_job(key, digest=digest)
|
||||
job, raw = self._omv_job(key, digest)
|
||||
if job["managed_by"] != "application":
|
||||
raise ScheduledJobRefused(
|
||||
f"{key} was not written by an application; it is only switched"
|
||||
)
|
||||
self._omv_clean()
|
||||
self._omv_rpc("Cron", "delete", {"uuid": raw["uuid"]})
|
||||
self._omv_rpc("Config", "applyChanges", dict(_APPLY), _APPLY_TIMEOUT)
|
||||
return {"success": True, "output": f"{key} deleted"}
|
||||
@@ -38,6 +38,7 @@ from napalm_device_types.models import (
|
||||
from napalm_linux.linux import LinuxDriver
|
||||
|
||||
from napalm_openmediavault._local_accounts import OmvLocalAccountsMixin
|
||||
from napalm_openmediavault._scheduled_jobs import OmvScheduledJobsMixin
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -159,15 +160,18 @@ def _pool_of(disk: str, members: dict[str, str]) -> str:
|
||||
return next((array for member, array in members.items() if own.fullmatch(member)), "")
|
||||
|
||||
|
||||
class OpenMediaVaultDriver(OmvLocalAccountsMixin, StorageDriver, LinuxDriver):
|
||||
class OpenMediaVaultDriver(
|
||||
OmvLocalAccountsMixin, OmvScheduledJobsMixin, StorageDriver, LinuxDriver
|
||||
):
|
||||
"""NAPALM driver for OpenMediaVault NAS systems.
|
||||
|
||||
Declares both roles it fills: a NAS built on Debian. Storage comes first,
|
||||
so netOrk shows it as storage rather than as a Linux host.
|
||||
|
||||
Inherits all Linux OS functionality from LinuxDriver (packages, services,
|
||||
users, processes, cron, Docker) -- OMV really is Debian, so apt applies --
|
||||
and adds OMV-specific storage management on top.
|
||||
users, processes, scheduled jobs, Docker) -- OMV really is Debian, so apt
|
||||
applies -- and adds OMV-specific storage management on top. The scheduled
|
||||
jobs of OMV's web UI go through its RPC (``_scheduled_jobs``).
|
||||
|
||||
NAS services are ``get_storage_services()``; the systemd service list stays
|
||||
``get_services()``. Two names for two different things.
|
||||
|
||||
+2
-2
@@ -27,8 +27,8 @@ classifiers = [
|
||||
]
|
||||
dependencies = [
|
||||
"napalm>=4.0",
|
||||
"napalm-device-types>=4.26.0",
|
||||
"napalm-linux>=0.15.0",
|
||||
"napalm-device-types>=4.27.0",
|
||||
"napalm-linux>=0.16.0",
|
||||
]
|
||||
|
||||
[project.optional-dependencies]
|
||||
|
||||
@@ -0,0 +1,707 @@
|
||||
"""Scheduled jobs on OpenMediaVault: the web UI's through RPC, the rest as on Linux (#18).
|
||||
|
||||
OMV writes its cron files itself, from its own database: the web UI's "Scheduled
|
||||
Jobs" (and the power-management schedule) go to ``/etc/cron.d/openmediavault-*``
|
||||
on every apply of the ``cron`` module, rsync jobs, the mail-queue flush and the
|
||||
apticron run likewise. An edit there would be taken back without a word. So those
|
||||
files are ``platform`` and never written through the Linux path; the lines of the
|
||||
web UI's file are replaced by the jobs ``Cron.getList`` returns, and those are
|
||||
switched, run, made and removed through ``Cron.*`` -- then ``Config.applyChanges``
|
||||
for ``cron`` alone, so changes someone left pending in the web UI stay pending.
|
||||
|
||||
The NAS below is synthetic: the report napalm-linux's reader prints, and the RPC
|
||||
answers ``omv-rpc`` gives, in OMV 8's shapes (``Cron.getList`` keeps each time
|
||||
field a comma-separated string, ``Cron.set`` takes lists).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import shlex
|
||||
|
||||
import pytest
|
||||
from napalm_device_types import (
|
||||
CommandResult,
|
||||
ScheduledJobChanged,
|
||||
ScheduledJobRefused,
|
||||
)
|
||||
|
||||
from napalm_openmediavault import _scheduled_jobs as sj
|
||||
from napalm_openmediavault.openmediavault import OpenMediaVaultDriver
|
||||
|
||||
NEW = "fa4b1c66-ef79-11e5-87a0-0002b3a176b4"
|
||||
SYNC = "2c1d3e4f-0a1b-4c2d-8e3f-405162738495"
|
||||
WEEKLY = "3d2e4f50-1b2c-4d3e-9f40-5162738495a6"
|
||||
OWN = "4e3f5061-2c3d-4e4f-a051-62738495a6b7"
|
||||
POWER = "5f406172-3d4e-4f50-b162-738495a6b7c8"
|
||||
|
||||
REPORT = """SJ_BEGIN
|
||||
[system /etc/cron.d/openmediavault-userdefined]
|
||||
|# This file is auto-generated by openmediavault (https://www.openmediavault.org)
|
||||
|SHELL=/bin/sh
|
||||
|15 2 * * 1,3 root /var/lib/openmediavault/cron.d/userdefined-2c1d3e4f-0a1b-4c2d-8e3f-405162738495 >/dev/null 2>&1
|
||||
[system /etc/cron.d/openmediavault-powermngmt]
|
||||
|0 23 * * * root systemctl poweroff >/dev/null 2>&1
|
||||
[system /etc/cron.d/openmediavault-rsync]
|
||||
|0 3 * * * root /var/lib/openmediavault/cron.d/rsync-6a7b8c9d >/dev/null 2>&1
|
||||
[system /etc/cron.d/certbot]
|
||||
|0 */12 * * * root certbot -q renew
|
||||
[run-parts hourly 1 1234 /etc/cron.hourly/openmediavault-flushmailq]
|
||||
[run-parts daily 1 5678 /etc/cron.daily/openmediavault-check_locked_users]
|
||||
[tz]
|
||||
/usr/share/zoneinfo/Europe/Berlin
|
||||
[owners]
|
||||
certbot: /etc/cron.d/certbot
|
||||
openmediavault: /etc/cron.daily/openmediavault-check_locked_users
|
||||
SJ_END
|
||||
"""
|
||||
|
||||
|
||||
def _job(uuid: str, **fields: object) -> dict[str, object]:
|
||||
job: dict[str, object] = {
|
||||
"uuid": uuid,
|
||||
"enable": True,
|
||||
"execution": "exactly",
|
||||
"sendemail": False,
|
||||
"comment": "",
|
||||
"type": "userdefined",
|
||||
"minute": "0",
|
||||
"everynminute": False,
|
||||
"hour": "0",
|
||||
"everynhour": False,
|
||||
"month": "*",
|
||||
"dayofmonth": "*",
|
||||
"everyndayofmonth": False,
|
||||
"dayofweek": "*",
|
||||
"username": "root",
|
||||
"command": "true",
|
||||
}
|
||||
job.update(fields)
|
||||
return job
|
||||
|
||||
|
||||
def _jobs() -> list[dict[str, object]]:
|
||||
return [
|
||||
_job(
|
||||
SYNC,
|
||||
minute="15",
|
||||
everynminute=True,
|
||||
hour="2",
|
||||
dayofweek="1,3",
|
||||
comment="Nightly sync",
|
||||
command="rsync -a /srv/a /srv/b",
|
||||
),
|
||||
_job(WEEKLY, enable=False, execution="weekly", username="backup", command="backup.sh"),
|
||||
_job(
|
||||
OWN,
|
||||
execution="daily",
|
||||
sendemail=True,
|
||||
comment="napalm-scheduled-job: cleanup Remove old snapshots",
|
||||
command="cleanup --old\necho done",
|
||||
),
|
||||
_job(POWER, type="shutdown", hour="23", command="systemctl poweroff"),
|
||||
]
|
||||
|
||||
|
||||
class Nas:
|
||||
"""An OMV NAS as the driver sees it: ``run_command`` and ``omv-rpc`` through sudo."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.jobs = _jobs()
|
||||
self.dirty = ["apt"]
|
||||
self.users = {"root": "root", "backup": "backup"}
|
||||
self.commands: list[str] = []
|
||||
self.calls: list[tuple[str, object]] = []
|
||||
self.listed = None # what Cron.getList says, if not the jobs
|
||||
self.output = [("hello\n", True), ("world\n", False)]
|
||||
self.fail = ""
|
||||
|
||||
def run_command(self, command, *, privileged=False, timeout=60, stdin=None):
|
||||
self.commands.append(command)
|
||||
if command.startswith("printf '%s%s\\n' SJ_"):
|
||||
return CommandResult(REPORT, "", 0)
|
||||
if command.startswith("t=$(mktemp)"):
|
||||
return CommandResult("/tmp/tmp.Staged", "", 0)
|
||||
argv = shlex.split(command)
|
||||
if argv[0] == "stat":
|
||||
return CommandResult("644 root root\n", "", 0)
|
||||
if argv[:3] == ["id", "-gn", "--"]:
|
||||
group = self.users.get(argv[3])
|
||||
return CommandResult(group or "", "" if group else "no such user", 0 if group else 1)
|
||||
return CommandResult("", "", 0)
|
||||
|
||||
def sudo_status(self, command: str, read_timeout: float = 100):
|
||||
if command.startswith("cat ") and "dirtymodules" in command:
|
||||
return json.dumps(self.dirty), 0
|
||||
argv = shlex.split(command)
|
||||
assert argv[:3] == ["omv-rpc", "-u", "admin"], command
|
||||
method = f"{argv[3]}.{argv[4]}"
|
||||
params = json.loads(argv[5]) if len(argv) > 5 else None
|
||||
self.calls.append((method, params))
|
||||
if method == self.fail:
|
||||
return "ERROR: the job failed: exit 3", 1
|
||||
return json.dumps(self._answer(method, params)), 0
|
||||
|
||||
def _answer(self, method: str, params): # noqa: C901 - one branch per RPC method
|
||||
if method == "Cron.getList":
|
||||
assert params["type"] == ["userdefined", "reboot", "shutdown", "standby"]
|
||||
data = self.jobs if self.listed is None else self.listed
|
||||
return {"total": len(self.jobs), "data": data}
|
||||
if method == "Cron.set":
|
||||
stored = {k: ",".join(v) if isinstance(v, list) else v for k, v in params.items()}
|
||||
if stored["uuid"] == NEW:
|
||||
stored["uuid"] = "6b7c8d9e-4e5f-4061-8273-8495a6b7c8d9"
|
||||
self.jobs.append(stored)
|
||||
else:
|
||||
self.jobs = [stored if j["uuid"] == stored["uuid"] else j for j in self.jobs]
|
||||
self.dirty.append("cron")
|
||||
return stored
|
||||
if method == "Cron.delete":
|
||||
self.jobs = [j for j in self.jobs if j["uuid"] != params["uuid"]]
|
||||
self.dirty.append("cron")
|
||||
return {}
|
||||
if method == "Cron.execute":
|
||||
return "/tmp/bgstatusQx1"
|
||||
if method == "Exec.getOutput":
|
||||
chunk, running = self.output.pop(0)
|
||||
return {
|
||||
"filename": params["filename"],
|
||||
"pos": params["pos"] + len(chunk),
|
||||
"output": chunk,
|
||||
"running": running,
|
||||
}
|
||||
if method == "Exec.stop":
|
||||
return None
|
||||
if method == "Config.applyChanges":
|
||||
self.dirty = [m for m in self.dirty if m not in params["modules"]]
|
||||
return None
|
||||
raise AssertionError(method)
|
||||
|
||||
def called(self, method: str) -> list[object]:
|
||||
return [params for m, params in self.calls if m == method]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def nas() -> Nas:
|
||||
return Nas()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def driver(nas: Nas) -> OpenMediaVaultDriver:
|
||||
d = OpenMediaVaultDriver("nas", "admin", "p")
|
||||
d.run_command = nas.run_command # type: ignore[method-assign]
|
||||
d._sudo_status = nas.sudo_status # type: ignore[method-assign]
|
||||
return d
|
||||
|
||||
|
||||
def _by_key(driver: OpenMediaVaultDriver) -> dict[str, dict[str, object]]:
|
||||
return {str(j["key"]): dict(j) for j in driver.get_scheduled_jobs()["jobs"]}
|
||||
|
||||
|
||||
# -- what OMV offers -------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_omv_offers_every_verb_of_the_contract():
|
||||
for verb in (
|
||||
"get_scheduled_jobs",
|
||||
"manage_scheduled_job",
|
||||
"put_scheduled_job",
|
||||
"delete_scheduled_job",
|
||||
):
|
||||
assert callable(getattr(OpenMediaVaultDriver, verb)), verb
|
||||
|
||||
|
||||
def test_every_file_omv_generates_is_platform():
|
||||
for path in (
|
||||
"/etc/cron.d/openmediavault-userdefined",
|
||||
"/etc/cron.d/openmediavault-powermngmt",
|
||||
"/etc/cron.d/openmediavault-rsync",
|
||||
"/etc/cron.d/openmediavault-mkrrdgraph",
|
||||
"/etc/cron.hourly/openmediavault-flushmailq",
|
||||
"/etc/cron.daily/openmediavault-apticron",
|
||||
"/etc/cron.weekly/openmediavault-apticron",
|
||||
"/etc/cron.daily/openmediavault-samba-recycle",
|
||||
):
|
||||
assert sj.generated(path), path
|
||||
assert not sj.generated("/etc/cron.d/certbot")
|
||||
assert not sj.generated("/etc/cron.daily/openmediavault-check_locked_users")
|
||||
|
||||
|
||||
# -- reading ---------------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_the_web_ui_jobs_come_from_rpc(driver):
|
||||
job = _by_key(driver)[f"omv:{SYNC}"]
|
||||
|
||||
assert job["kind"] == "cron"
|
||||
assert job["source"] == "omv:cron"
|
||||
assert job["schedule"] == "*/15 2 * * 1,3"
|
||||
assert job["schedule_kind"] == "cron"
|
||||
assert (job["run_as"], job["command"], job["enabled"]) == (
|
||||
"root",
|
||||
"rsync -a /srv/a /srv/b",
|
||||
True,
|
||||
)
|
||||
assert job["managed_by"] == "admin"
|
||||
assert job["actions"] == ["enable", "disable", "run"]
|
||||
assert job["description"] == "Nightly sync"
|
||||
|
||||
|
||||
def test_the_generated_lines_of_the_web_ui_jobs_are_not_reported_twice(driver):
|
||||
sources = {str(j["source"]) for j in driver.get_scheduled_jobs()["jobs"]}
|
||||
|
||||
assert "/etc/cron.d/openmediavault-userdefined" not in sources
|
||||
assert "/etc/cron.d/openmediavault-powermngmt" not in sources
|
||||
|
||||
|
||||
def test_a_shortcut_is_the_cron_alias_and_a_disabled_job_is_reported(driver):
|
||||
job = _by_key(driver)[f"omv:{WEEKLY}"]
|
||||
|
||||
assert (job["schedule"], job["enabled"], job["run_as"]) == ("@weekly", False, "backup")
|
||||
|
||||
|
||||
def test_an_applications_job_is_known_by_its_comment(driver):
|
||||
job = _by_key(driver)[f"omv:{OWN}"]
|
||||
|
||||
assert job["managed_by"] == "application"
|
||||
assert (job["name"], job["description"]) == ("cleanup", "Remove old snapshots")
|
||||
assert job["actions"] == ["enable", "disable", "run", "edit", "delete"]
|
||||
assert job["command"] == "cleanup --old\necho done"
|
||||
|
||||
|
||||
def test_a_power_management_job_is_switched_but_not_run(driver):
|
||||
job = _by_key(driver)[f"omv:{POWER}"]
|
||||
|
||||
assert job["source"] == "omv:powermgmt"
|
||||
assert job["schedule"] == "0 23 * * *"
|
||||
assert job["actions"] == ["enable", "disable"]
|
||||
|
||||
|
||||
def test_the_rest_of_the_host_is_read_as_on_linux(driver):
|
||||
reading = driver.get_scheduled_jobs()
|
||||
certbot = next(j for j in reading["jobs"] if j["source"] == "/etc/cron.d/certbot")
|
||||
package = _by_key(driver)["run-parts:/etc/cron.daily/openmediavault-check_locked_users"]
|
||||
|
||||
assert reading["time_zone"] == "Europe/Berlin"
|
||||
assert certbot["managed_by"] == "package"
|
||||
assert certbot["actions"] == ["enable", "disable", "run"]
|
||||
assert package["managed_by"] == "package"
|
||||
|
||||
|
||||
def test_a_generated_job_is_platform_and_only_runs(driver):
|
||||
jobs = _by_key(driver)
|
||||
rsync = next(j for j in jobs.values() if j["source"] == "/etc/cron.d/openmediavault-rsync")
|
||||
flush = jobs["run-parts:/etc/cron.hourly/openmediavault-flushmailq"]
|
||||
|
||||
for job in (rsync, flush):
|
||||
assert job["managed_by"] == "platform"
|
||||
assert job["actions"] == ["run"]
|
||||
|
||||
|
||||
def test_a_list_cut_short_raises(driver, nas):
|
||||
nas.listed = nas.jobs[:2]
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
driver.get_scheduled_jobs()
|
||||
|
||||
|
||||
def test_a_failed_list_raises(driver, nas):
|
||||
nas.fail = "Cron.getList"
|
||||
|
||||
with pytest.raises(RuntimeError):
|
||||
driver.get_scheduled_jobs()
|
||||
|
||||
|
||||
def test_the_digest_sees_a_change_of_the_email_setting(driver, nas):
|
||||
before = _by_key(driver)[f"omv:{SYNC}"]["digest"]
|
||||
nas.jobs[0]["sendemail"] = True
|
||||
|
||||
assert _by_key(driver)[f"omv:{SYNC}"]["digest"] != before
|
||||
|
||||
|
||||
# -- never a generated file ------------------------------------------------------------------
|
||||
|
||||
|
||||
_WRITES = {"install", "crontab", "chmod", "rm", "tee", "mv", "cp", "sed", "systemctl"}
|
||||
#: What reads a file or runs a job, and so may name a generated file.
|
||||
_READS = {"cat", "timeout", "id"}
|
||||
|
||||
|
||||
def _wrote(nas: Nas) -> list[str]:
|
||||
"""Every command but the report that writes, or that names a generated file to do so."""
|
||||
commands = [c for c in nas.commands if not c.startswith("printf '%s%s\\n' SJ_")]
|
||||
return [
|
||||
c
|
||||
for c in commands
|
||||
if shlex.split(c)[0] in _WRITES
|
||||
or ("openmediavault-" in c and shlex.split(c)[0] not in _READS)
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("action", ["enable", "disable"])
|
||||
def test_a_generated_job_is_never_switched_by_the_linux_path(driver, nas, action):
|
||||
for job in _by_key(driver).values():
|
||||
if job["managed_by"] != "platform":
|
||||
continue
|
||||
with pytest.raises(ScheduledJobRefused):
|
||||
driver.manage_scheduled_job(str(job["key"]), action, digest=str(job["digest"]))
|
||||
|
||||
assert _wrote(nas) == []
|
||||
|
||||
|
||||
def test_the_linux_writers_refuse_a_generated_file(driver, nas):
|
||||
flush = _by_key(driver)["run-parts:/etc/cron.hourly/openmediavault-flushmailq"]
|
||||
|
||||
with pytest.raises(ScheduledJobRefused):
|
||||
driver._sj_write("/etc/cron.d/openmediavault-userdefined", "* * * * * root true\n")
|
||||
with pytest.raises(ScheduledJobRefused):
|
||||
driver._sj_switch(flush, False)
|
||||
with pytest.raises(ScheduledJobRefused):
|
||||
driver._sj_delete_cron({"source": "/etc/cron.d/openmediavault-rsync", "name": "x"})
|
||||
assert _wrote(nas) == []
|
||||
|
||||
|
||||
def test_the_linux_writers_still_write_any_other_file(driver, nas):
|
||||
driver._sj_write("/etc/cron.d/certbot", "0 */12 * * * root certbot -q renew\n")
|
||||
driver._sj_delete_cron({"source": "/etc/cron.d/tidy", "name": "tidy", "run_as": "root"})
|
||||
|
||||
assert any(c.startswith("install") and c.endswith("/etc/cron.d/certbot") for c in nas.commands)
|
||||
assert shlex.join(["rm", "-f", "--", "/etc/cron.d/tidy"]) in nas.commands
|
||||
|
||||
|
||||
def test_a_generated_cron_line_still_runs(driver, nas):
|
||||
rsync = next(
|
||||
j for j in _by_key(driver).values() if j["source"] == "/etc/cron.d/openmediavault-rsync"
|
||||
)
|
||||
nas.run_command = _with_file(nas, "/etc/cron.d/openmediavault-rsync") # type: ignore[method-assign]
|
||||
driver.run_command = nas.run_command # type: ignore[method-assign]
|
||||
|
||||
result = driver.manage_scheduled_job(str(rsync["key"]), "run", digest=str(rsync["digest"]))
|
||||
|
||||
assert result["success"] is True
|
||||
assert any("rsync-6a7b8c9d" in c for c in nas.commands)
|
||||
assert _wrote(nas) == []
|
||||
|
||||
|
||||
def _with_file(nas: Nas, path: str):
|
||||
text = "0 3 * * * root /var/lib/openmediavault/cron.d/rsync-6a7b8c9d >/dev/null 2>&1\n"
|
||||
original = nas.run_command
|
||||
|
||||
def run_command(command, **kwargs):
|
||||
if command == shlex.join(["cat", "--", path]):
|
||||
nas.commands.append(command)
|
||||
return CommandResult(text, "", 0)
|
||||
return original(command, **kwargs)
|
||||
|
||||
return run_command
|
||||
|
||||
|
||||
def test_any_other_job_keeps_the_linux_behaviour(driver, nas):
|
||||
job = _by_key(driver)["run-parts:/etc/cron.daily/openmediavault-check_locked_users"]
|
||||
|
||||
driver.manage_scheduled_job(str(job["key"]), "disable", digest=str(job["digest"]))
|
||||
|
||||
assert (
|
||||
shlex.join(["chmod", "-x", "--", "/etc/cron.daily/openmediavault-check_locked_users"])
|
||||
in nas.commands
|
||||
)
|
||||
assert nas.called("Cron.set") == []
|
||||
|
||||
|
||||
# -- switching and running a web UI job ------------------------------------------------------
|
||||
|
||||
|
||||
def test_disable_sets_the_job_unchanged_but_enable(driver, nas):
|
||||
job = _by_key(driver)[f"omv:{SYNC}"]
|
||||
|
||||
result = driver.manage_scheduled_job(job["key"], "disable", digest=job["digest"])
|
||||
|
||||
(sent,) = nas.called("Cron.set")
|
||||
assert sent == {
|
||||
**_jobs()[0],
|
||||
"enable": False,
|
||||
"minute": ["15"],
|
||||
"hour": ["2"],
|
||||
"month": ["*"],
|
||||
"dayofmonth": ["*"],
|
||||
"dayofweek": ["1", "3"],
|
||||
}
|
||||
assert result["success"] is True
|
||||
assert nas.called("Config.applyChanges") == [{"modules": ["cron"], "force": False}]
|
||||
assert nas.dirty == ["apt"]
|
||||
assert _by_key(driver)[f"omv:{SYNC}"]["enabled"] is False
|
||||
|
||||
|
||||
def test_enable_switches_a_disabled_job_on(driver, nas):
|
||||
job = _by_key(driver)[f"omv:{WEEKLY}"]
|
||||
|
||||
driver.manage_scheduled_job(job["key"], "enable", digest=job["digest"])
|
||||
|
||||
assert nas.called("Cron.set")[0]["enable"] is True
|
||||
|
||||
|
||||
def test_a_job_changed_since_it_was_read_is_not_written(driver, nas):
|
||||
job = _by_key(driver)[f"omv:{SYNC}"]
|
||||
nas.jobs[0]["command"] = "rsync -a /srv/a /srv/c"
|
||||
|
||||
with pytest.raises(ScheduledJobChanged):
|
||||
driver.manage_scheduled_job(job["key"], "disable", digest=job["digest"])
|
||||
assert nas.called("Cron.set") == []
|
||||
assert nas.called("Config.applyChanges") == []
|
||||
|
||||
|
||||
def test_an_unknown_job_is_a_lookup_error(driver):
|
||||
with pytest.raises(LookupError):
|
||||
driver.manage_scheduled_job("omv:00000000-0000-4000-8000-000000000000", "run", digest="x")
|
||||
|
||||
|
||||
def test_an_unknown_action_is_refused(driver):
|
||||
job = _by_key(driver)[f"omv:{SYNC}"]
|
||||
|
||||
with pytest.raises(ScheduledJobRefused):
|
||||
driver.manage_scheduled_job(job["key"], "edit", digest=job["digest"])
|
||||
|
||||
|
||||
def test_a_power_management_job_is_not_run(driver, nas):
|
||||
job = _by_key(driver)[f"omv:{POWER}"]
|
||||
|
||||
with pytest.raises(ScheduledJobRefused):
|
||||
driver.manage_scheduled_job(job["key"], "run", digest=job["digest"])
|
||||
assert nas.called("Cron.execute") == []
|
||||
|
||||
|
||||
def test_run_follows_the_output_to_the_end(driver, nas, monkeypatch):
|
||||
monkeypatch.setattr(sj, "_sleep", lambda _s: None)
|
||||
job = _by_key(driver)[f"omv:{SYNC}"]
|
||||
|
||||
result = driver.manage_scheduled_job(job["key"], "run", digest=job["digest"])
|
||||
|
||||
assert nas.called("Cron.execute") == [{"uuid": SYNC}]
|
||||
assert nas.called("Exec.getOutput") == [
|
||||
{"filename": "/tmp/bgstatusQx1", "pos": 0},
|
||||
{"filename": "/tmp/bgstatusQx1", "pos": 6},
|
||||
]
|
||||
assert result == {"success": True, "output": "hello\nworld"}
|
||||
|
||||
|
||||
def test_a_run_that_fails_says_so(driver, nas, monkeypatch):
|
||||
monkeypatch.setattr(sj, "_sleep", lambda _s: None)
|
||||
job = _by_key(driver)[f"omv:{SYNC}"]
|
||||
nas.output = [("hello\n", True)]
|
||||
|
||||
def status(command: str, read_timeout: float = 100):
|
||||
"""The job ends with exit 3: OMV raises from the next ``Exec.getOutput``."""
|
||||
if "getOutput" in command and '"pos": 6' in command:
|
||||
return "ERROR: the job failed: exit 3", 1
|
||||
return nas.sudo_status(command, read_timeout)
|
||||
|
||||
driver._sudo_status = status # type: ignore[method-assign]
|
||||
result = driver.manage_scheduled_job(job["key"], "run", digest=job["digest"])
|
||||
|
||||
assert result["success"] is False
|
||||
assert "hello" in result["output"]
|
||||
assert "exit 3" in result["output"]
|
||||
|
||||
|
||||
def test_a_run_past_the_timeout_is_stopped(driver, nas, monkeypatch):
|
||||
clock = iter(range(0, 10_000, 200))
|
||||
monkeypatch.setattr(sj, "_sleep", lambda _s: None)
|
||||
monkeypatch.setattr(sj, "_monotonic", lambda: next(clock))
|
||||
nas.output = [("tick\n", True)] * 10
|
||||
job = _by_key(driver)[f"omv:{SYNC}"]
|
||||
|
||||
result = driver.manage_scheduled_job(job["key"], "run", digest=job["digest"])
|
||||
|
||||
assert nas.called("Exec.stop") == [{"filename": "/tmp/bgstatusQx1"}]
|
||||
assert result["success"] is False
|
||||
assert "stopped after 300 s" in result["output"]
|
||||
|
||||
|
||||
# -- an application's own job ----------------------------------------------------------------
|
||||
|
||||
|
||||
def _spec(**fields):
|
||||
spec = {
|
||||
"name": "rotate",
|
||||
"schedule": "*/10 1-3 * * 0",
|
||||
"run_as": "backup",
|
||||
"command": "rotate-logs\necho rotated",
|
||||
"enabled": True,
|
||||
"description": "Rotate the logs",
|
||||
}
|
||||
spec.update(fields)
|
||||
return spec
|
||||
|
||||
|
||||
def test_put_creates_an_omv_job(driver, nas):
|
||||
result = driver.put_scheduled_job(_spec())
|
||||
|
||||
(sent,) = nas.called("Cron.set")
|
||||
assert sent == {
|
||||
"uuid": NEW,
|
||||
"enable": True,
|
||||
"execution": "exactly",
|
||||
"sendemail": False,
|
||||
"comment": "napalm-scheduled-job: rotate Rotate the logs",
|
||||
"type": "userdefined",
|
||||
"minute": ["10"],
|
||||
"everynminute": True,
|
||||
"hour": ["1", "2", "3"],
|
||||
"everynhour": False,
|
||||
"month": ["*"],
|
||||
"dayofmonth": ["*"],
|
||||
"everyndayofmonth": False,
|
||||
"dayofweek": ["7"],
|
||||
"username": "backup",
|
||||
"command": "rotate-logs\necho rotated",
|
||||
}
|
||||
assert nas.called("Config.applyChanges") == [{"modules": ["cron"], "force": False}]
|
||||
assert result == {"success": True, "output": "omv:6b7c8d9e-4e5f-4061-8273-8495a6b7c8d9 created"}
|
||||
job = _by_key(driver)["omv:6b7c8d9e-4e5f-4061-8273-8495a6b7c8d9"]
|
||||
assert (job["managed_by"], job["name"], job["schedule"]) == (
|
||||
"application",
|
||||
"rotate",
|
||||
"*/10 1,2,3 * * 7",
|
||||
)
|
||||
|
||||
|
||||
def test_put_changes_the_applications_job_and_keeps_its_email_setting(driver, nas):
|
||||
job = _by_key(driver)[f"omv:{OWN}"]
|
||||
|
||||
result = driver.put_scheduled_job(
|
||||
_spec(name="cleanup", schedule="@daily", run_as="root"), digest=job["digest"]
|
||||
)
|
||||
|
||||
(sent,) = nas.called("Cron.set")
|
||||
assert (sent["uuid"], sent["execution"], sent["sendemail"]) == (OWN, "daily", True)
|
||||
assert result["output"] == f"omv:{OWN} changed"
|
||||
|
||||
|
||||
def test_put_of_a_name_already_there_without_digest_is_a_change_unseen(driver, nas):
|
||||
with pytest.raises(ScheduledJobChanged):
|
||||
driver.put_scheduled_job(_spec(name="cleanup"))
|
||||
assert nas.called("Cron.set") == []
|
||||
|
||||
|
||||
def test_put_with_a_digest_for_a_job_not_there_is_a_lookup_error(driver):
|
||||
with pytest.raises(LookupError):
|
||||
driver.put_scheduled_job(_spec(), digest="0123456789abcdef")
|
||||
|
||||
|
||||
def test_put_over_a_changed_job_is_refused(driver, nas):
|
||||
job = _by_key(driver)[f"omv:{OWN}"]
|
||||
nas.jobs[2]["command"] = "something else"
|
||||
|
||||
with pytest.raises(ScheduledJobChanged):
|
||||
driver.put_scheduled_job(_spec(name="cleanup"), digest=job["digest"])
|
||||
assert nas.called("Cron.set") == []
|
||||
|
||||
|
||||
def test_put_for_a_user_the_nas_does_not_have_is_refused(driver, nas):
|
||||
with pytest.raises(ScheduledJobRefused):
|
||||
driver.put_scheduled_job(_spec(run_as="nobody"))
|
||||
assert nas.called("Cron.set") == []
|
||||
|
||||
|
||||
def test_put_of_an_invalid_spec_is_refused_before_anything_runs(driver, nas):
|
||||
with pytest.raises(ScheduledJobRefused):
|
||||
driver.put_scheduled_job(_spec(schedule="61 * * * *"))
|
||||
assert nas.calls == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("schedule", "execution"),
|
||||
[
|
||||
("@reboot", "reboot"),
|
||||
("@hourly", "hourly"),
|
||||
("@daily", "daily"),
|
||||
("@midnight", "daily"),
|
||||
("@weekly", "weekly"),
|
||||
("@monthly", "monthly"),
|
||||
("@yearly", "yearly"),
|
||||
("@annually", "yearly"),
|
||||
],
|
||||
)
|
||||
def test_a_cron_alias_is_omvs_shortcut(schedule, execution):
|
||||
assert sj.omv_schedule(schedule)["execution"] == execution
|
||||
|
||||
|
||||
def test_a_step_on_month_and_weekday_is_written_as_its_values():
|
||||
fields = sj.omv_schedule("0 0 * */4 1-5/2")
|
||||
|
||||
assert fields["month"] == ["1", "5", "9"]
|
||||
assert fields["dayofweek"] == ["1", "3", "5"]
|
||||
|
||||
|
||||
def test_a_day_field_keeps_its_star_meaning():
|
||||
"""``1-31`` is not ``*``: with a weekday set, cron runs on either."""
|
||||
assert sj.omv_schedule("0 0 1-31 * 1")["dayofmonth"] == [str(d) for d in range(1, 32)]
|
||||
|
||||
|
||||
def test_a_schedule_cron_would_not_take_is_refused():
|
||||
with pytest.raises(ScheduledJobRefused):
|
||||
sj.omv_schedule("61 * * * *")
|
||||
|
||||
|
||||
def test_a_weekday_step_beside_a_day_of_month_is_refused():
|
||||
"""OMV has no ``*/n`` for weekdays; its values would turn cron's AND into an OR."""
|
||||
with pytest.raises(ScheduledJobRefused):
|
||||
sj.omv_schedule("0 0 1 * */2")
|
||||
|
||||
|
||||
def test_a_cron_schedule_reads_back_from_omvs_fields():
|
||||
for schedule in ("*/10 1,2,3 * * 7", "0 0 */2 1,6 *", "@reboot", "@yearly"):
|
||||
fields = {
|
||||
k: ",".join(v) if isinstance(v, list) else v
|
||||
for k, v in sj.omv_schedule(schedule).items()
|
||||
}
|
||||
assert sj.cron_schedule(fields) == schedule
|
||||
|
||||
|
||||
# -- deleting --------------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_delete_removes_the_applications_job(driver, nas):
|
||||
job = _by_key(driver)[f"omv:{OWN}"]
|
||||
|
||||
result = driver.delete_scheduled_job(job["key"], digest=job["digest"])
|
||||
|
||||
assert nas.called("Cron.delete") == [{"uuid": OWN}]
|
||||
assert nas.called("Config.applyChanges") == [{"modules": ["cron"], "force": False}]
|
||||
assert result == {"success": True, "output": f"omv:{OWN} deleted"}
|
||||
|
||||
|
||||
def test_delete_of_an_admins_job_is_refused(driver, nas):
|
||||
job = _by_key(driver)[f"omv:{SYNC}"]
|
||||
|
||||
with pytest.raises(ScheduledJobRefused):
|
||||
driver.delete_scheduled_job(job["key"], digest=job["digest"])
|
||||
assert nas.called("Cron.delete") == []
|
||||
|
||||
|
||||
def test_delete_of_a_changed_job_is_refused(driver, nas):
|
||||
job = _by_key(driver)[f"omv:{OWN}"]
|
||||
nas.jobs[2]["enable"] = False
|
||||
|
||||
with pytest.raises(ScheduledJobChanged):
|
||||
driver.delete_scheduled_job(job["key"], digest=job["digest"])
|
||||
assert nas.called("Cron.delete") == []
|
||||
|
||||
|
||||
def test_delete_of_any_other_job_is_the_linux_one(driver, nas):
|
||||
certbot = next(
|
||||
j for j in driver.get_scheduled_jobs()["jobs"] if j["source"] == "/etc/cron.d/certbot"
|
||||
)
|
||||
|
||||
with pytest.raises(ScheduledJobRefused):
|
||||
driver.delete_scheduled_job(certbot["key"], digest=certbot["digest"])
|
||||
assert _wrote(nas) == []
|
||||
|
||||
|
||||
def test_a_cron_module_with_changes_pending_refuses_every_write(driver, nas):
|
||||
"""An apply would put someone's half-finished web-UI edit live along with ours."""
|
||||
nas.dirty = ["apt", "cron"]
|
||||
job = _by_key(driver)[f"omv:{SYNC}"]
|
||||
with pytest.raises(ScheduledJobRefused, match="unapplied changes"):
|
||||
driver.manage_scheduled_job(job["key"], "disable", digest=job["digest"])
|
||||
assert nas.called("Cron.set") == [] and nas.called("Config.applyChanges") == []
|
||||
Reference in new issue
Block a user