#!/usr/bin/env python3 """ Settings for journal-postfix. """ import os import datetime from typing import Union, Optional from systemd import journal from yaml import load main_config_file: str = '/etc/journal-postfix/main.yml' """ Filepath to the main config file. Can be overriden by environment variable JOURNAL_POSTFIX_MAIN_CONF. """ systemd_unitname: str = 'postfix@-.service' """ Name of the systemd unit running the postfix service. """ journal_poll_interval: Union[float, int] = 10.0 """ Poll timeout in seconds for fetching messages from the journal. Will be overriden if set in the main config. If the poll times out, it is checked whether the last commit lies more than max_delay_before_commit seconds in the past; if so, the current database transaction will be committed. """ max_delay_before_commit: datetime.timedelta = datetime.timedelta(seconds=30) """ How much time may pass before committing a database transaction? Will be overriden if set in the main config. (The actual maximal delay can be one journal_poll_interval in addition.) """ max_messages_per_commit: int = 1000 """ How many messages to cache at most before committing a database transaction? Will be overriden if set in the main config. """ delete_deliveries_after_days: int = 0 """ After how many days shall deliveries be deleted from the database? A value of 0 means that data are never deleted. """ def get_config() -> Optional[dict]: """ Load config from the main config and return it. The default main config file path (global main_config_file) can be overriden with environment variable JOURNAL_POSTFIX_MAIN_CONF. """ try: filename = os.environ['JOURNAL_POSTFIX_MAIN_CONF'] global main_config_file main_config_file = filename except Exception: filename = main_config_file try: with open(filename, 'r') as config_file: config_raw = config_file.read() except Exception: msg = f'ERROR: cannot read config file {filename}' journal.send(msg, PRIORITY=journal.LOG_CRIT) return None try: config = load(config_raw) except Exception as err: msg = f'ERROR: invalid yaml syntax in {filename}: {err}' journal.send(msg, PRIORITY=journal.LOG_CRIT) return None # override some global variables _global_value_from_config(config['postfix'], 'systemd_unitname', str) _global_value_from_config(config, 'journal_poll_interval', float) _global_value_from_config(config, 'max_delay_before_commit', 'seconds') _global_value_from_config(config, 'max_messages_per_commit', int) _global_value_from_config(config, 'delete_deliveries_after_days', int) _global_value_from_config(config, 'delete_interval', 'seconds') return config def _global_value_from_config( config, name: str, type_: Union[type, str] ) -> None: """ Set a global variable to the value obtained from *config*. Also cast to *type_*. """ try: value = config.get(name) if type_ == 'seconds': value = datetime.timedelta(seconds=float(value)) else: value = type_(value) # type: ignore globals()[name] = value except Exception: if value is not None: msg = f'ERROR: configured value of {name} is invalid.' journal.send(msg, PRIORITY=journal.LOG_ERR) if __name__ == '__main__': print(get_config())