Add include support to the configs

This commit is contained in:
Thomas S Hatch 2012-02-09 14:40:40 -07:00
parent 26bbd93d0a
commit a54a6145dc

View File

@ -3,6 +3,7 @@ All salt configuration loading and defaults should be in this module
'''
# Import python modules
import glob
import os
import sys
import socket
@ -52,7 +53,7 @@ def load_config(opts, path, env_var):
# If the configuration file is missing, attempt to copy the template,
# after removing the first header line.
if not os.path.isfile(path):
template = "%s.template" % path
template = '{0}.template'.format(path)
if os.path.isfile(template):
with open(path, 'w') as out:
with open(template, 'r') as f:
@ -78,6 +79,34 @@ def load_config(opts, path, env_var):
log.debug('Missing configuration file: {0}'.format(path))
def include_config(opts, orig_path):
'''
Parses an extra configuration file specified in an include list in the
main config file.
'''
include = opts.get('include', [])
if isinstance(include, basestring):
include = [include]
for path in include:
if not os.path.isabs(path):
path = os.path.join(os.path.dirname(orig_path), path)
for fn_ in glob.glob(path):
try:
conf_opts = yaml.safe_load(open(fn_, 'r'))
if conf_opts is None:
# The config file is empty and the yaml.load returned None
conf_opts = {}
else:
# allow using numeric ids: convert int to string
if 'id' in conf_opts:
conf_opts['id'] = str(conf_opts['id'])
opts.update(conf_opts)
except Exception, e:
msg = 'Error parsing configuration file: {0} - {1}'
log.warn(msg.format(fn_, e))
return opts
def prepend_root_dir(opts, path_options):
'''
Prepends the options that represent filesystem paths with value of the
@ -127,6 +156,9 @@ def minion_config(path):
load_config(opts, path, 'SALT_MINION_CONFIG')
if 'include' in opts:
opts = include_config(opts, path)
opts['master_ip'] = dns_check(opts['master'])
opts['master_uri'] = 'tcp://' + opts['master_ip'] + ':'\
@ -186,6 +218,9 @@ def master_config(path):
load_config(opts, path, 'SALT_MASTER_CONFIG')
if 'include' in opts:
opts = include_config(opts, path)
opts['aes'] = salt.crypt.Crypticle.generate_key_string()
# Prepend root_dir to other paths