def get_bcs_context(access_token, project_id): # 获取项目相关的信息 context = {} project = paas_cc.get_project(access_token, project_id) if project.get("code") != 0: raise ValidationError("{}(project_id:{}):{}".format(_("查询项目信息出错"), project_id, project.get("message"))) project = project.get("data") or {} context["SYS_CC_APP_ID"] = project.get("cc_app_id") # TODO 以下变量未初始化到变量表中 context["SYS_PROJECT_KIND"] = ProjectKindName context["SYS_PROJECT_CODE"] = project.get("english_name") # 获取标准日志采集的dataid data_info = get_data_id_by_project_id(project_id) context["SYS_STANDARD_DATA_ID"] = data_info.get("standard_data_id") context["SYS_NON_STANDARD_DATA_ID"] = data_info.get("non_standard_data_id") return context
def collect_system_variable(access_token, project_id, namespace_id): sys_variables = {} # 获取标准日志采集的dataid data_info = get_data_id_by_project_id(project_id) sys_variables['SYS_STANDARD_DATA_ID'] = data_info.get('standard_data_id') sys_variables['SYS_NON_STANDARD_DATA_ID'] = data_info.get('non_standard_data_id') resp = paas_cc.get_project(access_token, project_id) if resp.get('code') != 0: logger.error( "查询project的信息出错(project_id:{project_id}):{message}".format( project_id=project_id, message=resp.get('message') ) ) project_info = resp["data"] sys_variables["SYS_CC_APP_ID"] = project_info["cc_app_id"] sys_variables['SYS_PROJECT_KIND'] = project_info["kind"] sys_variables['SYS_PROJECT_CODE'] = project_info["english_name"] resp = paas_cc.get_namespace(access_token, project_id, namespace_id) if resp.get('code') != 0: logger.error( "查询命名空间的信息出错(namespace_id:{project_id}-{namespace_id}):{message}".format( namespace_id=namespace_id, project_id=project_id, message=resp.get('message') ) ) namespace_info = resp["data"] sys_variables["SYS_NAMESPACE"] = namespace_info["name"] sys_variables["SYS_CLUSTER_ID"] = namespace_info["cluster_id"] sys_variables["SYS_PROJECT_ID"] = namespace_info["project_id"] # SYS_JFROG_DOMAIN # SYS_NON_STANDARD_DATA_ID # 获取镜像地址 jfrog_domain = paas_cc.get_jfrog_domain(access_token, project_id, sys_variables['SYS_CLUSTER_ID']) sys_variables['SYS_JFROG_DOMAIN'] = jfrog_domain return sys_variables
def handle_lb(username, access_token, project_id, lb_info, cc_app_id, **params): """ 1. 组装 lb 配置文件 2. 调用 bcs api 创建 Deployment """ cluster_id = lb_info.get('cluster_id') # 查询zk的信息 zk_res = paas_cc.get_zk_config(access_token, project_id, cluster_id) if zk_res.get("code") != ErrorCode.NoError: logger.error('获取zk信息出错,%s', zk_res) raise error_codes.APIError(_("获取zk信息出错")) try: zk_data = zk_res.get("data", [])[0] except Exception: logger.error('获取zk信息出错,%s', zk_res) raise error_codes.APIError(_("获取zk信息出错")) bcs_zookeeper = zk_data.get('bcs_zookeeper') zookeeper = zk_data.get('zookeeper') # 调度约束 try: intersection_item = json.loads(lb_info.get("data")) except Exception: logger.exception("命名空间中的调度约束信息出错") raise error_codes.JsonFormatError(_("命名空间中的调度约束信息出错")) new_intersection_item = handle_intersection_item(intersection_item) constraint = {"IntersectionItem": new_intersection_item} # vip 组装为labels try: ip_list = json.loads(lb_info.get('ip_list')) except Exception: logger.exception("命名空间中的IP集信息出错") raise error_codes.JsonFormatError(_("命名空间中的IP集信息出错")) labels = {} for i, ip in enumerate(ip_list): _key = "io.tencent.bcs.netsvc.requestip.%s" % i labels[_key] = ip lb_name = lb_info.get('name') # 配置文件中的变量赋值 now_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') # 获取 namespace data_dict = lb_info['data_dict'] if data_dict: data_dict = json.loads(data_dict) else: data_dict = {} lb_image_url = get_image_url(data_dict.get("image_url"), params["use_custom_image_url"], access_token, project_id, cluster_id) resource_limit = data_dict.get('resources', {}).get('limits', {}) ns_name = get_namespace_name(access_token, project_id, data_dict) # 获取data标准日志输出 data_info = get_data_id_by_project_id(project_id) data_id = str(data_info.get('standard_data_id')) context = { 'SYS_PROJECT_KIND': 2, # 固定为mesos 'SYS_STANDARD_DATA_ID': data_id, 'SYS_CC_APP_ID': cc_app_id, 'SYS_PROJECT_ID': project_id, 'SYS_OPERATOR': username, 'SYS_CLUSTER_ID': cluster_id, 'SYS_BCSGROUP': lb_name, 'SYS_CC_ZK': zookeeper, 'SYS_BCS_ZK': bcs_zookeeper, 'SYS_CREATOR': username, 'SYS_UPDATOR': username, 'SYS_CREATE_TIME': now_time, 'SYS_UPDATE_TIME': now_time, 'LB_IMAGE_URL': lb_image_url, 'CPU': str(resource_limit.get('cpu', 1)), 'MEMORY': str(resource_limit.get('memory', 1024)), 'IMAGE_VERSION': data_dict.get('image_version') or '1.1.0', 'FORWARD_MODE': data_dict.get('forward_mode') or 'haproxy', 'SYS_NAMESPACE': ns_name, 'ETH_VALUE': data_dict.get('eth_value') or 'eth1', 'LB_ADMIN_PORT': DEFAULT_LB_ADMIN_PORT, } # 组装 lb 配置文件 lb_config = copy.deepcopy(inst_constants.LB_SYS_CONFIG) lb_config['spec']['instance'] = data_dict.get('instance', 1) lb_config['constraint'] = constraint lb_config['spec']['template']['metadata']['labels'] = labels lb_config['spec']['template']['spec']['containers'][0]['ports'][0][ 'hostPort'] = (data_dict.get('host_port') or 31000) # 处理网络模式 spec = lb_config.get('spec', {}).get('template', {}).get('spec', {}) spec['networkMode'] = data_dict.get('networkMode') spec['networkType'] = data_dict.get('networkType') spec['custom_value'] = data_dict.get('custom_value') lb_config = handel_custom_network_mode(lb_config) lb_config = json.dumps(lb_config) try: config_profile = render_mako_context(lb_config, context) except Exception: logger.exception(u"LoadBalance配置文件变量替换错误\nconfig:%s\ncontext:%s" % (lb_config, context)) raise ValidationError(_("配置文件中有未替换的变量")) config_profile = json.loads(config_profile) # 调用bcs api 创建 client = mesos.MesosClient(access_token, project_id, cluster_id, env=None) result = client.create_deployment(ns_name, config_profile) if not result.get('result'): error_msg = result.get('message', '') logger.error(f"命名空间[{ns_name}]下创建LoadBalance[{lb_name}]出错:{error_msg}") return False, error_msg return True, ''
def bkdata_container_stdlog_dataid(self): # should return str data_info = get_data_id_by_project_id(self.project_id) return str(data_info.get('standard_data_id'))
def get_stdlog_data_id(project_id): data_info = get_data_id_by_project_id(project_id) return str(data_info.get('standard_data_id'))