allow config override from commandline

This commit is contained in:
Yohann Rebattu 2016-10-11 18:17:01 +02:00
parent 0c1dbc30bd
commit 11710f75a8
2 changed files with 104 additions and 67 deletions

View File

@ -36,38 +36,64 @@ from . import (
VERSION, Application, RequestHandler, ThreadedHTTPServer,
ThreadedHTTPSServer, config, log)
opt_dict = {
'--server_daemon': {
'help': 'launch as daemon',
'aliases': ['-d', '--daemon']},
'--server_pid': {
'help': 'set PID filename for daemon mode',
'aliases': ['-p', '--pid']},
'--server_hosts': {
'help': 'set server hostnames and ports',
'aliases': ['-H', '--hosts'],
},
'--server_ssl': {
'help': 'use SSL connection',
'aliases': ['-s', '--ssl'],
},
'--server_key': {
'help': 'set private key file',
'aliases': ['-k', '--key']
},
'--server_certificate': {
'help': 'set certificate file',
'aliases': ['-c', '--certificate']
},
'--logging_debug': {
'help': 'print debug informations',
'aliases': ['-D', '--debug']
}
}
def run():
"""Run Radicale as a standalone server."""
# Get command-line options
parser = optparse.OptionParser(version=VERSION)
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")
for section, values in config.INITIAL_CONFIG.items():
group = optparse.OptionGroup(parser, section)
for option, default_value in values.items():
long_name = '--{0}_{1}'.format(section, option)
kwargs = {}
args = [long_name]
if default_value.lower() in ('true', 'false'):
kwargs['action'] = 'store_true'
if long_name in opt_dict:
args.extend(opt_dict[long_name].get('aliases'))
opt_dict[long_name].pop('aliases')
kwargs.update(opt_dict[long_name])
group.add_option(*args, **kwargs)
if section == 'server':
group.add_option(
"-f", "--foreground", action="store_false",
dest="server_daemon",
help="launch in foreground (opposite of --daemon)")
group.add_option(
"-S", "--no-ssl", action="store_false", dest="server_ssl",
help="do not use SSL connection (opposite of --ssl)")
parser.add_option_group(group)
parser.add_option(
"-C", "--config",
help="use a specific configuration file")
@ -90,11 +116,21 @@ def run():
for option in parser.option_list:
key = option.dest
if key:
section = "logging" if key == "debug" else "server"
section = option.dest.split('_')[0]
value = getattr(options, key)
if value is not None:
configuration.set(section, key, str(value))
for group in parser.option_groups:
section = group.title
for option in group.option_list:
key = option.dest
config_key = key.split('_', 1)[1]
if key:
value = getattr(options, key)
if value is not None:
configuration.set(section, config_key, str(value))
# Start logging
filename = os.path.expanduser(configuration.get("logging", "config"))
debug = configuration.getboolean("logging", "debug")

View File

@ -24,48 +24,49 @@ Give a configparser-like interface to read and write configuration.
"""
import os
from collections import OrderedDict
from configparser import RawConfigParser as ConfigParser
# Default configuration
INITIAL_CONFIG = {
"server": {
"hosts": "0.0.0.0:5232",
"daemon": "False",
"pid": "",
"max_connections": "20",
"max_content_length": "10000000",
"timeout": "10",
"ssl": "False",
"certificate": "/etc/apache2/ssl/server.crt",
"key": "/etc/apache2/ssl/server.key",
"protocol": "PROTOCOL_SSLv23",
"ciphers": "",
"dns_lookup": "True",
"base_prefix": "/",
"can_skip_base_prefix": "False",
"realm": "Radicale - Password Required"},
"encoding": {
"request": "utf-8",
"stock": "utf-8"},
"auth": {
"type": "None",
"htpasswd_filename": "/etc/radicale/users",
"htpasswd_encryption": "crypt"},
"rights": {
"type": "None",
"file": "~/.config/radicale/rights"},
"storage": {
"type": "multifilesystem",
"filesystem_folder": os.path.expanduser(
"~/.config/radicale/collections"),
"fsync": "True",
"hook": "",
"close_lock_file": "False"},
"logging": {
"config": "/etc/radicale/logging",
"debug": "False",
"full_environment": "False",
"mask_passwords": "True"}}
INITIAL_CONFIG = OrderedDict([
("server", OrderedDict([
("hosts", "0.0.0.0:5232"),
("daemon", "False"),
("pid", ""),
("max_connections", "20"),
("max_content_length", "10000000"),
("timeout", "10"),
("ssl", "False"),
("certificate", "/etc/apache2/ssl/server.crt"),
("key", "/etc/apache2/ssl/server.key"),
("protocol", "PROTOCOL_SSLv23"),
("ciphers", ""),
("dns_lookup", "True"),
("base_prefix", "/"),
("can_skip_base_prefix", "False"),
("realm", "Radicale - Password Required")])),
("encoding", OrderedDict([
("request", "utf-8"),
("stock", "utf-8")])),
("auth", OrderedDict([
("type", "None"),
("htpasswd_filename", "/etc/radicale/users"),
("htpasswd_encryption", "crypt")])),
("rights", OrderedDict([
("type", "None"),
("file", "~/.config/radicale/rights")])),
("storage", OrderedDict([
("type", "multifilesystem"),
("filesystem_folder", os.path.expanduser(
"~/.config/radicale/collections")),
("fsync", "True"),
("hook", ""),
("close_lock_file", "False")])),
("logging", OrderedDict([
("config", "/etc/radicale/logging"),
("debug", "False"),
("full_environment", "False"),
("mask_passwords", "True")]))])
def load(paths=(), extra_config=None):