Skip to content

Commit

Permalink
Add YAML config schema and validation
Browse files Browse the repository at this point in the history
Signed-off-by: Tobias Wolf <[email protected]>
  • Loading branch information
NotTheEvilOne authored May 6, 2024
1 parent f267d13 commit 27aa23a
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 8 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ repos:
rev: v4.5.0
hooks:
- id: check-yaml
args: [ --allow-multiple-documents ]
- id: check-json
- id: check-toml
- id: check-merge-conflict
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ rsa==4.9
six==1.16.0
structlog==24.1.0
urllib3==2.2.1
yamale==5.1.0
websocket-client==1.7.0
wrapt==1.16.0
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ install_requires=file:requirements.txt
[options.extras_require]
tests =
pytest==8.0.2

[options.package_data]
rookify=config.schema.yaml
11 changes: 5 additions & 6 deletions src/rookify/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

import os
import rookify.modules

from types import MappingProxyType
from .yaml import load_yaml, save_yaml
from rookify.logger import configure_logging, get_logger
from .logger import configure_logging, get_logger
from .yaml import load_config, load_module_data, save_module_data


def main() -> None:
# Load configuration file
try:
config = load_yaml("config.yaml")
config = load_config("config.yaml")
except FileNotFoundError as err:
raise SystemExit(f"Could not load config: {err}")

Expand All @@ -31,7 +30,7 @@ def main() -> None:
module_data = dict()

try:
module_data.update(load_yaml(config["general"]["module_data_file"]))
module_data.update(load_module_data(config["general"]["module_data_file"]))
except FileNotFoundError:
pass

Expand Down Expand Up @@ -70,7 +69,7 @@ def main() -> None:
result = handler.run()
module_data[migration_module.MODULE_NAME] = result

save_yaml(config["general"]["module_data_file"], module_data)
save_module_data(config["general"]["module_data_file"], module_data)

log.info("Data was updated to module_data_file.")

Expand Down
26 changes: 26 additions & 0 deletions src/rookify/config.schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
general:
module_data_file: str()

ceph:
conf_file: str()
keyring: str()

ssh:
private_key: str()
hosts: map(include("ssh_host"), key=str(), min=1)

kubernetes:
config: str()

rook:
cluster:
name: str()
namespace: str()
ceph:
image: str()

migration_modules: list(str())
---
ssh_host:
address: ip()
user: str()
26 changes: 24 additions & 2 deletions src/rookify/yaml.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
# -*- coding: utf-8 -*-

import importlib.resources
import importlib.resources.abc
import yamale
import yaml
from pathlib import Path
from typing import Any, Dict


def load_yaml(path: str) -> Dict[str, Any]:
_config_schema_file: Path | importlib.resources.abc.Traversable = Path(
"rookify", "config.schema.yaml"
)
for entry in importlib.resources.files("rookify").iterdir():
if entry.name == "config.schema.yaml":
_config_schema_file = entry


def load_config(path: str) -> Dict[str, Any]:
schema = yamale.make_schema(_config_schema_file)
data = yamale.make_data(path)

yamale.validate(schema, data)

assert isinstance(data[0][0], dict)
return data[0][0]


def load_module_data(path: str) -> Dict[str, Any]:
with open(path, "r") as file:
data = yaml.safe_load(file)
assert isinstance(data, dict)
return data


def save_yaml(path: str, data: Dict[str, Any]) -> None:
def save_module_data(path: str, data: Dict[str, Any]) -> None:
with open(path, "w") as file:
yaml.safe_dump(data, file)

0 comments on commit 27aa23a

Please sign in to comment.