This commit is contained in:
Luca Bilke 2024-12-13 22:48:16 +01:00
commit f68bd7f79c
13 changed files with 96 additions and 100 deletions

View file

@ -13,17 +13,6 @@ import yaml
if TYPE_CHECKING: if TYPE_CHECKING:
from ansible.module_utils.basic import AnsibleModule # type: ignore[reportMissingStubFile] from ansible.module_utils.basic import AnsibleModule # type: ignore[reportMissingStubFile]
BASE_ARGS = {
"name": {
"type": "str",
"required": True,
},
"project_dir": {
"type": "path",
"default": "/var/lib/ez_compose",
},
}
@dataclass(frozen=True) @dataclass(frozen=True)
class Result: class Result:

View file

@ -13,13 +13,13 @@ if TYPE_CHECKING:
) )
EXTRA_ARGS = { EXTRA_ARGS = {
"stop": {"type": "bool"}, "stop": {"type": "bool", "default": True},
} }
def helper(state: State) -> State: def helper(state: State, service_name: str, params: dict[str, Any]) -> State:
stop = state.module.params.get("stop", True) stop: bool = params["stop"]
project_name = state.module.params["project_name"] project_name: str = state.module.params["name"]
update: dict[str, Any] = {} update: dict[str, Any] = {}
@ -28,4 +28,4 @@ def helper(state: State) -> State:
"docker-volume-backup.stop-during-backup": project_name, "docker-volume-backup.stop-during-backup": project_name,
} }
return service.update(state, update) return service.update(service_name, state, update)

View file

@ -3,7 +3,7 @@
from __future__ import annotations from __future__ import annotations
from typing import TYPE_CHECKING from typing import TYPE_CHECKING, Any
import ansible_collections.ssnailed.ez_compose.plugins.module_utils.service.common as service # type: ignore[reportMissingTypeStubs] import ansible_collections.ssnailed.ez_compose.plugins.module_utils.service.common as service # type: ignore[reportMissingTypeStubs]
@ -20,13 +20,12 @@ EXTRA_ARGS = {
} }
def helper(state: State) -> State: def helper(state: State, service_name: str, params: dict[str, Any]) -> State:
service_name = state.module.params["name"] project_name: str = state.module.params["name"]
project_name = state.module.params["project_name"] middleware: str = params["middleware"]
middleware = state.module.params["middleware"] settings: dict[str, str] = params["settings"]
settings = state.module.params["settings"] proxy_type: str = state.module.params.get("proxy_type", "http")
proxy_type = state.module.params.get("proxy_type", "http") middleware_name: str = state.module.params.get(
middleware_name = state.module.params.get(
"middleware_name", "middleware_name",
f"{project_name}_{service_name}_{proxy_type}_{middleware.lower()}", f"{project_name}_{service_name}_{proxy_type}_{middleware.lower()}",
) )
@ -39,4 +38,4 @@ def helper(state: State) -> State:
"labels": labels, "labels": labels,
} }
return service.update(state, update) return service.update(service_name, state, update)

View file

