From a0f7ba50154c1014c408b675ae195e10d511ab19 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Thu, 14 Dec 2023 19:06:57 +0100 Subject: [PATCH] Implemented defrost timer --- group_vars/all | 14 +++++++++----- .../files/system-maintenance-service-freezer.py | 4 +++- .../handlers/main.yml | 6 ++++++ .../tasks/main.yml | 11 ++++++++++- .../system-maintenance-service-defrost.timer.j2 | 10 ++++++++++ .../system-maintenance-service-freezer.service.j2 | 3 ++- 6 files changed, 40 insertions(+), 8 deletions(-) create mode 100644 roles/system-maintenance-service-freezer/handlers/main.yml create mode 100644 roles/system-maintenance-service-freezer/templates/system-maintenance-service-defrost.timer.j2 diff --git a/group_vars/all b/group_vars/all index 2ed58c51..131dec4b 100644 --- a/group_vars/all +++ b/group_vars/all @@ -1,15 +1,18 @@ # Server Tact Variables +## Ours in which the server is 100% working. Rest of the time is reserved for maintanance +hours_server_awake: "{{ range(9, 24) | list + range(0, 3) | list }}" + ## Random delay for systemd timers to avoid peak loads. -randomized_delay_sec: "15min" +randomized_delay_sec: "5min" ## Schedule for Health Checks on_calendar_health_btrfs: "*-*-* 00:00:00" on_calendar_health_journalctl: "*-*-* 00:00:00" on_calendar_health_disc_space: "*-*-* 06,12,18,00:00:00" -on_calendar_health_docker_container: "*-*-* 09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,00,01,02:00:00" -on_calendar_health_docker_volumes: "*-*-* 09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,00,01,02:15:00" -on_calendar_health_nginx: "*-*-* 09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,00,01,02:45:00" +on_calendar_health_docker_container: "*-*-* {{ hours_server_awake | join(',') }}:00:00" +on_calendar_health_docker_volumes: "*-*-* {{ hours_server_awake | join(',') }}:15:00" +on_calendar_health_nginx: "*-*-* {{ hours_server_awake | join(',') }}:45:00" ## Schedule for Cleanup Tasks on_calendar_cleanup_backups: "*-*-* 06,12,18,00:30:00" @@ -20,7 +23,8 @@ on_calendar_backup_docker_to_local: "*-*-* 03:30:00" on_calendar_backup_remote_to_local: "*-*-* 21:30:00" ## Schedule for Maintenance Tasks -on_calendar_heal_docker: "*-*-* 09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,00,01:30:00" +on_calendar_heal_docker: "*-*-* {{ hours_server_awake | join(',') }}:30:00" +on_calendar_defrost: "*-*-* 00:00:00" on_calendar_renew_lets_encrypt_certificates: "*-*-* 12,00:30:00" on_calendar_deploy_mailu_certificates: "*-*-* 13,01:30:00" on_calendar_msi_keyboard_color: "*-*-* *:*:00" diff --git a/roles/system-maintenance-service-freezer/files/system-maintenance-service-freezer.py b/roles/system-maintenance-service-freezer/files/system-maintenance-service-freezer.py index 662026c1..746e8cc3 100644 --- a/roles/system-maintenance-service-freezer/files/system-maintenance-service-freezer.py +++ b/roles/system-maintenance-service-freezer/files/system-maintenance-service-freezer.py @@ -51,7 +51,9 @@ def stop_timer(service): """ Stop and disable a systemd timer for a service if it exists. """ - if service_file_exists(service, "timer"): + if service == "system-maintenance-service-defrost": + print(f"Ignore: {service}") + elif service_file_exists(service, "timer"): timer_name = f"{service}.timer" subprocess.run(['systemctl', 'stop', timer_name]) subprocess.run(['systemctl', 'disable', timer_name]) diff --git a/roles/system-maintenance-service-freezer/handlers/main.yml b/roles/system-maintenance-service-freezer/handlers/main.yml new file mode 100644 index 00000000..34f2c7ab --- /dev/null +++ b/roles/system-maintenance-service-freezer/handlers/main.yml @@ -0,0 +1,6 @@ +- name: "restart system-maintenance-service-defrost.timer" + systemd: + name: system-maintenance-service-defrost.timer + state: restarted + enabled: yes + daemon_reload: yes diff --git a/roles/system-maintenance-service-freezer/tasks/main.yml b/roles/system-maintenance-service-freezer/tasks/main.yml index 04b8045c..4e24ca99 100644 --- a/roles/system-maintenance-service-freezer/tasks/main.yml +++ b/roles/system-maintenance-service-freezer/tasks/main.yml @@ -20,4 +20,13 @@ - name: "set variable to prevent loading when action status didn't change" set_fact: - maintenance_service_freeze_action_last: "{{system_maintenance_service_freeze_action}}" \ No newline at end of file + maintenance_service_freeze_action_last: "{{system_maintenance_service_freeze_action}}" + +- name: create system-maintenance-service-defrost.timer + template: + src: system-maintenance-service-defrost.timer.j2 + dest: "/etc/systemd/system/system-maintenance-service-defrost.timer" + register: system_maintenance_service_defrost_timer + changed_when: system_maintenance_service_defrost_timer.changed or activate_all_timers | bool + notify: restart system-maintenance-service-defrost.timer + when: run_once_system_maintenance_service_freeze is not defined \ No newline at end of file diff --git a/roles/system-maintenance-service-freezer/templates/system-maintenance-service-defrost.timer.j2 b/roles/system-maintenance-service-freezer/templates/system-maintenance-service-defrost.timer.j2 new file mode 100644 index 00000000..3885217e --- /dev/null +++ b/roles/system-maintenance-service-freezer/templates/system-maintenance-service-defrost.timer.j2 @@ -0,0 +1,10 @@ +[Unit] +Description=starts system-maintenance-service-defrost.service + +[Timer] +OnCalendar={{on_calendar_defrost}} +RandomizedDelaySec={{randomized_delay_sec}} +Persistent=false + +[Install] +WantedBy=timers.target diff --git a/roles/system-maintenance-service-freezer/templates/system-maintenance-service-freezer.service.j2 b/roles/system-maintenance-service-freezer/templates/system-maintenance-service-freezer.service.j2 index 6dd42f0c..d88e745a 100644 --- a/roles/system-maintenance-service-freezer/templates/system-maintenance-service-freezer.service.j2 +++ b/roles/system-maintenance-service-freezer/templates/system-maintenance-service-freezer.service.j2 @@ -4,4 +4,5 @@ OnFailure=systemd-notifier@%n.service [Service] Type=oneshot -ExecStart=/bin/sh -c '/usr/bin/python {{ path_system_maintenance_service_freezer_script }} {{item}} {{ system_maintenance_services | join(",") }}' \ No newline at end of file +ExecPreStart=/bin/sh -c '/usr/bin/python {{ path_system_maintenance_service_freezer_script }} {{item}} "{{ system_maintenance_services + [item] }}"' +ExecStart=/bin/sh -c '/usr/bin/python {{ path_system_maintenance_service_freezer_script }} {{item}} "{{ system_maintenance_services }}"' \ No newline at end of file