示例#1
0
文件: views.py 项目: levvli/bk
def inquiry(request):
    try:
        biz_id = request.POST.get("biz_id")
        username = request.POST.get("username")
        script_id = request.POST.get("script_id")
        time = request.POST.get("time")
        doinfo = Doinfo.objects.all()
        doinfo = doinfo.filter(businessname=int(biz_id)).filter(
            username=username).filter(script_id=int(script_id))
        starttime, endtime = time.split("-")
        starttime = starttime.strip().replace("/", "-") + " 00:00:00"
        endtime = endtime.strip().replace("/", "-") + " 23:59:00"
        start_time = datetime.strptime(starttime, "%Y-%m-%d  %H:%M:%S")
        end_time = datetime.strptime(endtime, "%Y-%m-%d  %H:%M:%S")
        doinfo = doinfo.filter(starttime__range=(start_time, end_time))
        data = [info.to_dict() for info in doinfo]
        table_data = render_to_string("home_application/record_tbody.html",
                                      {"doinfos": data})
        result = True
        message = "success"
    except Exception as e:
        table_data = []
        result = False
        message = str(e)
    return JsonResponse({
        "result": result,
        "message": message,
        "data": table_data
    })
示例#2
0
文件: views.py 项目: levvli/bk
def execute_script(request):
    biz_id = request.POST.get("biz_id")
    script_id = request.POST.get("script_id")
    obj = SelectScript.objects.get(id=script_id)
    objtest = base64.b64encode(obj.scriptcontent.encode("utf-8"))
    ip_id = request.POST.getlist("ip_id[]")
    ips = {"bk_cloud_id": 0, "ip": 0}
    ip_info = []
    for i in ip_id:
        ips['id'] = i
        ip_info.append(copy.deepcopy(ips))
    kwargs = {
        "bz_biz_id": biz_id,
        "script_content": str(objtest, "utf-8"),
        "account": "root",
        "script_type": 1,
        "ip_list": ip_info,
    }
    execute_data = client.job.fast_execute_script(kwargs)
    if execute_data.get("result", False):
        data = execute_data["data"]
        result = True
        message = str(execute_data.get("message"))
        async_status.apply_async(args=[client, data, biz_id, obj, ip_id],
                                 kwargs={})
    else:
        data = []
        result = False
        message = "False"
        logger.error(u"查询主机列表失败: %s" % execute_data.get("message"))
    return JsonResponse({"result": result, "message": message, "data": data})
示例#3
0
文件: views.py 项目: levvli/bk
def get_host(request):
    biz_id = request.GET.get("biz_id")
    if biz_id:
        biz_id = int(biz_id)
    else:
        return JsonResponse({
            "result": False,
            "message": "must provide biz_id to get hosts"
        })
    data = ser_host(biz_id)
    table_data = render_to_string("home_application/execute_tbody.html",
                                  {'data': data})
    return JsonResponse({
        "result": True,
        "message": "success",
        "data": table_data
    })
示例#4
0
 def get(self, req):
     advs = Advertise.objects.filter(is_used=True)
     data = [model_to_dict(i) for i in advs]
     print(data)
     return JsonResponse({
         "code": 1,
         "msg": "OK",
         "data": data,
         "hint": "领钱",
         "scan": "支付"
     })
示例#5
0
def check_master_dbstate(request):
    """
    检查主数据库状态
    """
    info = '必要参数缺失'
    code = 0
    user = request.POST.get('user', '')
    mip = request.POST.get('mip', '')
    password = request.POST.get('password', '')
    dbuser = request.POST.get('dbuser', '')
    dbpassword = request.POST.get('dbpassword', '')
    if user.strip() and mip.strip() and password.strip() and dbuser.strip(
    ) and dbpassword.strip():
        component_guid = '2e445090-a26c-11eb-bc9a-405bd8b00cd6'
        component_input = [{
            "code": "inputusername",
            "value": user
        }, {
            "code": "inputhost",
            "value": mip
        }, {
            "code": "dbusername",
            "value": dbuser
        }, {
            "code": "dbpassword",
            "value": dbpassword
        }]

        newJob = Job()
        state = newJob.execute_workflow(component_guid, input=component_input)

        if state == 'NOTEXIST':
            info = "组件不存在,请联系管理员。"
            code = 0
        elif state == 'ERROR':
            info = "ERROR"
            code = 0
        else:
            info = "数据库状态正常。"
            code = 1
    return JsonResponse({"info": info, "code": code})
示例#6
0
def task_status_ajax(request, task_id):
    """
    returns a JSON representation of the task state
    """
    if settings.DEBUG:
        # show results for task in debug mode
        valid_request = True
    else:
        valid_request = request.is_ajax()

    if valid_request:
        try:
            task = celery.AsyncResult(task_id)
            if task.state == TaskState.PENDING:
                response = {
                    "state": "pending",
                    "status_message": "try to start task"
                }

            elif task.state == TaskState.STARTED or task.state.lower(
            ) == TaskState.PROCESSING:
                response = {
                    "state": "processing",
                    "status_message": task.info.get("status_message", "")
                }

            elif task.state == TaskState.SUCCESS:
                response = {
                    "state": "success",
                    "status_message": task.info.get("status_message", "")
                }
                if "error_message" in task.info:
                    response["error_message"] = task.info["error_message"]

                if "data" in task.info:
                    response["data"] = task.info["data"]

            else:
                # something went wrong in the within the task
                response = {
                    "state": "failed",
                    "error_message":
                    str(task.info),  # this is the exception that was raised
                }

        except redis.ConnectionError:
            logger.error("cannot get task update", exc_info=True)
            response = {
                "state":
                "failed",
                "error_message":
                "A server process (redis) is not running, please contact the administrator"
            }

        except Exception:
            logger.error("cannot get task update", exc_info=True)
            response = {
                "state": "failed",
                "error_message": "Unknown error: " +
                str(task.info),  # this is the exception raised
            }
        logger.debug("task state for %s is\n%s" % (task_id, str(response)))

        return JsonResponse(response)

    else:
        return HttpResponse("Bad Request", status=400)