Beispiel #1
0
 def __init__(self, operator=None):
     self.operator = operator or BaseScene()
Beispiel #2
0
def clear_not_use_resources():
    logger.info('clear not use resources start')
    # 清除场景相关的资源
    try:
        from common_env.handlers.local_lib import scene
        from common_env.models import Env, EnvTerminal, EnvNet, EnvGateway, StandardDeviceEditServer
        from common_env.setting import api_settings as common_env_settings
        from common_scene.setting import api_settings as common_scene_settings
        from common_scene.complex.views import BaseScene
        from common_scene.clients.neutron_client import Client as NeutronClient

        operator = BaseScene()

        using_status = Env.ActiveStatusList

        vms = operator.list_server(prefix=common_env_settings.BASE_GROUP_NAME)
        dockers = operator.list_container(
            prefix=common_env_settings.BASE_GROUP_NAME)
        routers = operator.list_router(
            prefix=common_env_settings.BASE_GROUP_NAME)
        firewalls = operator.list_firewall(
            prefix=common_env_settings.BASE_GROUP_NAME)
        networks = operator.list_network(
            prefix=common_env_settings.BASE_GROUP_NAME)

        _clear_resources([vm.id for vm in vms], scene.vm.delete, [
            (EnvTerminal.objects.filter(
                env__status__in=using_status,
                image_type=EnvTerminal.ImageType.VM), 'vm_id'),
            (StandardDeviceEditServer.objects.exclude(
                status=StandardDeviceEditServer.Status.DELETED), 'tmp_vm_id')
        ], 'clear not use env resources: not_use_vm_ids - %s')
        _clear_resources(
            [docker.id for docker in dockers], scene.docker.delete,
            [(EnvTerminal.objects.filter(
                env__status__in=using_status,
                image_type=EnvTerminal.ImageType.DOCKER), 'vm_id'),
             (StandardDeviceEditServer.objects.exclude(
                 status=StandardDeviceEditServer.Status.DELETED),
              'tmp_docker_id')],
            'clear not use env resources: not_use_docker_ids - %s')
        _clear_resources(
            [router['id'] for router in routers], scene.router.delete,
            [(EnvGateway.objects.filter(env__status__in=using_status),
              'router_id'),
             (StandardDeviceEditServer.objects.exclude(
                 status=StandardDeviceEditServer.Status.DELETED),
              'tmp_router_id')],
            'clear not use env resources: not_use_router_ids - %s')
        _clear_resources(
            [firewall['id'] for firewall in firewalls], scene.firewall.delete,
            [
                (EnvGateway.objects.filter(env__status__in=using_status),
                 'firewall_id'),
            ], 'clear not use env resources: not_use_firewall_ids - %s')
        _clear_resources(
            [network['id'] for network in networks], scene.network.delete,
            [(EnvNet.objects.filter(env__status__in=using_status), 'net_id'),
             (StandardDeviceEditServer.objects.exclude(
                 status=StandardDeviceEditServer.Status.DELETED),
              'tmp_network_id')],
            'clear not use env resources: not_use_network_ids - %s')

        operator.clear_unused_port()

        logger.info('clear not use env resources ok')
    except Exception as e:
        logger.error('clear not use env resources error: %s' % str(e))

    # 清除攻防赛相关的资源, 攻防赛机器使用的是场景的, 只需清理路由和网络
    try:
        from event_attack_defense.models import AttackDefenseEvent
        from event_attack_defense.setting import api_settings as event_ad_settings

        routers = operator.list_router(
            prefix=event_ad_settings.BASE_GROUP_NAME)
        networks = operator.list_network(
            prefix=event_ad_settings.BASE_GROUP_NAME)

        _clear_resources(
            [router['id'] for router in routers], scene.router.delete, [
                (AttackDefenseEvent.objects.all(), 'global_router'),
            ], 'clear not use ad event resources: not_use_router_ids - %s')

        _clear_resources(
            [network['id'] for network in networks], scene.network.delete, [
                (AttackDefenseEvent.objects.all(), 'global_network'),
            ], 'clear not use ad event resources: not_use_network_ids - %s')

        logger.info('clear not use ad event resources ok')

    except Exception as e:
        logger.error('clear not use ad event resources error: %s' % str(e))

    # 清除错误资源
    try:
        error_vms = operator.list_server(
            prefix=common_env_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:
                scene.vm.delete(vm_id)

        dockers = operator.list_container(
            prefix=common_env_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:
                scene.docker.delete(docker_id)
    except Exception as e:
        logger.error('clear error resources error: %s' % str(e))

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

    # 一天执行一次
    from common_framework.utils import delay_task
    delay_task.new_task(clear_not_use_resources, 60 * 60 * 24, ())