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