Merge pull request #527 from Kozea/command_config

Command config
This commit is contained in:
Guillaume Ayoub 2016-12-08 11:43:35 +01:00 committed by GitHub
commit 42e7347d5e
6 changed files with 199 additions and 115 deletions

13
config
View File

@ -45,7 +45,7 @@
# SSL Protocol used. See python's ssl module for available values # SSL Protocol used. See python's ssl module for available values
#protocol = PROTOCOL_SSLv23 #protocol = PROTOCOL_SSLv23
# Ciphers available. See python's ssl module for available ciphers # Available ciphers. See python's ssl module for available ciphers
#ciphers = #ciphers =
# Reverse DNS to resolve client address in logs # Reverse DNS to resolve client address in logs
@ -106,11 +106,16 @@
# Sync all changes to disk during requests. (This can impair performance.) # Sync all changes to disk during requests. (This can impair performance.)
# Disabling it increases the risk of data loss, when the system crashes or # Disabling it increases the risk of data loss, when the system crashes or
# power fails! # power fails!
#fsync = True #filesystem_fsync = True
# Close the lock file when no more clients are waiting.
# This option is not very useful in general, but on Windows files that are
# opened cannot be deleted.
#filesystem_close_lock_file = False
# Command that is run after changes to storage # Command that is run after changes to storage
#hook =
# Example: git add -A && (git diff --cached --quiet || git commit -m "Changes by "%(user)s) # Example: git add -A && (git diff --cached --quiet || git commit -m "Changes by "%(user)s)
#hook =
[logging] [logging]
@ -120,8 +125,10 @@
# For more information about the syntax of the configuration file, see: # For more information about the syntax of the configuration file, see:
# http://docs.python.org/library/logging.config.html # http://docs.python.org/library/logging.config.html
#config = /etc/radicale/logging #config = /etc/radicale/logging
# Set the default logging level to debug # Set the default logging level to debug
#debug = False #debug = False
# Store all environment variables (including those set in the shell) # Store all environment variables (including those set in the shell)
#full_environment = False #full_environment = False

View File

