From 164232ba4dd1c1862e515a95dcf627cd6be00eee Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Wed, 22 Nov 2017 09:45:27 -0800 Subject: [PATCH] Modify pylorax.api.config.configure so it can also be used for tests. --- src/pylorax/api/config.py | 27 +++++++++++++++++++-------- src/sbin/lorax-composer | 2 +- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/pylorax/api/config.py b/src/pylorax/api/config.py index 879692a2..c3a56adb 100644 --- a/src/pylorax/api/config.py +++ b/src/pylorax/api/config.py @@ -17,6 +17,8 @@ import ConfigParser import os +from pylorax.sysutils import joinpaths + class ComposerConfig(ConfigParser.SafeConfigParser): def get_default(self, section, option, default): try: @@ -25,15 +27,23 @@ class ComposerConfig(ConfigParser.SafeConfigParser): return default -def configure(conf_file="/etc/lorax/composer.conf"): - """lorax-composer configuration""" +def configure(conf_file="/etc/lorax/composer.conf", root_dir="/", test_config=False): + """lorax-composer configuration + + :param conf_file: Path to the config file overriding the default settings + :type conf_file: str + :param root_dir: Directory to prepend to paths, defaults to / + :type root_dir: str + :param test_config: Set to True to skip reading conf_file + :type test_config: bool + """ conf = ComposerConfig() # set defaults conf.add_section("composer") - conf.set("composer", "yum_conf", "/var/lib/lorax/composer/yum.conf") - conf.set("composer", "repo_dir", "/var/lib/lorax/composer/repos.d/") - conf.set("composer", "cache_dir", "/var/cache/lorax/composer/yum/") + conf.set("composer", "yum_conf", joinpaths(root_dir, "/var/lib/lorax/composer/yum.conf")) + conf.set("composer", "repo_dir", joinpaths(root_dir, "/var/lib/lorax/composer/repos.d/")) + conf.set("composer", "cache_dir", joinpaths(root_dir, "/var/cache/lorax/composer/yum/")) conf.add_section("users") conf.set("users", "root", "1") @@ -43,9 +53,10 @@ def configure(conf_file="/etc/lorax/composer.conf"): conf.set("repos", "use_system_repos", "1") conf.set("repos", "enabled", "*") - # read the config file - if os.path.isfile(conf_file): - conf.read(conf_file) + if not test_config: + # read the config file + if os.path.isfile(conf_file): + conf.read(conf_file) # Create any missing directories for section, key in [("composer", "yum_conf"), ("composer", "repo_dir"), ("composer", "cache_dir")]: diff --git a/src/sbin/lorax-composer b/src/sbin/lorax-composer index 0b74a674..44fd64e8 100755 --- a/src/sbin/lorax-composer +++ b/src/sbin/lorax-composer @@ -126,7 +126,7 @@ if __name__ == '__main__': server.config["REPO_DIR"] = opts.RECIPES repo = open_or_create_repo(server.config["REPO_DIR"]) server.config["GITLOCK"] = GitLock(repo=repo, lock=Lock(), dir=opts.RECIPES) - server.config["COMPOSER_CFG"] = configure(opts.config) + server.config["COMPOSER_CFG"] = configure(conf_file=opts.config) # Get a YumBase to share with the requests yb = get_base_object(server.config["COMPOSER_CFG"])