2 from pathlib import Path
3 from texttable import Texttable
5 from nsconfig.core import Nsc, NscZonePrimary, NscZoneSecondary, NscZoneAlias
8 def do_test(nsc: Nsc) -> None:
9 test_dir = Path('test')
10 test_dir.mkdir(exist_ok=True)
11 for z in nsc.get_zones():
12 print(f'Zone: {z.name}')
13 print(f'Type: {z.zone_type.name}')
14 if isinstance(z, NscZonePrimary):
16 aliases = ' '.join([alias.name for alias in z.aliases])
17 print(f'Aliases: {aliases}')
18 print(f'Old serial: {z.prev_state.serial}')
19 print(f'Old hash: {z.prev_state.hash}')
20 print(f'New serial: {z.state.serial}')
21 print(f'New hash: {z.state.hash}')
22 out_file = test_dir / z.safe_name
23 print(f'Dumping to: {out_file}')
24 with open(out_file, 'w') as f:
26 elif isinstance(z, NscZoneSecondary):
27 print(f'Primary: {z.primary_server}')
28 elif isinstance(z, NscZoneAlias):
29 print(f'Alias for: {z.alias_for.name}')
33 conf_file = test_dir / 'daemon.conf'
34 print(f'Dumping daemon config to {conf_file}')
35 with open(conf_file, 'w') as f:
36 nsc.daemon.dump_config(file=f)
39 def do_status(nsc: Nsc) -> None:
40 table = Texttable(max_width=0)
41 table.header(['Zone', 'Old serial', 'Old hash', 'New serial', 'New hash', 'S'])
42 table.set_cols_dtype(['t', 'i', 't', 'i', 't', 't'])
43 table.set_deco(Texttable.HEADER)
45 for z in nsc.get_zones():
50 if isinstance(z, NscZonePrimary):
59 elif isinstance(z, NscZoneSecondary):
60 table.add_row([z.name, 'secondary', "", "", "", action])
61 elif isinstance(z, NscZoneAlias):
62 table.add_row([z.name, 'alias', "", "", "", action])
64 raise NotImplementedError()
69 def do_update(nsc: Nsc) -> None:
70 nsc.daemon.write_config()
72 for z in nsc.get_zones():
73 if isinstance(z, NscZonePrimary) and z.is_changed():
74 print(f'Updating zone {z.name} (serial {z.state.serial})')
76 nsc.daemon.reload_zone(z)
77 for alias in z.aliases:
78 nsc.daemon.reload_zone(z)
81 nsc.daemon.reload_daemon()
84 def main(nsc: Nsc) -> None:
85 parser = argparse.ArgumentParser(description='Configure name server')
86 subparsers = parser.add_subparsers(help='action to perform', dest='action', required=True, metavar='ACTION')
88 test_parser = subparsers.add_parser('test', help='test new configuration', description='Test new configuration')
90 status_parser = subparsers.add_parser('status', help='list status of zones', description='List status of zones')
92 update_parser = subparsers.add_parser('update', help='update configuration', description='Update zone files and daemon configuration as needed')
94 args = parser.parse_args()
98 if args.action == 'test':
100 elif args.action == 'status':
102 elif args.action == 'update':