From 79054037a760b190214650f0ba5a950f85d5b732 Mon Sep 17 00:00:00 2001 From: Antonio Quartulli Date: Fri, 29 Dec 2017 19:55:30 +0800 Subject: [PATCH] conf: make config file a CLI parameter The config file might be stores in an alternate location for several reasons (i.e. testing different configs or running multiple nodes on the same host). Make the config file a CLI parameter so that it can be specified when launching the lbrynet-daemon. Related to #1039 Signed-off-by: Antonio Quartulli --- lbrynet/conf.py | 14 ++++++++++++-- lbrynet/daemon/DaemonControl.py | 15 ++++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lbrynet/conf.py b/lbrynet/conf.py index 014019806..3fad69588 100644 --- a/lbrynet/conf.py +++ b/lbrynet/conf.py @@ -56,6 +56,8 @@ settings_encoders = { '.yml': yaml.safe_dump } +# set by CLI when the user specifies an alternate config file path +conf_file = None def _win_path_to_bytes(path): """ @@ -476,7 +478,11 @@ class Config(object): } def save_conf_file_settings(self): - path = self.get_conf_filename() + if conf_file: + path = conf_file + else: + path = self.get_conf_filename() + ext = os.path.splitext(path)[1] encoder = settings_encoders.get(ext, False) assert encoder is not False, 'Unknown settings format %s' % ext @@ -484,7 +490,11 @@ class Config(object): settings_file.write(encoder(self._data[TYPE_PERSISTED])) def load_conf_file_settings(self): - path = self.get_conf_filename() + if conf_file: + path = conf_file + else: + path = self.get_conf_filename() + ext = os.path.splitext(path)[1] decoder = settings_decoders.get(ext, False) assert decoder is not False, 'Unknown settings format %s' % ext diff --git a/lbrynet/daemon/DaemonControl.py b/lbrynet/daemon/DaemonControl.py index db60ce01c..2638bca4b 100644 --- a/lbrynet/daemon/DaemonControl.py +++ b/lbrynet/daemon/DaemonControl.py @@ -20,9 +20,18 @@ def test_internet_connection(): def start(): """The primary entry point for launching the daemon.""" - conf.initialize_settings() + + # postpone loading the config file to after the CLI arguments + # have been parsed, as they may contain an alternate config file location + conf.initialize_settings(load_conf_file=False) parser = argparse.ArgumentParser(description="Launch lbrynet-daemon") + parser.add_argument( + "--conf", + help="specify an alternative configuration file", + type=str, + default=None + ) parser.add_argument( "--wallet", help="lbryum or ptc for testing, default lbryum", @@ -49,6 +58,8 @@ def start(): args = parser.parse_args() update_settings_from_args(args) + conf.settings.load_conf_file_settings() + if args.version: version = system_info.get_platform(get_ip=False) version['installation_id'] = conf.settings.installation_id @@ -83,6 +94,8 @@ def update_settings_from_args(args): 'wallet': args.wallet, }, data_types=(conf.TYPE_CLI,)) + conf.conf_file = args.conf + @defer.inlineCallbacks def start_server_and_listen(use_auth, analytics_manager):