123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- # SPDX-FileCopyrightText: 2016-2023 Helmut Pozimski <helmut@pozimski.eu>
- #
- # SPDX-License-Identifier: GPL-2.0-only
- # -*- coding: utf8 -*-
- """ Main function of acme-updater, parses command line arguments, sets up
- logging and executes the modules.
- """
- import argparse
- import logging
- import json
- import sys
- from amulib.cert_path_provider import AcmeToolCertPathProvider, GetSslCertPathProvider
- from amulib.helpers import get_log_level
- from amulib import apache
- from amulib import postfix
- from amulib import dovecot
- from amulib import ejabberd
- def main():
- """
- Main function of acme-updater.
- """
- parser = argparse.ArgumentParser()
- parser.add_argument("--apache", help="use the apache module",
- action="store_true")
- parser.add_argument("--postfix", help="use the postfix module",
- action="store_true")
- parser.add_argument("--dovecot", help="use the dovecot module",
- action="store_true")
- parser.add_argument("--ejabberd", help="use the ejabberd module",
- action="store_true")
- parser.add_argument("--verbose", "-V", help="be verbose, enables debug "
- "output",
- action="store_true")
- parser.add_argument("--config", "-C", help="path to the configuration "
- "file", type=str)
- args = parser.parse_args()
- logger = logging.getLogger("acme-updater")
- logger.addHandler(logging.StreamHandler())
- config = read_config(args, logger)
- set_log_level(args, config, logger)
- cert_path_provider = create_cert_path_provider(config, logger)
- execute_services(args, cert_path_provider, config)
- def read_config(args, logger):
- if args.config:
- try:
- with open(args.config, "r") as fobj:
- config = json.load(fobj)
- except IOError:
- logger.error("Error: Could not open configuration file")
- sys.exit(1)
- except json.JSONDecodeError:
- logger.error("Error: Could not parse configuration file")
- sys.exit(1)
- else:
- logger.error("A configuration file has to be provided with --config")
- sys.exit(1)
- return config
- def set_log_level(args, config, logger):
- if args.verbose:
- logger.setLevel(logging.DEBUG)
- elif config:
- logger.setLevel(get_log_level(config["loglevel"]))
- else:
- logger.setLevel(logging.INFO)
- def create_cert_path_provider(config, logger):
- if "acme_tool" in config:
- if config["acme_tool"] == "acmetool":
- return AcmeToolCertPathProvider(config["acme_dir"])
- elif config["acme_tool"] == "getssl":
- return GetSslCertPathProvider(config["acme_dir"])
- else:
- logger.error("Invalid acme tooling specified")
- sys.exit(1)
- else:
- logger.error("No acme tooling specified in configuration")
- sys.exit(1)
- def execute_services(args, cert_path_provider, config):
- if args.apache:
- if config:
- apache.run(cert_path_provider, config["apache"],
- config["named_key_path"], config["dns_server"])
- else:
- apache.run(cert_path_provider)
- if args.postfix:
- if config:
- postfix.run(cert_path_provider, config["postfix"],
- config["named_key_path"], config["dns_server"])
- else:
- postfix.run(cert_path_provider)
- if args.dovecot:
- if config:
- dovecot.run(cert_path_provider, config["dovecot"],
- config["named_key_path"], config["dns_server"])
- else:
- dovecot.run(cert_path_provider)
- if args.ejabberd:
- if config:
- ejabberd.run(cert_path_provider, config["ejabberd"],
- config["dns_server"])
- else:
- ejabberd.run(cert_path_provider)
|