From 25712b40dfbe197c1ad8bdc783a2235e3c87c7de Mon Sep 17 00:00:00 2001 From: harlowja Date: Sun, 1 Jul 2012 12:08:08 -0700 Subject: 1. Rename util functions to is_true and is_false 2. Move the config loading functions to where they are used (in stages) 3. Adjust cc_set_passwords to use the is_true and is_false renamed functions 4. Adjust the init stage to have a _read_base_config function used to load the base 'initial' configuration from the following locations a. Kernel cmdline b. Conf.d location (+ the cloud.cfg location) c. Built-in configuration --- cloudinit/config/cc_set_passwords.py | 4 +-- cloudinit/stages.py | 27 +++++++++++------ cloudinit/util.py | 56 +++++++++++------------------------- 3 files changed, 37 insertions(+), 50 deletions(-) diff --git a/cloudinit/config/cc_set_passwords.py b/cloudinit/config/cc_set_passwords.py index 5b72224b..ab266741 100644 --- a/cloudinit/config/cc_set_passwords.py +++ b/cloudinit/config/cc_set_passwords.py @@ -96,9 +96,9 @@ def handle(_name, cfg, cloud, log, args): pw_auth = None if 'ssh_pwauth' in cfg: change_pwauth = True - if util.is_true_str(cfg['ssh_pwauth']): + if util.is_true(cfg['ssh_pwauth']): pw_auth = 'yes' - if util.is_false_str(cfg['ssh_pwauth']): + if util.is_false(cfg['ssh_pwauth']): pw_auth = 'no' if change_pwauth: diff --git a/cloudinit/stages.py b/cloudinit/stages.py index 2f175934..79663b27 100644 --- a/cloudinit/stages.py +++ b/cloudinit/stages.py @@ -26,7 +26,7 @@ import copy import os import sys -from cloudinit.settings import (PER_INSTANCE, FREQUENCIES) +from cloudinit.settings import (PER_INSTANCE, FREQUENCIES, CLOUD_CONFIG) from cloudinit import handlers @@ -146,16 +146,27 @@ class Init(object): self._cfg = self._read_cfg(extra_fns) # LOG.debug("Loaded 'init' config %s", self._cfg) + def _read_base_cfg(self): + base_cfgs = [] + default_cfg = util.get_builtin_cfg() + kern_contents = util.read_cc_from_cmdline() + # Kernel/cmdline parameters override system config + if kern_contents: + base_cfgs.append(util.load_yaml(kern_contents, default={})) + # Anything in your conf.d location?? + if os.path.isfile(CLOUD_CONFIG): + base_cfgs.append(util.read_conf_with_confd(CLOUD_CONFIG)) + # And finally the default gets to play + if default_cfg: + base_cfgs.append(default_cfg) + return util.mergemanydict(base_cfgs) + def _read_cfg(self, extra_fns): - try: - base_conf = util.get_base_cfg(builtin=util.get_builtin_cfg()) - except Exception: - base_conf = util.get_builtin_cfg() - no_cfg_pths = helpers.Paths({}, self.datasource) - merger = helpers.ConfigMerger(paths=no_cfg_pths, + no_cfg_paths = helpers.Paths({}, self.datasource) + merger = helpers.ConfigMerger(paths=no_cfg_paths, datasource=self.datasource, additional_fns=extra_fns, - base_cfg=base_conf) + base_cfg=self._read_base_cfg()) return merger.cfg def _restore_from_cache(self): diff --git a/cloudinit/util.py b/cloudinit/util.py index 0c592656..f07d22e7 100644 --- a/cloudinit/util.py +++ b/cloudinit/util.py @@ -50,7 +50,7 @@ from cloudinit import importer from cloudinit import log as logging from cloudinit import url_helper as uhelp -from cloudinit.settings import (CFG_BUILTIN, CLOUD_CONFIG) +from cloudinit.settings import (CFG_BUILTIN) LOG = logging.getLogger(__name__) @@ -192,7 +192,9 @@ def fork_cb(child_cb, *args): fid, obj_name(child_cb)) -def is_true_str(val, addons=None): +def is_true(val, addons=None): + if isinstance(val, (bool)): + return val is True check_set = ['true', '1', 'on', 'yes'] if addons: check_set = check_set + addons @@ -201,7 +203,9 @@ def is_true_str(val, addons=None): return False -def is_false_str(val, addons=None): +def is_false(val, addons=None): + if isinstance(val, (bool)): + return val is False check_set = ['off', '0', 'no', 'false'] if addons: check_set = check_set + addons @@ -218,7 +222,7 @@ def translate_bool(val, addons=None): # If its already a boolean skip if isinstance(val, (bool)): return val - return is_true_str(val, addons) + return is_true(val, addons) def rand_str(strlen=32, select_from=None): @@ -285,29 +289,6 @@ def is_ipv4(instr): return (len(toks) == 4) -def merge_base_cfg(cfgfile, cfg_builtin=None): - syscfg = read_conf_with_confd(cfgfile) - - kern_contents = read_cc_from_cmdline() - kerncfg = {} - if kern_contents: - kerncfg = load_yaml(kern_contents, default={}) - - # Kernel parameters override system config - if kerncfg: - combined = mergedict(kerncfg, syscfg) - else: - combined = syscfg - - if cfg_builtin: - # Combined over-ride anything builtin - fin = mergedict(combined, cfg_builtin) - else: - fin = combined - - return fin - - def get_cfg_option_bool(yobj, key, default=False): if key not in yobj: return default @@ -622,15 +603,17 @@ def read_seeded(base="", ext="", timeout=5, retries=10, file_retries=0): def read_conf_d(confd): - # get reverse sorted list (later trumps newer) + # Get reverse sorted list (later trumps newer) confs = sorted(os.listdir(confd), reverse=True) - # remove anything not ending in '.cfg' + # Remove anything not ending in '.cfg' confs = [f for f in confs if f.endswith(".cfg")] - # remove anything not a file - confs = [f for f in confs if os.path.isfile(os.path.join(confd, f))] + # Remove anything not a file + confs = [f for f in confs + if os.path.isfile(os.path.join(confd, f))] + # Load them all so that they can be merged cfgs = [] for fn in confs: cfgs.append(read_conf(os.path.join(confd, fn))) @@ -658,7 +641,8 @@ def read_conf_with_confd(cfgfile): return cfg # Conf.d settings override input configuration - return mergedict(read_conf_d(confd), cfg) + confd_cfg = read_conf_d(confd) + return mergedict(confd_cfg, cfg) def read_cc_from_cmdline(cmdline=None): @@ -1076,14 +1060,6 @@ def ensure_dir(path, mode=None): chmod(path, mode) -def get_base_cfg(cfg_path=None, builtin=None): - if not cfg_path: - cfg_path = CLOUD_CONFIG - if not builtin: - builtin = get_builtin_cfg() - return merge_base_cfg(cfg_path, builtin) - - @contextlib.contextmanager def unmounter(umount): try: -- cgit v1.2.3