]> mj.ucw.cz Git - pynsc.git/blob - nsconfig/daemon/bind.py
More daemon configuration
[pynsc.git] / nsconfig / daemon / bind.py
1 from pathlib import Path
2 import sys
3 from typing import TextIO
4
5 from nsconfig.core import NscZone, NscZonePrimary, NscZoneSecondary
6 from nsconfig.daemon import NscDaemon
7
8
9 class NscDaemonBind(NscDaemon):
10     config_path: Path
11     control_command: str
12     need_full_reload: bool
13
14     def __init__(self,
15                  config_file: str = 'named.conf.nsc',
16                  control_command: str = 'rndc') -> None:
17         super().__init__()
18         self.config_path = Path(config_file)
19         self.control_command = control_command
20         self.need_full_reload = False
21
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')
34             else:
35                 raise NotImplementedError()
36             file.write('}\n\n')
37
38     def write_config(self) -> None:
39         if self._write_config(self.config_path):
40             self.need_full_reload = True
41
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])
46
47     def reload_daemon(self) -> None:
48         if self.need_full_reload:
49             print('Reloading daemon')
50             self._run_command([self.control_command, 'reload'])