@ -51,28 +51,28 @@ from . import auth, rights, storage, xmlutils
VERSION = "2.0.0rc0" VERSION = "2.0.0rc0"
NOT_ALLOWED = ( NOT_ALLOWED = (
client.FORBIDDEN, (("Content-type", "text/plain"),), client.FORBIDDEN, (("Content-Type", "text/plain"),),
"Access to the requested resource forbidden.") "Access to the requested resource forbidden.")
NOT_FOUND = ( NOT_FOUND = (
client.NOT_FOUND, (("Content-type", "text/plain"),), client.NOT_FOUND, (("Content-Type", "text/plain"),),
"The requested resource could not be found.") "The requested resource could not be found.")
WEBDAV_PRECONDITION_FAILED = ( WEBDAV_PRECONDITION_FAILED = (
client.CONFLICT, (("Content-type", "text/plain"),), client.CONFLICT, (("Content-Type", "text/plain"),),
"WebDAV precondition failed.") "WebDAV precondition failed.")
PRECONDITION_FAILED = ( PRECONDITION_FAILED = (
client.PRECONDITION_FAILED, client.PRECONDITION_FAILED,
(("Content-type", "text/plain"),), "Precondition failed.") (("Content-Type", "text/plain"),), "Precondition failed.")
REQUEST_TIMEOUT = ( REQUEST_TIMEOUT = (
client.REQUEST_TIMEOUT, (("Content-type", "text/plain"),), client.REQUEST_TIMEOUT, (("Content-Type", "text/plain"),),
"Connection timed out.") "Connection timed out.")
REQUEST_ENTITY_TOO_LARGE = ( REQUEST_ENTITY_TOO_LARGE = (
client.REQUEST_ENTITY_TOO_LARGE, (("Content-type", "text/plain"),), client.REQUEST_ENTITY_TOO_LARGE, (("Content-Type", "text/plain"),),
"Request body too large.") "Request body too large.")
REMOTE_DESTINATION = ( REMOTE_DESTINATION = (
client.BAD_GATEWAY, (("Content-type", "text/plain"),), client.BAD_GATEWAY, (("Content-Type", "text/plain"),),
"Remote destination not supported.") "Remote destination not supported.")
DIRECTORY_LISTING = ( DIRECTORY_LISTING = (
client.FORBIDDEN, (("Content-type", "text/plain"),), client.FORBIDDEN, (("Content-Type", "text/plain"),),
"Directory listings are not supported.") "Directory listings are not supported.")
DAV_HEADERS = "1, 2, 3, calendar-access, addressbook, extended-mkcol" DAV_HEADERS = "1, 2, 3, calendar-access, addressbook, extended-mkcol"
@ -288,6 +288,7 @@ class Application:
headers["Content-Encoding"] = "gzip" headers["Content-Encoding"] = "gzip"
headers["Content-Length"] = str(len(answer)) headers["Content-Length"] = str(len(answer))
headers["Content-Type"] += "; charset=%s" % self.encoding
# Add extra headers set in configuration # Add extra headers set in configuration
if self.configuration.has_section("headers"): if self.configuration.has_section("headers"):
@ -441,13 +442,13 @@ class Application:
answer = xmlutils.delete(path, item) answer = xmlutils.delete(path, item)
else: else:
answer = xmlutils.delete(path, item.collection, item.href) answer = xmlutils.delete(path, item.collection, item.href)
return client.OK, {}, answer return client.OK, {"Content-Type": "text/xml"}, answer
def do_GET(self, environ, path, user): def do_GET(self, environ, path, user):
"""Manage GET request.""" """Manage GET request."""
# Display a "Radicale works!" message if the root URL is requested # Display a "Radicale works!" message if the root URL is requested
if not path.strip("/"): if not path.strip("/"):
return client.OK, {"Content-type": "text/plain"}, "Radicale works!" return client.OK, {"Content-Type": "text/plain"}, "Radicale works!"
if not self._access(user, path, "r"): if not self._access(user, path, "r"):
return NOT_ALLOWED return NOT_ALLOWED
with self.Collection.acquire_lock("r", user): with self.Collection.acquire_lock("r", user):
@ -458,7 +459,8 @@ class Application:
return NOT_FOUND return NOT_FOUND
if isinstance(item, self.Collection): if isinstance(item, self.Collection):
collection = item collection = item
if collection.get_meta("tag") not in ("VADDRESSBOOK", "VCALENDAR"): if collection.get_meta("tag") not in (
"VADDRESSBOOK", "VCALENDAR"):
return DIRECTORY_LISTING return DIRECTORY_LISTING
else: else:
collection = item.collection collection = item.collection

View File

