Exemplo n.º 1
0
def fetch(hostname, template_name=None):
    try:
        cfg = helper.host_config(hostname)
    except ConfigException as e:
        abort(404, "Host {} not found.".format(hostname))
    if template_name is None:
        ymlcfg = helper.yaml_dump(cfg)
        return Response(ymlcfg, mimetype='text/plain')
    try:
        rendered_content = fetch_template(template_name, hostname)
    except ConfigException as e:
        abort(404, "{}".format(e))
    return Response(rendered_content, mimetype='text/plain')
Exemplo n.º 2
0
def host_yaml(hostname):
    """Print yaml configuration using discovered and configured data"""
    if (len(hostname)<1):
        raise click.ClickException("No hosts found. Hint: Use % as wildcard.")
    data = host_query(hostname)
    if len(data)>0:
        hosts = {}
        for host in data:
            hosts[host['name']] = {
                    'interface':host['interface'],
                    'mac':host['mac'],
                    'ip':host['ip'],
                    }
            if host['class'] is not None:
                hosts[host['name']]['class'] = host['class']

        click.echo(yaml_dump({'hosts':hosts}))
Exemplo n.º 3
0
def api_hosts(pattern=None):
    # build the query
    query = HostInfo.query
    if pattern is not None:
        # extend the query
        query = query.filter(
            HostInfo.name.ilike('{}'.format(pattern))
            | HostInfo.mac.ilike('{}'.format(pattern)))
    hosts_config = helper.load_hosts_config()
    host_data = []
    for q in query.all():
        h = Host(_model=q)
        # update from host_config
        if h['name'] is not None and h['name'] in hosts_config:
            h.from_dict(hosts_config[h['name']])
        elif h['mac'] is not None:
            n = helper.find_hostname_by_mac(h['mac'], hosts_config)
            if n is not None:
                h.from_dict(hosts_config[n])
        host_data.append(h)
    hostnames_query = [x['name'] for x in host_data if 'name' in x.data]
    # complete data with hosts_data
    for missing_host in [
            x for x in hosts_config.keys() if x not in hostnames_query
    ]:
        try:
            missing_mac = hosts_config[missing_host]['mac']
        except KeyError:
            missing_mac = None
        if pattern is not None and missing_mac is not None:
            pattern_re = re.escape(pattern)
            pattern_re = pattern_re.replace('\\%', '.*')
            pattern_re = "^{}$".format(pattern_re)
            if not re.match(pattern_re, missing_host) and not re.match(
                    pattern_re, missing_mac):
                continue
        host_data.append(Host(_dict=hosts_config[missing_host]))
    yaml_str = helper.yaml_dump([h.data for h in host_data])
    return Response(yaml_str, mimetype='text/plain')
Exemplo n.º 4
0
 def save(self):
     mkdir_p(self.config_dir)
     with open(self.config_file,'w') as f:
         f.write(yaml_dump(self))