radicale.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # This file is part of Radicale Server - Calendar Server
  5. # Copyright © 2008-2011 Guillaume Ayoub
  6. # Copyright © 2008 Nicolas Kandel
  7. # Copyright © 2008 Pascal Halter
  8. #
  9. # This library is free software: you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation, either version 3 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This library is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  21. # This file is just a script, allow [a-z0-9]* variable names
  22. # pylint: disable-msg=C0103
  23. # ``import radicale`` refers to the ``radicale`` module, not ``radicale.py``
  24. # pylint: disable-msg=W0406
  25. """
  26. Radicale Server entry point.
  27. Launch the Radicale Server according to configuration and command-line
  28. arguments.
  29. """
  30. # TODO: Manage smart and configurable logs
  31. import os
  32. import sys
  33. import optparse
  34. import signal
  35. import threading
  36. import radicale
  37. # Get command-line options
  38. parser = optparse.OptionParser()
  39. parser.add_option(
  40. "-v", "--version", action="store_true",
  41. default=False,
  42. help="show version and exit")
  43. parser.add_option(
  44. "-d", "--daemon", action="store_true",
  45. default=radicale.config.getboolean("server", "daemon"),
  46. help="launch as daemon")
  47. parser.add_option(
  48. "-f", "--foreground", action="store_false", dest="daemon",
  49. help="launch in foreground (opposite of --daemon)")
  50. parser.add_option(
  51. "-H", "--hosts",
  52. default=radicale.config.get("server", "hosts"),
  53. help="set server hostnames")
  54. parser.add_option(
  55. "-s", "--ssl", action="store_true",
  56. default=radicale.config.getboolean("server", "ssl"),
  57. help="use SSL connection")
  58. parser.add_option(
  59. "-S", "--no-ssl", action="store_false", dest="ssl",
  60. help="do not use SSL connection (opposite of --ssl)")
  61. parser.add_option(
  62. "-k", "--key",
  63. default=radicale.config.get("server", "key"),
  64. help="private key file ")
  65. parser.add_option(
  66. "-c", "--certificate",
  67. default=radicale.config.get("server", "certificate"),
  68. help="certificate file ")
  69. options = parser.parse_args()[0]
  70. # Update Radicale configuration according to options
  71. for option in parser.option_list:
  72. key = option.dest
  73. if key:
  74. value = getattr(options, key)
  75. radicale.config.set("server", key, value)
  76. # Print version and exit if the option is given
  77. if options.version:
  78. print(radicale.VERSION)
  79. sys.exit()
  80. # Fork if Radicale is launched as daemon
  81. if options.daemon:
  82. if os.fork():
  83. sys.exit()
  84. sys.stdout = sys.stderr = open(os.devnull, "w")
  85. # Launch calendar servers
  86. servers = []
  87. server_class = radicale.HTTPSServer if options.ssl else radicale.HTTPServer
  88. def exit():
  89. """Cleanly shutdown servers."""
  90. while servers:
  91. servers.pop().shutdown()
  92. def serve_forever(server):
  93. """Serve a server forever with no traceback on keyboard interrupts."""
  94. try:
  95. server.serve_forever()
  96. except KeyboardInterrupt:
  97. # No unwanted traceback
  98. pass
  99. finally:
  100. exit()
  101. # Clean exit on SIGTERM
  102. signal.signal(signal.SIGTERM, lambda *_: exit())
  103. for host in options.hosts.split(','):
  104. address, port = host.strip().rsplit(':', 1)
  105. address, port = address.strip('[] '), int(port)
  106. servers.append(server_class((address, port), radicale.CalendarHTTPHandler))
  107. for server in servers[:-1]:
  108. # More servers to come, launch a new thread
  109. threading.Thread(target=serve_forever, args=(server,)).start()
  110. # Last server, no more thread
  111. serve_forever(servers[-1])