1 from pathlib import Path
3 from typing import TextIO
5 from nsconfig.core import NscZone, NscZonePrimary, NscZoneSecondary
6 from nsconfig.daemon import NscDaemon
9 class NscDaemonBind(NscDaemon):
12 need_full_reload: bool
15 config_file: str = 'named.conf.nsc',
16 control_command: str = 'rndc') -> None:
18 self.config_path = Path(config_file)
19 self.control_command = control_command
20 self.need_full_reload = False
22 def dump_config(self, file: TextIO = sys.stdout) -> None:
23 file.write('# Domains managed by NSC\n')
24 file.write('# This file was automatically generated by NSC, please do not edit manually.\n\n')
25 for z in self.nsc.get_zones():
26 file.write(f'zone "{z.name}" in {{\n') # broken editor: }}
27 if isinstance(z, NscZonePrimary):
28 file.write('\ttype master;\n')
29 file.write(f'\tfile "{z.zone_file}";\n')
30 elif isinstance(z, NscZoneSecondary):
31 file.write('\ttype slave;\n')
32 file.write(f'\tfile "zone/{z.secondary_file}";\n')
33 file.write(f'\tmasters {{ {z.primary_server}; }};\n')
35 raise NotImplementedError()
38 def write_config(self) -> None:
39 if self._write_config(self.config_path):
40 self.need_full_reload = True
42 def reload_zone(self, z: NscZone) -> None:
43 if isinstance(z, NscZonePrimary) and not self.need_full_reload:
44 print(f'Reloading zone {z.name}')
45 self._run_command([self.control_command, 'reload', z.name])
47 def reload_daemon(self) -> None:
48 if self.need_full_reload:
49 print('Reloading daemon')
50 self._run_command([self.control_command, 'reload'])