Modify pylorax.api.config.configure so it can also be used for tests.

This commit is contained in:
Brian C. Lane 2017-11-22 09:45:27 -08:00
parent f26af06739
commit 164232ba4d
2 changed files with 20 additions and 9 deletions

View File

@ -17,6 +17,8 @@
import ConfigParser import ConfigParser
import os import os
from pylorax.sysutils import joinpaths
class ComposerConfig(ConfigParser.SafeConfigParser): class ComposerConfig(ConfigParser.SafeConfigParser):
def get_default(self, section, option, default): def get_default(self, section, option, default):
try: try:
@ -25,15 +27,23 @@ class ComposerConfig(ConfigParser.SafeConfigParser):
return default return default
def configure(conf_file="/etc/lorax/composer.conf"): def configure(conf_file="/etc/lorax/composer.conf", root_dir="/", test_config=False):
"""lorax-composer configuration""" """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() conf = ComposerConfig()
# set defaults # set defaults
conf.add_section("composer") conf.add_section("composer")
conf.set("composer", "yum_conf", "/var/lib/lorax/composer/yum.conf") conf.set("composer", "yum_conf", joinpaths(root_dir, "/var/lib/lorax/composer/yum.conf"))
conf.set("composer", "repo_dir", "/var/lib/lorax/composer/repos.d/") conf.set("composer", "repo_dir", joinpaths(root_dir, "/var/lib/lorax/composer/repos.d/"))
conf.set("composer", "cache_dir", "/var/cache/lorax/composer/yum/") conf.set("composer", "cache_dir", joinpaths(root_dir, "/var/cache/lorax/composer/yum/"))
conf.add_section("users") conf.add_section("users")
conf.set("users", "root", "1") conf.set("users", "root", "1")
@ -43,6 +53,7 @@ def configure(conf_file="/etc/lorax/composer.conf"):
conf.set("repos", "use_system_repos", "1") conf.set("repos", "use_system_repos", "1")
conf.set("repos", "enabled", "*") conf.set("repos", "enabled", "*")
if not test_config:
# read the config file # read the config file
if os.path.isfile(conf_file): if os.path.isfile(conf_file):
conf.read(conf_file) conf.read(conf_file)

View File

@ -126,7 +126,7 @@ if __name__ == '__main__':
server.config["REPO_DIR"] = opts.RECIPES server.config["REPO_DIR"] = opts.RECIPES
repo = open_or_create_repo(server.config["REPO_DIR"]) repo = open_or_create_repo(server.config["REPO_DIR"])
server.config["GITLOCK"] = GitLock(repo=repo, lock=Lock(), dir=opts.RECIPES) 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 # Get a YumBase to share with the requests
yb = get_base_object(server.config["COMPOSER_CFG"]) yb = get_base_object(server.config["COMPOSER_CFG"])