]> mj.ucw.cz Git - pynsc.git/blob - nsconfig/cli.py
Updating zones
[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
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'Old serial:  {z.prev_state.serial}')
14         print(f'Old hash:    {z.prev_state.hash}')
15         print(f'New serial:  {z.state.serial}')
16         print(f'New hash:    {z.state.hash}')
17         out_file = test_dir / z.safe_name
18         print(f'Dumping to:  {out_file}')
19         with open(out_file, 'w') as f:
20             z.dump(file=f)
21         print()
22
23
24 def do_status(nsc: Nsc) -> None:
25     table = Texttable(max_width=0)
26     table.header(['Zone', 'Old serial', 'Old hash', 'New serial', 'New hash', 'S'])
27     table.set_cols_dtype(['t', 'i', 't', 'i', 't', 't'])
28     table.set_deco(Texttable.HEADER)
29
30     for z in nsc.get_zones():
31         if z.state.serial == z.prev_state.serial:
32             action = ""
33         else:
34             action = '*'
35         table.add_row([
36             z.name,
37             z.prev_state.serial,
38             z.prev_state.hash,
39             z.state.serial,
40             z.state.hash,
41             action,
42         ])
43
44     print(table.draw())
45
46
47 def do_update(nsc: Nsc) -> None:
48     for z in nsc.get_zones():
49         if z.state.serial != z.prev_state.serial:
50             print(f'Updating zone {z.name} (serial {z.state.serial})')
51             z.write_zone()
52             z.write_state()
53
54
55 def main(nsc: Nsc) -> None:
56     parser = argparse.ArgumentParser(description='Configure name server')
57     subparsers = parser.add_subparsers(help='action to perform', dest='action', required=True, metavar='ACTION')
58
59     test_parser = subparsers.add_parser('test', help='test new configuration', description='Test new configuration')
60
61     status_parser = subparsers.add_parser('status', help='list status of zones', description='List status of zones')
62
63     update_parser = subparsers.add_parser('update', help='update configuration', description='Update zone files and daemon configuration as needed')
64
65     args = parser.parse_args()
66
67     nsc.process()
68
69     if args.action == 'test':
70         do_test(nsc)
71     elif args.action == 'status':
72         do_status(nsc)
73     elif args.action == 'update':
74         do_update(nsc)