Пример #1
0
def action_sync():
    engine = create_engine(kc.get_config('db', 'database_uri'))
    Base.metadata.bind = engine
    DBSession = sessionmaker(bind=engine)
    session = DBSession()
    for region in regions:
        nc = Nova(options.config, debug=options.debug, log=logger, region=region)
        net = Neutron(options.config, debug=options.debug, log=logger, region=region)
        network_list = list()
        for network in net.list_networks():
            network_list.append(network['name'])
        instances = nc.get_all_instances()
        for i in instances:
            owner = dict()
            # Get IP address
            if not i.addresses:
                continue
            for network in network_list:
                if str(network) not in i.addresses:
                    continue
                for addr in i.addresses[str(network)]:
                    if addr['version'] == 4:
                        owner['ip'] = addr['addr']
            # Get project, admin and org
            project = kc.get_by_id('project', i.tenant_id)
            if project:
                owner['project_name'] = project.name
                owner['admin'] = project.admin if hasattr(project, 'admin') else None
                domain = project.admin.split("@")[1] if hasattr(project, 'admin') else ''
                if len(domain.split(".")) > 1:
                    org = domain.split(".")[-2]
                else:
                    org = 'unknown'
                owner['organization'] = org
            else:
                owner['project_name'] = 'unknown'
                owner['organization'] = 'unknown'
            # Get user and instance_id
            user = kc.get_by_id('user', i.user_id)
            owner['user'] = user.name.lower() if user else None
            owner['instance_id'] = i.id
            # Update owner
            old = session.query(Owner).filter(Owner.ip == owner['ip']).first()
            if old is not None:
                logger.debug('=> update owner for ip %s', owner['ip'])
                old.update(owner)
            else:
                logger.debug('=> create owner for ip %s', owner['ip'])
                session.add(Owner(**owner))
            session.commit()
    session.close()
Пример #2
0
def action_purge():
    engine = create_engine(kc.get_config('db', 'database_uri'))
    Base.metadata.bind = engine
    DBSession = sessionmaker(bind=engine)
    session = DBSession()
    for region in regions:
        nc = Nova(options.config, debug=options.debug, log=logger, region=region)
        instances = nc.get_all_instances({'all_tenants': 1, 'deleted': 1})
        for i in instances:
            old = session.query(Owner).filter(Owner.instance_id == i.id).first()
            if old is not None:
                logger.debug('=> purge owner for instance %s', old.ip)
                session.delete(old)
                session.commit()
    session.close()
Пример #3
0
def image_usage(detailed=False):
    status = 'deactivated' if options.deactive else 'active'
    novaclient = Nova(options.config, debug=options.debug, log=logger, region=options.region)
    filters = {'status': status, 'visibility': options.visibility, 'tag': tags}
    logger.debug('=> filter: %s' % filters)
    image_usage = dict()
    images = glclient.get_images(limit=1000, page_size=999, filters=filters)
    for image in images:
        image_usage[image.id] = image
        image_usage[image.id]['count'] = 0
        search_opts = {'image': image.id}
        instances = novaclient.get_all_instances(search_opts=search_opts)
        if detailed:
            image_usage[image.id]['instances'] = list()
        for i in instances:
            image_usage[image.id]['count'] += 1
            if detailed:
                image_usage[image.id]['instances'].append(i.id)
    return image_usage
Пример #4
0
def image_usage(detailed=False):
    status = 'deactivated' if options.deactive else 'active'
    novaclient = Nova(options.config,
                      debug=options.debug,
                      log=logger,
                      region=options.region)
    filters = {'status': status, 'visibility': options.visibility, 'tag': tags}
    logger.debug('=> filter: %s' % filters)
    image_usage = dict()
    images = glclient.get_images(limit=1000, page_size=999, filters=filters)
    for image in images:
        image_usage[image.id] = image
        image_usage[image.id]['count'] = 0
        search_opts = {'image': image.id}
        instances = novaclient.get_all_instances(search_opts=search_opts)
        if detailed:
            image_usage[image.id]['instances'] = list()
        for i in instances:
            image_usage[image.id]['count'] += 1
            if detailed:
                image_usage[image.id]['instances'].append(i.id)
    return image_usage