feat(scheduled-jobs): scheduled jobs on Proxmox VE and Backup Server (#56)
driver / lint (pull_request) Successful in 15s
driver / standard (pull_request) Successful in 19s
driver / types (pull_request) Failing after 28s
driver / test (3.13) (pull_request) Failing after 15s
driver / test (3.12) (pull_request) Failing after 17s
driver / lowest (pull_request) Failing after 19s
driver / build (pull_request) Successful in 33s
driver / security (pull_request) Failing after 37s
CI / driver (pull_request) Failing after 1m12s
driver / publish (pull_request) Skipped
driver / lint (pull_request) Successful in 15s
driver / standard (pull_request) Successful in 19s
driver / types (pull_request) Failing after 28s
driver / test (3.13) (pull_request) Failing after 15s
driver / test (3.12) (pull_request) Failing after 17s
driver / lowest (pull_request) Failing after 19s
driver / build (pull_request) Successful in 33s
driver / security (pull_request) Failing after 37s
CI / driver (pull_request) Failing after 1m12s
driver / publish (pull_request) Skipped
Both drivers mix in napalm-device-types' PosixScheduledJobsMixin over the node's root SSH session: get_scheduled_jobs reads cron, run-parts, anacron and every systemd timer in one round trip; manage_scheduled_job enables, disables or runs one now; put_ and delete_scheduled_job write and remove an application's own job. What Proxmox ships is platform, so it is only switched and run: pve-* units, pvesr.timer and /etc/cron.d/vzdump on Proxmox VE, proxmox-backup* units on a backup server. A vzdump cron line is only run, because the file is a link into /etc/pve and rewriting it would replace the link with a plain file. Backup jobs (/cluster/backup), replication and a backup server's verify, prune, sync and GC jobs run in Proxmox's own daemons and stay with the backup features. Requires napalm-device-types 4.26.0. Closes #56
This commit is contained in:
1 parent
d369f1ad76
commit
dce794644c
7 files changed
+220
-1
No files matched your search
@@ -0,0 +1,137 @@
|
||||
"""Scheduled jobs on a Proxmox VE node and a Proxmox Backup Server (#56).
|
||||
|
||||
The report, its parse and every write are napalm-device-types'
|
||||
``PosixScheduledJobsMixin``'s; the drivers carry the command across
|
||||
``run_command`` as root in one round trip and say which jobs Proxmox itself
|
||||
ships (``platform``).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
from napalm_device_types import CommandResult, PosixScheduledJobsMixin, ScheduledJobRefused
|
||||
|
||||
from napalm_proxmox import ProxmoxBackupDriver, ProxmoxDriver
|
||||
|
||||
_TIMER = """|Id={name}.timer
|
||||
|Unit={name}.service
|
||||
|Description={name}
|
||||
|TimersCalendar={{ OnCalendar=*-*-* 06:00:00 ; next_elapse=n/a }}
|
||||
|NextElapseUSecRealtime=@1762236000
|
||||
|LastTriggerUSec=@1762149600
|
||||
|UnitFileState=enabled
|
||||
|ActiveState=active
|
||||
|FragmentPath={path}
|
||||
|"""
|
||||
|
||||
_UNIT = """|Id={name}.service
|
||||
|ExecStart={{ path=/usr/bin/{name} ; argv[]=/usr/bin/{name} ; }}
|
||||
|Result=success
|
||||
|FragmentPath={path}
|
||||
|"""
|
||||
|
||||
|
||||
def _report(*timers: tuple[str, str]) -> str:
|
||||
return "\n".join(
|
||||
[
|
||||
"SJ_BEGIN",
|
||||
"[system /etc/cron.d/vzdump]",
|
||||
"|0 1 * * 6 root vzdump 100 --mode snapshot --quiet 1",
|
||||
"[system /etc/cron.d/zfsutils-linux]",
|
||||
"|0 0 * * 0 root /usr/lib/zfs-linux/scrub",
|
||||
"[user /var/spool/cron/crontabs/root]",
|
||||
"|*/5 * * * * /root/check.sh",
|
||||
"[systemd]",
|
||||
"[timers]",
|
||||
*(_TIMER.format(name=n, path=p) for n, p in timers),
|
||||
"[units]",
|
||||
*(_UNIT.format(name=n, path=p.replace(".timer", ".service")) for n, p in timers),
|
||||
"[tz]",
|
||||
"/usr/share/zoneinfo/Europe/Berlin",
|
||||
"[owners]",
|
||||
"zfsutils-linux: /etc/cron.d/zfsutils-linux",
|
||||
*(f"{n.split('-')[0]}: {p}" for n, p in timers),
|
||||
"SJ_END",
|
||||
"",
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
PVE_REPORT = _report(
|
||||
("pve-daily-update", "/lib/systemd/system/pve-daily-update.timer"),
|
||||
("pvesr", "/usr/lib/systemd/system/pvesr.timer"),
|
||||
("apt-daily", "/usr/lib/systemd/system/apt-daily.timer"),
|
||||
)
|
||||
PBS_REPORT = _report(
|
||||
("proxmox-backup-daily-update", "/usr/lib/systemd/system/proxmox-backup-daily-update.timer"),
|
||||
("pve-daily-update", "/lib/systemd/system/pve-daily-update.timer"),
|
||||
("apt-daily", "/usr/lib/systemd/system/apt-daily.timer"),
|
||||
)
|
||||
VERBS = ("get_scheduled_jobs", "manage_scheduled_job", "put_scheduled_job", "delete_scheduled_job")
|
||||
|
||||
|
||||
def _driver(cls: type, report: str) -> MagicMock:
|
||||
driver = cls("host", "root@pam", "pw")
|
||||
driver.run_command = MagicMock(return_value=CommandResult(report, "", 0))
|
||||
return driver
|
||||
|
||||
|
||||
def _by_source(driver: MagicMock) -> dict[str, dict[str, object]]:
|
||||
return {str(j["source"]): dict(j) for j in driver.get_scheduled_jobs()["jobs"]}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("cls", "report"),
|
||||
[(ProxmoxDriver, PVE_REPORT), (ProxmoxBackupDriver, PBS_REPORT)],
|
||||
ids=["pve", "pbs"],
|
||||
)
|
||||
def test_each_driver_offers_every_verb_and_reads_as_root_once(cls: type, report: str) -> None:
|
||||
assert issubclass(cls, PosixScheduledJobsMixin)
|
||||
for verb in VERBS:
|
||||
assert callable(getattr(cls, verb))
|
||||
driver = _driver(cls, report)
|
||||
reading = driver.get_scheduled_jobs()
|
||||
assert reading["time_zone"] == "Europe/Berlin"
|
||||
assert len(reading["jobs"]) == 6
|
||||
driver.run_command.assert_called_once()
|
||||
assert driver.run_command.call_args.kwargs["privileged"] is True
|
||||
|
||||
|
||||
def test_what_proxmox_ve_ships_is_platform() -> None:
|
||||
jobs = _by_source(_driver(ProxmoxDriver, PVE_REPORT))
|
||||
managed = {source: job["managed_by"] for source, job in jobs.items()}
|
||||
assert managed == {
|
||||
"/lib/systemd/system/pve-daily-update.timer": "platform",
|
||||
"/usr/lib/systemd/system/pvesr.timer": "platform",
|
||||
"/etc/cron.d/vzdump": "platform",
|
||||
"/usr/lib/systemd/system/apt-daily.timer": "package",
|
||||
"/etc/cron.d/zfsutils-linux": "package",
|
||||
"/var/spool/cron/crontabs/root": "admin",
|
||||
}
|
||||
assert jobs["/lib/systemd/system/pve-daily-update.timer"]["actions"] == [
|
||||
"enable",
|
||||
"disable",
|
||||
"run",
|
||||
]
|
||||
|
||||
|
||||
def test_what_a_backup_server_ships_is_platform() -> None:
|
||||
jobs = _by_source(_driver(ProxmoxBackupDriver, PBS_REPORT))
|
||||
managed = {source: job["managed_by"] for source, job in jobs.items()}
|
||||
assert managed["/usr/lib/systemd/system/proxmox-backup-daily-update.timer"] == "platform"
|
||||
# Proxmox VE's names mean nothing on a backup server.
|
||||
assert managed["/lib/systemd/system/pve-daily-update.timer"] == "package"
|
||||
assert managed["/etc/cron.d/vzdump"] == "admin"
|
||||
assert managed["/usr/lib/systemd/system/apt-daily.timer"] == "package"
|
||||
|
||||
|
||||
def test_a_vzdump_cron_line_only_runs() -> None:
|
||||
"""``/etc/cron.d/vzdump`` links into ``/etc/pve``; switching it would replace the link."""
|
||||
driver = _driver(ProxmoxDriver, PVE_REPORT)
|
||||
job = _by_source(driver)["/etc/cron.d/vzdump"]
|
||||
assert job["actions"] == ["run"]
|
||||
with pytest.raises(ScheduledJobRefused):
|
||||
driver.manage_scheduled_job(str(job["key"]), "disable", digest=str(job["digest"]))
|
||||
assert driver.run_command.call_count == 2 # two readings, nothing written
|
||||
Reference in new issue
Block a user