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

View File

@ -24,48 +24,49 @@ 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", "0.0.0.0:5232"),
"daemon": "False", ("daemon", "False"),
"pid": "", ("pid", ""),
"max_connections": "20", ("max_connections", "20"),
"max_content_length": "10000000", ("max_content_length", "10000000"),
"timeout": "10", ("timeout", "10"),
"ssl": "False", ("ssl", "False"),
"certificate": "/etc/apache2/ssl/server.crt", ("certificate", "/etc/apache2/ssl/server.crt"),
"key": "/etc/apache2/ssl/server.key", ("key", "/etc/apache2/ssl/server.key"),
"protocol": "PROTOCOL_SSLv23", ("protocol", "PROTOCOL_SSLv23"),
"ciphers": "", ("ciphers", ""),
"dns_lookup": "True", ("dns_lookup", "True"),
"base_prefix": "/", ("base_prefix", "/"),
"can_skip_base_prefix": "False", ("can_skip_base_prefix", "False"),
"realm": "Radicale - Password Required"}, ("realm", "Radicale - Password Required")])),
"encoding": { ("encoding", OrderedDict([
"request": "utf-8", ("request", "utf-8"),
"stock": "utf-8"}, ("stock", "utf-8")])),
"auth": { ("auth", OrderedDict([
"type": "None", ("type", "None"),
"htpasswd_filename": "/etc/radicale/users", ("htpasswd_filename", "/etc/radicale/users"),
"htpasswd_encryption": "crypt"}, ("htpasswd_encryption", "crypt")])),
"rights": { ("rights", OrderedDict([
"type": "None", ("type", "None"),
"file": "~/.config/radicale/rights"}, ("file", "~/.config/radicale/rights")])),
"storage": { ("storage", OrderedDict([
"type": "multifilesystem", ("type", "multifilesystem"),
"filesystem_folder": os.path.expanduser( ("filesystem_folder", os.path.expanduser(
"~/.config/radicale/collections"), "~/.config/radicale/collections")),
"fsync": "True", ("fsync", "True"),
"hook": "", ("hook", ""),
"close_lock_file": "False"}, ("close_lock_file", "False")])),
"logging": { ("logging", OrderedDict([
"config": "/etc/radicale/logging", ("config", "/etc/radicale/logging"),
"debug": "False", ("debug", "False"),
"full_environment": "False", ("full_environment", "False"),
"mask_passwords": "True"}} ("mask_passwords", "True")]))])
def load(paths=(), extra_config=None): def load(paths=(), extra_config=None):