def hosts(self, request, project_id): """查询指定业务拓扑下主机列表""" params = self.params_validate(FetchCCHostSLZ) username = request.user.username access_token = request.user.token.access_token bk_biz_id = request.project.cc_app_id # 从 CMDB 获取可用主机信息,业务名称信息 try: host_list = utils.fetch_cc_app_hosts(username, bk_biz_id, params['set_id'], params['module_id']) except CompParseBkCommonResponseError as e: raise error_codes.ComponentError(str(e)) except BaseCompError as e: logger.error('获取主机信息失败:%s', e) raise error_codes.ComponentError(_('发生未知错误,请稍候再试')) cc_app_name = cc.get_application_name(bk_biz_id) # 根据指定的 IP 过滤 host_list = filter_by_ips(host_list, params['ip_list'], key='bk_host_innerip', fuzzy=params['fuzzy']) response_data = {'count': 0, 'results': [], 'cc_app_name': cc_app_name} # 补充节点使用情况,包含使用的项目 & 集群 project_cluster_info = utils.fetch_project_cluster_info(access_token) all_cluster_nodes = utils.fetch_all_cluster_nodes(access_token) host_list = utils.attach_project_cluster_info(host_list, all_cluster_nodes, project_cluster_info) # 更新 可选择的机器 数量信息 response_data['selectable_cnt'] = len( [h for h in host_list if utils.is_host_selectable(h)]) # 如没有符合过滤条件的,直接返回默认值 if not host_list: return Response(response_data) # 被使用 / agent 异常的机器均视为 不可使用 response_data['unavailable_ip_count'] = len( [h for h in host_list if h['is_used'] or not h['is_valid']]) # 支持获取全量数据(仅跨页全选时候使用) if params['desire_all_data']: ret = {'count': len(host_list), 'results': host_list} else: ret = custom_paginator(host_list, params['offset'], params['limit']) # 更新 Host 的 GSE Agent 状态信息 ret['results'] = utils.update_gse_agent_status(username, ret['results']) response_data['results'] = ret['results'] response_data['count'] = ret['count'] return Response(response_data)
def biz_inst_topo(self, request, project_id): """查询业务实例拓扑""" try: topo_info = cc.BizTopoQueryService( request.user.username, request.project.cc_app_id).fetch() except CompParseBkCommonResponseError as e: raise error_codes.ComponentError(_('查询业务拓扑信息失败:{}').format(e)) except BaseCompError as e: logger.error('查询业务拓扑信息失败:%s', e) raise error_codes.ComponentError(_('发生未知错误,请稍候再试')) return Response(data=topo_info)
def create_project_path_by_api(access_token, project_id, project_code): """调用仓库API创建项目仓库路径 { "result": true, "message": "success", "data": { "project_id": 6, "name": "test", "creation_time": "2018-12-25 16:13:10", "update_time": "2018-12-25 16:13:10", "repo_count": 2 }, "code": 0 } """ client = HarborClient(access_token, project_id, project_code) resp = client.create_project_path() # api调用失败 if resp.get("code") != 0: error_message = "%s, %s" % ( error_codes.DepotError.f(_("创建项目仓库路径失败"), replace=True), resp.get("message", ""), ) logger.error(error_message) raise error_codes.ComponentError(error_message) return True
def create_ns_by_bcs(self, client, name, data): ns_config = {"apiVersion": "v1", "kind": "Namespace", "metadata": {"name": name}} result = client.create_namespace(ns_config) # 通过错误消息判断 Namespace 是否已经存在,已经存在则直接进行下一步 res_msg = result.get('message') or '' is_already_exists = res_msg.endswith("already exists") if result.get('code') != 0 and not is_already_exists: raise error_codes.ComponentError(_("创建Namespace失败,{}").format(result.get('message')))
def handler_update_application(self, ns, cluster_id, spec): client = mesos.MesosClient(self.access_token, self.project_id, cluster_id, env=None) result = client.update_application(cluster_id, ns, spec) if result.get('code') != 0: raise error_codes.ComponentError( _("更新application失败,{}").format(result.get('message')))
def handler_update_service(self, ns, cluster_id, spec): """""" client = mesos.MesosClient(self.access_token, self.project_id, cluster_id, env=None) result = client.update_service(ns, spec) if result.get('code') != 0: raise error_codes.ComponentError( _("更新service失败,{}".format(result.get('message'))))
def list(self, request): """查询用户有权限的项目列表""" resp = list_auth_projects(request.user.token.access_token, request.user.username) if resp.get('code') != ErrorCode.NoError: logger.error('list_auth_projects error: %s', resp.get('message')) raise error_codes.ComponentError('list auth projects error') projects = resp['data'] if not projects: return Response([]) projects.sort(key=operator.itemgetter('created_at'), reverse=True) return Response(projects)
def get_jfrog_account(access_token, project_code, project_id, is_bk=False): """ 获取项目的镜像账号 """ client = HarborClient(access_token, project_id, project_code) resp = client.create_account() # api调用失败 if resp.get('code') != 0: error_message = ('%s, %s' % (bk_error_codes.DepotError("创建项目仓库账号失败"), resp.get('message', ''))) logger.error(error_message) raise error_codes.ComponentError(error_message) return resp.get('data')
def get_jfrog_account(access_token, project_code, project_id, is_bk=False): """ 获取项目的镜像账号 """ client = HarborClient(access_token, project_id, project_code) resp = client.create_account() # api调用失败 if resp.get('code') != 0: message = bk_error_codes.DepotError(_("创建项目仓库账号失败")) error_message = f'{message}, {resp.get("message", "")}' logger.error(error_message) raise error_codes.ComponentError(error_message) return resp.get('data')
def create_ns_by_bcs(self, client, name, data, project_code): # 注解中添加上标识projectcode的信息,用于查询当前项目下,共享集群中的命名空间 ns_config = { "apiVersion": "v1", "kind": "Namespace", "metadata": { "name": name, "annotations": { PROJ_CODE_ANNO_KEY: project_code } }, } result = client.create_namespace(ns_config) # 通过错误消息判断 Namespace 是否已经存在,已经存在则直接进行下一步 res_msg = result.get('message') or '' is_already_exists = res_msg.endswith("already exists") if result.get('code') != 0 and not is_already_exists: raise error_codes.ComponentError( _("创建Namespace失败,{}").format(result.get('message')))
def run_with_helm(self, operation): err_msg = "" with bcs_client.make_helm_client( project_id=self.helm_args.project_id, cluster_id=self.helm_args.cluster_id, access_token=self.access_token, ) as (client, err): if err is not None: err_msg = f"make helm client failed: {err}" if not err_msg: try: self._run_with_helm(client, operation) except Exception as e: err_msg = f"helm {operation} failed: {e}" if err_msg: raise error_codes.ComponentError(err_msg)
def _run_with_kubectl(self, operation, namespace, manifests): err_msg = "" with make_kubectl_client( project_id=self.project_id, cluster_id=self.cluster_id, access_token=self.access_token ) as (client, err): if err is not None: if isinstance(err, ApiException): err = f"Code: {err.status}, Reason: {err.reason}" err_msg = f"make client failed: {err}" if not err_msg: try: if operation == "apply": client.ensure_namespace(namespace) client.apply(manifests, namespace) elif operation == "delete": client.ensure_namespace(namespace) client.delete(manifests, namespace) except Exception as e: err_msg = f"client {operation} failed: {e}" if err_msg: raise error_codes.ComponentError(err_msg)