journald compatible log level

This commit is contained in:
Daniel Langbein 2023-12-08 22:17:11 +01:00
parent 6f9a388d23
commit df90b54f3a
Signed by: langfingaz
GPG Key ID: 6C47C753F0823002
2 changed files with 68 additions and 4 deletions

View File

@ -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)

View File

@ -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)