Пример #1
0
def create_share_image_task(image_info, user_info):
    """
    创建共享镜像任务
    :param image_info: 镜像信息
    :param user_info: 用户信息
    :return:
    """
    image_name = image_info.image_name
    user_id = user_info.id
    setting_config = get_setting_config()
    args = {
        "share_username": setting_config["share_username"],
        "image_name": image_name,
        "username": setting_config["username"],
        "pwd": setting_config["pwd"]
    }
    task_info = TaskInfo(task_name="共享镜像:" + image_name,
                         user_id=user_id,
                         task_status=1,
                         task_msg=json.dumps({}),
                         task_start_date=timezone.now(),
                         operation_type=5,
                         operation_args=json.dumps(args),
                         create_date=timezone.now(),
                         update_date=timezone.now())
    task_info.save()
    return str(task_info.task_id)
Пример #2
0
def create_container_task(container_vul, user_info, request_ip):
    """
    创建漏洞容器
    :param container_vul: container vul
    :param user_info: user info
    :param request_ip: request ip
    :return:
    """
    image_info = container_vul.image_id
    user_id = user_info.id
    task_id = create_run_container_task(container_vul, user_info)
    if user_info.is_superuser or user_id == container_vul.user_id:
        operation_args = ImageInfoSerializer(image_info).data
        sys_log = SysLog(user_id=user_id,
                         operation_type="容器",
                         operation_name="启动",
                         ip=request_ip,
                         operation_value=operation_args["image_vul_name"],
                         operation_args=json.dumps(operation_args))
        sys_log.save()
        setting_config = get_setting_config()
        try:
            countdown = int(setting_config["time"])
        except:
            countdown = int(DEFAULT_CONFIG["time"])
        if countdown == 0:
            run_container.delay(container_vul.container_id, user_id, task_id)
        elif countdown != 0 and countdown > 60:
            # run_container(container_vul.container_id, user_id, task_id, countdown)
            add_chain_sig = chain(
                run_container.s(container_vul.container_id, user_id, task_id,
                                countdown)
                | stop_container.s().set(countdown=countdown))
            add_chain_sig.apply_async()
        else:
            task_info = TaskInfo.objects.filter(task_id=task_id).first()
            task_info.task_msg = json.dumps(R.build(msg="停止时间最小为 1 分钟"))
            task_info.task_status = 4
            task_info.update_date = timezone.now()
            task_info.save()
    else:
        task_info = TaskInfo.objects.filter(task_id=task_id).first()
        task_info.task_msg = json.dumps(R.build(msg="权限不足"))
        task_info.task_status = 3
        task_info.update_date = timezone.now()
        task_info.save()
    return task_id