diff --git a/bundles/uberspace-crontab-dir/files/crontab-dir-generate-crontab.sh b/bundles/uberspace-crontab-dir/files/crontab-dir-generate-crontab.sh new file mode 100755 index 0000000..db70fc2 --- /dev/null +++ b/bundles/uberspace-crontab-dir/files/crontab-dir-generate-crontab.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +set -euo pipefail + +echo "# Generated from ~/.config/cron.d" +echo "# Use crontab-dir-update to regenerate" +echo +echo "PATH=$HOME/.local/bin:$HOME/bin:\$PATH" +echo + +find $HOME/.config/cron.d -mindepth 1 -maxdepth 1 -type f | sort | xargs cat diff --git a/bundles/uberspace-crontab-dir/files/crontab-dir-is-sync.sh b/bundles/uberspace-crontab-dir/files/crontab-dir-is-sync.sh new file mode 100755 index 0000000..e404136 --- /dev/null +++ b/bundles/uberspace-crontab-dir/files/crontab-dir-is-sync.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +set -euo pipefail + +if [[ "$(crontab-dir-generate-crontab)" == "$(crontab -l)" ]]; then + echo "crontab is up to date" + exit 0 +else + echo "crontab needs update" + exit 1 +fi diff --git a/bundles/uberspace-crontab-dir/files/crontab-dir-update.sh b/bundles/uberspace-crontab-dir/files/crontab-dir-update.sh new file mode 100755 index 0000000..9ed01c2 --- /dev/null +++ b/bundles/uberspace-crontab-dir/files/crontab-dir-update.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +set -euo pipefail + +crontab-dir-generate-crontab | crontab - diff --git a/bundles/uberspace-crontab-dir/items.py b/bundles/uberspace-crontab-dir/items.py new file mode 100644 index 0000000..45867f5 --- /dev/null +++ b/bundles/uberspace-crontab-dir/items.py @@ -0,0 +1,46 @@ +uberspaceify = repo.libs.uberspace.Uberspaceify(node) + +directories = uberspaceify.directories({ + f'/home/{node.username}/.config/cron.d': { + "purge": True, + }, +}) + +crontab_files = {} +for crontab_name, crontab_line in node.metadata.get("cron/jobs", {}).items(): + crontab_files[f'/home/{node.username}/.config/cron.d/{crontab_name}'] = { + "content": crontab_line + "\n", + } + +files = uberspaceify.files({ + f'/home/{node.username}/bin/crontab-dir-generate-crontab': { + "source": "crontab-dir-generate-crontab.sh", + "mode": "0750", + }, + f'/home/{node.username}/bin/crontab-dir-is-sync': { + "source": "crontab-dir-is-sync.sh", + "mode": "0750", + "needs": [ + f'file:/home/{node.username}/bin/crontab-dir-generate-crontab', + ], + }, + f'/home/{node.username}/bin/crontab-dir-update': { + "source": "crontab-dir-update.sh", + "mode": "0750", + "needs": [ + f'file:/home/{node.username}/bin/crontab-dir-generate-crontab', + ], + }, + **crontab_files, +}) + +actions = { + "update_crontab": { + "command": 'crontab-dir-update', + "unless": 'crontab-dir-is-sync', + "needs": [ + f'file:/home/{node.username}/bin/crontab-dir-update', + f'file:/home/{node.username}/bin/crontab-dir-is-sync', + ], + }, +} diff --git a/nodes.py b/nodes.py index 1e92614..3ae84ab 100644 --- a/nodes.py +++ b/nodes.py @@ -6,6 +6,7 @@ nodes = uberspaceify.nodes({ "bundles": ( "uberspace-redirect-clerie", "uberspace-clerie-backup", + "uberspace-crontab-dir", ), "metadata": { "clerie-backup": { @@ -32,6 +33,12 @@ nodes = uberspaceify.nodes({ }, }, }, + "cron": { + "jobs": { + "backup_main-cyan": "42 */2 * * * clerie-backup main-cyan backup", + "backup_main-magenta": "13 */2 * * * clerie-backup main-magenta backup", + }, + }, }, }, "cleriewi.uber.space": {