95 lines
2.1 KiB
Python
95 lines
2.1 KiB
Python
#!/usr/bin/python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright: (c) 2024, Luca Bilke <luca@bil.ke>
|
|
# 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"]
|
|
name = state.module.params["name"]
|
|
project_name = state.module.params["project_name"]
|
|
|
|
volumes = [
|
|
{
|
|
"source": 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"{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},
|
|
}
|
|
run_service(extra_args, helper)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|