mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Merge pull request #34606 from isbm/isbm-config-reading-exit-2015.8
Bugfix: Exit on configuration read (backport)
This commit is contained in:
commit
5c13ee0e72
5 changed files with 60 additions and 30 deletions
|
@ -39,6 +39,7 @@ import salt.utils.xdg
|
|||
import salt.exceptions
|
||||
import salt.utils.sdb
|
||||
from salt.utils.locales import sdecode
|
||||
import salt.defaults.exitcodes
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
@ -1468,18 +1469,18 @@ def _read_conf_file(path):
|
|||
try:
|
||||
conf_opts = yaml.safe_load(conf_file.read()) or {}
|
||||
except yaml.YAMLError as err:
|
||||
log.error(
|
||||
'Error parsing configuration file: {0} - {1}'.format(path, err)
|
||||
)
|
||||
conf_opts = {}
|
||||
message = 'Error parsing configuration file: {0} - {1}'.format(path, err)
|
||||
log.error(message)
|
||||
raise salt.exceptions.SaltConfigurationError(message)
|
||||
|
||||
# only interpret documents as a valid conf, not things like strings,
|
||||
# which might have been caused by invalid yaml syntax
|
||||
if not isinstance(conf_opts, dict):
|
||||
log.error(
|
||||
'Error parsing configuration file: {0} - conf should be a '
|
||||
'document, not {1}.'.format(path, type(conf_opts))
|
||||
)
|
||||
conf_opts = {}
|
||||
message = 'Error parsing configuration file: {0} - conf ' \
|
||||
'should be a document, not {1}.'.format(path, type(conf_opts))
|
||||
log.error(message)
|
||||
raise salt.exceptions.SaltConfigurationError(message)
|
||||
|
||||
# allow using numeric ids: convert int to string
|
||||
if 'id' in conf_opts:
|
||||
if not isinstance(conf_opts['id'], six.string_types):
|
||||
|
@ -1563,15 +1564,19 @@ def load_config(path, env_var, default_path=None):
|
|||
out.write(ifile.read())
|
||||
|
||||
if salt.utils.validate.path.is_readable(path):
|
||||
opts = _read_conf_file(path)
|
||||
opts['conf_file'] = path
|
||||
return opts
|
||||
try:
|
||||
opts = _read_conf_file(path)
|
||||
opts['conf_file'] = path
|
||||
return opts
|
||||
except salt.exceptions.SaltConfigurationError as error:
|
||||
log.error(error)
|
||||
sys.exit(salt.defaults.exitcodes.EX_GENERIC)
|
||||
|
||||
log.debug('Missing configuration file: {0}'.format(path))
|
||||
return {}
|
||||
|
||||
|
||||
def include_config(include, orig_path, verbose):
|
||||
def include_config(include, orig_path, verbose, exit_on_config_errors=False):
|
||||
'''
|
||||
Parses extra configuration file(s) specified in an include list in the
|
||||
main config file.
|
||||
|
@ -1607,7 +1612,12 @@ def include_config(include, orig_path, verbose):
|
|||
|
||||
for fn_ in sorted(glob.glob(path)):
|
||||
log.debug('Including configuration from {0!r}'.format(fn_))
|
||||
opts = _read_conf_file(fn_)
|
||||
try:
|
||||
opts = _read_conf_file(fn_)
|
||||
except salt.exceptions.SaltConfigurationError as error:
|
||||
log.error(error)
|
||||
if exit_on_config_errors:
|
||||
sys.exit(salt.defaults.exitcodes.EX_GENERIC)
|
||||
|
||||
include = opts.get('include', [])
|
||||
if include:
|
||||
|
@ -1650,7 +1660,8 @@ def insert_system_path(opts, paths):
|
|||
def minion_config(path,
|
||||
env_var='SALT_MINION_CONFIG',
|
||||
defaults=None,
|
||||
cache_minion_id=False):
|
||||
cache_minion_id=False,
|
||||
ignore_config_errors=True):
|
||||
'''
|
||||
Reads in the minion configuration file and sets up special options
|
||||
|
||||
|
@ -1685,8 +1696,10 @@ def minion_config(path,
|
|||
defaults['default_include'])
|
||||
include = overrides.get('include', [])
|
||||
|
||||
overrides.update(include_config(default_include, path, verbose=False))
|
||||
overrides.update(include_config(include, path, verbose=True))
|
||||
overrides.update(include_config(default_include, path, verbose=False,
|
||||
exit_on_config_errors=not ignore_config_errors))
|
||||
overrides.update(include_config(include, path, verbose=True,
|
||||
exit_on_config_errors=not ignore_config_errors))
|
||||
|
||||
opts = apply_minion_config(overrides, defaults, cache_minion_id=cache_minion_id)
|
||||
_validate_opts(opts)
|
||||
|
@ -2894,7 +2907,7 @@ def apply_minion_config(overrides=None,
|
|||
return opts
|
||||
|
||||
|
||||
def master_config(path, env_var='SALT_MASTER_CONFIG', defaults=None):
|
||||
def master_config(path, env_var='SALT_MASTER_CONFIG', defaults=None, exit_on_config_errors=False):
|
||||
'''
|
||||
Reads in the master configuration file and sets up default options
|
||||
|
||||
|
@ -2921,8 +2934,8 @@ def master_config(path, env_var='SALT_MASTER_CONFIG', defaults=None):
|
|||
defaults['default_include'])
|
||||
include = overrides.get('include', [])
|
||||
|
||||
overrides.update(include_config(default_include, path, verbose=False))
|
||||
overrides.update(include_config(include, path, verbose=True))
|
||||
overrides.update(include_config(default_include, path, verbose=False), exit_on_config_errors=exit_on_config_errors)
|
||||
overrides.update(include_config(include, path, verbose=True), exit_on_config_errors=exit_on_config_errors)
|
||||
opts = apply_master_config(overrides, defaults)
|
||||
_validate_opts(opts)
|
||||
# If 'nodegroups:' is uncommented in the master config file, and there are
|
||||
|
|
|
@ -259,6 +259,12 @@ class SaltWheelError(SaltException):
|
|||
'''
|
||||
|
||||
|
||||
class SaltConfigurationError(SaltException):
|
||||
'''
|
||||
Configuration error
|
||||
'''
|
||||
|
||||
|
||||
class SaltSystemExit(SystemExit):
|
||||
'''
|
||||
This exception is raised when an unsolvable problem is found. There's
|
||||
|
|
|
@ -766,9 +766,9 @@ class MWorker(multiprocessing.Process):
|
|||
'''
|
||||
salt.utils.appendproctitle(self.__class__.__name__)
|
||||
self.clear_funcs = ClearFuncs(
|
||||
self.opts,
|
||||
self.key,
|
||||
)
|
||||
self.opts,
|
||||
self.key,
|
||||
)
|
||||
self.aes_funcs = AESFuncs(self.opts)
|
||||
salt.utils.reinit_crypto()
|
||||
self.__bind()
|
||||
|
@ -800,7 +800,9 @@ class AESFuncs(object):
|
|||
self.mminion = salt.minion.MasterMinion(
|
||||
self.opts,
|
||||
states=False,
|
||||
rend=False)
|
||||
rend=False,
|
||||
ignore_config_errors=True
|
||||
)
|
||||
self.__setup_fileserver()
|
||||
self.masterapi = salt.daemons.masterapi.RemoteFuncs(opts)
|
||||
|
||||
|
@ -1466,7 +1468,9 @@ class ClearFuncs(object):
|
|||
self.mminion = salt.minion.MasterMinion(
|
||||
self.opts,
|
||||
states=False,
|
||||
rend=False)
|
||||
rend=False,
|
||||
ignore_config_errors=True
|
||||
)
|
||||
# Make a wheel object
|
||||
self.wheel_ = salt.wheel.Wheel(opts)
|
||||
# Make a masterapi object
|
||||
|
|
|
@ -565,8 +565,9 @@ class MasterMinion(object):
|
|||
states=True,
|
||||
rend=True,
|
||||
matcher=True,
|
||||
whitelist=None):
|
||||
self.opts = salt.config.minion_config(opts['conf_file'])
|
||||
whitelist=None,
|
||||
ignore_config_errors=True):
|
||||
self.opts = salt.config.minion_config(opts['conf_file'], ignore_config_errors=ignore_config_errors)
|
||||
self.opts.update(opts)
|
||||
self.whitelist = whitelist
|
||||
self.opts['grains'] = salt.loader.grains(opts)
|
||||
|
|
|
@ -35,6 +35,7 @@ import salt.utils.xdg
|
|||
from salt.utils import kinds
|
||||
from salt.defaults import DEFAULT_TARGET_DELIM
|
||||
from salt.utils.validate.path import is_writeable
|
||||
import salt.exceptions
|
||||
|
||||
|
||||
def _sorted(mixins_or_funcs):
|
||||
|
@ -337,7 +338,12 @@ class SaltfileMixIn(six.with_metaclass(MixInMeta, object)):
|
|||
'Loading Saltfile from {0!r}'.format(self.options.saltfile)
|
||||
)
|
||||
|
||||
saltfile_config = config._read_conf_file(saltfile)
|
||||
try:
|
||||
saltfile_config = config._read_conf_file(saltfile)
|
||||
except salt.exceptions.SaltConfigurationError as error:
|
||||
self.error(error.message)
|
||||
self.exit(salt.defaults.exitcodes.EX_GENERIC,
|
||||
'{0}: error: {1}\n'.format(self.get_prog_name(), error.message))
|
||||
|
||||
if not saltfile_config:
|
||||
# No configuration was loaded from the Saltfile
|
||||
|
@ -1496,8 +1502,8 @@ class MinionOptionParser(six.with_metaclass(OptionParserMeta, MasterOptionParser
|
|||
_default_logging_logfile_ = os.path.join(syspaths.LOGS_DIR, 'minion')
|
||||
|
||||
def setup_config(self):
|
||||
return config.minion_config(self.get_config_file_path(),
|
||||
cache_minion_id=True)
|
||||
return config.minion_config(self.get_config_file_path(), # pylint: disable=no-member
|
||||
cache_minion_id=True, ignore_config_errors=False)
|
||||
|
||||
|
||||
class ProxyMinionOptionParser(six.with_metaclass(OptionParserMeta,
|
||||
|
|
Loading…
Add table
Reference in a new issue