diff options
author | Indrajit Raychaudhuri <irc@indrajit.com> | 2023-12-29 18:21:33 -0600 |
---|---|---|
committer | Indrajit Raychaudhuri <irc@indrajit.com> | 2024-01-21 13:29:25 -0600 |
commit | 1c1fb5fb4bd7c0d205b28caf90357ad56423464f (patch) | |
tree | 44730f00ce994eca774aac1dc1d02c61d7c5ca9c /src/op_mode/dns.py | |
parent | 119efb6d8d353482d598287f49e22aa68a22e960 (diff) | |
download | vyos-1x-1c1fb5fb4bd7c0d205b28caf90357ad56423464f.tar.gz vyos-1x-1c1fb5fb4bd7c0d205b28caf90357ad56423464f.zip |
dns: T5959: Streamline dns forwarding service
Streamline configuration and operation of dns forwarding service in
following ways:
- Remove `dns_forwarding_reset.py` as its functionality is now covered
by `dns.py`
- Adjust function names in `dns.py` to disambiguate between DNS
forwarding and dynamic DNS
- Remove `dns_forwarding_restart.sh` as its functionality is inlined in
`dns-forwarding.xml`
- Templatize systemd override for `pdns-recursor.service` and move the
generated override files in /run. This ensures that the override files
are always generated afresh after boot
- Simplify the systemd override file by removing the redundant overrides
- Relocate configuration path for pdns-recursor to `/run/pdns-recursor`
and utilize the `RuntimeDirectory` default that pdns-recursor expects
- We do not need to use custom `--socket-dir` path anymore, the default
path (viz., `/run/pdns-recursor` is fine)
Diffstat (limited to 'src/op_mode/dns.py')
-rwxr-xr-x | src/op_mode/dns.py | 64 |
1 files changed, 46 insertions, 18 deletions
diff --git a/src/op_mode/dns.py b/src/op_mode/dns.py index 2168aef89..309bef3b9 100755 --- a/src/op_mode/dns.py +++ b/src/op_mode/dns.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 # -# Copyright (C) 2022 VyOS maintainers and contributors +# Copyright (C) 2022-2024 VyOS maintainers and contributors # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 or later as @@ -15,17 +15,16 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. +import typing import sys +import vyos.opmode from tabulate import tabulate - from vyos.configquery import ConfigTreeQuery -from vyos.utils.process import cmd +from vyos.utils.process import cmd, rc_cmd -import vyos.opmode - -def _data_to_dict(data, sep="\t") -> dict: +def _forwarding_data_to_dict(data, sep="\t") -> dict: """ Return dictionary from plain text separated by tab @@ -52,15 +51,15 @@ def _data_to_dict(data, sep="\t") -> dict: return dictionary -def _get_raw_forwarding_statistics() -> dict: - command = cmd('rec_control --socket-dir=/run/powerdns get-all') - data = _data_to_dict(command) +def _get_forwarding_statistics_raw() -> dict: + command = cmd('rec_control get-all') + data = _forwarding_data_to_dict(command) data['cache-size'] = "{0:.2f}".format( int( - cmd('rec_control --socket-dir=/run/powerdns get cache-bytes')) / 1024 ) + cmd('rec_control get cache-bytes')) / 1024 ) return data -def _get_formatted_forwarding_statistics(data): +def _get_forwarding_statistics_formatted(data): cache_entries = data.get('cache-entries') max_cache_entries = data.get('max-cache-entries') cache_size = data.get('cache-size') @@ -69,19 +68,48 @@ def _get_formatted_forwarding_statistics(data): output = tabulate(data_entries, headers, numalign="left") return output +def _verify_forwarding(func): + """Decorator checks if DNS Forwarding config exists""" + from functools import wraps -def show_forwarding_statistics(raw: bool): - - config = ConfigTreeQuery() - if not config.exists('service dns forwarding'): - raise vyos.opmode.UnconfiguredSubsystem('DNS forwarding is not configured') + @wraps(func) + def _wrapper(*args, **kwargs): + config = ConfigTreeQuery() + if not config.exists('service dns forwarding'): + raise vyos.opmode.UnconfiguredSubsystem('DNS Forwarding is not configured') + return func(*args, **kwargs) + return _wrapper - dns_data = _get_raw_forwarding_statistics() +@_verify_forwarding +def show_forwarding_statistics(raw: bool): + dns_data = _get_forwarding_statistics_raw() if raw: return dns_data else: - return _get_formatted_forwarding_statistics(dns_data) + return _get_forwarding_statistics_formatted(dns_data) +@_verify_forwarding +def reset_forwarding(all: bool, domain: typing.Optional[str]): + """ + Reset DNS Forwarding cache + + :param all (bool): reset cache all domains + :param domain (str): reset cache for specified domain + """ + if all: + rc, output = rc_cmd('rec_control wipe-cache ".$"') + if rc != 0: + print(output) + return None + print('DNS Forwarding cache reset for all domains!') + return output + elif domain: + rc, output = rc_cmd(f'rec_control wipe-cache "{domain}$"') + if rc != 0: + print(output) + return None + print(f'DNS Forwarding cache reset for domain "{domain}"!') + return output if __name__ == '__main__': try: |