示例#1
0
 def __init__(self, operator=None):
     self.operator = operator or BaseScene()
def clear_nouse_resource():
    logger.info('clear not use resources start')

    operator = BaseScene()
    # 清除场景相关的资源
    try:
        vms = operator.list_server(prefix=app_settings.BASE_GROUP_NAME)
        _clear_resources(
            [vm.id for vm in vms],
            cloud.vm.delete,
            [
                (SceneTerminal.objects.filter(scene__status__in=using_status, image_type=SceneTerminal.ImageType.VM),
                 'server_id'),
            ],
            'clear not use vm resources: not_use_vm_ids - %s'
        )
    except Exception as e:
        logger.error('clear not use vm resources error: %s' % str(e))

    try:
        dockers = operator.list_container(prefix=app_settings.BASE_GROUP_NAME)
        _clear_resources(
            [docker.id for docker in dockers],
            cloud.docker.delete,
            [
                (
                    SceneTerminal.objects.filter(
                        scene__status__in=using_status,
                        image_type=SceneTerminal.ImageType.DOCKER
                    ),
                    'server_id',
                ),
            ],
            'clear not use docker resources: not_use_docker_ids - %s'
        )
    except Exception as e:
        logger.error('clear not use docker resources error: %s' % str(e))

    try:
        routers = operator.list_router(prefix=app_settings.BASE_GROUP_NAME)

        _clear_resources(
            [router['id'] for router in routers],
            cloud.router.delete,
            [
                (SceneGateway.objects.filter(scene__status__in=using_status), 'router_id'),
            ],
            'clear not use router resources: not_use_router_ids - %s'
        )
    except Exception as e:
        logger.error('clear not use router resources error: %s' % str(e))

    try:
        firewalls = operator.list_firewall(prefix=app_settings.BASE_GROUP_NAME)
        _clear_resources(
            [firewall['id'] for firewall in firewalls],
            cloud.firewall.delete,
            [
                (SceneGateway.objects.filter(scene__status__in=using_status), 'firewall_id'),
            ],
            'clear not use firewall resources: not_use_firewall_ids - %s'
        )
    except Exception as e:
        logger.error('clear not use firewall resources error: %s' % str(e))

    try:
        networks = operator.list_network(prefix=app_settings.BASE_GROUP_NAME)
        _clear_resources(
            [network['id'] for network in networks],
            cloud.network.delete,
            [
                (SceneNet.objects.filter(scene__status__in=using_status), 'net_id'),
            ],
            'clear not use network resources: not_use_network_ids - %s'
        )
    except Exception as e:
        logger.error('clear not use network resources error: %s' % str(e))

    # 清除错误资源
    try:
        error_vms = operator.list_server(prefix=app_settings.BASE_GROUP_NAME,
                                         search_opts={'status': 'ERROR'})
        error_vm_ids = [error_vm.id for error_vm in error_vms]
        if error_vm_ids:
            logger.info('clear error resources: error_vm_ids - %s', error_vm_ids)
            for vm_id in error_vm_ids:
                cloud.vm.delete(vm_id)
    except Exception as e:
        logger.error('clear error vm resources error: %s' % str(e))

    try:
        dockers = operator.list_container(prefix=app_settings.BASE_GROUP_NAME)
        error_docker_ids = [docker.id for docker in dockers if docker.status.lower() == 'error']
        if error_docker_ids:
            logger.info('clear error resources: error_docker_ids - %s', error_docker_ids)
            for docker_id in error_docker_ids:
                cloud.docker.delete(docker_id)
    except Exception as e:
        logger.error('clear error docker resources error: %s' % str(e))

    logger.info('clear not use resources end')