Example #1
0
def _get_site_planning_API(site, site_planning, ignore_besteffort):
    try:
        alive_nodes = set([
            str(node['network_address']) for node in get_resource_attributes(
                '/sites/' + site +
                '/internal/oarapi/resources/details.json?limit=2^30')['items']
            if node['type'] == 'default' and node['state'] != 'Dead'
            and node['maintenance'] != 'YES'
        ])

        for host in alive_nodes:
            host_cluster = get_host_cluster(str(host))
            if host_cluster in site_planning:
                site_planning[host_cluster].update(
                    {host: {
                        'busy': [],
                        'free': []
                    }})
        if 'vlans' in site_planning:
            site_planning['vlans'] = {}
            for vlan in _get_vlans_API(site):
                site_planning['vlans'][vlan] = {'busy': [], 'free': []}
        # STORAGE AND SUBNETS MISSING
        # Retrieving jobs

        site_jobs = get_resource_attributes(
            '/sites/' + site +
            '/jobs?limit=1073741824&state=waiting,launching,running')['items']
        jobs_links = [ link['href'] for job in site_jobs for link in job['links'] \
                      if link['rel'] == 'self' and (ignore_besteffort == False or job['queue'] != 'besteffort') ]
        threads = []
        for link in jobs_links:
            t = Thread(target=_get_job_link_attr_API,
                       args=('/' + str(link).split('/', 2)[2], ))
            t.broken = False
            t.attr = None
            t.ex = None
            threads.append(t)
            t.start()
        for t in threads:
            t.join()
            if t.broken:
                raise t.ex
            attr = t.attr
            try:
                start_time = attr['started_at'] if attr[
                    'started_at'] != 0 else attr['scheduled_at']
                end_time = start_time + attr['walltime']
            except:
                continue
            start_time, end_time = _fix_job(start_time, end_time)
            nodes = attr['assigned_nodes']
            for node in nodes:
                cluster = node.split('.', 1)[0].split('-')[0]
                if cluster in site_planning and node in site_planning[cluster]:
                    site_planning[cluster][node]['busy'].append(
                        (start_time, end_time))
            if 'vlans' in site_planning and 'vlans' in attr['resources_by_type'] \
                and int(attr['resources_by_type']['vlans'][0]) > 3:

                kavname = 'kavlan-' + str(
                    attr['resources_by_type']['vlans'][0])
                site_planning['vlans'][kavname]['busy'].append(
                    (start_time, end_time))
            if 'subnets' in site_planning and 'subnets' in attr[
                    'resources_by_type']:
                for subnet in attr['resources_by_type']['subnets']:
                    if subnet not in site_planning['subnets']:
                        site_planning['subnets'][subnet] = {
                            'busy': [],
                            'free': []
                        }
                    site_planning['subnets'][subnet]['busy'].append(
                        (start_time, end_time))
            # STORAGE IS MISSING
    except Exception as e:
        logger.warn(
            'error connecting to oar database / getting planning from ' + site)
        logger.detail("exception:\n" + format_exc())
        currentThread().broken = True