Skip to content

Commit

Permalink
feat: easier way to define timers
Browse files Browse the repository at this point in the history
  • Loading branch information
c0rydoras committed Jul 14, 2023
1 parent 844065d commit 107012a
Show file tree
Hide file tree
Showing 14 changed files with 324 additions and 36 deletions.
25 changes: 25 additions & 0 deletions systemd/generate-timers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env python

from os import mkdir
from shutil import rmtree
from tomllib import loads

from jinja2 import Environment, FileSystemLoader

if __name__ == "__main__":
with open("./timers.toml", "r") as f:
timers = loads(f.read())["timer"]
if not timers:
exit()
env = Environment(loader=FileSystemLoader("./templates"))
templates = {
"service": env.get_template("service"),
"timer": env.get_template("timer"),
}
rmtree("output")
mkdir("output")
for timer in timers:
base_path = "./output/outdated-" + timer["command"]
for type in ["service", "timer"]:
with open(f"{base_path}.{type}", "w") as f:
f.write(templates[type].render(**timer))
9 changes: 0 additions & 9 deletions systemd/notifications.service

This file was deleted.

9 changes: 0 additions & 9 deletions systemd/notifications.timer

This file was deleted.

9 changes: 9 additions & 0 deletions systemd/output/outdated-notify.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[Unit]
Description=Notify maintainers about their projects if necessary
Wants=outdated-notify.timer

[Service]
ExecStart=docker exec outdated-api sh -c './manage.py notify'

[Install]
WantedBy=multi-user.target
9 changes: 9 additions & 0 deletions systemd/output/outdated-notify.timer
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[Unit]
Description=Timer for Notify maintainers about their projects if necessary

[Timer]
Unit=outdated-notify.service
OnCalendar=Mon *-*-* 00:00:00

[Install]
WantedBy=timers.target
9 changes: 9 additions & 0 deletions systemd/output/outdated-syncprojects.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[Unit]
Description=Sync all projects with their remote counterparts
Wants=outdated-syncprojects.timer

[Service]
ExecStart=docker exec outdated-api sh -c './manage.py syncprojects'

[Install]
WantedBy=multi-user.target
9 changes: 9 additions & 0 deletions systemd/output/outdated-syncprojects.timer
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[Unit]
Description=Timer for Sync all projects with their remote counterparts

[Timer]
Unit=outdated-syncprojects.service
OnCalendar=*-*-* 00:00:00

[Install]
WantedBy=timers.target
216 changes: 216 additions & 0 deletions systemd/poetry.lock

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions systemd/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[tool.poetry]
name = "systemd-timer-creation-tool"
version = "0.1.0"
description = "create systemd timers without boilerplate"
authors = ["Adfinis AG"]
readme = "README.md"
packages = [{include = "systemd_timer_creation_tool"}]

[tool.poetry.dependencies]
python = "^3.11"
jinja2 = "^3.1.2"


[tool.poetry.group.dev.dependencies]
black = "^23.7.0"
isort = "^5.12.0"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
9 changes: 0 additions & 9 deletions systemd/syncprojects.service

This file was deleted.

9 changes: 0 additions & 9 deletions systemd/syncprojects.timer

This file was deleted.

9 changes: 9 additions & 0 deletions systemd/templates/service
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[Unit]
Description={{description}}
Wants=outdated-{{command}}.timer

[Service]
ExecStart=docker exec outdated-api sh -c './manage.py {{command}}'

[Install]
WantedBy=multi-user.target
9 changes: 9 additions & 0 deletions systemd/templates/timer
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[Unit]
Description=Timer for {{description}}

[Timer]
Unit=outdated-{{command}}.service
OnCalendar={{schedule}}

[Install]
WantedBy=timers.target
9 changes: 9 additions & 0 deletions systemd/timers.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[[timer]]
description="Sync all projects with their remote counterparts"
command="syncprojects"
schedule="*-*-* 00:00:00"

[[timer]]
description="Notify maintainers about their projects if necessary"
command="notify"
schedule="Mon *-*-* 00:00:00"

0 comments on commit 107012a

Please sign in to comment.