Skip to content

Commit

Permalink
Migrate to systemd timers
Browse files Browse the repository at this point in the history
Timers are easier to debug and give more control about the scheduling.
  • Loading branch information
corny committed Jul 16, 2020
1 parent 29c56ee commit be7cceb
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 31 deletions.
10 changes: 9 additions & 1 deletion roles/icvpn/tasks/bird.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,13 @@
- name: Install cronjob to automatically update bgp bird configurations
template:
src: icvpn-update
dest: /etc/cron.hourly/icvpn-update
dest: /usr/local/bin/icvpn-update
mode: 0755

- name: Install timer
import_role:
name: timer
vars:
timer_name: icvpn-update
timer_exec: /usr/local/bin/icvpn-update
timer_interval: 1hour
16 changes: 12 additions & 4 deletions roles/mesh-announce/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,18 @@
name: respondd
enabled: yes

- name: Install cronjob to automatically update alfred announce
template:
src: cronjob
dest: /etc/cron.d/mesh-announce-alfred
- name: Remove legacy cronjob
file:
path: /etc/cron.d/mesh-announce-alfred
state: absent

- name: Install timer
import_role:
name: timer
vars:
timer_name: mesh-announce
timer_exec: "/opt/{{ site_code }}/mesh-announce/announce.sh -i {{ alfred_mtu_interface if alfred_master else main_bridge }} -b {{ batman_interface }}"
timer_interval: 1min
when: mesh_announce_alfred

- name: Open firewall for respondd
Expand Down
7 changes: 0 additions & 7 deletions roles/mesh-announce/templates/cronjob

This file was deleted.

19 changes: 12 additions & 7 deletions roles/meshviewer/tasks/geojson.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@
group: root
mode: 0755

- name: Install cronjob to copy valide geojson
template:
src: geojson.cronjob
dest: /etc/cron.d/geojson-update
owner: root
group: root
mode: 0644
- name: Install timer
import_role:
name: timer
vars:
timer_name: geojson-update
timer_exec: /usr/local/bin/geojson-update
timer_interval: 5min

- name: Remove legacy cronjob
file:
path: /etc/cron.d/geojson-update
state: absent
2 changes: 0 additions & 2 deletions roles/meshviewer/templates/geojson.cronjob

This file was deleted.

File renamed without changes.
28 changes: 18 additions & 10 deletions roles/nsd/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,30 @@

- name: Install script to generate nodes zone
copy:
src: zonegen.py
dest: /usr/local/bin/zonegen.py
src: "{{ item }}"
dest: /usr/local/bin/
owner: root
group: root
mode: 0755
with_items:
- zonegen.py
- zonegen-all

- name: Install cronjob to generate nodes zone
copy:
src: cronjob
dest: /etc/cron.hourly/zonegen
owner: root
group: root
mode: 0755
- name: Remove legacy cronjob
file:
path: /etc/cron.hourly/zonegen
state: absent

- name: Install timer
import_role:
name: timer
vars:
timer_name: zonegen
timer_exec: /usr/local/bin/zonegen-all
timer_interval: 60min

- name: Initially generate nodes zone
command: /etc/cron.hourly/zonegen
service: name=zonegen.service state=started

- name: Open firewall for DNS
copy:
Expand Down
7 changes: 7 additions & 0 deletions roles/timer/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
timer_name:
timer_descr:
timer_exec: /bin/false
timer_user: root
timer_interval:
timer_calendar:
timer_randomized_delay:
20 changes: 20 additions & 0 deletions roles/timer/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
- name: Fail when timer_name is empty
fail: msg="timer_name is empty"
when: not timer_name

- name: Upload service and timer
template:
src: "job.{{ item }}"
dest: /lib/systemd/system/{{ timer_name }}.{{ item }}
with_items:
- service
- timer
register: template_result

- name: Reload systemd
command: systemctl daemon-reload
when: template_result.changed

- name: Enable and start timer
service: {{ timer_name }}.timer enabled=yes state=started
10 changes: 10 additions & 0 deletions roles/timer/templates/service
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# {{ ansible_managed }}

[Unit]
Description={{ timer_descr | default(timer_name + " Timer") }}

[Service]
Type=oneshot
User={{ timer_user }}

ExecStart={{ timer_exec }}
20 changes: 20 additions & 0 deletions roles/timer/templates/timer
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# {{ ansible_managed }}

[Unit]
Description={{ timer_name }} Service

[Timer]
Unit={{ timer_name }}.service
{% if timer_calendar is defined %}
OnCalendar={{ timer_calendar }}
{% else %}
OnBootSec={{ timer_startup_delay | default("60") }}
OnUnitInactiveSec={{ timer_interval }}
{% endif %}

{% if timer_randomized_delay is defined %}
RandomizedDelaySec={{ timer_randomized_delay }}
{% endif %}

[Install]
WantedBy=basic.target

0 comments on commit be7cceb

Please sign in to comment.