diff options
author | Joshua Harlow <harlowja@yahoo-inc.com> | 2012-06-19 15:39:54 -0700 |
---|---|---|
committer | Joshua Harlow <harlowja@yahoo-inc.com> | 2012-06-19 15:39:54 -0700 |
commit | e731e0b9a06f27d705a0635e848e68f00f2b16cc (patch) | |
tree | 3d363e0e85c5452fad00b7f303b4cb631ceb45fa /cloudinit | |
parent | a8241225f408b8df258af0e6ea0820fb6894ea29 (diff) | |
download | vyos-cloud-init-e731e0b9a06f27d705a0635e848e68f00f2b16cc.tar.gz vyos-cloud-init-e731e0b9a06f27d705a0635e848e68f00f2b16cc.zip |
1. Cleanup variable names to match more of the pythonic underscore pattern
2. Seperate config loading from the actual final 'merging' process.
a. A util function will now merge multiple config dictionaries after they have all been loaded instead of
loading and merging at the same time, which can get confusing to follow.
Diffstat (limited to 'cloudinit')
-rw-r--r-- | cloudinit/stages.py | 87 | ||||
-rw-r--r-- | cloudinit/util.py | 11 |
2 files changed, 58 insertions, 40 deletions
diff --git a/cloudinit/stages.py b/cloudinit/stages.py index 6eb211db..558de035 100644 --- a/cloudinit/stages.py +++ b/cloudinit/stages.py @@ -156,20 +156,27 @@ class Init(object): # None check so that we don't keep on re-loading if empty if self._cfg is None: self._cfg = self._read_cfg(extra_fns) - LOG.debug("Loaded init config %s", self._cfg) + LOG.debug("Loaded %s config %s", util.obj_name(self), self._cfg) def _read_cfg(self, extra_fns): - builtin_cfg = util.get_builtin_cfg() - try: - conf = util.get_base_cfg(builtin=builtin_cfg) - except Exception: - conf = builtin_cfg - m_cfg = util.mergedict(conf, self._read_cfg_old()) + # Read extra files provided (if any) + i_cfgs = [] if extra_fns: for fn in extra_fns: - # Any extras over-ride the existing configs - m_cfg = util.mergedict(util.read_conf(fn), m_cfg) - return m_cfg + try: + fn_cfg = util.read_conf(fn) + i_cfgs.append(fn_cfg) + except: + util.logexc(LOG, ("Failed loading of additional" + " configuration from %s"), fn) + # Now read in the built-in + base + old + try: + conf = util.get_base_cfg(builtin=util.get_builtin_cfg()) + except Exception: + conf = util.get_builtin_cfg() + i_cfgs.append(conf) + i_cfgs.append(self._read_cfg_old()) + return util.mergemanydict(i_cfgs) def _restore_from_cache(self): pickled_fn = self.paths.get_ipath_cur('obj_pkl') @@ -371,46 +378,46 @@ class Init(object): class Transforms(object): - def __init__(self, init, cfgfile=None): + def __init__(self, init, cfg_files=None): self.datasource = init.fetch() - self.cfgfile = cfgfile - self.basecfg = copy.deepcopy(init.cfg) + self.cfg_files = cfg_files + self.base_cfg = copy.deepcopy(init.cfg) self.init = init # Created on first use - self._cachedcfg = None + self._cached_cfg = None @property def cfg(self): - if self._cachedcfg is None: - self._cachedcfg = self._get_config(self.cfgfile) - LOG.debug("Loading module config %s", self._cachedcfg) - return self._cachedcfg + # None check to avoid empty case + if self._cached_cfg is None: + self._cached_cfg = self._get_config() + LOG.debug("Loading %s config %s", + util.obj_name(self), self._cached_cfg) + return self._cached_cfg + + def _get_config(self): + t_cfgs = [] + if self.cfg_files: + for fn in self.cfg_files: + try: + t_cfgs.append(util.read_conf(fn)) + except: + util.logexc(LOG, ("Failed loading of configuration" + " from %s"), fn) - def _get_config(self, cfgfile): - mcfg = None - - if self.cfgfile: + if self.datasource: try: - mcfg = util.read_conf(cfgfile) + d_cfg = self.datasource.get_config_obj() + if d_cfg: + t_cfgs.append(d_cfg) except: - util.logexc(LOG, ("Failed loading of cloud config '%s'. " - "Continuing with an empty config."), cfgfile) - if not mcfg: - mcfg = {} + util.logexc(LOG, ("Failed loading of datasource" + " config object from %s"), self.datasource) + + if self.base_cfg: + t_cfgs.append(self.base_cfg) - ds_cfg = None - try: - ds_cfg = self.datasource.get_config_obj() - except: - util.logexc(LOG, "Failed loading of datasource config object.") - if not ds_cfg: - ds_cfg = {} - - mcfg = util.mergedict(mcfg, ds_cfg) - if self.basecfg: - return util.mergedict(mcfg, self.basecfg) - else: - return mcfg + return util.mergemanydict(t_cfgs) def _read_transforms(self, name): diff --git a/cloudinit/util.py b/cloudinit/util.py index 164bcea8..91d20a76 100644 --- a/cloudinit/util.py +++ b/cloudinit/util.py @@ -360,6 +360,7 @@ def get_cfg_by_path(yobj, keyp, default=None): def fixup_output(cfg, mode): (outfmt, errfmt) = get_output_cfg(cfg, mode) redirect_output(outfmt, errfmt) + return (outfmt, errfmt) # redirect_output(outfmt, errfmt, orig_out, orig_err) @@ -448,6 +449,16 @@ def obj_name(obj): return obj_name(obj.__class__) +def mergemanydict(srcs, reverse=False): + if reverse: + srcs = reversed(srcs) + m_cfg = {} + for a_cfg in srcs: + if a_cfg: + m_cfg = mergedict(m_cfg, a_cfg) + return m_cfg + + def mergedict(src, cand): """ Merge values from C{cand} into C{src}. |