__main__.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. # This file is part of Radicale Server - Calendar Server
  2. # Copyright © 2011-2017 Guillaume Ayoub
  3. # Copyright © 2017-2019 Unrud <unrud@outlook.com>
  4. #
  5. # This library is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This library is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  17. """
  18. Radicale executable module.
  19. This module can be executed from a command line with ``$python -m radicale``.
  20. """
  21. import argparse
  22. import contextlib
  23. import os
  24. import signal
  25. import socket
  26. from radicale import VERSION, config, log, server, storage
  27. from radicale.log import logger
  28. def run():
  29. """Run Radicale as a standalone server."""
  30. log.setup()
  31. # Get command-line arguments
  32. parser = argparse.ArgumentParser(usage="radicale [OPTIONS]")
  33. parser.add_argument("--version", action="version", version=VERSION)
  34. parser.add_argument("--verify-storage", action="store_true",
  35. help="check the storage for errors and exit")
  36. parser.add_argument(
  37. "-C", "--config", help="use a specific configuration file")
  38. parser.add_argument("-D", "--debug", action="store_true",
  39. help="print debug information")
  40. groups = {}
  41. for section, values in config.DEFAULT_CONFIG_SCHEMA.items():
  42. if values.get("_internal", False):
  43. continue
  44. group = parser.add_argument_group(section)
  45. groups[group] = []
  46. for option, data in values.items():
  47. if option.startswith("_"):
  48. continue
  49. kwargs = data.copy()
  50. long_name = "--{0}-{1}".format(
  51. section, option.replace("_", "-"))
  52. args = kwargs.pop("aliases", [])
  53. args.append(long_name)
  54. kwargs["dest"] = "{0}_{1}".format(section, option)
  55. groups[group].append(kwargs["dest"])
  56. del kwargs["value"]
  57. if "internal" in kwargs:
  58. del kwargs["internal"]
  59. if kwargs["type"] == bool:
  60. del kwargs["type"]
  61. kwargs["action"] = "store_const"
  62. kwargs["const"] = "True"
  63. opposite_args = kwargs.pop("opposite", [])
  64. opposite_args.append("--no{0}".format(long_name[1:]))
  65. group.add_argument(*args, **kwargs)
  66. kwargs["const"] = "False"
  67. kwargs["help"] = "do not {0} (opposite of {1})".format(
  68. kwargs["help"], long_name)
  69. group.add_argument(*opposite_args, **kwargs)
  70. else:
  71. del kwargs["type"]
  72. group.add_argument(*args, **kwargs)
  73. args = parser.parse_args()
  74. # Preliminary configure logging
  75. if args.debug:
  76. args.logging_level = "debug"
  77. with contextlib.suppress(ValueError):
  78. log.set_level(config.DEFAULT_CONFIG_SCHEMA["logging"]["level"]["type"](
  79. args.logging_level))
  80. # Update Radicale configuration according to arguments
  81. arguments_config = {}
  82. for group, actions in groups.items():
  83. section = group.title
  84. section_config = {}
  85. for action in actions:
  86. value = getattr(args, action)
  87. if value is not None:
  88. section_config[action.split('_', 1)[1]] = value
  89. if section_config:
  90. arguments_config[section] = section_config
  91. try:
  92. configuration = config.load(config.parse_compound_paths(
  93. config.DEFAULT_CONFIG_PATH,
  94. os.environ.get("RADICALE_CONFIG"),
  95. args.config))
  96. if arguments_config:
  97. configuration.update(
  98. arguments_config, "arguments", internal=False)
  99. except Exception as e:
  100. logger.fatal("Invalid configuration: %s", e, exc_info=True)
  101. exit(1)
  102. # Configure logging
  103. log.set_level(configuration.get("logging", "level"))
  104. # Log configuration after logger is configured
  105. configuration.log_config_sources()
  106. if args.verify_storage:
  107. logger.info("Verifying storage")
  108. try:
  109. Collection = storage.load(configuration)
  110. with Collection.acquire_lock("r"):
  111. if not Collection.verify():
  112. logger.fatal("Storage verifcation failed")
  113. exit(1)
  114. except Exception as e:
  115. logger.fatal("An exception occurred during storage verification: "
  116. "%s", e, exc_info=True)
  117. exit(1)
  118. return
  119. # Create a socket pair to notify the server of program shutdown
  120. shutdown_socket, shutdown_socket_out = socket.socketpair()
  121. # SIGTERM and SIGINT (aka KeyboardInterrupt) shutdown the server
  122. def shutdown(*args):
  123. shutdown_socket.sendall(b" ")
  124. signal.signal(signal.SIGTERM, shutdown)
  125. signal.signal(signal.SIGINT, shutdown)
  126. try:
  127. server.serve(configuration, shutdown_socket_out)
  128. except Exception as e:
  129. logger.fatal("An exception occurred during server startup: %s", e,
  130. exc_info=True)
  131. exit(1)
  132. if __name__ == "__main__":
  133. run()