@ -22,8 +22,8 @@ from a python programme with ``radicale.__main__.run()``.
""" """
import argparse
import atexit import atexit
import optparse
import os import os
import select import select
import signal import signal
@ -39,44 +39,44 @@ from . import (
def run(): def run():
"""Run Radicale as a standalone server.""" """Run Radicale as a standalone server."""
# Get command-line options # Get command-line arguments
parser = optparse.OptionParser(version=VERSION) parser = argparse.ArgumentParser(usage="radicale [OPTIONS]")
parser.add_option(
"-d", "--daemon", action="store_true",
help="launch as daemon")
parser.add_option(
"-p", "--pid",
help="set PID filename for daemon mode")
parser.add_option(
"-f", "--foreground", action="store_false", dest="daemon",
help="launch in foreground (opposite of --daemon)")
parser.add_option(
"-H", "--hosts",
help="set server hostnames and ports")
parser.add_option(
"-s", "--ssl", action="store_true",
help="use SSL connection")
parser.add_option(
"-S", "--no-ssl", action="store_false", dest="ssl",
help="do not use SSL connection (opposite of --ssl)")
parser.add_option(
"-k", "--key",
help="set private key file")
parser.add_option(
"-c", "--certificate",
help="set certificate file")
parser.add_option(
"-D", "--debug", action="store_true",
help="print debug information")
parser.add_option(
"-C", "--config",
help="use a specific configuration file")
options = parser.parse_args()[0] parser.add_argument("--version", action="version", version=VERSION)
parser.add_argument(
"-C", "--config", help="use a specific configuration file")
if options.config: groups = {}
for section, values in config.INITIAL_CONFIG.items():
group = parser.add_argument_group(section)
groups[group] = []
for option, data in values.items():
kwargs = data.copy()
long_name = "--{0}-{1}".format(
section, option.replace("_", "-"))
args = kwargs.pop("aliases", [])
args.append(long_name)
kwargs["dest"] = "{0}_{1}".format(section, option)
groups[group].append(kwargs["dest"])
if kwargs.pop("value") in ("True", "False"):
kwargs["action"] = "store_const"
kwargs["const"] = "True"
opposite_args = kwargs.pop("opposite", [])
opposite_args.append("--no{0}".format(long_name[1:]))
group.add_argument(*args, **kwargs)
kwargs["const"] = "False"
kwargs["help"] = "do not {0} (opposite of {1})".format(
kwargs["help"], long_name)
group.add_argument(*opposite_args, **kwargs)
else:
group.add_argument(*args, **kwargs)
args = parser.parse_args()
if args.config:
configuration = config.load() configuration = config.load()
configuration_found = configuration.read(options.config) configuration_found = configuration.read(args.config)
else: else:
configuration_paths = [ configuration_paths = [
"/etc/radicale/config", "/etc/radicale/config",
@ -86,14 +86,13 @@ def run():
configuration = config.load(configuration_paths) configuration = config.load(configuration_paths)
configuration_found = True configuration_found = True
# Update Radicale configuration according to options # Update Radicale configuration according to arguments
for option in parser.option_list: for group, actions in groups.items():
key = option.dest section = group.title
if key: for action in actions:
section = "logging" if key == "debug" else "server" value = getattr(args, action)
value = getattr(options, key)
if value is not None: if value is not None:
configuration.set(section, key, str(value)) configuration.set(section, action.split('_', 1)[1], value)
# Start logging # Start logging
filename = os.path.expanduser(configuration.get("logging", "config")) filename = os.path.expanduser(configuration.get("logging", "config"))
@ -102,7 +101,7 @@ def run():
# Log a warning if the configuration file of the command line is not found # Log a warning if the configuration file of the command line is not found
if not configuration_found: if not configuration_found:
logger.warning("Configuration file '%s' not found" % options.config) logger.warning("Configuration file '%s' not found" % args.config)
try: try:
serve(configuration, logger) serve(configuration, logger)

View File

@ -24,56 +24,128 @@ Give a configparser-like interface to read and write configuration.
""" """
import os import os
from collections import OrderedDict
from configparser import RawConfigParser as ConfigParser from configparser import RawConfigParser as ConfigParser
# Default configuration # Default configuration
INITIAL_CONFIG = { INITIAL_CONFIG = OrderedDict([
"server": { ("server", OrderedDict([
"hosts": "0.0.0.0:5232", ("hosts", {
"daemon": "False", "value": "0.0.0.0:5232",
"pid": "", "help": "set server hostnames including ports",
"max_connections": "20", "aliases": ["-H", "--hosts"]}),
"max_content_length": "10000000", ("daemon", {
"timeout": "10", "value": "False",
"ssl": "False", "help": "launch as daemon",
"certificate": "/etc/apache2/ssl/server.crt", "aliases": ["-d", "--daemon"],
"key": "/etc/apache2/ssl/server.key", "opposite": ["-f", "--foreground"]}),
"protocol": "PROTOCOL_SSLv23", ("pid", {
"ciphers": "", "value": "",
"dns_lookup": "True", "help": "set PID filename for daemon mode",
"base_prefix": "/", "aliases": ["-p", "--pid"]}),
"can_skip_base_prefix": "False", ("max_connections", {
"realm": "Radicale - Password Required"}, "value": "20",
"encoding": { "help": "maximum number of parallel connections"}),
"request": "utf-8", ("max_content_length", {
"stock": "utf-8"}, "value": "10000000",
"auth": { "help": "maximum size of request body in bytes"}),
"type": "None", ("timeout", {
"htpasswd_filename": "/etc/radicale/users", "value": "10",
"htpasswd_encryption": "crypt"}, "help": "socket timeout"}),
"rights": { ("ssl", {
"type": "None", "value": "False",
"file": "~/.config/radicale/rights"}, "help": "use SSL connection",
"storage": { "aliases": ["-s", "--ssl"],
"type": "multifilesystem", "opposite": ["-S", "--no-ssl"]}),
"filesystem_folder": os.path.expanduser( ("certificate", {
"value": "/etc/apache2/ssl/server.crt",
"help": "set certificate file",
"aliases": ["-c", "--certificate"]}),
("key", {
"value": "/etc/apache2/ssl/server.key",
"help": "set private key file",
"aliases": ["-k", "--key"]}),
("protocol", {
"value": "PROTOCOL_SSLv23",
"help": "SSL protocol used"}),
("ciphers", {
"value": "",
"help": "available ciphers"}),
("dns_lookup", {
"value": "True",
"help": "use reverse DNS to resolve client address in logs"}),
("base_prefix", {
"value": "/",
"help": "root URL of Radicale, starting and ending with a slash"}),
("can_skip_base_prefix", {
"value": "False",
"help": "allow URLs cleaned by a HTTP server"}),
("realm", {
"value": "Radicale - Password Required",
"help": "message displayed when a password is needed"})])),
("encoding", OrderedDict([
("request", {
"value": "utf-8",
"help": "encoding for responding requests"}),
("stock", {
"value": "utf-8",
"help": "encoding for storing local collections"})])),
("auth", OrderedDict([
("type", {
"value": "None",
"help": "authentication method"}),
("htpasswd_filename", {
"value": "/etc/radicale/users",
"help": "htpasswd filename"}),
("htpasswd_encryption", {
"value": "crypt",
"help": "htpasswd encryption method"})])),
("rights", OrderedDict([
("type", {
"value": "None",
"help": "rights backend"}),
("file", {
"value": "~/.config/radicale/rights",
"help": "file for rights management from_file"})])),
("storage", OrderedDict([
("type", {
"value": "multifilesystem",
"help": "storage backend"}),
("filesystem_folder", {
"value": os.path.expanduser(
"~/.config/radicale/collections"), "~/.config/radicale/collections"),
"fsync": "True", "help": "file for rights management from_file"}),
"hook": "", ("filesystem_fsync", {
"close_lock_file": "False"}, "value": "True",
"logging": { "help": "sync all changes to filesystem during requests"}),
"config": "/etc/radicale/logging", ("filesystem_close_lock_file", {
"debug": "False", "value": "False",
"full_environment": "False", "help": "close the lock file when no more clients are waiting"}),
"mask_passwords": "True"}} ("hook", {
"value": "",
"help": "command that is run after changes to storage"})])),
("logging", OrderedDict([
("config", {
"value": "/etc/radicale/logging",
"help": "logging configuration file"}),
("debug", {
"value": "False",
"help": "print debug information",
"aliases": ["-D", "--debug"]}),
("full_environment", {
"value": "False",
"help": "store all environment variables"}),
("mask_passwords", {
"value": "True",
"help": "mask passwords in logs"})]))])
def load(paths=(), extra_config=None): def load(paths=(), extra_config=None):
config = ConfigParser() config = ConfigParser()
for section, values in INITIAL_CONFIG.items(): for section, values in INITIAL_CONFIG.items():
config.add_section(section) config.add_section(section)
for key, value in values.items(): for key, data in values.items():
config.set(section, key, value) config.set(section, key, data["value"])
if extra_config: if extra_config:
for section, values in extra_config.items(): for section, values in extra_config.items():
for key, value in values.items(): for key, value in values.items():

View File

@ -389,7 +389,7 @@ class Collection(BaseCollection):
delete=False, prefix=".Radicale.tmp-", newline=newline) delete=False, prefix=".Radicale.tmp-", newline=newline)
try: try:
yield tmp yield tmp
if self.configuration.getboolean("storage", "fsync"): if self.configuration.getboolean("storage", "filesystem_fsync"):
if os.name == "posix" and hasattr(fcntl, "F_FULLFSYNC"): if os.name == "posix" and hasattr(fcntl, "F_FULLFSYNC"):
fcntl.fcntl(tmp.fileno(), fcntl.F_FULLFSYNC) fcntl.fcntl(tmp.fileno(), fcntl.F_FULLFSYNC)
else: else:
@ -418,7 +418,7 @@ class Collection(BaseCollection):
This only works on POSIX and does nothing on other systems. This only works on POSIX and does nothing on other systems.
""" """
if not cls.configuration.getboolean("storage", "fsync"): if not cls.configuration.getboolean("storage", "filesystem_fsync"):
return return
if os.name == "posix": if os.name == "posix":
fd = os.open(path, 0) fd = os.open(path, 0)
@ -550,13 +550,15 @@ class Collection(BaseCollection):
new_collection = vobject.iCalendar() new_collection = vobject.iCalendar()
for item in items: for item in items:
new_collection.add(item) new_collection.add(item)
href = self._find_available_file_name(vobject_items.get) href = self._find_available_file_name(
vobject_items.get)
vobject_items[href] = new_collection vobject_items[href] = new_collection
self.upload_all_nonatomic(vobject_items) self.upload_all_nonatomic(vobject_items)
elif props.get("tag") == "VCARD": elif props.get("tag") == "VCARD":
vobject_items = {} vobject_items = {}
for card in collection: for card in collection:
href = self._find_available_file_name(vobject_items.get) href = self._find_available_file_name(
vobject_items.get)
vobject_items[href] = card vobject_items[href] = card
self.upload_all_nonatomic(vobject_items) self.upload_all_nonatomic(vobject_items)
@ -583,7 +585,7 @@ class Collection(BaseCollection):
fs.append(open(path, "w", encoding=self.encoding, newline="")) fs.append(open(path, "w", encoding=self.encoding, newline=""))
fs[-1].write(item.serialize()) fs[-1].write(item.serialize())
fsync_fn = lambda fd: None fsync_fn = lambda fd: None
if self.configuration.getboolean("storage", "fsync"): if self.configuration.getboolean("storage", "filesystem_fsync"):
if os.name == "posix" and hasattr(fcntl, "F_FULLFSYNC"): if os.name == "posix" and hasattr(fcntl, "F_FULLFSYNC"):
fsync_fn = lambda fd: fcntl.fcntl(fd, fcntl.F_FULLFSYNC) fsync_fn = lambda fd: fcntl.fcntl(fd, fcntl.F_FULLFSYNC)
else: else:
@ -811,7 +813,8 @@ class Collection(BaseCollection):
cls._lock_file_locked = False cls._lock_file_locked = False
if cls._waiters: if cls._waiters:
cls._waiters[0].notify() cls._waiters[0].notify()
if (cls.configuration.getboolean("storage", "close_lock_file") if (cls.configuration.getboolean(
"storage", "filesystem_close_lock_file")
and cls._readers == 0 and not cls._waiters): and cls._readers == 0 and not cls._waiters):
cls._lock_file.close() cls._lock_file.close()
cls._lock_file = None cls._lock_file = None

View File

@ -792,15 +792,15 @@ class BaseRequestsMixIn:
def test_fsync(self): def test_fsync(self):
"""Create a directory and file with syncing enabled.""" """Create a directory and file with syncing enabled."""
self.configuration.set("storage", "fsync", "True") self.configuration.set("storage", "filesystem_fsync", "True")
status, headers, answer = self.request("MKCALENDAR", "/calendar.ics/") status, headers, answer = self.request("MKCALENDAR", "/calendar.ics/")
assert status == 201 assert status == 201
def test_hook(self): def test_hook(self):
"""Run hook.""" """Run hook."""
self.configuration.set( self.configuration.set(
"storage", "hook", "mkdir %s" % os.path.join("collection-root", "storage", "hook", "mkdir %s" % os.path.join(
"created_by_hook")) "collection-root", "created_by_hook"))
status, headers, answer = self.request("MKCOL", "/calendar.ics/") status, headers, answer = self.request("MKCOL", "/calendar.ics/")
assert status == 201 assert status == 201
status, headers, answer = self.request("PROPFIND", "/created_by_hook/") status, headers, answer = self.request("PROPFIND", "/created_by_hook/")
@ -809,8 +809,8 @@ class BaseRequestsMixIn:
def test_hook_read_access(self): def test_hook_read_access(self):
"""Verify that hook is not run for read accesses.""" """Verify that hook is not run for read accesses."""
self.configuration.set( self.configuration.set(
"storage", "hook", "mkdir %s" % os.path.join("collection-root", "storage", "hook", "mkdir %s" % os.path.join(
"created_by_hook")) "collection-root", "created_by_hook"))
status, headers, answer = self.request("GET", "/") status, headers, answer = self.request("GET", "/")
assert status == 200 assert status == 200
status, headers, answer = self.request("GET", "/created_by_hook/") status, headers, answer = self.request("GET", "/created_by_hook/")
@ -828,8 +828,8 @@ class BaseRequestsMixIn:
def test_hook_principal_collection_creation(self): def test_hook_principal_collection_creation(self):
"""Verify that the hooks runs when a new user is created.""" """Verify that the hooks runs when a new user is created."""
self.configuration.set( self.configuration.set(
"storage", "hook", "mkdir %s" % os.path.join("collection-root", "storage", "hook", "mkdir %s" % os.path.join(
"created_by_hook")) "collection-root", "created_by_hook"))
status, headers, answer = self.request("GET", "/", REMOTE_USER="user") status, headers, answer = self.request("GET", "/", REMOTE_USER="user")
assert status == 200 assert status == 200
status, headers, answer = self.request("PROPFIND", "/created_by_hook/") status, headers, answer = self.request("PROPFIND", "/created_by_hook/")
@ -852,7 +852,8 @@ class BaseRequestsMixIn:
status, headers, answer = self.request("GET", "/") status, headers, answer = self.request("GET", "/")
assert headers.get("test") == "123" assert headers.get("test") == "123"
# Test if header is set on failure # Test if header is set on failure
status, headers, answer = self.request("GET", "/.well-known/does not exist") status, headers, answer = self.request(
"GET", "/.well-known/does not exist")
assert headers.get("test") == "123" assert headers.get("test") == "123"
@ -867,7 +868,7 @@ class BaseFileSystemTest(BaseTest):
self.colpath = tempfile.mkdtemp() self.colpath = tempfile.mkdtemp()
self.configuration.set("storage", "filesystem_folder", self.colpath) self.configuration.set("storage", "filesystem_folder", self.colpath)
# Disable syncing to disk for better performance # Disable syncing to disk for better performance
self.configuration.set("storage", "fsync", "False") self.configuration.set("storage", "filesystem_fsync", "False")
# Required on Windows, doesn't matter on Unix # Required on Windows, doesn't matter on Unix
self.configuration.set("storage", "close_lock_file", "True") self.configuration.set("storage", "close_lock_file", "True")
self.application = Application(self.configuration, self.logger) self.application = Application(self.configuration, self.logger)