(refactor) Remove YAML config import and export path
Drop the pre-Django YAML import/export management commands and remove the file-based config loader fallback from the backup and retention engines. Keep the runtime config bridge backed by Django models, and add tests that ensure engine operations require an explicit Django config source.
This commit is contained in:
@@ -1,13 +1,10 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
|
||||
from pobsync.config.schemas import GLOBAL_SCHEMA, HOST_SCHEMA
|
||||
from pobsync.paths import PobsyncPaths
|
||||
from pobsync.util import write_yaml_atomic
|
||||
from pobsync.validate import validate_dict
|
||||
|
||||
from .models import GlobalConfig, HostConfig
|
||||
@@ -17,7 +14,7 @@ class ConfigRepositoryError(RuntimeError):
|
||||
pass
|
||||
|
||||
|
||||
def _global_yaml_data(global_config: GlobalConfig) -> dict[str, Any]:
|
||||
def _global_runtime_data(global_config: GlobalConfig) -> dict[str, Any]:
|
||||
data = {
|
||||
"backup_root": global_config.backup_root,
|
||||
"pobsync_home": global_config.pobsync_home,
|
||||
@@ -48,7 +45,7 @@ def _global_yaml_data(global_config: GlobalConfig) -> dict[str, Any]:
|
||||
return validate_dict(data, GLOBAL_SCHEMA, path="global")
|
||||
|
||||
|
||||
def _host_yaml_data(host_config: HostConfig) -> dict[str, Any]:
|
||||
def _host_runtime_data(host_config: HostConfig) -> dict[str, Any]:
|
||||
data: dict[str, Any] = {
|
||||
"host": host_config.host,
|
||||
"address": host_config.address,
|
||||
@@ -78,11 +75,11 @@ def _host_yaml_data(host_config: HostConfig) -> dict[str, Any]:
|
||||
|
||||
|
||||
def global_config_object_data(global_config: GlobalConfig) -> dict[str, Any]:
|
||||
return _global_yaml_data(global_config)
|
||||
return _global_runtime_data(global_config)
|
||||
|
||||
|
||||
def host_config_object_data(host_config: HostConfig) -> dict[str, Any]:
|
||||
return _host_yaml_data(host_config)
|
||||
return _host_runtime_data(host_config)
|
||||
|
||||
|
||||
def global_config_data(name: str = "default") -> dict[str, Any]:
|
||||
@@ -90,7 +87,7 @@ def global_config_data(name: str = "default") -> dict[str, Any]:
|
||||
global_config = GlobalConfig.objects.get(name=name)
|
||||
except ObjectDoesNotExist as exc:
|
||||
raise ConfigRepositoryError(f"Missing GlobalConfig {name!r}") from exc
|
||||
return _global_yaml_data(global_config)
|
||||
return _global_runtime_data(global_config)
|
||||
|
||||
|
||||
def host_config_data(host: str) -> dict[str, Any]:
|
||||
@@ -98,37 +95,4 @@ def host_config_data(host: str) -> dict[str, Any]:
|
||||
host_config = HostConfig.objects.get(host=host, enabled=True)
|
||||
except ObjectDoesNotExist as exc:
|
||||
raise ConfigRepositoryError(f"Missing enabled HostConfig {host!r}") from exc
|
||||
return _host_yaml_data(host_config)
|
||||
|
||||
|
||||
def export_global_config(prefix: Path, name: str = "default") -> Path:
|
||||
try:
|
||||
global_config = GlobalConfig.objects.get(name=name)
|
||||
except ObjectDoesNotExist as exc:
|
||||
raise ConfigRepositoryError(f"Missing GlobalConfig {name!r}") from exc
|
||||
|
||||
paths = PobsyncPaths(home=prefix)
|
||||
write_yaml_atomic(paths.global_config_path, _global_yaml_data(global_config))
|
||||
return paths.global_config_path
|
||||
|
||||
|
||||
def export_host_config(prefix: Path, host: str) -> Path:
|
||||
try:
|
||||
host_config = HostConfig.objects.get(host=host, enabled=True)
|
||||
except ObjectDoesNotExist as exc:
|
||||
raise ConfigRepositoryError(f"Missing enabled HostConfig {host!r}") from exc
|
||||
|
||||
paths = PobsyncPaths(home=prefix)
|
||||
target = paths.hosts_dir / f"{host_config.host}.yaml"
|
||||
write_yaml_atomic(target, _host_yaml_data(host_config))
|
||||
return target
|
||||
|
||||
|
||||
def export_runtime_configs(prefix: Path, host: str | None = None) -> list[Path]:
|
||||
written = [export_global_config(prefix)]
|
||||
hosts = HostConfig.objects.filter(enabled=True).order_by("host")
|
||||
if host is not None:
|
||||
hosts = hosts.filter(host=host)
|
||||
for host_config in hosts:
|
||||
written.append(export_host_config(prefix, host_config.host))
|
||||
return written
|
||||
return _host_runtime_data(host_config)
|
||||
|
||||
Reference in New Issue
Block a user