@ -3,7 +3,7 @@
from __future__ import annotations from __future__ import annotations
from typing import TYPE_CHECKING from typing import TYPE_CHECKING, Any
import ansible_collections.ssnailed.ez_compose.plugins.module_utils.service.common as service # type: ignore[reportMissingTypeStubs] import ansible_collections.ssnailed.ez_compose.plugins.module_utils.service.common as service # type: ignore[reportMissingTypeStubs]
@ -13,9 +13,9 @@ if TYPE_CHECKING:
) )
EXTRA_ARGS = { EXTRA_ARGS = {
"proxy_type": {"type": "str"},
"router_name": {"type": "str"}, "router_name": {"type": "str"},
"rule": {"type": "str", "required": True}, "rule": {"type": "str", "required": True},
"proxy_type": {"type": "str", "default": "http"},
"service": {"type": "str"}, "service": {"type": "str"},
"certresolver": {"type": "str"}, "certresolver": {"type": "str"},
"entrypoints": {"type": "list"}, "entrypoints": {"type": "list"},
@ -23,16 +23,15 @@ EXTRA_ARGS = {
} }
def helper(state: State) -> State: def helper(state: State, service_name: str, params: dict[str, Any]) -> State:
service_name = state.module.params["name"] project_name: str = state.module.params["name"]
project_name = state.module.params["project_name"] rule: str = params["rule"]
rule = state.module.params["rule"] traefik_service: str | None = params.get("service")
traefik_service = state.module.params.get("service") entrypoints: list[str] | None = params.get("entrypoints")
entrypoints = state.module.params.get("entrypoints") middlewares: list[str] | None = params.get("middlewares")
middlewares = state.module.params.get("middlewares") certresolver: str | None = params.get("certresolver")
certresolver = state.module.params.get("certresolver") proxy_type: str = params["proxy_type"]
proxy_type = state.module.params.get("proxy_type", "http") router_name: str = params.get(
router_name = state.module.params.get(
"router_name", "router_name",
f"{project_name}_{service_name}_{proxy_type}", f"{project_name}_{service_name}_{proxy_type}",
) )
@ -60,4 +59,4 @@ def helper(state: State) -> State:
"labels": labels, "labels": labels,
} }
return service.update(state, update) return service.update(service_name, state, update)

View file

@ -13,23 +13,22 @@ if TYPE_CHECKING:
) )
EXTRA_ARGS = { EXTRA_ARGS = {
"proxy_type": {"type": "str"}, "proxy_type": {"type": "str", "default": "http"},
"service_name": {"type": "string"}, "service_name": {"type": "string"},
"port": {"type": "int"}, "port": {"type": "int"},
} }
def helper(state: State) -> State: def helper(state: State, service_name: str, params: dict[str, Any]) -> State:
service_name = state.module.params["name"] project_name: str = state.module.params["name"]
project_name = state.module.params["project_name"] port: int | None = params.get("port")
port = state.module.params.get("port") proxy_type: str = params["proxy_type"]
proxy_type = state.module.params.get("proxy_type", "http") traefik_service_name: str = params.get(
service_name = state.module.params.get(
"service_name", "service_name",
f"{project_name}_{service_name}_{proxy_type}", f"{project_name}_{service_name}_{proxy_type}",
) )
prefix = f"traefik.{proxy_type}.services.{service_name}" prefix = f"traefik.{proxy_type}.services.{traefik_service_name}"
labels: dict[str, Any] = {} labels: dict[str, Any] = {}
@ -40,4 +39,4 @@ def helper(state: State) -> State:
"labels": labels, "labels": labels,
} }
return service.update(state, update) return service.update(service_name, state, update)

View file

@ -21,11 +21,12 @@ BASE_ARGS = {
"name": {"type": "str", "required": True}, "name": {"type": "str", "required": True},
"image": {"type": "str", "required": True}, "image": {"type": "str", "required": True},
"defaults": {"type": "dict"}, "defaults": {"type": "dict"},
"overwrite": {"type": "dict"},
} }
def apply_base(state: State, params: dict[str, Any]) -> State: def apply_base(state: State, params: dict[str, Any]) -> State:
project_name = state.module.params["name"] project_name: str = state.module.params["name"]
new: dict[str, Any] = { new: dict[str, Any] = {
"container_name": f"{project_name}_{params["name"]}", "container_name": f"{project_name}_{params["name"]}",
@ -40,26 +41,26 @@ def apply_base(state: State, params: dict[str, Any]) -> State:
}, },
} }
return update(state, new) return update(params["name"], state, new)
def set_defaults(state: State, params: dict[str, Any]) -> State: def set_definition(state: State, params: dict[str, Any], param_name: str) -> State:
container_name = params["name"] service_name: str = params["name"]
defaults = params.get("defaults", {}) definition: dict[str, Any] = params.get(param_name, {})
project = copy.deepcopy(state.after) project = copy.deepcopy(state.after)
services = project["services"] services: dict[str, Any] = project["services"]
service = services[container_name] service: dict[str, Any] = services[service_name]
_ = recursive_update(service, defaults) _ = recursive_update(service, definition)
services.update({container_name: service}) services.update({service_name: service})
return replace(state, after=project) return replace(state, after=project)
def update(state: State, update: dict[str, Any]) -> State: def update(service_name: str, state: State, update: dict[str, Any]) -> State:
project = copy.deepcopy(state.after) project = copy.deepcopy(state.after)
service_name = state.module.params["name"]
_ = recursive_update(project["services"][service_name], update) _ = recursive_update(project["services"][service_name], update)
@ -72,6 +73,7 @@ def run_helper(
helper: Callable[[State, dict[str, Any]], State] = lambda x, _: x, helper: Callable[[State, dict[str, Any]], State] = lambda x, _: x,
) -> State: ) -> State:
state = apply_base(state, params) state = apply_base(state, params)
state = set_defaults(state, params) state = set_definition(state, params, "defaults")
state = helper(state, params) state = helper(state, params)
state = set_definition(state, params, "overwrite")
return update_project(state) return update_project(state)

