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

@ -1,18 +0,0 @@
class FilterModule(object):
def filters(self):
return {
'acmedns_to_lego': self.acmedns_to_lego,
}
def acmedns_to_lego(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

@ -1,44 +0,0 @@
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 {
'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

@ -1,100 +0,0 @@
class FilterModule(object):
def filters(self):
return {
'hosts_to_gatus': self.hosts_to_gatus,
'hosts_backup_to_gatus': self.hosts_backup_to_gatus,
'services_to_gatus': self.services_to_gatus,
}
default_alerts = [
{
"type": "ntfy",
"send-on-resolved": True,
},
{
"type": "email",
"send-on-resolved": True,
},
]
def hosts_to_gatus(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 hosts_backup_to_gatus(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 services_to_gatus(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

@ -1,48 +0,0 @@
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 {
'map_backup_locations': self.map_backup_locations
}
def map_backup_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,9 +1,8 @@
class FilterModule(object):
def filters(self):
return {
'list_prefix_suffix': self.list_prefix_suffix,
'list_prefix_path_suffix': self.list_prefix_path_suffix,
'postgresql_restart_required': self.postgresql_restart_required,
'utils_list_prefix_suffix': self.list_prefix_suffix,
'utils_list_prefix_path_suffix': self.list_prefix_path_suffix,
}
def list_prefix_suffix(self, values, prefix, suffix):
@ -18,9 +17,3 @@ class FilterModule(object):
for value in values:
result.append(f"{prefix}{value}{suffix}")
return result
def postgresql_restart_required(self, results):
for result in results:
if result.get('restart_required') and result.get('changed'):
return True
return False