Scheduled jobs on OpenWrt through napalm-device-types' PosixScheduledJobsMixin: BusyBox crond's crontabs in /etc/crontabs/<user> are read, switched, run now, and an application's own job is written as marked lines in the user's crontab.
Changes
OpenWrtScheduledJobsMixin (napalm_openwrt/scheduled_jobs_mixin.py), mixed into OpenWrtDriver: no /etc/crontab, cron.d, run-parts, anacron or systemd; the spool is /etc/crontabs; own jobs go into the user's crontab.
Time zone from uci -q get system.@system[0].zonename (LuCI's spaces become underscores); owners from opkg search, none on apk-based OpenWrt.
After every crontab write: /etc/init.d/cron enable && /etc/init.d/cron restart (OpenWrt does not start crond while /etc/crontabs is empty).
BusyBox differences the driver handles itself:
a crontab is read from its file: crontab -l cannot tell a missing crontab from an unreadable one; one that cannot be read is never written;
every write is read back: BusyBox crontab FILE exits 0 when it could not write;
@daily and the other aliases are written as five fields, @reboot is refused (FEATURE_CROND_SPECIAL_TIMES is off in OpenWrt's build);
a run is the command as BusyBox crond runs it: whole command to sh -c, % is no stdin, only SHELL/PATH from the crontab, HOME/LOGNAME/USER set, from the user's home;
no su in OpenWrt's BusyBox: another user's job runs through start-stop-daemon -S -p /dev/null -c <user> -x /bin/sh; root needs none;
timeout is in OpenWrt's BusyBox only from 25.x: the run is bounded by it where command -v timeout finds it, else only by the channel's wait.
stat, cksum and install (absent in OpenWrt's BusyBox) are never reached: only crontabs are written, through crontab.
Every write changes only the job's own line; netOrk's reboot failback line in /etc/crontabs/root is left as it is.
Floor on napalm-device-types raised to 4.26.0; CHANGELOG and README method table.
tests/unit/test_scheduled_jobs.py: 27 tests run by a real sh over a directory standing in for the router, PATH holding only applets OpenWrt's BusyBox is built with (BusyBox's own where the test host has BusyBox), and logging fakes for crontab (with BusyBox's exit-0-on-failure), uci, opkg, apk, start-stop-daemon, timeout and the cron init script. Covers the failback line surviving switch, put, change and delete byte for byte.
Applet availability checked against OpenWrt's package/utils/busybox/Config-defaults.in for 21.02, 22.03, 23.05, 24.10 and main; BusyBox crontab/crond/start-stop-daemon behaviour checked against the 1.36 sources. The reading command is far below dropbear's 9000-byte MAX_CMD_LEN (run_command is an exec channel, so ash's 512-character typed-line limit does not apply).
Full suite 273 passed; ruff, mypy, napalm-standard check and ratchet, bandit clean. Not tested on a router.
## Summary
Scheduled jobs on OpenWrt through napalm-device-types' `PosixScheduledJobsMixin`: BusyBox crond's crontabs in `/etc/crontabs/<user>` are read, switched, run now, and an application's own job is written as marked lines in the user's crontab.
## Changes
- `OpenWrtScheduledJobsMixin` (`napalm_openwrt/scheduled_jobs_mixin.py`), mixed into `OpenWrtDriver`: no `/etc/crontab`, `cron.d`, run-parts, anacron or systemd; the spool is `/etc/crontabs`; own jobs go into the user's crontab.
- Time zone from `uci -q get system.@system[0].zonename` (LuCI's spaces become underscores); owners from `opkg search`, none on apk-based OpenWrt.
- After every crontab write: `/etc/init.d/cron enable && /etc/init.d/cron restart` (OpenWrt does not start crond while `/etc/crontabs` is empty).
- BusyBox differences the driver handles itself:
- a crontab is read from its file: `crontab -l` cannot tell a missing crontab from an unreadable one; one that cannot be read is never written;
- every write is read back: BusyBox `crontab FILE` exits 0 when it could not write;
- `@daily` and the other aliases are written as five fields, `@reboot` is refused (`FEATURE_CROND_SPECIAL_TIMES` is off in OpenWrt's build);
- a run is the command as BusyBox crond runs it: whole command to `sh -c`, `%` is no stdin, only `SHELL`/`PATH` from the crontab, `HOME`/`LOGNAME`/`USER` set, from the user's home;
- no `su` in OpenWrt's BusyBox: another user's job runs through `start-stop-daemon -S -p /dev/null -c <user> -x /bin/sh`; root needs none;
- `timeout` is in OpenWrt's BusyBox only from 25.x: the run is bounded by it where `command -v timeout` finds it, else only by the channel's wait.
- `stat`, `cksum` and `install` (absent in OpenWrt's BusyBox) are never reached: only crontabs are written, through `crontab`.
- Every write changes only the job's own line; netOrk's reboot failback line in `/etc/crontabs/root` is left as it is.
- Floor on napalm-device-types raised to 4.26.0; CHANGELOG and README method table.
## Related Issue
Closes #32
Part of NetOrk/netork#1933 (builds on NAPALM/napalm-device-types#87).
## Testing
- `tests/unit/test_scheduled_jobs.py`: 27 tests run by a real `sh` over a directory standing in for the router, `PATH` holding only applets OpenWrt's BusyBox is built with (BusyBox's own where the test host has BusyBox), and logging fakes for `crontab` (with BusyBox's exit-0-on-failure), `uci`, `opkg`, `apk`, `start-stop-daemon`, `timeout` and the cron init script. Covers the failback line surviving switch, put, change and delete byte for byte.
- Applet availability checked against OpenWrt's `package/utils/busybox/Config-defaults.in` for 21.02, 22.03, 23.05, 24.10 and main; BusyBox `crontab`/`crond`/`start-stop-daemon` behaviour checked against the 1.36 sources. The reading command is far below dropbear's 9000-byte `MAX_CMD_LEN` (run_command is an exec channel, so ash's 512-character typed-line limit does not apply).
- Full suite 273 passed; ruff, mypy, napalm-standard check and ratchet, bandit clean. Not tested on a router.
get_scheduled_jobs reads every crontab in /etc/crontabs in one round trip
as root, with the time zone from UCI and each crontab's package from opkg;
manage_scheduled_job enables, disables or runs one now; put_ and
delete_scheduled_job write and remove an application's own job as marked
lines in the user's crontab. BusyBox crond has no /etc/crontab, cron.d,
run-parts, anacron or systemd, so none is read.
OpenWrt's BusyBox differs from what the POSIX mixin assumes, so a few
steps are the driver's own:
- crontab -l says only "can't open" for a missing crontab, as for an
unreadable one: the file is read directly, a missing one is empty, and
one that cannot be read is never written.
- crontab FILE exits 0 when it could not write: every write is read back.
- cron is enabled and restarted after each write; OpenWrt does not start
crond while /etc/crontabs is empty.
- crond has no special times: aliases are written as five fields and
@reboot is refused.
- crond hands the whole command to sh and takes only SHELL and PATH from
the crontab, so a run now does the same (% is no stdin).
- there is no su and no timeout before 25.x: another user's job runs
through start-stop-daemon -c, and the run is bounded by timeout only
where the router has it.
Every write changes only the job's own line, so the reboot failback line
in /etc/crontabs/root stays as it is.
Requires napalm-device-types 4.26.0.
Closes#32
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Summary
Scheduled jobs on OpenWrt through napalm-device-types'
PosixScheduledJobsMixin: BusyBox crond's crontabs in/etc/crontabs/<user>are read, switched, run now, and an application's own job is written as marked lines in the user's crontab.Changes
OpenWrtScheduledJobsMixin(napalm_openwrt/scheduled_jobs_mixin.py), mixed intoOpenWrtDriver: no/etc/crontab,cron.d, run-parts, anacron or systemd; the spool is/etc/crontabs; own jobs go into the user's crontab.uci -q get system.@system[0].zonename(LuCI's spaces become underscores); owners fromopkg search, none on apk-based OpenWrt./etc/init.d/cron enable && /etc/init.d/cron restart(OpenWrt does not start crond while/etc/crontabsis empty).crontab -lcannot tell a missing crontab from an unreadable one; one that cannot be read is never written;crontab FILEexits 0 when it could not write;@dailyand the other aliases are written as five fields,@rebootis refused (FEATURE_CROND_SPECIAL_TIMESis off in OpenWrt's build);sh -c,%is no stdin, onlySHELL/PATHfrom the crontab,HOME/LOGNAME/USERset, from the user's home;suin OpenWrt's BusyBox: another user's job runs throughstart-stop-daemon -S -p /dev/null -c <user> -x /bin/sh; root needs none;timeoutis in OpenWrt's BusyBox only from 25.x: the run is bounded by it wherecommand -v timeoutfinds it, else only by the channel's wait.stat,cksumandinstall(absent in OpenWrt's BusyBox) are never reached: only crontabs are written, throughcrontab./etc/crontabs/rootis left as it is.Related Issue
Closes #32
Part of NetOrk/netork#1933 (builds on NAPALM/napalm-device-types#87).
Testing
tests/unit/test_scheduled_jobs.py: 27 tests run by a realshover a directory standing in for the router,PATHholding only applets OpenWrt's BusyBox is built with (BusyBox's own where the test host has BusyBox), and logging fakes forcrontab(with BusyBox's exit-0-on-failure),uci,opkg,apk,start-stop-daemon,timeoutand the cron init script. Covers the failback line surviving switch, put, change and delete byte for byte.package/utils/busybox/Config-defaults.infor 21.02, 22.03, 23.05, 24.10 and main; BusyBoxcrontab/crond/start-stop-daemonbehaviour checked against the 1.36 sources. The reading command is far below dropbear's 9000-byteMAX_CMD_LEN(run_command is an exec channel, so ash's 512-character typed-line limit does not apply).