View file

@ -6,8 +6,9 @@ from __future__ import annotations
import copy import copy
from typing import TYPE_CHECKING, Any from typing import TYPE_CHECKING, Any
from ansible_collections.ssnailed.ez_compose.plugins.module_utils.service import ( # type: ignore[reportMissingTypeStubs] from ansible_collections.ssnailed.ez_compose.plugins.module_utils import ( # type: ignore[reportMissingTypeStubs]
common as service, label,
service,
) )
if TYPE_CHECKING: if TYPE_CHECKING:
@ -15,6 +16,7 @@ if TYPE_CHECKING:
State, State,
) )
# NOTE: Label helper arguments are added in the compose module itself
EXTRA_ARGS = { EXTRA_ARGS = {
"internal_network": {"type": "bool", "default": False}, "internal_network": {"type": "bool", "default": False},
"definition": {"type": "dict", "required": True}, "definition": {"type": "dict", "required": True},
@ -22,8 +24,8 @@ EXTRA_ARGS = {
def helper(state: State, params: dict[str, Any]) -> State: def helper(state: State, params: dict[str, Any]) -> State:
definition = params["definition"] definition: dict[str, Any] = params["definition"]
internal_network = params["internal_network"] internal_network: bool = params["internal_network"]
update = copy.deepcopy(definition) update = copy.deepcopy(definition)
@ -34,4 +36,7 @@ def helper(state: State, params: dict[str, Any]) -> State:
update["networks"] = networks update["networks"] = networks
return service.update(state, update) for name, args in params["label_helpers"].items():
state = getattr(label, name).helper(state, params["name"], args)
return service.common.update(params["name"], state, update)

View file

@ -14,19 +14,12 @@ if TYPE_CHECKING:
State, State,
) )
EXTRA_ARGS = { EXTRA_ARGS = {}
"command": {"type": "list"},
}
def helper(state: State, params: dict[str, Any]) -> State: def helper(state: State, params: dict[str, Any]) -> State:
command = params.get("command")
update = { update = {
"privileged": True, "privileged": True,
} }
if command: return service.update(params["name"], state, update)
update["command"] = command
return service.update(state, update)

View file

@ -15,12 +15,12 @@ if TYPE_CHECKING:
) )
EXTRA_ARGS = { EXTRA_ARGS = {
"read_only": {"type": "bool"}, "read_only": {"type": "bool", "default": True},
} }
def helper(state: State, params: dict[str, Any]) -> State: def helper(state: State, params: dict[str, Any]) -> State:
read_only = params.get("read_only", True) read_only = params["read_only"]
volumes = [ volumes = [
{ {
@ -35,4 +35,4 @@ def helper(state: State, params: dict[str, Any]) -> State:
"volumes": volumes, "volumes": volumes,
} }
return service.update(state, update) return service.update(params["name"], state, update)

View file

@ -16,24 +16,24 @@ if TYPE_CHECKING:
EXTRA_ARGS = { EXTRA_ARGS = {
"archive": {"type": "str"}, "archive": {"type": "str"},
"backup_volumes": {"type": "list"}, "backup_volumes": {"type": "list", "elements": "str"},
} }
def helper(state: State, params: dict[str, Any]) -> State: def helper(state: State, params: dict[str, Any]) -> State:
archive = params.get("archive") archive: str | None = params.get("archive")
backup_volumes = params.get("backup_volumes", []) backup_volumes: list[str] | None = params.get("backup_volumes", [])
service_name = params["name"] service_name = params["name"]
project_name = params["project_name"] project_name = params["project_name"]
volumes = [ volumes: list[dict[str, Any]] = [
{ {
"type": "volume", "type": "volume",
"source": volume, "source": volume,
"target": f"/backup/{volume}", "target": f"/backup/{volume}",
"read_only": True, "read_only": True,
} }
for volume in backup_volumes for volume in (backup_volumes or [])
] ]
environment = { environment = {
@ -60,4 +60,4 @@ def helper(state: State, params: dict[str, Any]) -> State:
"volumes": volumes, "volumes": volumes,
} }
return service.update(state, update) return service.update(params["name"], state, update)

View file

@ -16,7 +16,7 @@ if TYPE_CHECKING:
) )
EXTRA_ARGS = { EXTRA_ARGS = {
"archive": {"type": "bool"}, "backup": {"type": "bool", "default": True},
"database": {"type": "str", "required": True}, "database": {"type": "str", "required": True},
"username": {"type": "str", "required": True}, "username": {"type": "str", "required": True},
"password": {"type": "str", "required": True}, "password": {"type": "str", "required": True},
@ -25,13 +25,13 @@ EXTRA_ARGS = {
def helper(state: State, params: dict[str, Any]) -> State: def helper(state: State, params: dict[str, Any]) -> State:
backup = params.get("backup", True) project_name: str = state.module.params["project_name"]
database = params["database"] backup: bool = params["backup"]
username = params["username"] database: str = params["database"]
password = params["password"] username: str = params["username"]
root_password = params["root_password"] password: str = params["password"]
service_name = params["name"] root_password: str | None = params.get("root_password")
project_name = params["project_name"] service_name: str = params["name"]
volumes = [ volumes = [
{ {
@ -84,4 +84,4 @@ def helper(state: State, params: dict[str, Any]) -> State:
"labels": labels, "labels": labels,
} }
return service.update(state, update) return service.update(params["name"], state, update)

View file

@ -16,7 +16,7 @@ if TYPE_CHECKING:
) )
EXTRA_ARGS = { EXTRA_ARGS = {
"archive": {"type": "bool"}, "backup": {"type": "bool", "default": True},
"database": {"type": "str", "required": True}, "database": {"type": "str", "required": True},
"username": {"type": "str", "required": True}, "username": {"type": "str", "required": True},
"password": {"type": "str", "required": True}, "password": {"type": "str", "required": True},
@ -24,12 +24,12 @@ EXTRA_ARGS = {
def helper(state: State, params: dict[str, Any]) -> State: def helper(state: State, params: dict[str, Any]) -> State:
backup = params.get("backup", True) project_name: str = state.module.params["project_name"]
database = params["database"] backup: bool = params["backup"]
username = params["username"] database: str = params["database"]
password = params["password"] username: str = params["username"]
service_name = params["name"] password: str = params["password"]
project_name = params["project_name"] service_name: str = params["name"]
volumes = [ volumes = [
{ {
@ -75,4 +75,4 @@ def helper(state: State, params: dict[str, Any]) -> State:
"labels": labels, "labels": labels,
} }
return service.update(state, update) return service.update(params["name"], state, update)

View file

@ -67,13 +67,23 @@ def service_argument_spec() -> dict[str, Any]:
def main() -> None: def main() -> None:
module = AnsibleModule( module = AnsibleModule(
argument_spec={ argument_spec={
**common.BASE_ARGS, "name": {
"type": "str",
"required": True,
},
"project_dir": {
"type": "path",
"default": "/var/lib/ez_compose",
},
"services": service_argument_spec(), "services": service_argument_spec(),
}, },
) )
state = common.get_state(module) state = common.get_state(module)
for name, args in module.params["services"].items():
state = getattr(service, name).helper(state, args)
if __name__ == "__main__": if __name__ == "__main__":
main() main()