67 lines
1.5 KiB
Python
67 lines
1.5 KiB
Python
# Copyright: (c) 2024, Luca Bilke <luca@bil.ke>
|
|
# MIT License (see LICENSE)
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
import yaml
|
|
from pydantic import BaseModel, ConfigDict
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
|
|
class Result(BaseModel):
|
|
model_config = ConfigDict(frozen=True)
|
|
|
|
changed: bool = False
|
|
diff: dict[str, Any] = {}
|
|
|
|
|
|
class Settings(BaseModel):
|
|
model_config = ConfigDict(frozen=True)
|
|
|
|
projects_dir: str = "/usr/local/share/ez_compose/"
|
|
|
|
|
|
class State(BaseModel):
|
|
model_config = ConfigDict(frozen=True)
|
|
|
|
module: AnsibleModule
|
|
result: Result
|
|
settings: Settings
|
|
compose_filename: str
|
|
before: dict[str, Any] = {}
|
|
after: dict[str, Any] = {}
|
|
|
|
|
|
def new_state(module: AnsibleModule) -> State:
|
|
settings = Settings(**module.params["settings"])
|
|
|
|
return State(
|
|
module=module,
|
|
result=Result(),
|
|
settings=settings,
|
|
compose_filename=(
|
|
f"{settings.projects_dir}/"
|
|
+ f"{module.params['project_name']}/"
|
|
+ f"{module.params['name']}.yml"
|
|
),
|
|
)
|
|
|
|
|
|
def get_compose(state: State) -> State:
|
|
file = state.compose_filename
|
|
|
|
with open(file, "r") as stream:
|
|
compose = yaml.safe_load(stream)
|
|
|
|
return state.model_copy(update={"before": compose})
|
|
|
|
|
|
def write_compose(state: State) -> State:
|
|
file = state.compose_filename
|
|
|
|
with open(file, mode="w") as stream:
|
|
yaml.dump(state.after, stream)
|
|
|
|
return state
|