Refactor filter_plugins

This commit is contained in:
Tobias Reisinger 2026-02-08 19:34:58 +01:00
parent 8ee096949d
commit bdf1f8891b
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
14 changed files with 34 additions and 30 deletions

View file

@ -46,7 +46,7 @@ backup_yml:
backends: "{{ backup_backends | mandatory }}"
locations: "{{ backup_list | map_backup_locations(backup_backends | mandatory, backup_default_hooks) }}"
locations: "{{ backup_list | backup_map_locations(backup_backends | mandatory, backup_default_hooks) }}"
global: "{{ backup_global }}"
@ -55,6 +55,6 @@ backup_yml_all:
backends: "{{ backup_backends | mandatory }}"
locations: "{{ backup_list_all | map_backup_locations(backup_backends | mandatory, backup_default_hooks) }}"
locations: "{{ backup_list_all | backup_map_locations(backup_backends | mandatory, backup_default_hooks) }}"
global: "{{ backup_global }}"

View file

@ -0,0 +1,48 @@
import copy
def hooks_add_or_create(location, key, value):
if key in location["hooks"]:
location["hooks"][key].append(value)
else:
location["hooks"][key] = [value]
class FilterModule(object):
def filters(self):
return {
'backup_map_locations': self.map_locations
}
def map_locations(self, locations, backends, hooks):
result = {}
backends_list = list(backends.keys())
for location in locations:
name = location["name"]
new_location = {
"to": backends_list,
"forget": "no",
"hooks": copy.deepcopy(hooks)
}
if location["type"] == "docker" or location["type"] == "docker_cifs":
new_location["from"] = name
new_location["type"] = "volume"
if location["type"] == "hook":
backup_dir = f"/opt/services/_backup/{name}"
new_location["from"] = backup_dir
for hook_type in ["prevalidate", "before", "after", "failure", "success"]:
hooks_add_or_create(
new_location,
hook_type,
f"/opt/services/backup/hooks/{name} '{backup_dir}' {hook_type}"
)
if location["type"] == "directory":
new_location["from"] = location["path"]
result[name.lower()] = new_location
return result

View file

@ -1,8 +1,8 @@
---
gatus_external_endpoints_backups: "{{ hostvars | hosts_backup_to_gatus() }}"
gatus_external_endpoints_backups: "{{ hostvars | gatus_from_hosts_backup() }}"
gatus_endpoints_hosts: "{{ opentofu.hosts | hosts_to_gatus() }}"
gatus_endpoints_services: "{{ all_services | services_to_gatus() }}"
gatus_endpoints_hosts: "{{ opentofu.hosts | gatus_from_hosts() }}"
gatus_endpoints_services: "{{ all_services | gatus_from_services() }}"
gatus_federation_tester: "https://federationtester.matrix.org/api/report?server_name=msrg.cc"

View file

@ -0,0 +1,100 @@
class FilterModule(object):
def filters(self):
return {
'gatus_from_hosts': self.from_hosts,
'gatus_from_hosts_backup': self.from_hosts_backup,
'gatus_from_services': self.from_services,
}
default_alerts = [
{
"type": "ntfy",
"send-on-resolved": True,
},
{
"type": "email",
"send-on-resolved": True,
},
]
def from_hosts(self, hosts):
result = []
for host in hosts.values():
result.append({
"name": host["hostname"],
"url": f"icmp://{host['fqdn']}",
"group": "1-hosts",
"conditions": [
"[CONNECTED] == true",
],
"alerts": self.default_alerts,
})
return result
def from_hosts_backup(self, hostvars):
result = []
backup_alerts = []
for a in self.default_alerts:
backup_alerts.append(dict(a, **{
'failure-threshold': 1,
'success-threshold': 1
}))
for name, host_data in hostvars.items():
if not host_data.get("host_backup_gatus_token"):
continue
result.append({
"name": f"backup@{name}",
"group": "8-backups",
"token": host_data["host_backup_gatus_token"],
"alerts": backup_alerts,
})
return result
def from_services(self, services):
result = []
default_conditions = [
"[STATUS] == any(200, 204)",
"[CERTIFICATE_EXPIRATION] > 48h"
]
for name, service in services.items():
if not bool(service.get("host")):
continue
if mon := service.get("monitoring"):
if service.get("dns"):
url = f"https://{service["dns"][0]['domain']}"
if mon_url := mon.get("url"):
if mon_url.startswith("/"):
url += mon_url
else:
url = mon_url
if conditions := mon.get("conditions"):
if conditions[0] == "DEFAULT":
conditions.pop(0)
conditions[:0] = default_conditions
else:
conditions = conditions
else:
conditions = default_conditions
new_endpoint = {
"name": name,
"group": mon.get("group"),
"url": url,
"conditions": conditions,
"interval": mon.get("interval"),
"alerts": self.default_alerts,
"ui": {
"hide-url": True
}
}
result.append(new_endpoint)
return result

