radicale.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #!/usr/bin/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 threading, signal
  35. import radicale
  36. # Get command-line options
  37. parser = optparse.OptionParser()
  38. parser.add_option(
  39. "-v", "--version", action="store_true",
  40. default=False,
  41. help="show version and exit")
  42. parser.add_option(
  43. "-d", "--daemon", action="store_true",
  44. default=radicale.config.getboolean("server", "daemon"),
  45. help="launch as daemon")
  46. parser.add_option(
  47. "-f", "--foreground", action="store_false", dest="daemon",
  48. help="launch in foreground (opposite of --daemon)")
  49. parser.add_option(
  50. "-H", "--host",
  51. default=radicale.config.get("server", "host"),
  52. help="set server hostname")
  53. parser.add_option(
  54. "-p", "--port", type="int",
  55. default=radicale.config.getint("server", "port"),
  56. help="set server port")
  57. parser.add_option(
  58. "-s", "--ssl", action="store_true",
  59. default=radicale.config.getboolean("server", "ssl"),
  60. help="use SSL connection")
  61. parser.add_option(
  62. "-S", "--no-ssl", action="store_false", dest="ssl",
  63. help="do not use SSL connection (opposite of --ssl)")
  64. parser.add_option(
  65. "-k", "--key",
  66. default=radicale.config.get("server", "key"),
  67. help="private key file ")
  68. parser.add_option(
  69. "-c", "--certificate",
  70. default=radicale.config.get("server", "certificate"),
  71. help="certificate file ")
  72. options = parser.parse_args()[0]
  73. # Update Radicale configuration according to options
  74. for option in parser.option_list:
  75. key = option.dest
  76. if key:
  77. value = getattr(options, key)
  78. radicale.config.set("server", key, value)
  79. # Print version and exit if the option is given
  80. if options.version:
  81. print(radicale.VERSION)
  82. sys.exit()
  83. # Fork if Radicale is launched as daemon
  84. if options.daemon:
  85. if os.fork():
  86. sys.exit()
  87. sys.stdout = sys.stderr = open(os.devnull, "w")
  88. def exit (servers):
  89. """Cleanly shutdown all servers.
  90. Might be called multiple times."""
  91. for s in servers:
  92. s.shutdown()
  93. # Launch calendar server
  94. server_class = radicale.HTTPSServer if options.ssl else radicale.HTTPServer
  95. servers = []
  96. threads = []
  97. for host in (x.strip() for x in options.host.split(',')):
  98. try:
  99. server = server_class(
  100. (host, options.port), radicale.CalendarHTTPHandler)
  101. servers.append(server)
  102. t = threading.Thread(target = server.serve_forever)
  103. threads.append(t)
  104. t.start()
  105. except:
  106. exit(servers)
  107. raise
  108. # clean exit on SIGTERM
  109. signal.signal(signal.SIGTERM, lambda *a: exit(servers))
  110. try:
  111. while threads:
  112. threads[0].join(1) # try one second
  113. if threading.active_count() <= len(threads): # one thread died
  114. break
  115. except KeyboardInterrupt:
  116. pass
  117. finally:
  118. exit(servers)