ez_docker/plugins/module_utils/service/docker_volume_backupper.py
2024-12-11 18:42:59 +01:00

66 lines
1.8 KiB
Python

# Copyright: (c) 2024, Luca Bilke <luca@bil.ke>
# MIT License (see LICENSE)
from __future__ import annotations
from typing import TYPE_CHECKING
from ansible_collections.snailed.ez_compose.plugins.module_utils.service import common as service
if TYPE_CHECKING:
from ansible_collections.snailed.ez_compose.plugins.module_utils.common import State
def helper(state: State) -> State:
archive = state.module.params.get("archive")
backup_volumes = state.module.params.get("backup_volumes", [])
service_name = state.module.params["name"]
project_name = state.module.params["project_name"]
volumes = [
{
"type": "volume",
"source": volume,
"target": f"/backup/{volume}",
"read_only": True,
}
for volume in backup_volumes
]
environment = {
"BACKUP_FILENAME": f"{project_name}-%Y-%m-%dT%H-%M-%S.{'{{ .Extension }}'}",
"BACKUP_LATEST_SYMLINK": f"{project_name}-latest",
"BACKUP_PRUNING_PREFIX": f"{project_name}-",
"BACKUP_STOP_DURING_BACKUP_LABEL": project_name,
"BACKUP_ARCHIVE": "/archive",
"DOCKER_HOST": f"tcp://{project_name}_{service_name}_socket_proxy:2375",
}
if archive:
volumes.append(
{
"type": "bind",
"source": f"{archive}/{project_name}",
"target": "/archive",
"bind": {"create_host_path": True},
}
)
update = {
"environment": environment,
"volumes": volumes,
}
return service.update(state, update)
def main():
extra_args = {
"archive": {"type": "str"},
"backup_volumes": {"type": "list"},
}
service.run(extra_args, helper)
if __name__ == "__main__":
main()