Refactor filter_plugins
This commit is contained in:
parent
8ee096949d
commit
bdf1f8891b
14 changed files with 34 additions and 30 deletions
|
|
@ -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 }}"
|
||||
|
|
|
|||
48
playbooks/roles/backup/filter_plugins/backup.py
Normal file
48
playbooks/roles/backup/filter_plugins/backup.py
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue