From df90b54f3a9d29e76b02f7df0c8c4b54c41558c0 Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Fri, 8 Dec 2023 22:17:11 +0100 Subject: [PATCH] journald compatible log level --- src/netcup_dns/main.py | 20 +++++++++++--- src/netcup_dns/print_util.py | 52 ++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 src/netcup_dns/print_util.py diff --git a/src/netcup_dns/main.py b/src/netcup_dns/main.py index ba18486..1c96020 100644 --- a/src/netcup_dns/main.py +++ b/src/netcup_dns/main.py @@ -13,6 +13,8 @@ from nc_dnsapi import Client, DNSRecord from netcup_dns.exception import UnknownIPException, MultipleRecordsException from netcup_dns.record_dst_cache import RecordDestinationCache +from src.netcup_dns.print_util import print_info, print_err + def main(): """ @@ -43,17 +45,25 @@ def main(): if type_ == 'A': # Lazy: Only determine external IPv4 if an A record shall be updated. - destination = external_ipv4() + try: + destination = external_ipv4() + except UnknownIPException as e: + print_err(str(e)) + exit(1) elif type_ == 'AAAA': # Lazy: Only determine external IPv6 if an AAAA record shall be updated. - destination = external_ipv6() + try: + destination = external_ipv6() + except UnknownIPException as e: + print_err(str(e)) + exit(1) else: raise ValueError(f'DNS record type {type_} is not supported.') if update_record_destination(api, domain, hostname, type_, destination, cache): - print(f'Set {hostname}.{domain} {type_} record to {destination}') + print_info(f'Set {hostname}.{domain} {type_} record to {destination}') else: - print(f'The {hostname}.{domain} {type_} record points already to {destination}') + print_info(f'The {hostname}.{domain} {type_} record points already to {destination}') def parse_args(): @@ -240,6 +250,7 @@ def external_ipv4(timeout: float = 5, endpoints: list[str] = None) -> str: :param endpoints: List of webservices that return ones public IPv4 address. :argument timeout: Timeout for each webservice in seconds. :return: Public IPv4 address. + :raises UnknownIPException: """ return external_ip(ipaddress.IPv4Address, timeout, endpoints) @@ -250,6 +261,7 @@ def external_ipv6(timeout: float = 5, endpoints: list[str] = None) -> str: :param endpoints: List of webservices that return ones public IPv6 address. :argument timeout: Timeout for each webservice in seconds. :return: Public IPv6 address. + :raises UnknownIPException: """ return external_ip(ipaddress.IPv6Address, timeout, endpoints) diff --git a/src/netcup_dns/print_util.py b/src/netcup_dns/print_util.py new file mode 100644 index 0000000..ddec96b --- /dev/null +++ b/src/netcup_dns/print_util.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +import sys + + +# https://www.freedesktop.org/software/systemd/man/latest/sd-daemon.html +# +# define SD_EMERG "<0>" /* system is unusable */ +# define SD_ALERT "<1>" /* action must be taken immediately */ +# define SD_CRIT "<2>" /* critical conditions */ +# define SD_ERR "<3>" /* error conditions */ +# define SD_WARNING "<4>" /* warning conditions */ +# define SD_NOTICE "<5>" /* normal but significant condition */ +# define SD_INFO "<6>" /* informational */ +# define SD_DEBUG "<7>" /* debug-level messages */ + +def print_emerg(message: object): + do_print('<0> ', message, file=sys.stderr) + + +def print_alert(message: object): + do_print('<1> ', message, file=sys.stderr) + + +def print_crit(message: object): + do_print('<2> ', message, file=sys.stderr) + + +def print_err(message: object): + do_print('<3> ', message, file=sys.stderr) + + +def print_warning(message: object): + do_print('<4> ', message, file=sys.stderr) + + +def print_notice(message: object): + do_print('<5> ', message) + + +def print_info(message: object): + do_print('<6> ', message) + + +def print_debug(message: object): + do_print('<7> ', message) + + +def do_print(line_prefix: str, message: object, file=sys.stdout) -> None: + # Prefix each line in str(object) with `line_prefix`. + prefixed = line_prefix + str(message).replace('\n', '\n' + line_prefix) + print(prefixed, file=file)