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
This commit is contained in:
parent
216d656c68
commit
71a802b036
@ -96,9 +96,9 @@ def handle(_name, cfg, cloud, log, args):
|
|||||||
pw_auth = None
|
pw_auth = None
|
||||||
if 'ssh_pwauth' in cfg:
|
if 'ssh_pwauth' in cfg:
|
||||||
change_pwauth = True
|
change_pwauth = True
|
||||||
if util.is_true_str(cfg['ssh_pwauth']):
|
if util.is_true(cfg['ssh_pwauth']):
|
||||||
pw_auth = 'yes'
|
pw_auth = 'yes'
|
||||||
if util.is_false_str(cfg['ssh_pwauth']):
|
if util.is_false(cfg['ssh_pwauth']):
|
||||||
pw_auth = 'no'
|
pw_auth = 'no'
|
||||||
|
|
||||||
if change_pwauth:
|
if change_pwauth:
|
||||||
|
@ -26,7 +26,7 @@ import copy
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from cloudinit.settings import (PER_INSTANCE, FREQUENCIES)
|
from cloudinit.settings import (PER_INSTANCE, FREQUENCIES, CLOUD_CONFIG)
|
||||||
|
|
||||||
from cloudinit import handlers
|
from cloudinit import handlers
|
||||||
|
|
||||||
@ -146,16 +146,27 @@ class Init(object):
|
|||||||
self._cfg = self._read_cfg(extra_fns)
|
self._cfg = self._read_cfg(extra_fns)
|
||||||
# LOG.debug("Loaded 'init' config %s", self._cfg)
|
# 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):
|
def _read_cfg(self, extra_fns):
|
||||||
try:
|
no_cfg_paths = helpers.Paths({}, self.datasource)
|
||||||
base_conf = util.get_base_cfg(builtin=util.get_builtin_cfg())
|
merger = helpers.ConfigMerger(paths=no_cfg_paths,
|
||||||
except Exception:
|
|
||||||
base_conf = util.get_builtin_cfg()
|
|
||||||
no_cfg_pths = helpers.Paths({}, self.datasource)
|
|
||||||
merger = helpers.ConfigMerger(paths=no_cfg_pths,
|
|
||||||
datasource=self.datasource,
|
datasource=self.datasource,
|
||||||
additional_fns=extra_fns,
|
additional_fns=extra_fns,
|
||||||
base_cfg=base_conf)
|
base_cfg=self._read_base_cfg())
|
||||||
return merger.cfg
|
return merger.cfg
|
||||||
|
|
||||||
def _restore_from_cache(self):
|
def _restore_from_cache(self):
|
||||||
|
@ -50,7 +50,7 @@ from cloudinit import importer
|
|||||||
from cloudinit import log as logging
|
from cloudinit import log as logging
|
||||||
from cloudinit import url_helper as uhelp
|
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__)
|
LOG = logging.getLogger(__name__)
|
||||||
@ -192,7 +192,9 @@ def fork_cb(child_cb, *args):
|
|||||||
fid, obj_name(child_cb))
|
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']
|
check_set = ['true', '1', 'on', 'yes']
|
||||||
if addons:
|
if addons:
|
||||||
check_set = check_set + addons
|
check_set = check_set + addons
|
||||||
@ -201,7 +203,9 @@ def is_true_str(val, addons=None):
|
|||||||
return False
|
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']
|
check_set = ['off', '0', 'no', 'false']
|
||||||
if addons:
|
if addons:
|
||||||
check_set = check_set + addons
|
check_set = check_set + addons
|
||||||
@ -218,7 +222,7 @@ def translate_bool(val, addons=None):
|
|||||||
# If its already a boolean skip
|
# If its already a boolean skip
|
||||||
if isinstance(val, (bool)):
|
if isinstance(val, (bool)):
|
||||||
return val
|
return val
|
||||||
return is_true_str(val, addons)
|
return is_true(val, addons)
|
||||||
|
|
||||||
|
|
||||||
def rand_str(strlen=32, select_from=None):
|
def rand_str(strlen=32, select_from=None):
|
||||||
@ -285,29 +289,6 @@ def is_ipv4(instr):
|
|||||||
return (len(toks) == 4)
|
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):
|
def get_cfg_option_bool(yobj, key, default=False):
|
||||||
if key not in yobj:
|
if key not in yobj:
|
||||||
return default
|
return default
|
||||||
@ -622,15 +603,17 @@ def read_seeded(base="", ext="", timeout=5, retries=10, file_retries=0):
|
|||||||
|
|
||||||
|
|
||||||
def read_conf_d(confd):
|
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)
|
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")]
|
confs = [f for f in confs if f.endswith(".cfg")]
|
||||||
|
|
||||||
# remove anything not a file
|
# Remove anything not a file
|
||||||
confs = [f for f in confs if os.path.isfile(os.path.join(confd, f))]
|
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 = []
|
cfgs = []
|
||||||
for fn in confs:
|
for fn in confs:
|
||||||
cfgs.append(read_conf(os.path.join(confd, fn)))
|
cfgs.append(read_conf(os.path.join(confd, fn)))
|
||||||
@ -658,7 +641,8 @@ def read_conf_with_confd(cfgfile):
|
|||||||
return cfg
|
return cfg
|
||||||
|
|
||||||
# Conf.d settings override input configuration
|
# 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):
|
def read_cc_from_cmdline(cmdline=None):
|
||||||
@ -1076,14 +1060,6 @@ def ensure_dir(path, mode=None):
|
|||||||
chmod(path, mode)
|
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
|
@contextlib.contextmanager
|
||||||
def unmounter(umount):
|
def unmounter(umount):
|
||||||
try:
|
try:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user