コード例 #1
0
ファイル: cluster.py プロジェクト: yasuolumia/bk-bcs-saas
def _polling_once(model, log):
    log = model.objects.filter(pk=log.pk).last()
    if not log:
        logger.error("log not found for pk: %s", log.pk)
        return log

    client = BCSClient(
        log.token, log.project_id, log.cluster_id, None
    )
    rsp = client.get_task_result(log.task_id)
    if rsp.get("code") != ErrorCode.NoError:
        if str(rsp.get("code")) == "5000":
            log.is_polling = False
            log.is_finished = True
            log.status = models.CommonStatus.InitialFailed
            log.save()
        return log

    data = rsp.get("data") or {}
    status = data.get("status", "UNKNOWN")
    gcloud_steps = data.get("steps") or {}
    steps = []
    failed = False
    for k, v in gcloud_steps.items():
        index = STEP_INDEX_REGEX.findall(k)
        if index:
            index = index[0]
        else:
            index = 0
        if v.get("state") in ["FAILURE", "REVOKED"]:
            failed = True
        steps.append((int(index), {
            "state": v.get("state"),
            "name": v.get("stage_name"),
        }))
    steps.sort()

    if status == "SUCCESS":
        log.status = "normal"
        log.is_finished = True
    elif status == "BLOCKED" and failed:
        if log.oper_type in [models.NodeOperType.NodeRemove, models.CommonStatus.Removing]:
            log.status = "remove_failed"
        else:
            log.status = "initial_failed"
        log.is_finished = True
    else:
        if log.oper_type == [models.NodeOperType.NodeRemove, models.CommonStatus.Removing]:
            log.status = "removing"
        else:
            log.status = "initializing"

    log.log = json.dumps({
        "state": status,
        "node_tasks": [
            i[1] for i in steps
        ],
    })
    log.save()
    return log
コード例 #2
0
def _get_event(access_token: str, project_id: str, env: str, query) -> EventComponentData:
    client = BCSClient(access_token, project_id, None, env)
    resp = client.get_events(query)
    result = resp.get('code') == ErrorCode.NoError
    message = resp.get('message', '')
    data = resp.get('data') or []
    count = int(resp.get('total') or 0)
    return EventComponentData(result, data, message, count)
コード例 #3
0
ファイル: cluster.py プロジェクト: yasuolumia/bk-bcs-saas
def node_exec_bcs_task(old_log, request=None):
    """执行bcs创建节点任务
    """
    if not old_log:
        return
    log_pk, log_type = old_log
    model = models.log_factory(log_type)
    if not model:
        logger.error("log not found for type: %s", log_type)
        return
    log = model.objects.filter(pk=log_pk).last()
    if not log:
        logger.error("log not found for pk: %s", log_pk)

    # 解析参数
    try:
        params = json.loads(log.params)
    except Exception:
        params = {}
    node_info = params.get("node_info") or {}
    user_token = log.token
    username = log.operator
    project_id = log.project_id
    cluster_id = log.cluster_id
    if request:
        user_token = request.user.token.access_token
        username = request.user.username
    new_log = models.NodeUpdateLog.objects.create(  # noqa
        project_id=project_id,
        cluster_id=cluster_id,
        token=user_token,
        node_id=",".join(node_info.values()),
        params=json.dumps(params),
        operator=username,
    )
    try:
        client = BCSClient(
            user_token, project_id, cluster_id, None
        )
        rsp = client.add_cluster_node(
            params.get("kind_name"), username,
            list(node_info.keys()), params.get("cc_app_id")
        )
    except Exception as error:
        logger.error("add add_cluster_node error: %s", error)
        node_ip_status(new_log, new_log.project_id, new_log.cluster_id, node_info)
        return

    if rsp.get("code") != ErrorCode.NoError:
        node_ip_status(new_log, new_log.project_id, new_log.cluster_id, node_info)
        push_sentry(new_log, _("节点初始化失败"))
        return

    data = rsp.get("data") or {}
    taskid = data.get("taskID")
    new_log.task_id = taskid
    new_log.save()
    return new_log.id