View file

@ -21,7 +21,7 @@
- name: Generate systemd timer names
ansible.builtin.set_fact:
healthcheck_systemd_timers: "{{ healthcheck_svc.checks | list_prefix_suffix('healthcheck@', '.timer') }}"
healthcheck_systemd_timers: "{{ healthcheck_svc.checks | utils_list_prefix_suffix('healthcheck@', '.timer') }}"
- name: Disable unused system timers
ansible.builtin.systemd_service:

View file

@ -0,0 +1,18 @@
class FilterModule(object):
def filters(self):
return {
'lego_from_acmedns': self.from_acmedns,
}
def from_acmedns(self, acmedns_registered):
result = {}
for (key, value) in acmedns_registered.items():
result[key] = {
"fulldomain": value["subd"] + "." + value["host"],
"subdomain": value["subd"],
"username": value["user"],
"password": value["pass"],
"server_url": "https://" + value["host"]
}
return result

View file

@ -10,7 +10,7 @@
- name: Create the acme-dns-accounts
ansible.builtin.copy:
dest: "{{ (lego_config_path, 'acme-dns-accounts.json') | path_join }}"
content: '{{ lego_acmedns_registered | acmedns_to_lego | to_json }}'
content: '{{ lego_acmedns_registered | lego_from_acmedns | to_json }}'
mode: "0644"
- name: Copy the hook script
ansible.builtin.copy:

View file

@ -21,7 +21,7 @@
- name: Generate systemd timer names
ansible.builtin.set_fact:
lego_systemd_timers: "{{ lego_host_certificates | list_prefix_suffix('lego@', '.timer') }}"
lego_systemd_timers: "{{ lego_host_certificates | utils_list_prefix_suffix('lego@', '.timer') }}"
- name: Disable unused system timers
ansible.builtin.systemd_service:

View file

@ -4,7 +4,7 @@ lgtm_stack_mimir_domain: mimir.serguzim.me
lgtm_stack_alloy_domain: alloy.serguzim.me
lgtm_stack_loki_domain: "{{ all_services | service_get_domain('loki') }}"
lgtm_stack_alloy_jobs: "{{ all_services | services_to_alloy() }}"
lgtm_stack_alloy_jobs: "{{ all_services | lgtm_stack_services_to_alloy() }}"
lgtm_stack_grafana_secret_key: "{{ undef() }}"

View file

@ -0,0 +1,44 @@
def transfer_optional_param(source, target, name, target_name=None):
if param := source.get(name):
target[target_name or name] = param
class FilterModule(object):
def filters(self):
return {
'lgtm_stack_services_to_alloy': self.services_to_alloy,
}
def services_to_alloy(self, services):
result = []
for name, service in services.items():
if not bool(service.get("host")):
continue
if targets := service.get("metrics") or []:
job = {
"name": name,
"targets": [],
"scrape_interval": "60s",
}
for target in targets:
address = target.get("address") or service["dns"][0]['domain']
transfer_optional_param(target, job, "interval", "scrape_interval")
new_target = {
"address": address,
"path": target["path"],
"instance": name
}
transfer_optional_param(target, new_target, "instance")
transfer_optional_param(target, new_target, "job")
job["targets"].append(new_target)
result.append(job)
return result

View file

@ -0,0 +1,11 @@
class FilterModule(object):
def filters(self):
return {
'postgresql_restart_required': self.restart_required,
}
def restart_required(self, results):
for result in results:
if result.get('restart_required') and result.get('changed'):
return True
return False