Beispiel #1
0
 def update_cluster_status(self, status=CommonStatus.Initializing):
     cluster_info = paas_cc.update_cluster(self.access_token,
                                           self.project_id, self.cluster_id,
                                           {'status': status})
     if cluster_info.get('code') != ErrorCode.NoError:
         raise error_codes.APIError(cluster_info.get('message'))
     return cluster_info.get('data', {})
Beispiel #2
0
def update_node_cluster_check_status(log, log_type, status=models.CommonStatus.InitialCheckFailed):
    """更新master or slave失败状态
    """
    if log_type == "ClusterInstallLog":
        result = paas_cc.update_cluster(
            log.token, log.project_id, log.cluster_id,
            {"status": status},
        )
    elif log_type == "NodeUpdateLog":
        try:
            params = json.loads(log.params)
        except Exception:
            params = {}
        result = paas_cc.update_node_list(
            log.token, log.project_id, log.cluster_id,
            [
                {
                    "inner_ip": ip,
                    "status": status
                }
                for ip in params.get("node_info").keys()
            ]
        )
    else:
        return
    if result.get("code") != ErrorCode.NoError:
        logger.error(u"更新配置中心状态失败,详情: %s" % result.get("message"))
Beispiel #3
0
def update_cluster_status(log):
    if log.oper_type == models.ClusterOperType.ClusterRemove:
        if log.status == models.CommonStatus.Normal:
            status = models.CommonStatus.Removed
        else:
            status = log.status
    else:
        status = log.status
    for i in range(2):
        token_dict = ssm.get_client_access_token()
        resp = paas_cc.update_cluster(token_dict["access_token"], log.project_id, log.cluster_id, {"status": status})
        if resp.get("code") != ErrorCode.NoError:
            logger.error("Update cluster[%s] status failed, detail: %s" % (log.cluster_id, resp.get("message")))
        else:
            logger.info("Update cluster[%s] success" % log.cluster_id)
            break
    if log.oper_type in [models.ClusterOperType.ClusterInstall, models.NodeOperType.NodeInstall]:
        return
    if log.status not in [models.CommonStatus.Normal]:
        return
    for i in range(2):
        # 删除集群及master
        token_dict = ssm.get_client_access_token()
        resp = paas_cc.delete_cluster(token_dict["access_token"], log.project_id, log.cluster_id)
        if resp.get("code") != ErrorCode.NoError:
            logger.error("Delete cluster[%s] failed, detail: %s" % (log.cluster_id, resp.get("message")))
        else:
            logger.info("Delete cluster[%s] success" % log.cluster_id)
            break
    # 删除iam集群
    delete_iam_cluster_resource(log)
Beispiel #4
0
def update_cluster_status(access_token, project_id, cluster_id, status):
    """更新集群状态"""
    data = {"status": status}
    resp = paas_cc.update_cluster(access_token, project_id, cluster_id, data)
    if resp.get("code") != ErrorCode.NoError:
        raise error_codes.APIError(_("更新集群状态失败,{}").format(resp.get("message")))
    return resp.get("data") or {}
Beispiel #5
0
def update_cluster_status(log, status=models.CommonStatus.RemoveFailed):
    """更新集群状态"""
    resp = paas_cc.update_cluster(log.token, log.project_id, log.cluster_id,
                                  {"status": status})
    if resp.get("code") != ErrorCode.NoError:
        log.status = models.CommonStatus.RemoveFailed
        save_record_log(log, "FAILURE", _("更新集群状态"), resp.get("message"),
                        models.CommonStatus.RemoveFailed)
        log.save()
Beispiel #6
0
 def update_cluster(self, request, project_id, cluster_id, data):
     result = paas_cc.update_cluster(request.user.token.access_token,
                                     project_id, cluster_id, data)
     if result.get('code') != ErrorCode.NoError:
         raise error_codes.APIError(result.get('message'))
     return result.get('data') or {}
Beispiel #7
0
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