main.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # SPDX-FileCopyrightText: 2016-2017 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.helpers import get_log_level
  13. from amulib import apache
  14. from amulib import postfix
  15. from amulib import dovecot
  16. from amulib import ejabberd
  17. def main():
  18. """
  19. Main function of acme-updater.
  20. """
  21. config = None
  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. if args.config:
  40. try:
  41. with open(args.config, "r") as fobj:
  42. config = json.load(fobj)
  43. except IOError:
  44. logger.error("Error: Could not open configuration file")
  45. sys.exit(1)
  46. except json.JSONDecodeError:
  47. logger.error("Error: Could not parse configuration file")
  48. sys.exit(1)
  49. if args.verbose:
  50. logger.setLevel(logging.DEBUG)
  51. elif config:
  52. logger.setLevel(get_log_level(config["loglevel"]))
  53. else:
  54. logger.setLevel(logging.INFO)
  55. if args.apache:
  56. if config:
  57. apache.run(config["apache"], config["acme_dir"],
  58. config["named_key_path"], config["dns_server"])
  59. else:
  60. apache.run()
  61. if args.postfix:
  62. if config:
  63. postfix.run(config["postfix"], config["acme_dir"],
  64. config["named_key_path"], config["dns_server"])
  65. else:
  66. postfix.run()
  67. if args.dovecot:
  68. if config:
  69. dovecot.run(config["dovecot"], config["acme_dir"],
  70. config["named_key_path"], config["dns_server"])
  71. else:
  72. dovecot.run()
  73. if args.ejabberd:
  74. if config:
  75. ejabberd.run(config["ejabberd"], config["acme_dir"],
  76. config["named_key_path"], config["dns_server"])
  77. else:
  78. ejabberd.run()