# Copyright: (c) 2025, Luca Bilke <luca@bil.ke> # MIT License (see LICENSE) from __future__ import annotations import shlex from typing import TYPE_CHECKING, Any if TYPE_CHECKING: from ansible_collections.snailed.ez_docker.plugins.module_utils.models import State EXTRA_ARGS = { "backup": {"type": "bool", "default": True}, "database": {"type": "str", "required": True}, "username": {"type": "str", "required": True}, "password": {"type": "str", "required": True}, "root_password": {"type": "str"}, } DOCUMENTATION = """ mariadb: description: - Configuration for mariadb service. type: list elements: dict suboptions: backup: description: - If true, add labels for the docker volume backupper. type: bool default: true database: description: - Name of database. type: str required: true username: description: - Username for database. type: str required: true password: description: - Password for database. type: str required: true root_password: description: - Root password for database. type: str """ def helper(state: State, params: dict[str, Any]) -> dict[str, Any]: project_name: str = state.params["name"] backup: bool = params["backup"] database: str = params["database"] username: str = params["username"] password: str = params["password"] root_password: str | None = params.get("root_password") service_name: str = params["name"] volumes = [ { "source": service_name, "target": "/var/lib/mysql", "type": "volume", }, ] environment = { "MARIADB_DATABASE": database, "MARIADB_USER": username, "MARIADB_PASSWORD": password, } labels: dict[str, str] = {} if root_password: environment.update( { "MARIADB_ROOT_PASSWORD": root_password, }, ) if backup: labels.update( { "docker-volume-backup.archive-pre": ( "/bin/sh -c '" "mysqldump --all-databases " f"-u {shlex.quote(username)} " f"-p {shlex.quote(password)} " f">/backup/{shlex.quote(project_name)}.sql" "'" ), }, ) # mysqldump -psecret --all-databases > /tmp/dumps/dump.sql volumes.append( { "type": "volume", "source": f"{service_name}_backup", "target": "/backup", }, ) return { "environment": environment, "volumes": volumes, "labels": labels, "networks": {"internal": {}}, }