2 from pathlib import Path
3 from texttable import Texttable
5 from nsconfig.core import Nsc, NscZonePrimary, NscZoneSecondary
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):
15 print(f'Old serial: {z.prev_state.serial}')
16 print(f'Old hash: {z.prev_state.hash}')
17 print(f'New serial: {z.state.serial}')
18 print(f'New hash: {z.state.hash}')
19 out_file = test_dir / z.safe_name
20 print(f'Dumping to: {out_file}')
21 with open(out_file, 'w') as f:
23 elif isinstance(z, NscZoneSecondary):
24 print(f'Primary: {z.primary_server}')
28 conf_file = test_dir / nsc.daemon.config_path.name
29 print(f'Dumping daemon config to {conf_file}')
30 with open(conf_file, 'w') as f:
31 nsc.daemon.dump_config(file=f)
34 def do_status(nsc: Nsc) -> None:
35 table = Texttable(max_width=0)
36 table.header(['Zone', 'Old serial', 'Old hash', 'New serial', 'New hash', 'S'])
37 table.set_cols_dtype(['t', 'i', 't', 'i', 't', 't'])
38 table.set_deco(Texttable.HEADER)
40 for z in nsc.get_zones():
41 if not isinstance(z, NscZonePrimary):
42 table.add_row([z.name, 'secondary', "", "", "", ""])
60 def do_update(nsc: Nsc) -> None:
61 for z in nsc.get_zones():
62 if isinstance(z, NscZonePrimary) and z.is_changed():
63 print(f'Updating zone {z.name} (serial {z.state.serial})')
68 def main(nsc: Nsc) -> None:
69 parser = argparse.ArgumentParser(description='Configure name server')
70 subparsers = parser.add_subparsers(help='action to perform', dest='action', required=True, metavar='ACTION')
72 test_parser = subparsers.add_parser('test', help='test new configuration', description='Test new configuration')
74 status_parser = subparsers.add_parser('status', help='list status of zones', description='List status of zones')
76 update_parser = subparsers.add_parser('update', help='update configuration', description='Update zone files and daemon configuration as needed')
78 args = parser.parse_args()
82 if args.action == 'test':
84 elif args.action == 'status':
86 elif args.action == 'update':