__main__.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. Uses the built-in WSGI server.
  21. """
  22. import argparse
  23. import contextlib
  24. import os
  25. import signal
  26. import socket
  27. from radicale import VERSION, config, log, server, storage
  28. from radicale.log import logger
  29. def run():
  30. """Run Radicale as a standalone server."""
  31. log.setup()
  32. # Get command-line arguments
  33. parser = argparse.ArgumentParser(usage="radicale [OPTIONS]")
  34. parser.add_argument("--version", action="version", version=VERSION)
  35. parser.add_argument("--verify-storage", action="store_true",
  36. help="check the storage for errors and exit")
  37. parser.add_argument(
  38. "-C", "--config", help="use specific configuration files", nargs="*")
  39. parser.add_argument("-D", "--debug", action="store_true",
  40. help="print debug information")
  41. groups = {}
  42. for section, values in config.DEFAULT_CONFIG_SCHEMA.items():
  43. if values.get("_internal", False):
  44. continue
  45. group = parser.add_argument_group(section)
  46. groups[group] = []
  47. for option, data in values.items():
  48. if option.startswith("_"):
  49. continue
  50. kwargs = data.copy()
  51. long_name = "--%s-%s" % (section, option.replace("_", "-"))
  52. args = kwargs.pop("aliases", [])
  53. args.append(long_name)
  54. kwargs["dest"] = "%s_%s" % (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%s" % long_name[1:])
  65. group.add_argument(*args, **kwargs)
  66. kwargs["const"] = "False"
  67. kwargs["help"] = "do not %s (opposite of %s)" % (
  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. os.pathsep.join(args.config) if args.config else None))
  96. if arguments_config:
  97. configuration.update(arguments_config, "arguments")
  98. except Exception as e:
  99. logger.fatal("Invalid configuration: %s", e, exc_info=True)
  100. exit(1)
  101. # Configure logging
  102. log.set_level(configuration.get("logging", "level"))
  103. # Log configuration after logger is configured
  104. configuration.log_config_sources()
  105. if args.verify_storage:
  106. logger.info("Verifying storage")
  107. try:
  108. storage_ = storage.load(configuration)
  109. with storage_.acquire_lock("r"):
  110. if not storage_.verify():
  111. logger.fatal("Storage verifcation failed")
  112. exit(1)
  113. except Exception as e:
  114. logger.fatal("An exception occurred during storage verification: "
  115. "%s", e, exc_info=True)
  116. exit(1)
  117. return
  118. # Create a socket pair to notify the server of program shutdown
  119. shutdown_socket, shutdown_socket_out = socket.socketpair()
  120. # SIGTERM and SIGINT (aka KeyboardInterrupt) shutdown the server
  121. def shutdown(signal_number, stack_frame):
  122. shutdown_socket.sendall(b" ")
  123. signal.signal(signal.SIGTERM, shutdown)
  124. signal.signal(signal.SIGINT, shutdown)
  125. try:
  126. server.serve(configuration, shutdown_socket_out)
  127. except Exception as e:
  128. logger.fatal("An exception occurred during server startup: %s", e,
  129. exc_info=True)
  130. exit(1)
  131. if __name__ == "__main__":
  132. run()