From c0ef35b39422148bb4a435d5edd29c5d6e37d337 Mon Sep 17 00:00:00 2001 From: Julian Kornberger Date: Thu, 16 Jul 2020 11:43:45 +0200 Subject: [PATCH 1/3] Migrate to systemd timers Timers are easier to debug and give more control about the scheduling. --- roles/icvpn/tasks/bird.yml | 15 +++++++++- roles/mesh-announce/tasks/main.yml | 19 +++++++----- roles/mesh-announce/templates/cronjob | 7 ----- roles/meshviewer/tasks/geojson.yml | 19 +++++++----- roles/meshviewer/templates/geojson.cronjob | 2 -- roles/nsd/files/{cronjob => zonegen-all} | 2 +- roles/nsd/tasks/main.yml | 35 ++++++++++++++-------- roles/timer/defaults/main.yml | 8 +++++ roles/timer/tasks/main.yml | 24 +++++++++++++++ roles/timer/templates/service | 10 +++++++ roles/timer/templates/timer | 20 +++++++++++++ 11 files changed, 123 insertions(+), 38 deletions(-) delete mode 100644 roles/mesh-announce/templates/cronjob delete mode 100644 roles/meshviewer/templates/geojson.cronjob rename roles/nsd/files/{cronjob => zonegen-all} (97%) create mode 100644 roles/timer/defaults/main.yml create mode 100644 roles/timer/tasks/main.yml create mode 100644 roles/timer/templates/service create mode 100644 roles/timer/templates/timer diff --git a/roles/icvpn/tasks/bird.yml b/roles/icvpn/tasks/bird.yml index 26e6625c..3a870d96 100644 --- a/roles/icvpn/tasks/bird.yml +++ b/roles/icvpn/tasks/bird.yml @@ -14,7 +14,20 @@ - 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 owner: root group: root + +- name: Install timer + import_role: + name: timer + vars: + timer_name: icvpn-update + timer_exec: /usr/local/bin/icvpn-update + timer_interval: 1hour + +- name: Remove legacy cronjob + file: + path: /etc/cron.hourly/icvpn-update + state: absent diff --git a/roles/mesh-announce/tasks/main.yml b/roles/mesh-announce/tasks/main.yml index e7ef8c68..f9981fb9 100644 --- a/roles/mesh-announce/tasks/main.yml +++ b/roles/mesh-announce/tasks/main.yml @@ -27,15 +27,20 @@ name: respondd enabled: yes -- name: Install cronjob to automatically update alfred announce - template: - src: cronjob - dest: /etc/cron.d/mesh-announce-alfred - mode: 0644 - owner: root - group: root +- 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: Remove legacy cronjob + file: + path: /etc/cron.d/mesh-announce-alfred + state: absent + - name: Open firewall for respondd template: src: firewall.sh diff --git a/roles/mesh-announce/templates/cronjob b/roles/mesh-announce/templates/cronjob deleted file mode 100644 index 55876ef1..00000000 --- a/roles/mesh-announce/templates/cronjob +++ /dev/null @@ -1,7 +0,0 @@ -# {{ ansible_managed }} -PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin -{% if alfred_master %} -* * * * * root sh /opt/{{ site_code }}/mesh-announce/announce.sh -i {{ alfred_mtu_interface }} -b {{ batman_interface }} -{% else %} -* * * * * root sh /opt/{{ site_code }}/mesh-announce/announce.sh -i {{ main_bridge }} -b {{ batman_interface }} -{% endif %} diff --git a/roles/meshviewer/tasks/geojson.yml b/roles/meshviewer/tasks/geojson.yml index 9be34246..9f8d23f4 100644 --- a/roles/meshviewer/tasks/geojson.yml +++ b/roles/meshviewer/tasks/geojson.yml @@ -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 diff --git a/roles/meshviewer/templates/geojson.cronjob b/roles/meshviewer/templates/geojson.cronjob deleted file mode 100644 index dfe333c6..00000000 --- a/roles/meshviewer/templates/geojson.cronjob +++ /dev/null @@ -1,2 +0,0 @@ -PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin -*/5 * * * * root /usr/local/bin/geojson-update diff --git a/roles/nsd/files/cronjob b/roles/nsd/files/zonegen-all similarity index 97% rename from roles/nsd/files/cronjob rename to roles/nsd/files/zonegen-all index 0129782c..a765bece 100644 --- a/roles/nsd/files/cronjob +++ b/roles/nsd/files/zonegen-all @@ -29,4 +29,4 @@ --nserver dns.bremen.freifunk.net ns2.afraid.org ns2.he.net \ --file=/var/lib/nsd/0.b.a.b.b.b.f.f.2.8.7.8.6.0.a.2.ip6.arpa.zone --reverse nodes.ffhb.de. || exit -nsd-control reload > /dev/null +nsd-control reload diff --git a/roles/nsd/tasks/main.yml b/roles/nsd/tasks/main.yml index 23c84565..81119dec 100644 --- a/roles/nsd/tasks/main.yml +++ b/roles/nsd/tasks/main.yml @@ -29,26 +29,35 @@ owner: root group: root +- name: Copy zone files + command: /usr/local/sbin/update-dns-zones.sh + - name: Install script to generate nodes zone copy: - src: zonegen.py - dest: /usr/local/bin/zonegen.py - mode: 0755 + src: "{{ item }}" + dest: /usr/local/bin/ owner: root group: root - -- name: Install cronjob to generate nodes zone - copy: - src: cronjob - dest: /etc/cron.hourly/zonegen mode: 0755 - owner: root - group: root + with_items: + - zonegen.py + - zonegen-all + +- name: Install timer + import_role: + name: timer + vars: + timer_name: zonegen + timer_exec: /usr/local/bin/zonegen-all + timer_interval: 60min + +- name: Remove legacy cronjob + file: + path: /etc/cron.hourly/zonegen + state: absent - name: Initially generate nodes zone - command: - cmd: /etc/cron.hourly/zonegen - creates: /var/lib/nsd/nodes.bremen.freifunk.net.zone + service: name=zonegen.service state=started - name: Open firewall for DNS copy: diff --git a/roles/timer/defaults/main.yml b/roles/timer/defaults/main.yml new file mode 100644 index 00000000..4aebd4c6 --- /dev/null +++ b/roles/timer/defaults/main.yml @@ -0,0 +1,8 @@ +timer_name: +timer_descr: "{{ timer_name }} Timer" +timer_exec: /bin/false +timer_user: root +timer_interval: +timer_calendar: false +timer_randomized_delay: +timer_startup_delay: 60 diff --git a/roles/timer/tasks/main.yml b/roles/timer/tasks/main.yml new file mode 100644 index 00000000..ff21528c --- /dev/null +++ b/roles/timer/tasks/main.yml @@ -0,0 +1,24 @@ +--- +- 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: + name: "{{ timer_name }}.timer" + daemon_reload: yes + enabled: yes + state: started diff --git a/roles/timer/templates/service b/roles/timer/templates/service new file mode 100644 index 00000000..1c613730 --- /dev/null +++ b/roles/timer/templates/service @@ -0,0 +1,10 @@ +# {{ ansible_managed }} + +[Unit] +Description={{ timer_descr }} + +[Service] +Type=oneshot +User={{ timer_user }} + +ExecStart={{ timer_exec }} diff --git a/roles/timer/templates/timer b/roles/timer/templates/timer new file mode 100644 index 00000000..ad673ad0 --- /dev/null +++ b/roles/timer/templates/timer @@ -0,0 +1,20 @@ +# {{ ansible_managed }} + +[Unit] +Description={{ timer_name }} Service + +[Timer] +Unit={{ timer_name }}.service +{% if timer_calendar %} +OnCalendar={{ timer_calendar }} +{% else %} +OnBootSec={{ timer_startup_delay }} +OnUnitInactiveSec={{ timer_interval }} +{% endif %} + +{% if timer_randomized_delay %} +RandomizedDelaySec={{ timer_randomized_delay }} +{% endif %} + +[Install] +WantedBy=basic.target From 32ececbb80bacfcf307749d6045cb64710ce8d7e Mon Sep 17 00:00:00 2001 From: mortzu Date: Sun, 26 Sep 2021 11:57:36 +0200 Subject: [PATCH 2/3] timer: Fixed path for systemd units --- roles/timer/tasks/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/roles/timer/tasks/main.yml b/roles/timer/tasks/main.yml index ff21528c..911d5918 100644 --- a/roles/timer/tasks/main.yml +++ b/roles/timer/tasks/main.yml @@ -6,7 +6,7 @@ - name: Upload service and timer template: src: "job.{{ item }}" - dest: /lib/systemd/system/{{ timer_name }}.{{ item }} + dest: "/etc/systemd/system/{{ timer_name }}.{{ item }}" with_items: - service - timer From 3c8d2d18cb40fb01c7f3887575c8e3fe8b940655 Mon Sep 17 00:00:00 2001 From: mortzu Date: Sun, 26 Sep 2021 11:59:47 +0200 Subject: [PATCH 3/3] timer: Reload systemd in the correct way --- roles/timer/handlers/main.yml | 4 ++++ roles/timer/tasks/main.yml | 7 +------ 2 files changed, 5 insertions(+), 6 deletions(-) create mode 100644 roles/timer/handlers/main.yml diff --git a/roles/timer/handlers/main.yml b/roles/timer/handlers/main.yml new file mode 100644 index 00000000..bb7fde2b --- /dev/null +++ b/roles/timer/handlers/main.yml @@ -0,0 +1,4 @@ +--- +- name: reload systemd + systemd: + daemon_reload: yes diff --git a/roles/timer/tasks/main.yml b/roles/timer/tasks/main.yml index 911d5918..1178f30e 100644 --- a/roles/timer/tasks/main.yml +++ b/roles/timer/tasks/main.yml @@ -10,15 +10,10 @@ with_items: - service - timer - register: template_result - -- name: Reload systemd - command: systemctl daemon-reload - when: template_result.changed + notify: reload systemd - name: Enable and start timer service: name: "{{ timer_name }}.timer" - daemon_reload: yes enabled: yes state: started