49 lines
1.7 KiB
Python
49 lines
1.7 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from pathlib import Path
|
||
|
|
from typing import Any
|
||
|
|
|
||
|
|
from django.conf import settings
|
||
|
|
from django.core.management.base import BaseCommand, CommandError
|
||
|
|
|
||
|
|
from pobsync.config.load import load_global_config, load_host_config
|
||
|
|
from pobsync.paths import PobsyncPaths
|
||
|
|
from pobsync_backend.models import GlobalConfig, HostConfig
|
||
|
|
|
||
|
|
|
||
|
|
class Command(BaseCommand):
|
||
|
|
help = "Import pobsync YAML configs into the Django database."
|
||
|
|
|
||
|
|
def add_arguments(self, parser) -> None:
|
||
|
|
parser.add_argument("--prefix", default=settings.POBSYNC_HOME, help="Pobsync home directory")
|
||
|
|
|
||
|
|
def handle(self, *args: Any, **options: Any) -> None:
|
||
|
|
paths = PobsyncPaths(home=Path(options["prefix"]))
|
||
|
|
if not paths.global_config_path.exists():
|
||
|
|
raise CommandError(f"Missing global config: {paths.global_config_path}")
|
||
|
|
|
||
|
|
global_cfg = load_global_config(paths.global_config_path)
|
||
|
|
GlobalConfig.objects.update_or_create(
|
||
|
|
name="default",
|
||
|
|
defaults={
|
||
|
|
"backup_root": global_cfg["backup_root"],
|
||
|
|
"pobsync_home": global_cfg.get("pobsync_home", str(paths.home)),
|
||
|
|
"data": global_cfg,
|
||
|
|
},
|
||
|
|
)
|
||
|
|
|
||
|
|
count = 0
|
||
|
|
for host_path in sorted(paths.hosts_dir.glob("*.yaml")):
|
||
|
|
host_cfg = load_host_config(host_path)
|
||
|
|
HostConfig.objects.update_or_create(
|
||
|
|
host=host_cfg["host"],
|
||
|
|
defaults={
|
||
|
|
"address": host_cfg["address"],
|
||
|
|
"config": host_cfg,
|
||
|
|
"enabled": True,
|
||
|
|
},
|
||
|
|
)
|
||
|
|
count += 1
|
||
|
|
|
||
|
|
self.stdout.write(self.style.SUCCESS(f"Imported global config and {count} host config(s)."))
|