Default option handling

Merge default options from [box] and [suite] blocks into the
respective [box.*]/[suite.*] sections.

Signed-off-by: Georg Pfuetzenreuter <mail@georg-pfuetzenreuter.net>
This commit is contained in:
Georg Pfuetzenreuter 2023-05-20 18:51:03 +02:00
parent c5e404c9c6
commit f110f92150
Signed by: Georg
GPG Key ID: 1ED2F138E7E6FF57

View File

@ -42,24 +42,33 @@ def _abort(msg):
sys.exit(1)
def _config():
configmap = {'box': {}, 'suites': {}}
configmap = {'boxes': {}, 'suites': {}}
if not config.options('box'):
_abort('No "box" section found in the configuration file')
for option in config.options('box'):
configmap['box'][option] = config.get('box', option)
boxes = [section for section in config.sections() if section.startswith('box.')]
suites = [section for section in config.sections() if section.startswith('suite.')]
if not len(suites):
_abort('No suites configured')
log.debug('Suites: {}'.format(str(suites)))
for section in suites:
suite = section.replace('suite.', '')
configmap['suites'][suite] = {}
multis = {'boxes': {'conf': boxes, 'prefix': 'box.'}, 'suites': {'conf': suites, 'prefix': 'suite.'}}
for multi, multiconf in multis.items():
for section in multiconf['conf']:
collection = section.replace(multiconf['prefix'], '')
configmap[multi][collection] = {}
for option in config.options(section):
if option in ['masters', 'minions']:
value = config.getint(section, option)
else:
value = config.get(section, option)
configmap['suites'][suite][option] = value
configmap[multi][collection][option] = value
# a bit of an ugly alternative to the "DEFAULT" section
multis = {'boxes': {'singular': 'box'}, 'suites': {'singular': 'suite'}}
for multis, multiconf in multis.items():
multi = multiconf['singular']
if multi in config.sections():
for option in config.options(multi):
for collection in configmap[multis]:
configmap[multis][collection][option] = config.get(multi, option)
log.debug('Config map: {}'.format(str(configmap)))
return configmap
@ -112,18 +121,24 @@ def vagrant_isup(suite):
def main_interactive():
configmap = _config()
box = configmap['box']
box_name = box.get('name', None)
box_image = box.get('image', None)
box_file = box.get('file', '{}/Vagrantfile-Template'.format(os.path.abspath(os.path.dirname(__file__))))
if None in [box_name, box_image, box_file]:
_abort('Box configuration is incomplete')
box_bootstrap = box.get('bootstrap', None)
boxes = configmap['boxes']
suites = configmap['suites']
suite = args.suite
if suite not in suites:
_abort('No suite named {}'.format(suite))
suiteconf = configmap['suites'][suite]
box = suiteconf.get('box', None)
if box is None:
_abort('Specified suite does not reference a box')
boxconf = configmap['boxes'].get(box, None)
if boxconf is None:
_abort('Suite referencs an undefined box')
box_name = boxconf.get('name', None)
box_image = boxconf.get('image', None)
box_file = boxconf.get('file', '{}/Vagrantfile-Template'.format(os.path.abspath(os.path.dirname(__file__))))
if None in [box_name, box_image, box_file]:
_abort('Box configuration is incomplete')
box_bootstrap = boxconf.get('bootstrap', None)
minions = None
masters = None
if suiteconf.get('minions', 0) > 0: