Skip to content
Snippets Groups Projects
Commit d7423a95 authored by Sybren A. Stüvel's avatar Sybren A. Stüvel
Browse files

Worker: Moved loading of config to config module.

parent 63e5fc19
Branches
No related tags found
No related merge requests found
"""Commandline interface entry points.""" """Commandline interface entry points."""
import argparse import argparse
import collections
import configparser
import logging import logging
import logging.config import logging.config
import os
DEFAULT_CONFIG = {
'flamenco-worker': collections.OrderedDict([
('manager_url', 'http://flamenco-manager/'),
('job_types', 'sleep blender_render_simple'),
('worker_id', ''),
('worker_secret', ''),
])
}
def main(): def main():
...@@ -61,40 +49,21 @@ def main(): ...@@ -61,40 +49,21 @@ def main():
log.debug('Starting') log.debug('Starting')
# Load configuration # Load configuration
confparser = configparser.ConfigParser() from . import config
confparser.read_dict(DEFAULT_CONFIG)
if args.config:
log.info('Loading configuration from %s', args.config)
confparser.read(args.config, encoding='utf8')
else:
from . import config as config_module
config_files = [config_module.GLOBAL_CONFIG_FILE,
config_module.HOME_CONFIG_FILE]
log.info('Loading configuration from %s', ', '.join(config_files))
confparser.read(config_files, encoding='utf8')
from .config import CONFIG_SECTION confparser = config.load_config(args.config, args.verbose)
if args.verbose:
import sys
log.info('Effective configuration:')
to_show = configparser.ConfigParser()
to_show.read_dict(confparser)
if to_show.get(CONFIG_SECTION, 'worker_secret'):
to_show.set(CONFIG_SECTION, 'worker_secret', '-hidden-')
to_show.write(sys.stderr)
from . import worker, upstream from . import worker, upstream
fmanager = upstream.FlamencoManager( fmanager = upstream.FlamencoManager(
manager_url=confparser.get(CONFIG_SECTION, 'manager_url'), manager_url=confparser.get(config.CONFIG_SECTION, 'manager_url'),
) )
fworker = worker.FlamencoWorker( fworker = worker.FlamencoWorker(
manager=fmanager, manager=fmanager,
job_types=confparser.get(CONFIG_SECTION, 'job_types').split(), job_types=confparser.get(config.CONFIG_SECTION, 'job_types').split(),
worker_id=confparser.get(CONFIG_SECTION, 'worker_id'), worker_id=confparser.get(config.CONFIG_SECTION, 'worker_id'),
worker_secret=confparser.get(CONFIG_SECTION, 'worker_secret'), worker_secret=confparser.get(config.CONFIG_SECTION, 'worker_secret'),
) )
try: try:
fworker.startup() fworker.startup()
......
"""Writes configuration to a config file in the home directory.""" """Writes configuration to a config file in the home directory."""
import collections
import configparser import configparser
import os.path import os.path
import logging import logging
...@@ -8,6 +9,15 @@ HOME_CONFIG_FILE = os.path.expanduser('~/.flamenco-worker.cfg') ...@@ -8,6 +9,15 @@ HOME_CONFIG_FILE = os.path.expanduser('~/.flamenco-worker.cfg')
GLOBAL_CONFIG_FILE = 'flamenco-worker.cfg' GLOBAL_CONFIG_FILE = 'flamenco-worker.cfg'
CONFIG_SECTION = 'flamenco-worker' CONFIG_SECTION = 'flamenco-worker'
DEFAULT_CONFIG = {
'flamenco-worker': collections.OrderedDict([
('manager_url', 'http://flamenco-manager/'),
('job_types', 'sleep blender_render_simple'),
('worker_id', ''),
('worker_secret', ''),
])
}
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
...@@ -30,3 +40,32 @@ def merge_with_home_config(new_conf: dict): ...@@ -30,3 +40,32 @@ def merge_with_home_config(new_conf: dict):
os.replace(tmpname, HOME_CONFIG_FILE) os.replace(tmpname, HOME_CONFIG_FILE)
log.info('Updated configuration file %s', HOME_CONFIG_FILE) log.info('Updated configuration file %s', HOME_CONFIG_FILE)
def load_config(config_file: str = None,
show_effective_config: bool = False) -> configparser.ConfigParser:
"""Loads one or more configuration files."""
confparser = configparser.ConfigParser()
confparser.read_dict(DEFAULT_CONFIG)
if config_file:
log.info('Loading configuration from %s', config_file)
loaded = confparser.read(config_file, encoding='utf8')
else:
config_files = [GLOBAL_CONFIG_FILE, HOME_CONFIG_FILE]
log.info('Loading configuration from %s', ', '.join(config_files))
loaded = confparser.read(config_files, encoding='utf8')
log.info('Succesfully loaded: %s', loaded)
if show_effective_config:
import sys
log.info('Effective configuration:')
to_show = configparser.ConfigParser()
to_show.read_dict(confparser)
if to_show.get(CONFIG_SECTION, 'worker_secret'):
to_show.set(CONFIG_SECTION, 'worker_secret', '-hidden-')
to_show.write(sys.stderr)
return confparser
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment