This commit is contained in:
Luca Bilke 2025-01-12 20:58:22 +01:00
parent 9ad56d6c75
commit 729cb75ab4
6 changed files with 89 additions and 36 deletions
plugins
roles/compose/tasks

View file

@ -11,7 +11,7 @@ from typing import TYPE_CHECKING, Any
import yaml
if TYPE_CHECKING:
from ansible.module_utils.basic import AnsibleModule # type: ignore[reportMissingStubFile]
from ansible.module_utils.basic import AnsibleModule # pyright: ignore[reportMissingTypeStubs]
@dataclass(frozen=True)

View file

@ -61,14 +61,13 @@ def apply_definition(state: State, params: dict[str, Any], definition: dict[str,
def apply_settings(state: State, params: dict[str, Any]) -> State:
settings = state.module.params.get("settings", {})
params = settings.get("service_default_args", {}).get(params["name"], {}) | params
return update(
state,
params,
(
state.module.params.get("settings", {})
.get("service_default_definitions", {})
.get(params["name"], {})
),
settings.get("service_default_definitions", {}).get(params["name"], {}),
)

View file

@ -1,13 +1,10 @@
#!/usr/bin/env python
#!/usr/bin/python
# Copyright: (c) 2024, Luca Bilke <luca@bil.ke>
# MIT License (see LICENSE)
# ruff: noqa: E402
from __future__ import annotations
from copy import deepcopy
DOCUMENTATION = """
---
module: compose
@ -81,7 +78,7 @@ options:
type: dict
service_default_args:
description:
- Default arguments for each service helper
- Default arguments for each service helper.
type: dict
suboptions:
custom:
@ -112,6 +109,27 @@ options:
description:
- Default args for Redis services.
type: dict
label_default_args:
description:
- Default arguments for each label helper.
type: dict
suboptions:
docker_volume_backupper:
description:
- Docker Volume Backupper label helper configuration.
type: dict
traefik_middleware:
description:
- Traefik Middleware label helper configuration.
type: dict
traefik_router:
description:
- Traefik Router label helper configuration.
type: dict
traefik_service:
description:
- Traefik Service label helper configuration.
type: dict
services:
description:
- Services to create in the project.
@ -120,7 +138,8 @@ options:
custom:
description:
- Custom service definition.
type: dict
type: list
elements: dict
suboptions:
name:
description:
@ -243,7 +262,8 @@ options:
docker_in_docker:
description:
- Docker-in-Docker service definition.
type: dict
type: list
elements: dict
suboptions:
name:
description:
@ -264,7 +284,8 @@ options:
docker_socket_proxy:
description:
- Docker Socket Proxy service definition.
type: dict
type: list
elements: dict
suboptions:
name:
description:
@ -289,8 +310,9 @@ options:
default: true
docker_volume_backupper:
description:
- Docker Socket Proxy service definition.
type: dict
- Docker Volume Backupper service definition.
type: list
elements: dict
suboptions:
name:
description:
@ -320,7 +342,8 @@ options:
mariadb:
description:
- MariaDB service definition.
type: dict
type: list
elements: dict
suboptions:
name:
description:
@ -364,7 +387,8 @@ options:
postgres:
description:
- PostgreSQL service definition.
type: dict
type: list
elements: dict
suboptions:
name:
description:
@ -404,7 +428,8 @@ options:
redis:
description:
- Redis service definition.
type: dict
type: list
elements: dict
suboptions:
name:
description:
@ -424,9 +449,11 @@ options:
type: dict
"""
from copy import deepcopy
from importlib.util import find_spec
from typing import Any
from ansible.module_utils.basic import AnsibleModule # type: ignore[reportMissingStubFile]
from ansible.module_utils.basic import AnsibleModule # pyright: ignore[reportMissingTypeStubs]
from ansible_collections.snailed.ez_compose.plugins.module_utils import (
common,
label,
@ -459,7 +486,8 @@ def label_argument_spec() -> dict[str, Any]:
def service_argument_spec() -> dict[str, Any]:
service_args: dict[str, Any] = {
"type": "dict",
"type": "list",
"elements": "dict",
"suboptions": {},
"required": True,
}
@ -512,6 +540,19 @@ def settings_spec() -> dict[str, Any]:
"suboptions": args,
}
for module in label.__all__:
if module == "common":
continue
args = deepcopy(getattr(label, module).EXTRA_ARGS)
for arg in args.values():
arg.pop("required", None)
settings["suboptions"]["label_default_args"]["suboptions"][module] = {
"type": "dict",
"suboptions": args,
}
return settings
@ -532,10 +573,14 @@ def main() -> None:
},
)
if not find_spec("yaml"):
module.fail_json("PyYAML seems to be missing on host") # pyright: ignore[reportUnknownMemberType]
state = common.get_state(module)
for name, args in module.params["services"].items():
state = getattr(service, name).helper(state, args)
for name, definitions in module.params["services"].items():
for definition in definitions:
state = getattr(service, name).helper(state, definition)
if __name__ == "__main__":

View file

@ -5,7 +5,17 @@ output-format = "grouped"
[tool.ruff.lint]
select = ["ALL"]
ignore = ["ERA001", "FIX002", "TD002", "TD003", "INP001", "D100", "D101", "D103", "D104"]
ignore = [
"ERA001",
"FIX002",
"TD002",
"TD003",
"INP001",
"D100",
"D101",
"D103",
"D104",
]
[tool.ruff.format]
line-ending = "lf"

View file

@ -47,17 +47,16 @@
- name: Discover project definitions
ansible.builtin.set_fact:
_project_keys: >-
{{
hostvars[inventory_hostname].keys()
| select('match', '^ez_compose_project_')
}}
ez_compose_projects: >-
{{
hostvars[inventory_hostname]
| dict2items
| selectattr('key', 'in', _project_keys)
| items2dict
| selectattr(
'key', 'in', (
hostvars[inventory_hostname].keys()
| select('match', '^ez_compose_project_')
)
)
}}
when: >-
ez_compose_projects is not defined
@ -66,10 +65,10 @@
- name: Import project tasks
ansible.builtin.include_tasks: project.yml
when: >-
[project.name, 'compose-all'] | intersect(ansible_run_tags) | length > 0
[project.key | split('_') | last, 'compose-all'] | intersect(ansible_run_tags) | length > 0
and not
[project.name, 'compose-all'] | intersect(ansible_skip_tags) | length > 0
[project.key | split('_') | last, 'compose-all'] | intersect(ansible_skip_tags) | length > 0
loop: "{{ ez_compose_projects }}"
loop_control:
loop_var: project
no_log: true
# no_log: true

View file

@ -1,7 +1,7 @@
---
- name: "project | Write compose file: {{ project.name }}"
snailed.ez_compose.compose:
name: "{{ project.name }}"
services: "{{ project.services }}"
name: "{{ project.key | split('_') | last }}"
services: "{{ project.value }}"
settings: "{{ ez_compose_settings }}"
project_dir: "{{ ez_compose_project_dir }}"