Esempio n. 1
0
def iter_servers_dict():
    server_collection = mongo.get_collection('servers')

    response = server_collection.aggregate([
        {'$project': {
            'host_id': '$instances.host_id',
            'client': '$instances.clients',
        }},
        {'$unwind': '$host_id'},
        {'$unwind': '$client'},
        {'$unwind': '$client'},
        {'$match': {
            'client.type': CERT_CLIENT,
        }},
        {'$group': {
            '_id': '$host_id',
            'clients': {'$addToSet': '$client.id'},
        }},
    ])['result']

    hosts_clients = {}
    for doc in response:
        hosts_clients[doc['_id']] = doc['clients']

    org_user_count = organization.get_user_count()

    response = server_collection.aggregate([
        {'$project': {
            'hosts': True,
            'organizations': True,
        }},
        {'$unwind': '$hosts'},
        {'$unwind': '$organizations'},
        {'$group': {
            '_id': '$hosts',
            'organizations': {'$addToSet': '$organizations'},
        }},
    ])['result']

    host_orgs = collections.defaultdict(list)
    for doc in response:
        host_orgs[doc['_id']] = doc['organizations']

    for doc in Host.collection.find().sort('name'):
        hst = Host(doc=doc)

        users_online = len(hosts_clients.get(hst.id, []))

        user_count = 0
        for org_id in host_orgs[hst.id]:
            user_count += org_user_count.get(org_id, 0)

        hst.user_count = user_count
        hst.users_online = users_online

        yield hst.dict()
Esempio n. 2
0
def iter_servers_dict():
    server_collection = mongo.get_collection('servers')

    response = server_collection.aggregate([
        {'$project': {
            'host_id': '$instances.host_id',
            'client': '$instances.clients',
        }},
        {'$unwind': '$host_id'},
        {'$unwind': '$client'},
        {'$unwind': '$client'},
        {'$match': {
            'client.type': CERT_CLIENT,
        }},
        {'$group': {
            '_id': '$host_id',
            'clients': {'$addToSet': '$client.id'},
        }},
    ])['result']

    hosts_clients = {}
    for doc in response:
        hosts_clients[doc['_id']] = doc['clients']

    org_user_count = organization.get_user_count()

    response = server_collection.aggregate([
        {'$project': {
            'hosts': True,
            'organizations': True,
        }},
        {'$unwind': '$hosts'},
        {'$unwind': '$organizations'},
        {'$group': {
            '_id': '$hosts',
            'organizations': {'$addToSet': '$organizations'},
        }},
    ])['result']

    host_orgs = collections.defaultdict(list)
    for doc in response:
        host_orgs[doc['_id']] = doc['organizations']

    for doc in Host.collection.find().sort('name'):
        hst = Host(doc=doc)

        users_online = len(hosts_clients.get(hst.id, ''))

        user_count = 0
        for org_id in host_orgs[hst.id]:
            user_count += org_user_count.get(org_id, 0)

        hst.user_count = user_count
        hst.users_online = users_online

        yield hst.dict()
Esempio n. 3
0
def init():
    settings.local.host = Host()

    try:
        settings.local.host.load()
    except NotFound:
        pass

    settings.local.host.status = ONLINE
    settings.local.host.users_online = 0
    settings.local.host.start_timestamp = utils.now()
    settings.local.host.ping_timestamp = utils.now()
    if settings.local.public_ip:
        settings.local.host.auto_public_address = settings.local.public_ip

    if settings.conf.local_address_interface == 'auto':
        try:
            settings.local.host.local_address = socket.gethostbyname(
                socket.gethostname())
        except:
            logger.exception('Failed to get local_address auto', 'host')
            settings.local.host.local_address = None
    else:
        try:
            settings.local.host.local_address = utils.get_interface_address(
                str(settings.conf.local_address_interface))
        except:
            logger.exception('Failed to get local_address',
                             'host',
                             interface=settings.conf.local_address_interface)
            settings.local.host.local_address = None

    settings.local.host.commit()
    event.Event(type=HOSTS_UPDATED)
Esempio n. 4
0
def init_host():
    settings.local.host = Host()

    try:
        settings.local.host.load()
    except NotFound:
        pass

    settings.local.host.status = ONLINE
    settings.local.host.users_online = 0
    settings.local.host.start_timestamp = datetime.datetime.utcnow()
    if settings.local.public_ip:
        settings.local.host.auto_public_address = settings.local.public_ip

    settings.local.host.commit()
    event.Event(type=HOSTS_UPDATED)
Esempio n. 5
0
def iter_hosts(spec=None, fields=None, page=None):
    limit = None
    skip = None
    page_count = settings.app.host_page_count

    if spec is None:
        spec = {}

    if fields:
        fields = {key: True for key in fields}

    if page is not None:
        limit = page_count
        skip = page * page_count if page else 0

    cursor = Host.collection.find(spec, fields).sort('name')

    if skip is not None:
        cursor = cursor.skip(page * page_count if page else 0)
    if limit is not None:
        cursor = cursor.limit(limit)

    for doc in cursor:
        yield Host(doc=doc, fields=fields)
Esempio n. 6
0
def iter_hosts():
    for doc in Host.collection.find().sort('name'):
        yield Host(doc=doc)
Esempio n. 7
0
def get_host(id):
    return Host(id=id)
Esempio n. 8
0
def get_by_id(id, fields=None):
    return Host(id=id, fields=fields)
Esempio n. 9
0
def iter_hosts(spec=None, fields=None):
    if fields:
        fields = {key: True for key in fields}

    for doc in Host.collection.find(spec or {}, fields).sort('name'):
        yield Host(doc=doc, fields=fields)
Esempio n. 10
0
def init():
    if not settings.local.host_id:
        raise ValueError('Host ID undefined')

    settings.local.host = Host(id=settings.local.host_id)

    try:
        settings.local.host.load()
    except NotFound:
        pass

    settings.local.host.status = ONLINE
    settings.local.host.users_online = 0
    settings.local.host.start_timestamp = utils.now()
    settings.local.host.ping_timestamp = utils.now()
    if settings.local.public_ip:
        settings.local.host.auto_public_address = settings.local.public_ip
    if settings.local.public_ip6:
        settings.local.host.auto_public_address6 = settings.local.public_ip6

    try:
        settings.local.host.hostname = socket.gethostname()
    except:
        logger.exception('Failed to get hostname', 'host')
        settings.local.host.hostname = None

    if settings.conf.local_address_interface == 'auto':
        try:
            settings.local.host.auto_local_address = utils.get_local_address()
        except:
            logger.exception('Failed to get auto_local_address', 'host')
            settings.local.host.local_address = None

        try:
            settings.local.host.auto_local_address6 = \
                utils.get_local_address6()
        except:
            logger.exception('Failed to get auto_local_address6', 'host')
            settings.local.host.local_address6 = None
    else:
        try:
            settings.local.host.auto_local_address = \
                utils.get_interface_address(
                    str(settings.conf.local_address_interface))
        except:
            logger.exception('Failed to get auto_local_address', 'host',
                interface=settings.conf.local_address_interface)
            settings.local.host.auto_local_address = None

        try:
            settings.local.host.auto_local_address6 = \
                utils.get_interface_address6(
                    str(settings.conf.local_address_interface))
        except:
            logger.exception('Failed to get auto_local_address6', 'host',
                interface=settings.conf.local_address_interface)
            settings.local.host.auto_local_address6 = None

    settings.local.host.auto_instance_id = utils.get_instance_id()
    settings.local.host.local_networks = utils.get_local_networks()

    settings.local.host.commit()
    event.Event(type=HOSTS_UPDATED)