]> mj.ucw.cz Git - pynsc.git/blob - nsconfig/cli.py
First bits of daemon configuration
[pynsc.git] / nsconfig / cli.py
1 import argparse
2 from pathlib import Path
3 from texttable import Texttable
4
5 from nsconfig.core import Nsc, NscZonePrimary, NscZoneSecondary
6
7
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:
22                 z.dump(file=f)
23         elif isinstance(z, NscZoneSecondary):
24             print(f'Primary:     {z.primary_server}')
25         print()
26
27     if nsc.daemon:
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)
32
33
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)
39
40     for z in nsc.get_zones():
41         if not isinstance(z, NscZonePrimary):
42             table.add_row([z.name, 'secondary', "", "", "", ""])
43             continue
44         if z.is_changed():
45             action = '*'
46         else:
47             action = ""
48         table.add_row([
49             z.name,
50             z.prev_state.serial,
51             z.prev_state.hash,
52             z.state.serial,
53             z.state.hash,
54             action,
55         ])
56
57     print(table.draw())
58
59
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})')
64             z.write_zone()
65             z.write_state()
66
67
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')
71
72     test_parser = subparsers.add_parser('test', help='test new configuration', description='Test new configuration')
73
74     status_parser = subparsers.add_parser('status', help='list status of zones', description='List status of zones')
75
76     update_parser = subparsers.add_parser('update', help='update configuration', description='Update zone files and daemon configuration as needed')
77
78     args = parser.parse_args()
79
80     nsc.process()
81
82     if args.action == 'test':
83         do_test(nsc)
84     elif args.action == 'status':
85         do_status(nsc)
86     elif args.action == 'update':
87         do_update(nsc)