WIP
This commit is contained in:
parent
558192c404
commit
72162785d3
13 changed files with 96 additions and 100 deletions
|
@ -13,17 +13,6 @@ import yaml
|
|||
if TYPE_CHECKING:
|
||||
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)
|
||||
class Result:
|
||||
|
|
|
@ -13,13 +13,13 @@ if TYPE_CHECKING:
|
|||
)
|
||||
|
||||
EXTRA_ARGS = {
|
||||
"stop": {"type": "bool"},
|
||||
"stop": {"type": "bool", "default": True},
|
||||
}
|
||||
|
||||
|
||||
def helper(state: State) -> State:
|
||||
stop = state.module.params.get("stop", True)
|
||||
project_name = state.module.params["project_name"]
|
||||
def helper(state: State, service_name: str, params: dict[str, Any]) -> State:
|
||||
stop: bool = params["stop"]
|
||||
project_name: str = state.module.params["name"]
|
||||
|
||||
update: dict[str, Any] = {}
|
||||
|
||||
|
@ -28,4 +28,4 @@ def helper(state: State) -> State:
|
|||
"docker-volume-backup.stop-during-backup": project_name,
|
||||
}
|
||||
|
||||
return service.update(state, update)
|
||||
return service.update(service_name, state, update)
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
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]
|
||||
|
||||
|
@ -20,13 +20,12 @@ EXTRA_ARGS = {
|
|||
}
|
||||
|
||||
|
||||
def helper(state: State) -> State:
|
||||
service_name = state.module.params["name"]
|
||||
project_name = state.module.params["project_name"]
|
||||
middleware = state.module.params["middleware"]
|
||||
settings = state.module.params["settings"]
|
||||
proxy_type = state.module.params.get("proxy_type", "http")
|
||||
middleware_name = state.module.params.get(
|
||||
def helper(state: State, service_name: str, params: dict[str, Any]) -> State:
|
||||
project_name: str = state.module.params["name"]
|
||||
middleware: str = params["middleware"]
|
||||
settings: dict[str, str] = params["settings"]
|
||||
proxy_type: str = state.module.params.get("proxy_type", "http")
|
||||
middleware_name: str = state.module.params.get(
|
||||
"middleware_name",
|
||||
f"{project_name}_{service_name}_{proxy_type}_{middleware.lower()}",
|
||||
)
|
||||
|
@ -39,4 +38,4 @@ def helper(state: State) -> State:
|
|||
"labels": labels,
|
||||
}
|
||||
|
||||
return service.update(state, update)
|
||||
return service.update(service_name, state, update)
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
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]
|
||||
|
||||
|
@ -13,9 +13,9 @@ if TYPE_CHECKING:
|
|||
)
|
||||
|
||||
EXTRA_ARGS = {
|
||||
"proxy_type": {"type": "str"},
|
||||
"router_name": {"type": "str"},
|
||||
"rule": {"type": "str", "required": True},
|
||||
"proxy_type": {"type": "str", "default": "http"},
|
||||
"service": {"type": "str"},
|
||||
"certresolver": {"type": "str"},
|
||||
"entrypoints": {"type": "list"},
|
||||
|
@ -23,16 +23,15 @@ EXTRA_ARGS = {
|
|||
}
|
||||
|
||||
|
||||
def helper(state: State) -> State:
|
||||
service_name = state.module.params["name"]
|
||||
project_name = state.module.params["project_name"]
|
||||
rule = state.module.params["rule"]
|
||||
traefik_service = state.module.params.get("service")
|
||||
entrypoints = state.module.params.get("entrypoints")
|
||||
middlewares = state.module.params.get("middlewares")
|
||||
certresolver = state.module.params.get("certresolver")
|
||||
proxy_type = state.module.params.get("proxy_type", "http")
|
||||
router_name = state.module.params.get(
|
||||
def helper(state: State, service_name: str, params: dict[str, Any]) -> State:
|
||||
project_name: str = state.module.params["name"]
|
||||
rule: str = params["rule"]
|
||||
traefik_service: str | None = params.get("service")
|
||||
entrypoints: list[str] | None = params.get("entrypoints")
|
||||
middlewares: list[str] | None = params.get("middlewares")
|
||||
certresolver: str | None = params.get("certresolver")
|
||||
proxy_type: str = params["proxy_type"]
|
||||
router_name: str = params.get(
|
||||
"router_name",
|
||||
f"{project_name}_{service_name}_{proxy_type}",
|
||||
)
|
||||
|
@ -60,4 +59,4 @@ def helper(state: State) -> State:
|
|||
"labels": labels,
|
||||
}
|
||||
|
||||
return service.update(state, update)
|
||||
return service.update(service_name, state, update)
|
||||
|
|
|
@ -13,23 +13,22 @@ if TYPE_CHECKING:
|
|||
)
|
||||
|
||||
EXTRA_ARGS = {
|
||||
"proxy_type": {"type": "str"},
|
||||
"proxy_type": {"type": "str", "default": "http"},
|
||||
"service_name": {"type": "string"},
|
||||
"port": {"type": "int"},
|
||||
}
|
||||
|
||||
|
||||
def helper(state: State) -> State:
|
||||
service_name = state.module.params["name"]
|
||||
project_name = state.module.params["project_name"]
|
||||
port = state.module.params.get("port")
|
||||
proxy_type = state.module.params.get("proxy_type", "http")
|
||||
service_name = state.module.params.get(
|
||||
def helper(state: State, service_name: str, params: dict[str, Any]) -> State:
|
||||
project_name: str = state.module.params["name"]
|
||||
port: int | None = params.get("port")
|
||||
proxy_type: str = params["proxy_type"]
|
||||
traefik_service_name: str = params.get(
|
||||
"service_name",
|
||||
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] = {}
|
||||
|
||||
|
@ -40,4 +39,4 @@ def helper(state: State) -> State:
|
|||
"labels": labels,
|
||||
}
|
||||
|
||||
return service.update(state, update)
|
||||
return service.update(service_name, state, update)
|
||||
|
|
|
@ -21,11 +21,12 @@ BASE_ARGS = {
|
|||
"name": {"type": "str", "required": True},
|
||||
"image": {"type": "str", "required": True},
|
||||
"defaults": {"type": "dict"},
|
||||
"overwrite": {"type": "dict"},
|
||||
}
|
||||
|
||||
|
||||
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] = {
|
||||
"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:
|
||||
container_name = params["name"]
|
||||
defaults = params.get("defaults", {})
|
||||
def set_definition(state: State, params: dict[str, Any], param_name: str) -> State:
|
||||
service_name: str = params["name"]
|
||||
definition: dict[str, Any] = params.get(param_name, {})
|
||||
|
||||
project = copy.deepcopy(state.after)
|
||||
services = project["services"]
|
||||
service = services[container_name]
|
||||
services: dict[str, Any] = project["services"]
|
||||
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)
|
||||
|
||||
|
||||
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)
|
||||
service_name = state.module.params["name"]
|
||||
|
||||
_ = recursive_update(project["services"][service_name], update)
|
||||
|
||||
|
@ -72,6 +73,7 @@ def run_helper(
|
|||
helper: Callable[[State, dict[str, Any]], State] = lambda x, _: x,
|
||||
) -> State:
|
||||
state = apply_base(state, params)
|
||||
state = set_defaults(state, params)
|
||||
state = set_definition(state, params, "defaults")
|
||||
state = helper(state, params)
|
||||
state = set_definition(state, params, "overwrite")
|
||||
return update_project(state)
|
||||
|
|
|
@ -6,8 +6,9 @@ from __future__ import annotations
|
|||
import copy
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from ansible_collections.ssnailed.ez_compose.plugins.module_utils.service import ( # type: ignore[reportMissingTypeStubs]
|
||||
common as service,
|
||||
from ansible_collections.ssnailed.ez_compose.plugins.module_utils import ( # type: ignore[reportMissingTypeStubs]
|
||||
label,
|
||||
service,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
|
@ -15,6 +16,7 @@ if TYPE_CHECKING:
|
|||
State,
|
||||
)
|
||||
|
||||
# NOTE: Label helper arguments are added in the compose module itself
|
||||
EXTRA_ARGS = {
|
||||
"internal_network": {"type": "bool", "default": False},
|
||||
"definition": {"type": "dict", "required": True},
|
||||
|
@ -22,8 +24,8 @@ EXTRA_ARGS = {
|
|||
|
||||
|
||||
def helper(state: State, params: dict[str, Any]) -> State:
|
||||
definition = params["definition"]
|
||||
internal_network = params["internal_network"]
|
||||
definition: dict[str, Any] = params["definition"]
|
||||
internal_network: bool = params["internal_network"]
|
||||
|
||||
update = copy.deepcopy(definition)
|
||||
|
||||
|
@ -34,4 +36,7 @@ def helper(state: State, params: dict[str, Any]) -> State:
|
|||
|
||||
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)
|
||||
|
|
|
@ -14,19 +14,12 @@ if TYPE_CHECKING:
|
|||
State,
|
||||
)
|
||||
|
||||
EXTRA_ARGS = {
|
||||
"command": {"type": "list"},
|
||||
}
|
||||
EXTRA_ARGS = {}
|
||||
|
||||
|
||||
def helper(state: State, params: dict[str, Any]) -> State:
|
||||
command = params.get("command")
|
||||
|
||||
update = {
|
||||
"privileged": True,
|
||||
}
|
||||
|
||||
if command:
|
||||
update["command"] = command
|
||||
|
||||
return service.update(state, update)
|
||||
return service.update(params["name"], state, update)
|
||||
|
|
|
@ -15,12 +15,12 @@ if TYPE_CHECKING:
|
|||
)
|
||||
|
||||
EXTRA_ARGS = {
|
||||
"read_only": {"type": "bool"},
|
||||
"read_only": {"type": "bool", "default": True},
|
||||
}
|
||||
|
||||
|
||||
def helper(state: State, params: dict[str, Any]) -> State:
|
||||
read_only = params.get("read_only", True)
|
||||
read_only = params["read_only"]
|
||||
|
||||
volumes = [
|
||||
{
|
||||
|
@ -35,4 +35,4 @@ def helper(state: State, params: dict[str, Any]) -> State:
|
|||
"volumes": volumes,
|
||||
}
|
||||
|
||||
return service.update(state, update)
|
||||
return service.update(params["name"], state, update)
|
||||
|
|
|
@ -16,24 +16,24 @@ if TYPE_CHECKING:
|
|||
|
||||
EXTRA_ARGS = {
|
||||
"archive": {"type": "str"},
|
||||
"backup_volumes": {"type": "list"},
|
||||
"backup_volumes": {"type": "list", "elements": "str"},
|
||||
}
|
||||
|
||||
|
||||
def helper(state: State, params: dict[str, Any]) -> State:
|
||||
archive = params.get("archive")
|
||||
backup_volumes = params.get("backup_volumes", [])
|
||||
archive: str | None = params.get("archive")
|
||||
backup_volumes: list[str] | None = params.get("backup_volumes", [])
|
||||
service_name = params["name"]
|
||||
project_name = params["project_name"]
|
||||
|
||||
volumes = [
|
||||
volumes: list[dict[str, Any]] = [
|
||||
{
|
||||
"type": "volume",
|
||||
"source": volume,
|
||||
"target": f"/backup/{volume}",
|
||||
"read_only": True,
|
||||
}
|
||||
for volume in backup_volumes
|
||||
for volume in (backup_volumes or [])
|
||||
]
|
||||
|
||||
environment = {
|
||||
|
@ -60,4 +60,4 @@ def helper(state: State, params: dict[str, Any]) -> State:
|
|||
"volumes": volumes,
|
||||
}
|
||||
|
||||
return service.update(state, update)
|
||||
return service.update(params["name"], state, update)
|
||||
|
|
|
@ -16,7 +16,7 @@ if TYPE_CHECKING:
|
|||
)
|
||||
|
||||
EXTRA_ARGS = {
|
||||
"archive": {"type": "bool"},
|
||||
"backup": {"type": "bool", "default": True},
|
||||
"database": {"type": "str", "required": True},
|
||||
"username": {"type": "str", "required": True},
|
||||
"password": {"type": "str", "required": True},
|
||||
|
@ -25,13 +25,13 @@ EXTRA_ARGS = {
|
|||
|
||||
|
||||
def helper(state: State, params: dict[str, Any]) -> State:
|
||||
backup = params.get("backup", True)
|
||||
database = params["database"]
|
||||
username = params["username"]
|
||||
password = params["password"]
|
||||
root_password = params["root_password"]
|
||||
service_name = params["name"]
|
||||
project_name = params["project_name"]
|
||||
project_name: str = state.module.params["project_name"]
|
||||
backup: bool = params["backup"]
|
||||
database: str = params["database"]
|
||||
username: str = params["username"]
|
||||
password: str = params["password"]
|
||||
root_password: str | None = params.get("root_password")
|
||||
service_name: str = params["name"]
|
||||
|
||||
volumes = [
|
||||
{
|
||||
|
@ -84,4 +84,4 @@ def helper(state: State, params: dict[str, Any]) -> State:
|
|||
"labels": labels,
|
||||
}
|
||||
|
||||
return service.update(state, update)
|
||||
return service.update(params["name"], state, update)
|
||||
|
|
|
@ -16,7 +16,7 @@ if TYPE_CHECKING:
|
|||
)
|
||||
|
||||
EXTRA_ARGS = {
|
||||
"archive": {"type": "bool"},
|
||||
"backup": {"type": "bool", "default": True},
|
||||
"database": {"type": "str", "required": True},
|
||||
"username": {"type": "str", "required": True},
|
||||
"password": {"type": "str", "required": True},
|
||||
|
@ -24,12 +24,12 @@ EXTRA_ARGS = {
|
|||
|
||||
|
||||
def helper(state: State, params: dict[str, Any]) -> State:
|
||||
backup = params.get("backup", True)
|
||||
database = params["database"]
|
||||
username = params["username"]
|
||||
password = params["password"]
|
||||
service_name = params["name"]
|
||||
project_name = params["project_name"]
|
||||
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 = [
|
||||
{
|
||||
|
@ -75,4 +75,4 @@ def helper(state: State, params: dict[str, Any]) -> State:
|
|||
"labels": labels,
|
||||
}
|
||||
|
||||
return service.update(state, update)
|
||||
return service.update(params["name"], state, update)
|
||||
|
|
|
@ -67,13 +67,23 @@ def service_argument_spec() -> dict[str, Any]:
|
|||
def main() -> None:
|
||||
module = AnsibleModule(
|
||||
argument_spec={
|
||||
**common.BASE_ARGS,
|
||||
"name": {
|
||||
"type": "str",
|
||||
"required": True,
|
||||
},
|
||||
"project_dir": {
|
||||
"type": "path",
|
||||
"default": "/var/lib/ez_compose",
|
||||
},
|
||||
"services": service_argument_spec(),
|
||||
},
|
||||
)
|
||||
|
||||
state = common.get_state(module)
|
||||
|
||||
for name, args in module.params["services"].items():
|
||||
state = getattr(service, name).helper(state, args)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
Loading…
Add table
Reference in a new issue