1 from io import StringIO
2 from pathlib import Path
3 from typing import TextIO
7 from nsconfig.core import Nsc, NscZone
13 def __init__(self) -> None:
16 def setup(self, nsc: Nsc) -> None:
19 def dump_config(self, file: TextIO = sys.stdout) -> None:
22 def write_config(self) -> None:
25 def reload_zone(self, z: NscZone) -> None:
28 def reload_daemon(self) -> None:
31 def _install_config(self, path: Path, new_contents: str) -> bool:
33 old_new_contents = path.read_text()
34 except FileNotFoundError:
35 old_new_contents = None
36 if new_contents == old_new_contents:
39 new_path = Path(str(path) + '.new')
40 with open(new_path, 'w') as f:
42 new_path.replace(path)
45 def _write_config(self, config_path: Path) -> bool:
46 string_stream = StringIO()
47 self.dump_config(string_stream)
48 if self._install_config(config_path, string_stream.getvalue()):
49 print('Wrote new daemon configuration')
52 print('Daemon configuration not changed')
55 def _run_command(self, argv, **kwargs) -> None:
56 res = subprocess.run(argv, **kwargs)
57 if res.returncode > 0:
58 print(f'Command failed: {argv}')
62 class NscDaemonNull(NscDaemon):