def compile_conf_templates(conf, tmpldirs, workdir, templates_key="templates"): """ Compile template config files. :param conf: Config object holding templates info :param tmpldirs: Template paths :param workdir: Working dir :param template_keys: Template keys to search each templates """ for k, v in conf.get(templates_key, {}).iteritems(): src = v.get("src", None) dst = v.get("dst", src) if src is None: logging.warn("{} lacks 'src' parameter".format(k)) continue if os.path.sep in src: logging.debug("Adding subdirs of source to template paths: " + src) srcdirs = [os.path.join(d, os.path.dirname(src)) for d in tmpldirs] else: srcdirs = tmpldirs # strip dir part as it will be searched from srcdir. src = os.path.basename(src) dst = os.path.join(workdir, dst) tpaths = srcdirs + [workdir] logging.debug("Generating {} from {} [{}]".format(dst, src, k)) logging.debug("Template path: " + ", ".join(tpaths)) anytemplate.render_to(src, conf, dst, at_paths=tpaths, at_engine="jinja2")
def compile_conf_templates(conf, tmpldirs, workdir, templates_key="templates"): """ :param conf: Config object holding templates info :param tmpldirs: Template paths :param workdir: Working dir :param template_keys: Template keys to search each templates """ for k, v in conf.get(templates_key, {}).iteritems(): src = v.get("src", None) dst = v.get("dst", src) if src is None: logging.warn("{} lacks 'src' parameter".format(k)) continue if os.path.sep in src: logging.debug("Adding subdirs of source to template paths: " + src) srcdirs = [os.path.join(d, os.path.dirname(src)) for d in tmpldirs] else: srcdirs = tmpldirs # strip dir part as it will be searched from srcdir. src = os.path.basename(src) dst = os.path.join(workdir, dst) tpaths = srcdirs + [workdir] logging.debug("Generating {} from {} [{}]".format(dst, src, k)) logging.debug("Template path: " + ", ".join(tpaths)) renderto(tpaths, conf, src, dst)
def _find_template(tmpldirs, template): for tdir in tmpldirs: tmpl = os.path.join(tdir, template) if os.path.exists(tmpl): return tmpl logging.warn("Template {} not found in paths: " "{}".format(template, ', '.join(tmpldirs))) return template
def _check_dups_by_ip_or_mac(nis): """ Check if duplicated IP or MAC found in host list and warns about them. :param nis: A list of network interfaces, {ip, mac, ...} """ for k, v, ns in U.find_dups_in_dicts_list_g(nis, ("ip", "mac")): if _ignorable_kv_pair(k, v): continue m = "Duplicated entries: key={}, v={}, hosts={}" logging.warn(m.format(k, v, ", ".join(n.get("host", str(n)) for n in ns)))
def _check_dups_by_ip_or_mac(nis): """ Check if duplicated IP or MAC found in host list and warns about them. :param nis: A list of network interfaces, {ip, mac, ...} """ for k, v, ns in U.find_dups_in_dicts_list_g(nis, ("ip", "mac")): if _ignorable_kv_pair(k, v): continue m = "Duplicated entries: key={}, v={}, hosts={}" logging.warn( m.format(k, v, ", ".join(n.get("host", str(n)) for n in ns)))
def load_site_ctxs(ctxs): """ Load context (conf) files from ``ctxs``. :param ctxs: List of context file[s], glob patterns of context files or dirs :: [str] """ conf = anyconfig.to_container() for ctxpath in ctxs: diff = load_site_ctx(ctxpath) if diff: conf.update(diff) else: logging.warn("No config loaded from: %s", ctxpath) if not conf: raise EmptyConfigError("No config available from: " + ','.join(ctxs)) return conf
def load_site_ctx(ctxpath): """ Load context (conf) file from ``ctxpath``. ``ctxpath`` may be a path to context file, glob pattern of context files or a dir. :param ctxpath: A ctx file, glob pattern of ctx files or dir :: str """ if os.path.isdir(ctxpath): ctxpath = os.path.join(ctxpath, G.M_CONF_PATTERN) else: if '*' not in ctxpath and not os.path.exists(ctxpath): logging.info("Not exist and skipped: %s", ctxpath) return None ctx = anyconfig.load(ctxpath, ac_template=True) if not ctx: logging.warn("No config loaded from: %s", ctxpath) return ctx
def gen_vnet_files(cf, tmpldirs, workdir, force): """ Generate libvirt network def. XML files. :param cf: An instance of miniascape.config.ConfFiles :param tmpldirs: Template search paths :param workdir: Working dir to save generated XML files :param force: Existing ones may be overwritten if True """ nets = cf.load_nets_confs() outdir = _netoutdir(workdir) tmpl = _find_template(tmpldirs, _netxml_path()) tpaths = [os.path.dirname(tmpl)] logging.debug("Network XML: tpaths={}, tmpl={}".format(tpaths, tmpl)) if not os.path.exists(outdir): os.makedirs(outdir) for name in nets: netconf = os.path.join(outdir, "{}.yml".format(name)) if os.path.exists(netconf) and not force: logging.warn("Net conf already exists: " + netconf) return logging.debug("Dump conf for the net: " + name) anyconfig.dump(nets[name], netconf) netxml = os.path.join(outdir, "{}.xml".format(name)) if os.path.exists(netxml) and not force: logging.warn("Net xml already exists: " + netxml) return nc = anyconfig.load(netconf, ac_template=True) nc["hosts"] = hosts_w_unique_ips(nc) nc["hosts_w_unique_macs"] = hosts_w_unique_macs(nc) logging.debug("Generating network xml: " + netxml) miniascape.template.render_to(tmpl, nc, netxml, tpaths)