76 lines
2 KiB
Python
76 lines
2 KiB
Python
# Copyright: (c) 2024, Luca Bilke <luca@bil.ke>
|
|
# MIT License (see LICENSE)
|
|
|
|
from __future__ import annotations
|
|
|
|
import shlex
|
|
from typing import TYPE_CHECKING, Any
|
|
|
|
from ansible_collections.ssnailed.ez_compose.plugins.module_utils import service
|
|
|
|
if TYPE_CHECKING:
|
|
from ansible_collections.ssnailed.ez_compose.plugins.module_utils.common import (
|
|
State,
|
|
)
|
|
|
|
EXTRA_ARGS = {
|
|
"backup": {"type": "bool", "default": True},
|
|
"database": {"type": "str", "required": True},
|
|
"username": {"type": "str", "required": True},
|
|
"password": {"type": "str", "required": True},
|
|
}
|
|
|
|
|
|
def helper(state: State, params: dict[str, Any]) -> State:
|
|
project_name: str = state.module.params["project_name"]
|
|
backup: bool = params["backup"]
|
|
database: str = params["database"]
|
|
username: str = params["username"]
|
|
password: str = params["password"]
|
|
service_name: str = params["name"]
|
|
|
|
volumes = [
|
|
{
|
|
"source": service_name,
|
|
"target": "/var/lib/postgresql/data",
|
|
"type": "volume",
|
|
},
|
|
]
|
|
|
|
environment = {
|
|
"POSTGRES_DB": database,
|
|
"POSTGRES_USER": username,
|
|
"POSTGRES_PASSWORD": password,
|
|
}
|
|
|
|
labels: dict[str, str] = {}
|
|
|
|
if backup:
|
|
labels.update(
|
|
{
|
|
"docker-volume-backup.archive-pre": (
|
|
"/bin/sh -c '"
|
|
f"PGPASSWORD={shlex.quote(password)} "
|
|
"pg_dumpall "
|
|
f"-U {shlex.quote(username)} "
|
|
f"-f /backup/{shlex.quote(project_name)}.sql"
|
|
"'"
|
|
),
|
|
},
|
|
)
|
|
|
|
volumes.append(
|
|
{
|
|
"type": "volume",
|
|
"source": f"{service_name}_backup",
|
|
"target": "/backup",
|
|
},
|
|
)
|
|
|
|
update = {
|
|
"environment": environment,
|
|
"volumes": volumes,
|
|
"labels": labels,
|
|
}
|
|
|
|
return service.common.update(state, params, update)
|