When there controller initializes user configuration in his home dir (in case it is not present during the first run), it only prepared the config. However, a config without a pool is useless. This required the user to edit the file manually to add it's own pool dir.
This commit adds automatic creation of the 'pool/' directory to the initialization sequence, so users don't have to do it manualy.
Signed-off-by: Radek Pazdera rpazdera@redhat.com --- lnst-ctl | 7 +++++-- lnst/Common/Config.py | 58 +++++++++++++++++++++++++++------------------------ lnst/Common/Utils.py | 10 +++++++++ 3 files changed, 46 insertions(+), 29 deletions(-)
diff --git a/lnst-ctl b/lnst-ctl index ac71925..1ed9b2f 100755 --- a/lnst-ctl +++ b/lnst-ctl @@ -20,6 +20,7 @@ import datetime from lnst.Common.Logs import LoggingCtl, log_exc_traceback from lnst.Common.Config import lnst_config from lnst.Common.Colours import load_presets_from_config +from lnst.Common.Utils import mkdir_p from lnst.Controller.NetTestController import NetTestController, NetTestError from lnst.Controller.NetTestResultSerializer import NetTestResultSerializer
@@ -133,8 +134,10 @@ def main(): if os.path.isfile(usr_cfg): lnst_config.load_config(usr_cfg) else: - if not os.path.isdir(os.path.dirname(usr_cfg)): - os.makedirs(os.path.dirname(usr_cfg)) + usr_cfg_dir = os.path.dirname(usr_cfg) + pool_dir = usr_cfg_dir + "/pool" + mkdir_p(pool_dir) + lnst_config.set_option("environment", "pool_dirs", [pool_dir]) with open(usr_cfg, 'w') as f: f.write(lnst_config.dump_config())
diff --git a/lnst/Common/Config.py b/lnst/Common/Config.py index 3255311..ffecfb6 100644 --- a/lnst/Common/Config.py +++ b/lnst/Common/Config.py @@ -29,42 +29,42 @@ class Config(): _scheme = None
def __init__(self): - self.options = dict() + self._options = dict()
def controller_init(self): - self.options['environment'] = dict() - self.options['environment']['mac_pool_range'] = {\ + self._options['environment'] = dict() + self._options['environment']['mac_pool_range'] = {\ "value" : ['52:54:01:00:00:01', '52:54:01:FF:FF:FF'], "additive" : False, "action" : self.optionMacRange, "name" : "mac_pool_range"} - self.options['environment']['rpcport'] = {\ + self._options['environment']['rpcport'] = {\ "value" : DefaultRPCPort, "additive" : False, "action" : self.optionPort, "name" : "rpcport"} - self.options['environment']['pool_dirs'] = {\ + self._options['environment']['pool_dirs'] = {\ "value" : [], "additive" : True, "action" : self.optionDirList, "name" : "machine_pool_dirs"} - self.options['environment']['tool_dirs'] = {\ + self._options['environment']['tool_dirs'] = {\ "value" : [], "additive" : True, "action" : self.optionDirList, "name" : "test_tool_dirs"} - self.options['environment']['module_dirs'] = {\ + self._options['environment']['module_dirs'] = {\ "value" : [], "additive" : True, "action" : self.optionDirList, "name" : "test_module_dirs"} - self.options['environment']['log_dir'] = {\ + self._options['environment']['log_dir'] = {\ "value" : os.path.abspath(os.path.join( os.path.dirname(sys.argv[0]), './Logs')), "additive" : False, "action" : self.optionPath, "name" : "log_dir"} - self.options['environment']['resource_dir'] = {\ + self._options['environment']['resource_dir'] = {\ "value" : "", "additive" : False, "action" : self.optionPath, @@ -73,28 +73,28 @@ class Config(): self.colours_scheme()
def slave_init(self): - self.options['environment'] = dict() - self.options['environment']['log_dir'] = {\ + self._options['environment'] = dict() + self._options['environment']['log_dir'] = {\ "value" : os.path.abspath(os.path.join( os.path.dirname(sys.argv[0]), './Logs')), "additive" : False, "action" : self.optionPath, "name" : "log_dir"} - self.options['environment']['use_nm'] = {\ + self._options['environment']['use_nm'] = {\ "value" : True, "additive" : False, "action" : self.optionBool, "name" : "use_nm"}
- self.options['cache'] = dict() - self.options['cache']['dir'] = {\ + self._options['cache'] = dict() + self._options['cache']['dir'] = {\ "value" : os.path.abspath(os.path.join( os.path.dirname(sys.argv[0]), './cache')), "additive" : False, "action" : self.optionPath, "name" : "cache_dir"}
- self.options['cache']['expiration_period'] = {\ + self._options['cache']['expiration_period'] = {\ "value" : 7*24*60*60, # 1 week "additive" : False, "action" : self.optionTimeval, @@ -103,25 +103,25 @@ class Config(): self.colours_scheme()
def colours_scheme(self): - self.options['colours'] = dict() - self.options['colours']["disable_colours"] = {\ + self._options['colours'] = dict() + self._options['colours']["disable_colours"] = {\ "value": False, "additive": False, "action": self.optionBool, "name": "disable_colours"}
for preset in ["faded", "alert", "highlight", "pass", "fail", "error", "info", "debug", "warning", "log_header"]: - self.options['colours'][preset] = {\ + self._options['colours'][preset] = {\ "value": get_preset_conf(preset), "additive": False, "action": self.optionColour, "name": preset}
def get_config(self): - return self.options + return self._options
def get_section(self, section): - if section not in self.options: + if section not in self._options: msg = 'Unknow section: %s' % section raise ConfigError(msg) - return self.options[section] + return self._options[section]
def get_option(self, section, option): sect = self.get_section(section) @@ -130,6 +130,10 @@ class Config(): raise ConfigError(msg) return sect[option]["value"]
+ def set_option(self, section, option, value): + sect = self.get_section(section) + sect[option]["value"] = value + def load_config(self, path): '''Parse and load the config file''' exp_path = os.path.expanduser(path) @@ -144,14 +148,14 @@ class Config():
def handleSections(self, sections, path): for section in sections: - if section in self.options: + if section in self._options: self.handleOptions(section, sections[section], path) else: msg = "Unknown section: %s" % section raise ConfigError(msg)
def handleOptions(self, section_name, config, cfg_path): - section = self.options[section_name] + section = self._options[section_name]
config.pop('__name__', None) for opt in config: @@ -262,18 +266,18 @@ class Config():
def dump_config(self): string = "" - for section in self.options: + for section in self._options: string += "[%s]\n" % section - for option in self.options[section]: + for option in self._options[section]: val = self.value_to_string(section, option) - opt_name = self.options[section][option]["name"] + opt_name = self._options[section][option]["name"] string += "%s = %s\n" % (opt_name, val)
return string
def value_to_string(self, section, option): string = "" - value = self.options[section][option]["value"] + value = self._options[section][option]["value"]
if type(value) == list: string = " ".join(value) diff --git a/lnst/Common/Utils.py b/lnst/Common/Utils.py index c3acafb..3b00afb 100644 --- a/lnst/Common/Utils.py +++ b/lnst/Common/Utils.py @@ -16,6 +16,7 @@ import os import hashlib import tempfile import subprocess +import errno from lnst.Common.ExecCmd import exec_cmd
def die_when_parent_die(): @@ -157,3 +158,12 @@ def check_process_running(process_name): return True except subprocess.CalledProcessError: return False + +def mkdir_p(path): + try: + os.makedirs(path) + except OSError as exc: + if exc.errno == errno.EEXIST and os.path.isdir(path): + pass + else: + raise
lnst-developers@lists.fedorahosted.org