mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-08-26 13:35:24 +02:00
refactor(systemd-services): migrate SYS_SERVICE_SUFFIX usage to get_service_name filter
Replaced all hardcoded service name concatenations with the new get_service_name filter. This ensures consistency, proper lowercase formatting, and correct handling of '@' suffixed units. Added unittests for the filter (normal, custom suffix, '@'-units, and lowercase normalization). Context: see ChatGPT discussion https://chatgpt.com/share/68a38beb-b9bc-800f-b7ed-cdd2b64b2604
This commit is contained in:
parent
4a600ac531
commit
bf63e01b98
@ -17,7 +17,7 @@ def get_service_name(systemctl_id, software_name, suffix="service"):
|
||||
base = sid[:-1] # drop the trailing '@'
|
||||
return f"{base}.{sw}@.{sfx}"
|
||||
else:
|
||||
return f"{sid}{sw}.{sfx}"
|
||||
return f"{sid}.{sw}.{sfx}"
|
||||
|
||||
|
||||
class FilterModule(object):
|
||||
|
@ -5,12 +5,12 @@
|
||||
SYS_SERVICE_SUFFIX: ".{{ SOFTWARE_NAME | lower }}.service"
|
||||
|
||||
## Names
|
||||
SYS_SERVICE_CLEANUP_BACKUPS_OLD: "sys-ctl-cln-backups{{ SYS_SERVICE_SUFFIX }}"
|
||||
SYS_SERVICE_CLEANUP_BACKUPS_FAILED: "sys-ctl-cln-faild-bkps{{ SYS_SERVICE_SUFFIX }}"
|
||||
SYS_SERVICE_OPTIMIZE_DRIVE: "svc-opt-ssd-hdd{{ SYS_SERVICE_SUFFIX }}"
|
||||
SYS_SERVICE_BACKUP_RMT_2_LOC: "svc-bkp-rmt-2-loc{{ SYS_SERVICE_SUFFIX }}"
|
||||
SYS_SERVICE_REPAIR_DOCKER_HARD: "sys-ctl-rpr-docker-hard{{ SYS_SERVICE_SUFFIX }}"
|
||||
SYS_SERVICE_UPDATE_DOCKER: "update-docker{{ SYS_SERVICE_SUFFIX }}"
|
||||
SYS_SERVICE_CLEANUP_BACKUPS_OLD: "{{ 'sys-ctl-cln-backups' | get_service_name(SOFTWARE_NAME) }}"
|
||||
SYS_SERVICE_CLEANUP_BACKUPS_FAILED: "{{ 'sys-ctl-cln-faild-bkps' | get_service_name(SOFTWARE_NAME) }}"
|
||||
SYS_SERVICE_OPTIMIZE_DRIVE: "{{ 'svc-opt-ssd-hdd' | get_service_name(SOFTWARE_NAME) }}"
|
||||
SYS_SERVICE_BACKUP_RMT_2_LOC: "{{ 'svc-bkp-rmt-2-loc' | get_service_name(SOFTWARE_NAME) }}"
|
||||
SYS_SERVICE_REPAIR_DOCKER_HARD: "{{ 'sys-ctl-rpr-docker-hard' | get_service_name(SOFTWARE_NAME) }}"
|
||||
SYS_SERVICE_UPDATE_DOCKER: "{{ 'update-docker' | get_service_name(SOFTWARE_NAME) }}"
|
||||
|
||||
## On Failure
|
||||
SYS_SERVICE_ON_FAILURE_COMPOSE: "sys-ctl-alm-compose.{{ SOFTWARE_NAME }}@%i.service"
|
||||
|
@ -1,5 +1,6 @@
|
||||
|
||||
- name: "reload svc-bkp-loc-2-usb service"
|
||||
systemd:
|
||||
name: svc-bkp-loc-2-usb{{ SYS_SERVICE_SUFFIX }}
|
||||
name: "{{ 'svc-bkp-loc-2-usb' | get_service_name(SOFTWARE_NAME) }}"
|
||||
state: reloaded
|
||||
daemon_reload: yes
|
||||
|
@ -9,17 +9,17 @@ To track what the service is doing, execute one of the following commands:
|
||||
#### Using systemctl
|
||||
|
||||
```bash
|
||||
watch -n2 "systemctl status sys-bkp-rmt-2-loc{{ SYS_SERVICE_SUFFIX }}"
|
||||
watch -n2 "systemctl status {{ 'sys-bkp-rmt-2-loc' | get_service_name(SOFTWARE_NAME) }}"
|
||||
```
|
||||
|
||||
#### Using journalctl
|
||||
|
||||
```bash
|
||||
journalctl -fu sys-bkp-rmt-2-loc{{ SYS_SERVICE_SUFFIX }}
|
||||
journalctl -fu {{ 'sys-bkp-rmt-2-loc' | get_service_name(SOFTWARE_NAME) }}
|
||||
```
|
||||
|
||||
### Viewing History
|
||||
|
||||
```bash
|
||||
sudo journalctl -u sys-bkp-rmt-2-loc{{ SYS_SERVICE_SUFFIX }}
|
||||
sudo journalctl -u {{ 'sys-bkp-rmt-2-loc' | get_service_name(SOFTWARE_NAME) }}
|
||||
```
|
@ -17,12 +17,12 @@
|
||||
### Activate Configuration
|
||||
```bash
|
||||
cp /path/to/wg0.conf /etc/wireguard/wg0.conf
|
||||
systemctl enable wg-quick@wg0{{ SYS_SERVICE_SUFFIX }} --now
|
||||
systemctl enable wg-quick@wg0.service --now
|
||||
```
|
||||
|
||||
### Check status
|
||||
```bash
|
||||
systemctl status wg-quick@wg0{{ SYS_SERVICE_SUFFIX }}
|
||||
systemctl status wg-quick@wg0.service
|
||||
```
|
||||
|
||||
## Other Resources
|
||||
|
@ -1,6 +1,6 @@
|
||||
- name: "restart wireguard"
|
||||
systemd:
|
||||
name: wg-quick@wg0{{ SYS_SERVICE_SUFFIX }}
|
||||
name: wg-quick@wg0.service
|
||||
state: restarted
|
||||
enabled: yes
|
||||
daemon_reload: yes
|
||||
|
@ -7,7 +7,7 @@ This role manages WireGuard on a client system. It sets up essential services an
|
||||
## Overview
|
||||
|
||||
Optimized for client configurations, this role:
|
||||
- Deploys a systemd service (`set-mtu{{ SYS_SERVICE_SUFFIX }}`) and its associated script to set the MTU on specified network interfaces.
|
||||
- Deploys a systemd service and its associated script to set the MTU on specified network interfaces.
|
||||
- Uses a Jinja2 template to generate the `set-mtu.sh` script.
|
||||
- Ensures that the MTU is configured correctly before starting WireGuard with [wg-quick](https://www.wireguard.com/quickstart/).
|
||||
|
||||
|
@ -1,10 +1,10 @@
|
||||
[Unit]
|
||||
Description=set MTU
|
||||
Before=wg-quick@wg0{{ SYS_SERVICE_SUFFIX }}
|
||||
Before=wg-quick@wg0.service
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
ExecStart=bash {{ systemctl_id | get_service_script_path('sh') }}
|
||||
|
||||
[Install]
|
||||
RequiredBy=wg-quick@wg0{{ SYS_SERVICE_SUFFIX }}
|
||||
RequiredBy=wg-quick@wg0.service
|
@ -6,4 +6,4 @@ OnFailure={{ SYS_SERVICE_ON_FAILURE_COMPOSE }} {{ SYS_SERVICE_CLEANUP_BACKUPS_FA
|
||||
Type=oneshot
|
||||
ExecStartPre=/bin/sh -c '/usr/bin/python {{ PATH_SYSTEM_LOCK_SCRIPT }} {{ SYS_SERVICE_GROUP_MANIPULATION | join(' ') }} --ignore {{ SYS_SERVICE_GROUP_BACKUPS | reject('equalto', role_name ~ '-everything') | join(' ') }} --timeout "{{ SYS_TIMEOUT_BACKUP_SERVICES }}"'
|
||||
ExecStart=/bin/sh -c '{{ BKP_DOCKER_2_LOC_EXEC }}'
|
||||
ExecStartPost=/bin/sh -c '/bin/systemctl start sys-ctl-rpr-docker-soft{{ SYS_SERVICE_SUFFIX }} &'
|
||||
ExecStartPost=/bin/sh -c '/bin/systemctl start {{ 'sys-ctl-rpr-docker-soft' | get_service_name(SOFTWARE_NAME) }} &'
|
@ -7,7 +7,7 @@ This Ansible role automates the detection, revocation and deletion of unused Let
|
||||
## Overview
|
||||
|
||||
- Installs the `certreap` cleanup tool using the `pkgmgr-install` role
|
||||
- Deploys and configures a `sys-ctl-cln-certs{{ SYS_SERVICE_SUFFIX }}` systemd unit
|
||||
- Deploys and configures a systemd unit
|
||||
- (Optionally) Sets up a recurring cleanup via a systemd timer using the `sys-timer` role
|
||||
- Integrates with `sys-ctl-alm-compose` to send failure notifications
|
||||
- Ensures idempotent execution with a `run_once_sys_ctl_cln_certs` flag
|
||||
@ -18,7 +18,7 @@ This Ansible role automates the detection, revocation and deletion of unused Let
|
||||
Uses `pkgmgr-install` to install the `certreap` binary.
|
||||
|
||||
- **Systemd Service Configuration**
|
||||
Deploys `sys-ctl-cln-certs{{ SYS_SERVICE_SUFFIX }}` and reloads/restarts it on changes.
|
||||
Deploys service and reloads/restarts it on changes.
|
||||
|
||||
- **Systemd Timer Scheduling**
|
||||
Optionally wires in a timer via the `sys-timer` role, controlled by the `on_calendar_cleanup_certs` variable.
|
||||
@ -27,7 +27,7 @@ This Ansible role automates the detection, revocation and deletion of unused Let
|
||||
Prevents multiple runs in one play by setting a `run_once_sys_ctl_cln_certs` fact.
|
||||
|
||||
- **Failure Notification**
|
||||
Triggers `sys-ctl-alm-compose.infinito@sys-ctl-cln-certs{{ SYS_SERVICE_SUFFIX }}` on failure.
|
||||
Triggers service on failure.
|
||||
|
||||
## Further Resources
|
||||
|
||||
|
@ -3,9 +3,9 @@
|
||||
name: sys-lock
|
||||
when: run_once_sys_lock is not defined
|
||||
|
||||
- name: "start sys-ctl-bkp-docker-2-loc-everything{{ SYS_SERVICE_SUFFIX }}"
|
||||
- name: "start {{ 'sys-ctl-bkp-docker-2-loc-everything' | get_service_name(SOFTWARE_NAME) }}"
|
||||
systemd:
|
||||
name: sys-ctl-bkp-docker-2-loc-everything{{ SYS_SERVICE_SUFFIX }}
|
||||
name: "{{ 'sys-ctl-bkp-docker-2-loc-everything' | get_service_name(SOFTWARE_NAME) }}"
|
||||
state: started
|
||||
when:
|
||||
- MODE_BACKUP | bool
|
||||
|
39
tests/unit/filter_plugins/test_get_service_name.py
Normal file
39
tests/unit/filter_plugins/test_get_service_name.py
Normal file
@ -0,0 +1,39 @@
|
||||
import unittest
|
||||
from filter_plugins import get_service_name
|
||||
|
||||
class TestGetServiceName(unittest.TestCase):
|
||||
def test_normal_service(self):
|
||||
# Expect a dot between id and software name
|
||||
self.assertEqual(
|
||||
get_service_name.get_service_name("sys-ctl-cln-backups", "nginx"),
|
||||
"sys-ctl-cln-backups.nginx.service"
|
||||
)
|
||||
|
||||
def test_normal_service_custom_suffix(self):
|
||||
self.assertEqual(
|
||||
get_service_name.get_service_name("sys-ctl-cln-backups", "nginx", "timer"),
|
||||
"sys-ctl-cln-backups.nginx.timer"
|
||||
)
|
||||
|
||||
def test_with_at_suffix(self):
|
||||
# If systemctl_id ends with '@', @ is moved behind software name
|
||||
self.assertEqual(
|
||||
get_service_name.get_service_name("sys-ctl-bkp@", "postgres"),
|
||||
"sys-ctl-bkp.postgres@.service"
|
||||
)
|
||||
|
||||
def test_with_at_and_custom_suffix(self):
|
||||
self.assertEqual(
|
||||
get_service_name.get_service_name("sys-ctl-bkp@", "postgres", "timer"),
|
||||
"sys-ctl-bkp.postgres@.timer"
|
||||
)
|
||||
|
||||
def test_case_is_lowered(self):
|
||||
self.assertEqual(
|
||||
get_service_name.get_service_name("Sys-CTL-BKP@", "Postgres", "SeRviCe"),
|
||||
"sys-ctl-bkp.postgres@.service"
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
Loading…
x
Reference in New Issue
Block a user