main.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # SPDX-FileCopyrightText: 2016-2023 Helmut Pozimski <helmut@pozimski.eu>
  2. #
  3. # SPDX-License-Identifier: GPL-2.0-only
  4. # -*- coding: utf8 -*-
  5. """ Main function of acme-updater, parses command line arguments, sets up
  6. logging and executes the modules.
  7. """
  8. import argparse
  9. import logging
  10. import json
  11. import sys
  12. from amulib.cert_path_provider import AcmeToolCertPathProvider, GetSslCertPathProvider
  13. from amulib.helpers import get_log_level
  14. from amulib import apache
  15. from amulib import postfix
  16. from amulib import dovecot
  17. from amulib import ejabberd
  18. def main():
  19. """
  20. Main function of acme-updater.
  21. """
  22. parser = argparse.ArgumentParser()
  23. parser.add_argument("--apache", help="use the apache module",
  24. action="store_true")
  25. parser.add_argument("--postfix", help="use the postfix module",
  26. action="store_true")
  27. parser.add_argument("--dovecot", help="use the dovecot module",
  28. action="store_true")
  29. parser.add_argument("--ejabberd", help="use the ejabberd module",
  30. action="store_true")
  31. parser.add_argument("--verbose", "-V", help="be verbose, enables debug "
  32. "output",
  33. action="store_true")
  34. parser.add_argument("--config", "-C", help="path to the configuration "
  35. "file", type=str)
  36. args = parser.parse_args()
  37. logger = logging.getLogger("acme-updater")
  38. logger.addHandler(logging.StreamHandler())
  39. config = read_config(args, logger)
  40. set_log_level(args, config, logger)
  41. cert_path_provider = create_cert_path_provider(config, logger)
  42. execute_services(args, cert_path_provider, config)
  43. def read_config(args, logger):
  44. if args.config:
  45. try:
  46. with open(args.config, "r") as fobj:
  47. config = json.load(fobj)
  48. except IOError:
  49. logger.error("Error: Could not open configuration file")
  50. sys.exit(1)
  51. except json.JSONDecodeError:
  52. logger.error("Error: Could not parse configuration file")
  53. sys.exit(1)
  54. else:
  55. logger.error("A configuration file has to be provided with --config")
  56. sys.exit(1)
  57. return config
  58. def set_log_level(args, config, logger):
  59. if args.verbose:
  60. logger.setLevel(logging.DEBUG)
  61. elif config:
  62. logger.setLevel(get_log_level(config["loglevel"]))
  63. else:
  64. logger.setLevel(logging.INFO)
  65. def create_cert_path_provider(config, logger):
  66. if "acme_tool" in config:
  67. if config["acme_tool"] == "acmetool":
  68. return AcmeToolCertPathProvider(config["acme_dir"])
  69. elif config["acme_tool"] == "getssl":
  70. return GetSslCertPathProvider(config["acme_dir"])
  71. else:
  72. logger.error("Invalid acme tooling specified")
  73. sys.exit(1)
  74. else:
  75. logger.error("No acme tooling specified in configuration")
  76. sys.exit(1)
  77. def execute_services(args, cert_path_provider, config):
  78. if args.apache:
  79. if config:
  80. apache.run(cert_path_provider, config["apache"],
  81. config["named_key_path"], config["dns_server"])
  82. else:
  83. apache.run(cert_path_provider)
  84. if args.postfix:
  85. if config:
  86. postfix.run(cert_path_provider, config["postfix"],
  87. config["named_key_path"], config["dns_server"])
  88. else:
  89. postfix.run(cert_path_provider)
  90. if args.dovecot:
  91. if config:
  92. dovecot.run(cert_path_provider, config["dovecot"],
  93. config["named_key_path"], config["dns_server"])
  94. else:
  95. dovecot.run(cert_path_provider)
  96. if args.ejabberd:
  97. if config:
  98. ejabberd.run(cert_path_provider, config["ejabberd"],
  99. config["dns_server"])
  100. else:
  101. ejabberd.run(cert_path_provider)