diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e5251a..97e877f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,9 @@ All notable changes to this driver. The format follows 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: `. Each write applies the `cron` module alone, - so changes pending in the web UI for other modules stay pending. The cron files OMV + 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 diff --git a/napalm_openmediavault/_scheduled_jobs.py b/napalm_openmediavault/_scheduled_jobs.py index 8ccedaf..6beb0db 100644 --- a/napalm_openmediavault/_scheduled_jobs.py +++ b/napalm_openmediavault/_scheduled_jobs.py @@ -23,7 +23,9 @@ They are switched with ``Cron.set`` (the job as it is, but ``enable``), run 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. +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 @@ -74,7 +76,9 @@ 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:" -_APPLY = {"modules": ["cron"], "force": False} +#: 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 = { @@ -236,6 +240,8 @@ class OmvScheduledJobsMixin(PosixScheduledJobsMixin): timeout: float = 60, ) -> Any: ... + def _omv_dirty_modules(self) -> list[str]: ... + # -- reading --------------------------------------------------------------------------------- def _sj_read(self) -> Reading: @@ -278,8 +284,17 @@ class OmvScheduledJobsMixin(PosixScheduledJobsMixin): # -- 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 @@ -404,6 +419,7 @@ class OmvScheduledJobsMixin(PosixScheduledJobsMixin): 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"} diff --git a/tests/test_scheduled_jobs.py b/tests/test_scheduled_jobs.py index 5c71616..398b12e 100644 --- a/tests/test_scheduled_jobs.py +++ b/tests/test_scheduled_jobs.py @@ -131,6 +131,8 @@ class Nas: 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]}" @@ -694,3 +696,12 @@ def test_delete_of_any_other_job_is_the_linux_one(driver, nas): 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") == []