main.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # This file is part of acme-updater, written by Helmut Pozimski 2016-2017.
  2. #
  3. # stov is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, version 2 of the License.
  6. #
  7. # stov is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License
  13. # along with stov. If not, see <http://www.gnu.org/licenses/>.
  14. # -*- coding: utf8 -*-
  15. """ Main function of acme-updater, parses command line arguments, sets up
  16. logging and executes the modules.
  17. """
  18. import argparse
  19. import logging
  20. import json
  21. import sys
  22. from amulib.helpers import get_log_level
  23. from amulib import apache
  24. from amulib import postfix
  25. from amulib import dovecot
  26. from amulib import ejabberd
  27. def main():
  28. """
  29. Main function of acme-updater.
  30. """
  31. config = None
  32. parser = argparse.ArgumentParser()
  33. parser.add_argument("--apache", help="use the apache module",
  34. action="store_true")
  35. parser.add_argument("--postfix", help="use the postfix module",
  36. action="store_true")
  37. parser.add_argument("--dovecot", help="use the dovecot module",
  38. action="store_true")
  39. parser.add_argument("--ejabberd", help="use the ejabberd module",
  40. action="store_true")
  41. parser.add_argument("--verbose", "-V", help="be verbose, enables debug "
  42. "output",
  43. action="store_true")
  44. parser.add_argument("--config", "-C", help="path to the configuration "
  45. "file", type=str)
  46. args = parser.parse_args()
  47. logger = logging.getLogger("acme-updater")
  48. logger.addHandler(logging.StreamHandler())
  49. if args.config:
  50. try:
  51. with open(args.config, "r") as fobj:
  52. config = json.load(fobj)
  53. except IOError:
  54. logger.error("Error: Could not open configuration file")
  55. sys.exit(1)
  56. except json.JSONDecodeError:
  57. logger.error("Error: Could not parse configuration file")
  58. sys.exit(1)
  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. if args.apache:
  66. if config:
  67. apache.run(config["apache"], config["acme_dir"],
  68. config["named_key_path"])
  69. else:
  70. apache.run()
  71. if args.postfix:
  72. if config:
  73. postfix.run(config["postfix"], config["acme_dir"],
  74. config["named_key_path"])
  75. else:
  76. postfix.run()
  77. if args.dovecot:
  78. if config:
  79. dovecot.run(config["dovecot"], config["acme_dir"],
  80. config["named_key_path"])
  81. else:
  82. dovecot.run()
  83. if args.ejabberd:
  84. if config:
  85. ejabberd.run(config["ejabberd"], config["acme_dir"],
  86. config["named_key_path"])
  87. else:
  88. ejabberd.run()