]> mj.ucw.cz Git - pynsc.git/blob - nsconfig/cli.py
Generic daemon module does not have a config path
[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, NscZoneAlias
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             if z.aliases:
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:
25                 z.dump(file=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}')
30         print()
31
32     if nsc.daemon:
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)
37
38
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)
44
45     for z in nsc.get_zones():
46         if z.is_changed():
47             action = '*'
48         else:
49             action = ""
50         if isinstance(z, NscZonePrimary):
51             table.add_row([
52                 z.name,
53                 z.prev_state.serial,
54                 z.prev_state.hash,
55                 z.state.serial,
56                 z.state.hash,
57                 action,
58             ])
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])
63         else:
64             raise NotImplementedError()
65
66     print(table.draw())
67
68
69 def do_update(nsc: Nsc) -> None:
70     nsc.daemon.write_config()
71
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})')
75             z.write_zone()
76             nsc.daemon.reload_zone(z)
77             for alias in z.aliases:
78                 nsc.daemon.reload_zone(z)
79             z.write_state()
80
81     nsc.daemon.reload_daemon()
82
83
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')
87
88     test_parser = subparsers.add_parser('test', help='test new configuration', description='Test new configuration')
89
90     status_parser = subparsers.add_parser('status', help='list status of zones', description='List status of zones')
91
92     update_parser = subparsers.add_parser('update', help='update configuration', description='Update zone files and daemon configuration as needed')
93
94     args = parser.parse_args()
95
96     nsc.process()
97
98     if args.action == 'test':
99         do_test(nsc)
100     elif args.action == 'status':
101         do_status(nsc)
102     elif args.action == 'update':
103         do_update(nsc)