import os
import pathlib
import yaml
from fortrace.utility.distribution_constants import OSType, PackageManager, ShellType
from fortrace.utility.logger_helper import setup_logger
logger = setup_logger(__name__)
[docs]
def validate_config(path: os.PathLike, collections: list[str] | None = None) -> bool:
"""Validate a configuration file.
In case of a root configuration, the children will be validated as well.
Args:
path: path to the configuration file
collections: list of collections
Returns:
True if config is valid, False otherwise. Refer to the logs to see complications
"""
if collections is None:
collections = []
valid = True
with open(path, "r") as f:
config = yaml.safe_load(f)
for key in ["name", "seed", "root", "domain"]:
if key not in config:
logger.error("'%s' is missing in config", key)
valid = False
for collection_name, _ in config.get("collections", {}).items():
collections.append(collection_name)
domain = config["domain"]
if isinstance(domain, list):
for dom in domain:
valid &= validate_config(dom, collections)
else:
valid &= validate_domain(domain, collections)
return valid
[docs]
def validate_domain(domain, collections: list[str]) -> bool:
valid = True
for key in ["username", "password", "virXMLConfig", "os_type", "shell_type"]:
if key not in domain:
logger.error("'%s' is missing in config", key)
valid = False
if type(eval(domain.get("osType"))) is not OSType:
logger.error("%s does not evaluate to OSType enum", domain["osType"])
valid = False
if type(eval(domain.get("shell_type"))) is not ShellType:
logger.error("%s does not evaluate to ShellType enum", domain["shell_type"])
valid = False
if type(eval(domain.get("package_manager"))) is not PackageManager:
logger.error(
"%s does not evaluate to PackageManager enum", domain["package_manager"]
)
valid = False
if not pathlib.Path(domain.get("virXMLConfig")).exists():
logger.error(
"%s does not exist. Make sure it is relative to ForTrace's root directory",
domain["virXMLConfig"],
)
valid = False
return valid
[docs]
def validate_generator():
pass