From 9b8d6771bd99c9f5177f12f0f77c998368861627 Mon Sep 17 00:00:00 2001 From: Luca Bilke Date: Tue, 22 Oct 2024 22:23:59 +0200 Subject: [PATCH] add mariadb service helper --- plugins/modules/mariadb.py | 104 +++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 plugins/modules/mariadb.py diff --git a/plugins/modules/mariadb.py b/plugins/modules/mariadb.py new file mode 100644 index 0000000..b1a012f --- /dev/null +++ b/plugins/modules/mariadb.py @@ -0,0 +1,104 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# Copyright: (c) 2024, Luca Bilke +# MIT License (see LICENSE) + +# TODO: write ansible sections + +DOCUMENTATION = r""" +""" + +EXAMPLES = r""" +""" + +RETURN = r""" +""" + +from __future__ import annotations # pyright: ignore[reportGeneralTypeIssues] + +import shlex + +from ansible_collections.snailed.ez_compose.plugins.module_utils.common import ( + State, + run_service, + update_service, +) + + +def helper(state: State) -> State: + backup = state.module.params.get("backup", True) + database = state.module.params["database"] + username = state.module.params["username"] + password = state.module.params["password"] + root_password = state.module.params["root_password"] + name = state.module.params["name"] + project_name = state.module.params["project_name"] + + volumes = [ + { + "source": 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"{name}_backup", + "target": "/backup", + } + ) + + update = { + "environment": environment, + "volumes": volumes, + "labels": labels, + } + + return update_service(state, update) + + +def main(): + extra_args = { + "archive": {"type": "bool"}, + "database": {"type": "str", "required": True}, + "username": {"type": "str", "required": True}, + "password": {"type": "str", "required": True}, + "root_password": {"type": "str"}, + } + run_service(extra_args, helper) + + +if __name__ == "__main__": + main()