def test_generate_hosts(self): model = { '=host10-12': { '=cmdb': { 'gw': ['192.168.1.1', '192.168.1.2'], '=ip': '192.168.1.10-12' } } } self.assertEqual( generate.generate_dict(model, prefix='='), { 'host10': { 'cmdb': { 'gw': ['192.168.1.1', '192.168.1.2'], 'ip': '192.168.1.10' } }, 'host11': { 'cmdb': { 'gw': ['192.168.1.1', '192.168.1.2'], 'ip': '192.168.1.11' } }, 'host12': { 'cmdb': { 'gw': ['192.168.1.1', '192.168.1.2'], 'ip': '192.168.1.12' } } })
def test_generate_hosts(self): model = OrderedDict([('host10', { 'foo': 'bar' }), ('=host10-12', { '=cmdb': { 'gw': ['192.168.1.1', '192.168.1.2'], '=ip': '192.168.1.10-12' } })]) self.assertEqual( generate.generate_dict(model, prefix='='), { 'host10': { 'cmdb': { 'gw': ['192.168.1.1', '192.168.1.2'], 'ip': '192.168.1.10' }, 'foo': 'bar' }, 'host11': { 'cmdb': { 'gw': ['192.168.1.1', '192.168.1.2'], 'ip': '192.168.1.11' } }, 'host12': { 'cmdb': { 'gw': ['192.168.1.1', '192.168.1.2'], 'ip': '192.168.1.12' } } })
def extract_from_yaml(key, yamlstr, lookup_all=False): '''Extract a value from a YAML string according to a key. If lookup_all is True find all the values. The key can describe a hierarchical structure using '.' as separator and use * as a wildcard.''' variables = load(yamlstr, Loader=Loader) if 'hosts' in variables: variables['hosts'] = generate.generate_dict(variables['hosts'], '=') keys = key.split('.') return _lookup_keys(keys, variables, lookup_all)
def collect(config_path): # check config directory path if not os.path.exists(config_path): print("Error: --config-dir='%s' does not exist." % config_path) sys.exit(1) # get state object state_obj = state.State() state_obj.load(os.path.join(config_path, 'edeploy') + '/') # get global conf global_conf = _get_yaml_content("%s/config-tools/global.yml" % config_path) # expand keys prefixed by "=" global_conf["hosts"] = generate.generate_dict(global_conf["hosts"], "=") # the virtual configuration of each host virt_platform = {"hosts": {}} for hostname in global_conf["hosts"]: # construct the host virtual configuration virt_platform["hosts"][hostname] = state_obj.hardware_info(hostname) # add the profile virt_platform["hosts"][hostname]["profile"] = \ global_conf["hosts"][hostname]["profile"] # release the lock obtained during the load call state_obj.unlock() # so far, the nodes are described excepted the install-server # the code below adds the install-server from the global conf. for hostname in global_conf["hosts"]: if global_conf["hosts"][hostname]["profile"] == "install-server": # add the admin_network config admin_network = global_conf["config"]["admin_network"] admin_network = netaddr.IPNetwork(admin_network) nics = [{ "name": "eth0", "ip": global_conf["hosts"][hostname]["ip"], "network": str(admin_network.network), "netmask": str(admin_network.netmask) }] virt_platform["hosts"][hostname]["nics"] = nics break return virt_platform
def expand_template(step, yamlstr, tmpl, ovrwt={}): '''Expand a template string according to the yaml variables augmented with information with steps.''' variables = get_vars(yamlstr) variables.update(ovrwt) if 'hosts' in variables: variables['hosts'] = generate.generate_dict(variables['hosts'], '=') validate(variables) variables['step'] = step reinject(variables) # for debugging purpose if os.getenv('CONFIGTOOL_GENERATED_YAML'): generated = open(os.getenv('CONFIGTOOL_GENERATED_YAML'), 'w') generated.write(dump(variables)) generated.close() return expand(tmpl, variables)
def test_generate_hosts(self): model = OrderedDict([('host10', {'foo': 'bar'}), ('=host10-12', {'=cmdb': {'gw': ['192.168.1.1', '192.168.1.2'], '=ip': '192.168.1.10-12'}})]) self.assertEqual( generate.generate_dict(model, prefix='='), {'host10': {'cmdb': {'gw': ['192.168.1.1', '192.168.1.2'], 'ip': '192.168.1.10'}, 'foo': 'bar'}, 'host11': {'cmdb': {'gw': ['192.168.1.1', '192.168.1.2'], 'ip': '192.168.1.11'}}, 'host12': {'cmdb': {'gw': ['192.168.1.1', '192.168.1.2'], 'ip': '192.168.1.12'}}} )