Esempio n. 1
0
def get_host(mac):
    """
    returns a host object if the host is either found in the database (was discovered)
    or was configured.
    """
    host_config = None
    # load configuration of all hosts
    hosts_config = helper.load_hosts_config()
    # check if the mac is found in the configuration
    n = helper.find_hostname_by_mac(mac, hosts_config)
    if n is not None:
        host_config = hosts_config[n]
    # check database
    from pyfounder.models import HostInfo
    # check for any discovery info
    query = HostInfo.query.filter_by(mac=mac.lower()).first()
    # no config or discovery data found?
    if query is None and host_config is None:
        return None
    # create and return Host object
    host = Host(_model=query, _dict=host_config)
    return host
Esempio n. 2
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')
Esempio n. 3
0
def config():
    settings = {}
    # collect all settings
    try:
        settings['PXECFG_DIRECTORY'] = (helper.get_pxecfg_directory(), None)
    except helper.ConfigException as e:
        settings['PXECFG_DIRECTORY'] = (None, "{}".format(e))
    try:
        settings['PYFOUNDER_HOSTS'] = (helper.get_hosts_yaml(), None)
    except helper.ConfigException as e:
        settings['PYFOUNDER_HOSTS'] = (None, "{}".format(e))
    try:
        settings['PYFOUNDER_TEMPLATES'] = (helper.get_template_directory(),
                                           None)
    except helper.ConfigException as e:
        settings['PYFOUNDER_TEMPLATES'] = (None, "{}".format(e))

    try:
        extra = pformat(helper.load_hosts_config())
    except helper.ConfigException as e:
        extra = "Error: {}".format(e)
    # add settings to the config_arr
    return render_template('config.html', settings=settings, extra=extra)