コード例 #4
0
 def query_events(self, request, project_id, cluster_id, params):
     """查询事件
     """
     client = BCSClient(request.user.token.access_token, project_id,
                        cluster_id, None)
     resp = client.get_events(params)
     if resp.get("code") != ErrorCode.NoError:
         return APIResponse({
             "code": resp.get("code", DEFAULT_ERROR_CODE),
             "message": resp.get("message", u"请求出现异常!")
         })
     return APIResponse({
         "data": {
             "data":
             resp.get("data", []),
             "total":
             100 if resp.get("total", 0) > 100 else resp.get("total", 0)
         }
     })
コード例 #5
0
ファイル: cluster.py プロジェクト: yasuolumia/bk-bcs-saas
def delete_cluster_node(new_log):
    log_type, log_id = new_log
    if not (log_type and log_id):
        return log_type, log_id
    model = models.log_factory(log_type)
    log = model.objects.filter(id=log_id).last()
    params = json.loads(log.params)
    # 触发bcs任务
    model.objects.filter(id=log.id)
    bcs_client = BCSClient(
        log.token, log.project_id, log.cluster_id, None
    )
    resp = bcs_client.delete_cluster_node(
        params.get("kind_name"), log.operator, list(params.get("nodes", {}).keys())
    )
    if not resp.get("result"):
        log.is_finished = True
        log.is_polling = False
        log.status = models.CommonStatus.RemoveFailed
        log.log = json.dumps({
            "state": "remove_failed",
            "node_tasks": [{
                "state": "FAILURE",
                "name": resp.get("message")
            }]
        })
        log.save()
        result = paas_cc.update_node(
            log.token, log.project_id, params["node_id"],
            {"status": models.CommonStatus.RemoveFailed}
        )
        if result.get("code") != ErrorCode.NoError:
            return None, None
        return None, None

    data = resp.get("data") or {}
    taskid = data.get("taskID")
    log.task_id = taskid
    log.is_polling = True
    log.save()
    return log_type, log.id
コード例 #6
0
ファイル: cluster.py プロジェクト: yasuolumia/bk-bcs-saas
def exec_bcs_task(old_log, request=None):
    """执行bcs创建集群任务
    """
    if not old_log:
        return
    # 判断是否可以执行后续
    log_pk, log_type = old_log
    model = models.log_factory(log_type)
    if not model:
        logger.error("log not found for type: %s", log_type)
        return
    log = model.objects.filter(pk=log_pk).last()
    if not log:
        logger.error("log not found for pk: %s", log_pk)
        return

    # 组装参数
    user_token = log.token
    username = log.operator
    if request:
        user_token = request.user.token.access_token
        username = request.user.username
    # 解析参数
    try:
        params = json.loads(log.params)
    except Exception:
        params = {}
    new_log = models.ClusterInstallLog.objects.create(
        project_id=log.project_id,
        cluster_id=log.cluster_id,
        token=user_token,
        status=models.CommonStatus.Initializing,
        params=log.params,
        operator=username
    )
    client = BCSClient(
        user_token, params.get("project_id"),
        params.get("cluster_id"), None
    )
    rsp = client.create_cluster(
        params.get("kind_name"), username,
        params.get("master_ips", []),
        data={
            "modules": params.get("module_list", ""),
            "appID": constants.BCS_APP_ID,
            "needNat": params.get("need_nat", True),
        }
    )
    if rsp.get("code") != ErrorCode.NoError:
        new_log.is_finished = True
        new_log.is_polling = False
        new_log.status = models.CommonStatus.InitialFailed
        # 记录错误信息到log
        new_log.log = json.dumps({
            "state": "FAILURE",
            "node_tasks": [{"state": "FAILURE", "name": rsp.get("message")}]
        })
        new_log.save()
        result = paas_cc.update_cluster(
            user_token, params.get("project_id"),
            params.get("cluster_id"),
            {"status": models.CommonStatus.InitialFailed}
        )
        # TODO: 怎样保证写入不成功时,可以再次写入
        if result.get("code") != ErrorCode.NoError:
            return
        push_sentry(new_log, _("初始化集群失败"))
        return

    data = rsp.get("data") or {}
    new_log.task_id = data.get("taskID")
    new_log.save()
    try:
        cc.host_standard_property(
            username, params.get("master_ips", []), bak_operator_flag=True
        )
    except Exception as err:
        logger.error("Request cc error, detail: %s" % err)
    # 触发新的任务
    return new_log.id