def execute(self, data, parent_data): executor = parent_data.get_one_of_inputs('executor') biz_cc_id = parent_data.get_one_of_inputs('biz_cc_id') supplier_account = parent_data.get_one_of_inputs('biz_supplier_account') client = get_client_by_user(executor) if parent_data.get_one_of_inputs('language'): setattr(client, 'language', parent_data.get_one_of_inputs('language')) translation.activate(parent_data.get_one_of_inputs('language')) # 查询主机id ip_list = get_ip_by_regex(data.get_one_of_inputs('cc_host_ip')) host_result = cc_get_host_id_by_innerip(executor, biz_cc_id, ip_list, supplier_account) if not host_result['result']: data.set_outputs('ex_data', host_result['message']) return False cc_module_select = cc_format_tree_mode_id(data.get_one_of_inputs('cc_module_select')) cc_is_increment = data.get_one_of_inputs('cc_is_increment') cc_kwargs = { "bk_biz_id": biz_cc_id, "bk_supplier_account": supplier_account, "bk_host_id": [int(host_id) for host_id in host_result['data']], "bk_module_id": cc_module_select, "is_increment": True if cc_is_increment == 'true' else False } cc_result = client.cc.transfer_host_module(cc_kwargs) if cc_result['result']: return True else: message = cc_handle_api_error('cc.transfer_host_module', cc_kwargs, cc_result['message']) data.set_outputs('ex_data', message) return False
def execute(self, data, parent_data): executor = parent_data.get_one_of_inputs('executor') biz_cc_id = parent_data.get_one_of_inputs('biz_cc_id') supplier_account = parent_data.get_one_of_inputs('biz_supplier_account') client = get_client_by_user(executor) if parent_data.get_one_of_inputs('language'): setattr(client, 'language', parent_data.get_one_of_inputs('language')) translation.activate(parent_data.get_one_of_inputs('language')) # 查询主机id ip_list = get_ip_by_regex(data.get_one_of_inputs('cc_host_ip')) host_result = cc_get_host_id_by_innerip(executor, biz_cc_id, ip_list, supplier_account) if not host_result['result']: data.set_outputs('ex_data', host_result['message']) return False transfer_kwargs = { 'bk_supplier_account': supplier_account, 'bk_biz_id': biz_cc_id, 'bk_host_id': [int(host_id) for host_id in host_result['data']] } transfer_result = client.cc.transfer_host_to_idlemodule(transfer_kwargs) if transfer_result['result']: return True else: message = cc_handle_api_error('cc.transfer_host_to_idlemodule', transfer_kwargs, transfer_result['message']) data.set_outputs('ex_data', message) return False
def cc_format_prop_data(executor, obj_id, prop_id, language, supplier_account): ret = { "result": True, "data": {} } client = get_client_by_user(executor) if language: setattr(client, 'language', language) cc_kwargs = { "bk_obj_id": obj_id, "bk_supplier_account": supplier_account } cc_result = client.cc.search_object_attribute(cc_kwargs) if not cc_result['result']: message = cc_handle_api_error('cc.search_object_attribute', cc_kwargs, cc_result['message']) ret['result'] = False ret['message'] = message return ret for prop in cc_result['data']: if prop['bk_property_id'] == prop_id: for item in prop['option']: ret['data'][item['name'].strip()] = item['id'] else: break return ret
def execute(self, data, parent_data): executor = parent_data.get_one_of_inputs('executor') biz_cc_id = parent_data.get_one_of_inputs('biz_cc_id') supplier_account = parent_data.get_one_of_inputs( 'biz_supplier_account') client = get_client_by_user(executor) if parent_data.get_one_of_inputs('language'): setattr(client, 'language', parent_data.get_one_of_inputs('language')) translation.activate(parent_data.get_one_of_inputs('language')) kwargs = { "bk_biz_id": biz_cc_id, "bk_supplier_account": supplier_account } tree_data = client.cc.search_biz_inst_topo(kwargs) if not tree_data['result']: message = cc_handle_api_error('cc.search_biz_inst_topo', kwargs, tree_data['message']) data.set_outputs('ex_data', message) return False cc_module_select = cc_format_tree_mode_id( data.get_one_of_inputs('cc_module_select')) cc_module_property = data.get_one_of_inputs('cc_module_property') if cc_module_property == "bk_module_type": bk_module_type = cc_format_prop_data( executor, 'module', 'bk_module_type', parent_data.get_one_of_inputs('language'), supplier_account) if not bk_module_type['result']: data.set_outputs('ex_data', bk_module_type['message']) return False cc_module_prop_value = bk_module_type['data'].get( data.get_one_of_inputs('cc_module_prop_value')) if not cc_module_prop_value: data.set_outputs('ex_data', _(u"模块类型校验失败,请重试并填写正确的模块类型")) return False else: cc_module_prop_value = data.get_one_of_inputs( 'cc_module_prop_value') for module_id in cc_module_select: cc_kwargs = { "bk_biz_id": biz_cc_id, "bk_supplier_account": supplier_account, "bk_set_id": get_module_set_id(tree_data['data'], module_id), "bk_module_id": module_id, "data": { cc_module_property: cc_module_prop_value } } cc_result = client.cc.update_module(cc_kwargs) if not cc_result['result']: message = cc_handle_api_error('cc.update_module', cc_kwargs, cc_result['message']) data.set_outputs('ex_data', message) return False return True
def execute(self, data, parent_data): executor = parent_data.get_one_of_inputs('executor') biz_cc_id = parent_data.get_one_of_inputs('biz_cc_id') supplier_account = parent_data.get_one_of_inputs('biz_supplier_account') client = get_client_by_user(executor) if parent_data.get_one_of_inputs('language'): setattr(client, 'language', parent_data.get_one_of_inputs('language')) translation.activate(parent_data.get_one_of_inputs('language')) cc_set_select = cc_format_tree_mode_id(data.get_one_of_inputs('cc_set_select')) for set_id in cc_set_select: cc_kwargs = { "bk_biz_id": biz_cc_id, "bk_supplier_account": supplier_account, "bk_set_id": set_id, "data": { "bk_service_status": data.get_one_of_inputs('cc_set_status') } } cc_result = client.cc.update_set(cc_kwargs) if not cc_result['result']: message = cc_handle_api_error('cc.update_set', cc_kwargs, cc_result['message']) data.set_outputs('ex_data', message) return False return True
def execute(self, data, parent_data): executor = parent_data.get_one_of_inputs('executor') biz_cc_id = parent_data.get_one_of_inputs('biz_cc_id') client = get_client_by_user(executor) client.set_bk_api_ver('v2') if parent_data.get_one_of_inputs('language'): setattr(client, 'language', parent_data.get_one_of_inputs('language')) translation.activate(parent_data.get_one_of_inputs('language')) original_global_var = data.get_one_of_inputs('job_global_var') global_vars = [] for _value in original_global_var: # 1-字符串,2-IP if _value['type'] == 2: var_ip = cc_get_ips_info_by_str(username=executor, biz_cc_id=biz_cc_id, ip_str=_value['value'], use_cache=False) ip_list = [{ 'ip': _ip['InnerIP'], 'bk_cloud_id': _ip['Source'] } for _ip in var_ip['ip_result']] if _value['value'].strip() and not ip_list: data.outputs.ex_data = _( u"无法从配置平台(CMDB)查询到对应 IP,请确认输入的 IP 是否合法") return False if ip_list: global_vars.append({ 'name': _value['name'], 'ip_list': ip_list, }) else: global_vars.append({ 'name': _value['name'], 'value': str(_value['value']).strip(), }) job_kwargs = { 'bk_biz_id': biz_cc_id, 'bk_job_id': data.get_one_of_inputs('job_task_id'), 'global_vars': global_vars, 'bk_callback_url': get_node_callback_url(self.id) } job_result = client.job.execute_job(job_kwargs) LOGGER.info('job_result: {result}, job_kwargs: {kwargs}'.format( result=job_result, kwargs=job_kwargs)) if job_result['result']: job_instance_id = job_result['data']['job_instance_id'] data.outputs.job_inst_url = get_job_instance_url( parent_data.inputs.biz_cc_id, job_instance_id) data.outputs.job_inst_id = job_instance_id data.outputs.job_inst_name = job_result['data'][ 'job_instance_name'] data.outputs.client = client return True else: data.outputs.ex_data = job_result['message'] return False
def execute(self, data, parent_data): executor = parent_data.get_one_of_inputs('executor') biz_cc_id = parent_data.get_one_of_inputs('biz_cc_id') client = get_client_by_user(executor) client.set_bk_api_ver('v2') if parent_data.get_one_of_inputs('language'): setattr(client, 'language', parent_data.get_one_of_inputs('language')) translation.activate(parent_data.get_one_of_inputs('language')) original_source_files = data.get_one_of_inputs('job_source_files', []) file_source = [] for item in original_source_files: ip_info = cc_get_ips_info_by_str(username=executor, biz_cc_id=biz_cc_id, ip_str=item['ip'], use_cache=False) file_source.append({ 'files': str(item['files']).strip().split("\n"), 'ip_list': [{ 'ip': _ip['InnerIP'], 'bk_cloud_id': _ip['Source'] } for _ip in ip_info['ip_result']], 'account': str(item['account']).strip(), }) original_ip_list = data.get_one_of_inputs('job_ip_list') ip_info = cc_get_ips_info_by_str(executor, biz_cc_id, original_ip_list) ip_list = [{ 'ip': _ip['InnerIP'], 'bk_cloud_id': _ip['Source'] } for _ip in ip_info['ip_result']] job_kwargs = { 'bk_biz_id': biz_cc_id, 'file_source': file_source, 'ip_list': ip_list, 'account': data.get_one_of_inputs('job_account'), 'file_target_path': data.get_one_of_inputs('job_target_path'), 'bk_callback_url': get_node_callback_url(self.id) } job_result = client.job.fast_push_file(job_kwargs) LOGGER.info('job_result: {result}, job_kwargs: {kwargs}'.format( result=job_result, kwargs=job_kwargs)) if job_result['result']: job_instance_id = job_result['data']['job_instance_id'] data.outputs.job_inst_id = job_instance_id data.outputs.job_inst_name = job_result['data'][ 'job_instance_name'] data.outputs.job_inst_url = get_job_instance_url( parent_data.inputs.biz_cc_id, job_instance_id) data.outputs.client = client return True else: data.outputs.ex_data = job_result['message'] return False
def get_job_instance_log(request, biz_cc_id): client = get_client_by_user(request.user.username) job_instance_id = request.GET.get('job_instance_id') log_kwargs = { "bk_biz_id": biz_cc_id, "job_instance_id": job_instance_id } log_result = client.job.get_job_instance_log(log_kwargs) return JsonResponse(log_result)
def cc_get_host_id_by_innerip(executor, bk_biz_id, ip_list, supplier_account): """ 获取主机ID :param executor: :param bk_biz_id: :param ip_list: :return: [1, 2, 3] id列表 """ cc_kwargs = { 'bk_biz_id': bk_biz_id, 'bk_supplier_account': supplier_account, 'ip': { 'data': ip_list, 'exact': 1, 'flag': 'bk_host_innerip' }, 'condition': [{ 'bk_obj_id': 'host', 'fields': ['bk_host_id', 'bk_host_innerip'] }], } client = get_client_by_user(executor) cc_result = client.cc.search_host(cc_kwargs) if not cc_result['result']: message = cc_handle_api_error('cc.search_host', cc_kwargs, cc_result['message']) return {'result': False, 'message': message} # change bk_host_id to str to use str.join() function ip_to_id = { item['host']['bk_host_innerip']: str(item['host']['bk_host_id']) for item in cc_result['data']['info'] } host_id_list = [] invalid_ip_list = [] for ip in ip_list: if ip in ip_to_id: host_id_list.append(ip_to_id[ip]) else: invalid_ip_list.append(ip) if invalid_ip_list: result = { 'result': False, 'message': _(u"查询配置平台(CMDB)接口cc.search_host表明,存在不属于当前业务的IP: {ip}").format( ip=','.join(invalid_ip_list)) } return result return {'result': True, 'data': host_id_list}
def cc_get_ip_list_by_biz_and_user(username, biz_cc_id, supplier_account, use_cache=True): """ @summary:根据当前用户和业务ID获取IP @note: 由于获取了全业务IP,接口会比较慢,需要加缓存 @note: 由于存在单主机多IP问题,需要取第一个IP作为实际值 @param-username @param-biz_cc_id """ cache_key = "cc_get_ip_list_by_biz_and_user_%s_%s" % (username, biz_cc_id) data = cache.get(cache_key) if not data or not use_cache: client = get_client_by_user(username) cc_result = client.cc.search_host({ 'bk_supplier_account': supplier_account, 'condition': [{ 'bk_obj_id': 'biz', 'fields': [], 'condition': [{ 'field': 'bk_biz_id', 'operator': '$eq', 'value': biz_cc_id }] }, { "bk_obj_id": "module", "fields": [], "condition": [] }, { "bk_obj_id": "set", "fields": [], "condition": [] }] }) if cc_result['result']: data = cc_result['data']['info'] # 多IP主机处理,取第一个IP for host in data: if ',' in host['host']['bk_host_innerip']: host['bk_host_innerip'] = host['bk_host_innerip'].split( ',')[0] cache.set(cache_key, data, 60) else: logger.warning( (u"search_host ERROR###biz_cc_id=%s" u"###cc_result=%s") % (biz_cc_id, json.dumps(cc_result))) if not data: return [] return data
def cc_get_inner_ip_by_module_id(username, biz_cc_id, module_id_list, supplier_account=0): """ @summary: 根据模块ID查询主机内网ip :param username: :param biz_cc_id: :param module_id_list: :param supplier_account: 开发商 ID,暂不使用 :return: """ client = get_client_by_user(username) cc_kwargs = { "bk_biz_id": biz_cc_id, "bk_supplier_account": supplier_account, "condition": [ { "bk_obj_id": "host", "fields": ["bk_host_innerip"], }, { "bk_obj_id": "module", "fields": [], "condition": [ { "field": "bk_module_id", "operator": "$in", "value": module_id_list } ] }, { "bk_obj_id": "set", "fields": [], "condition": [] }, { "bk_obj_id": "biz", "fields": [], "condition": [] } ] } cc_result = client.cc.search_host(cc_kwargs) result = [] if cc_result['result']: result = cc_result['data']['info'] else: logger.warning(u"client.cc.search_host ERROR###biz_cc_id=%s" u"###cc_result=%s" % (biz_cc_id, json.dumps(cc_result))) return result
def get_cmdb_topo_tree(username, bk_biz_id, bk_supplier_account): client = get_client_by_user(username) kwargs = { 'bk_biz_id': bk_biz_id, 'bk_supplier_account': bk_supplier_account, } topo_result = client.cc.search_biz_inst_topo(kwargs) if not topo_result['result']: message = handle_api_error(_(u"配置平台(CMDB)"), 'cc.search_biz_inst_topo', kwargs, topo_result['message']) result = {'result': False, 'code': 100, 'message': message, 'data': []} return result inter_result = client.cc.get_biz_internal_module(kwargs) if not inter_result['result']: message = handle_api_error(_(u"配置平台(CMDB)"), 'cc.get_biz_internal_module', kwargs, inter_result['message']) result = {'result': False, 'code': 101, 'message': message, 'data': []} return result inter_data = inter_result['data'] data = topo_result['data'] if 'bk_set_id' in inter_data: default_set = { 'default': 1, 'bk_obj_id': 'set', 'bk_obj_name': _(u"集群"), 'bk_inst_id': inter_data['bk_set_id'], 'bk_inst_name': inter_data['bk_set_name'], 'child': [{ 'default': 1, 'bk_obj_id': 'module', 'bk_obj_name': _(u"模块"), 'bk_inst_id': mod['bk_module_id'], 'bk_inst_name': mod['bk_module_name'], } for mod in inter_data['module']] } data[0]['child'].insert(0, default_set) return {'result': True, 'code': 0, 'data': data, 'messsage': ''}
def execute(self, data, parent_data): executor = parent_data.get_one_of_inputs('executor') biz_cc_id = parent_data.get_one_of_inputs('biz_cc_id') supplier_account = parent_data.get_one_of_inputs('biz_supplier_account') client = get_client_by_user(executor) if parent_data.get_one_of_inputs('language'): setattr(client, 'language', parent_data.get_one_of_inputs('language')) translation.activate(parent_data.get_one_of_inputs('language')) # 查询主机id ip_list = get_ip_by_regex(data.get_one_of_inputs('cc_host_ip')) host_result = cc_get_host_id_by_innerip(executor, biz_cc_id, ip_list, supplier_account) if not host_result['result']: data.set_outputs('ex_data', host_result['message']) return False # 更新主机属性 cc_host_property = data.get_one_of_inputs('cc_host_property') if cc_host_property == "bk_isp_name": bk_isp_name = cc_format_prop_data(executor, 'host', 'bk_isp_name', parent_data.get_one_of_inputs('language'), supplier_account) if not bk_isp_name['result']: data.set_outputs('ex_data', bk_isp_name['message']) return False cc_host_prop_value = bk_isp_name['data'].get(data.get_one_of_inputs('cc_host_prop_value')) if not cc_host_prop_value: data.set_outputs('ex_data', _(u"所属运营商校验失败,请重试并修改为正确的所属运营商")) return False elif cc_host_property == "bk_state_name": bk_state_name = cc_format_prop_data(executor, 'host', 'bk_state_name', parent_data.get_one_of_inputs('language'), supplier_account) if not bk_state_name['result']: data.set_outputs('ex_data', bk_state_name['message']) return False cc_host_prop_value = bk_state_name['data'].get(data.get_one_of_inputs('cc_host_prop_value')) if not cc_host_prop_value: data.set_outputs('ex_data', _(u"所在国家校验失败,请重试并修改为正确的所在国家")) return False elif cc_host_property == "bk_province_name": bk_province_name = cc_format_prop_data(executor, 'host', 'bk_province_name', parent_data.get_one_of_inputs('language'), supplier_account) if not bk_province_name['result']: data.set_outputs('ex_data', bk_province_name['message']) return False cc_host_prop_value = bk_province_name['data'].get(data.get_one_of_inputs('cc_host_prop_value')) if not cc_host_prop_value: data.set_outputs('ex_data', _(u"所在省份校验失败,请重试并修改为正确的所在省份")) return False else: cc_host_prop_value = data.get_one_of_inputs('cc_host_prop_value') cc_kwargs = { "bk_host_id": ",".join(host_result['data']), "bk_supplier_account": supplier_account, "data": { cc_host_property: cc_host_prop_value } } cc_result = client.cc.update_host(cc_kwargs) if cc_result['result']: return True else: message = cc_handle_api_error('cc.update_host', cc_kwargs, cc_result['message']) data.set_outputs('ex_data', message) return False
def get_ip_picker_result(username, bk_biz_id, bk_supplier_account, kwargs): selector = kwargs['selectors'][0] filters = kwargs['filters'] excludes = kwargs['excludes'] cc_kwargs = { 'bk_biz_id': bk_biz_id, 'bk_supplier_account': bk_supplier_account, 'condition': [{ 'bk_obj_id': 'module', 'fields': ['bk_module_id', 'bk_module_name'], 'condition': [] }] } topo_result = get_cmdb_topo_tree(username, bk_biz_id, bk_supplier_account) if not topo_result['result']: return topo_result biz_topo_tree = topo_result['data'][0] if selector == 'topo': topo = kwargs['topo'] if not topo: return {'result': False, 'data': [], 'message': ''} # transfer topo to modules topo_dct = {} for tp in topo: topo_dct.setdefault(tp['bk_obj_id'], []).append(tp['bk_inst_id']) topo_objects = get_objects_of_topo_tree(biz_topo_tree, topo_dct) topo_modules = [] for obj in topo_objects: topo_modules += get_modules_of_bk_obj(obj) topo_modules_id = get_modules_id(topo_modules) condition = [{ 'field': 'bk_module_id', 'operator': '$in', 'value': topo_modules_id }] cc_kwargs['condition'][0]['codition'] = condition else: host_list = kwargs['ip'] if not host_list: return [] ip_list = [ '%s:%s' % (str(host['cloud'][0]['id']), host['bk_host_innerip']) for host in host_list ] cc_kwargs['condition'].append({ "bk_obj_id": "host", "fields": [ "bk_host_id", "bk_host_innerip", "bk_host_outerip", "bk_host_name" ], "condition": [{ "field": "bk_host_innerip", "operator": "$in", "value": [host['bk_host_innerip'] for host in host_list] }] }) client = get_client_by_user(username) host_result = client.cc.search_host(cc_kwargs) if not host_result: message = handle_api_error(_(u"配置平台(CMDB)"), 'cc.search_host', cc_kwargs, host_result['message']) return {'result': False, 'data': [], 'message': message} host_info = host_result['data']['info'] data = [] for host in host_info: host_modules_id = get_modules_id(host['module']) if selector == 'topo' or '%s:%s' % ( str(host['host']['bk_cloud_id'][0]['id']), host['host']['bk_host_innerip']) in ip_list: data.append({ 'bk_host_id': host['host']['bk_host_id'], 'bk_host_innerip': host['host']['bk_host_innerip'], 'bk_host_outerip': host['host']['bk_host_outerip'], 'bk_host_name': host['host']['bk_host_name'], 'bk_cloud_id': host['host']['bk_cloud_id'][0]['id'], 'host_modules_id': host_modules_id }) if filters: filters_dct = {} for ft in filters: filters_dct.setdefault(ft['field'], []) filters_dct[ft['field']] += ft['value'] filter_modules = get_modules_of_filters(biz_topo_tree, filters_dct) filter_modules_id = get_modules_id(filter_modules) data = [ host for host in data if set(host['host_modules_id']) & set(filter_modules_id) ] if 'ip' in filters_dct: data = [ host for host in data if host['bk_host_innerip'] in filters_dct['ip'] ] if excludes: excludes_dct = {} for ex in filters: excludes_dct.setdefault(ex['field'], []) excludes_dct[ex['field']] += ex['value'] exclude_modules = get_modules_of_filters(biz_topo_tree, excludes_dct) exclude_modules_id = get_modules_id(exclude_modules) data = [ host for host in data if not set(host['host_modules_id']) & set(exclude_modules_id) ] if 'ip' in excludes_dct: data = [ host for host in data if host['bk_host_innerip'] not in excludes_dct['ip'] ] result = {'result': True, 'code': 0, 'data': data, 'message': ''} return result
def get_ip_picker_result(username, bk_biz_id, bk_supplier_account, kwargs): """ @summary:根据前端表单数据获取合法的IP @param username: @param bk_biz_id: @param bk_supplier_account: @param kwargs: @return: """ topo_result = get_cmdb_topo_tree(username, bk_biz_id, bk_supplier_account) if not topo_result['result']: return topo_result biz_topo_tree = topo_result['data'][0] build_result = build_cmdb_search_host_kwargs(bk_biz_id, bk_supplier_account, kwargs, biz_topo_tree) if not build_result['result']: return { 'result': False, 'code': ERROR_CODES.PARAMETERS_ERROR, 'data': [], 'message': build_result['message'] } cmdb_kwargs = build_result['data'] client = get_client_by_user(username) host_result = client.cc.search_host(cmdb_kwargs) if not host_result: message = handle_api_error(_(u"配置平台(CMDB)"), 'cc.search_host', cmdb_kwargs, host_result['message']) return {'result': False, 'data': [], 'message': message} host_info = host_result['data']['info'] # IP选择器 selector = kwargs['selectors'][0] if selector == 'ip': ip_list = [ '%s:%s' % (str(host['cloud'][0]['id']), host['bk_host_innerip']) for host in kwargs['ip'] ] else: ip_list = [] data = [] for host in host_info: host_modules_id = get_modules_id(host['module']) if selector == 'topo' or '%s:%s' % ( str(host['host']['bk_cloud_id'][0]['id']), host['host']['bk_host_innerip']) in ip_list: data.append({ 'bk_host_id': host['host']['bk_host_id'], 'bk_host_innerip': host['host']['bk_host_innerip'], 'bk_host_outerip': host['host']['bk_host_outerip'], 'bk_host_name': host['host']['bk_host_name'], 'bk_cloud_id': host['host']['bk_cloud_id'][0]['id'], 'host_modules_id': host_modules_id }) # 筛选条件 filters = kwargs['filters'] if filters: filters_dct = {} for ft in filters: filters_dct.setdefault(ft['field'], []) filters_dct[ft['field']] += format_condition_value(ft['value']) new_topo_tree = process_topo_tree_by_condition(biz_topo_tree, filters_dct) filter_host = set(filters_dct.pop('host', [])) # 把拓扑筛选条件转换成 modules 筛选条件 filter_modules = get_modules_by_condition(new_topo_tree, filters_dct) filter_modules_id = get_modules_id(filter_modules) data = [ host for host in data if set(host['host_modules_id']) & set(filter_modules_id) ] if filter_host: data = [ host for host in data if host['bk_host_innerip'] in filter_host ] # 过滤条件 excludes = kwargs['excludes'] if excludes: excludes_dct = {} for ex in excludes: excludes_dct.setdefault(ex['field'], []) excludes_dct[ex['field']] += format_condition_value(ex['value']) new_topo_tree = process_topo_tree_by_condition(biz_topo_tree, excludes_dct) exclude_host = set(excludes_dct.pop('host', [])) # 把拓扑排除条件转换成 modules 排除条件 exclude_modules = [] if not excludes_dct else get_modules_by_condition( new_topo_tree, excludes_dct) exclude_modules_id = get_modules_id(exclude_modules) data = [ host for host in data if not (set(host['host_modules_id']) & set(exclude_modules_id)) ] if exclude_host: data = [ host for host in data if host['bk_host_innerip'] not in exclude_host ] result = {'result': True, 'code': NO_ERROR, 'data': data, 'message': ''} return result
def execute(self, data, parent_data): executor = parent_data.get_one_of_inputs('executor') biz_cc_id = parent_data.get_one_of_inputs('biz_cc_id') supplier_account = parent_data.get_one_of_inputs('biz_supplier_account') client = get_client_by_user(executor) if parent_data.get_one_of_inputs('language'): setattr(client, 'language', parent_data.get_one_of_inputs('language')) translation.activate(parent_data.get_one_of_inputs('language')) cc_set_select = cc_format_tree_mode_id(data.get_one_of_inputs('cc_set_select')) cc_set_property = data.get_one_of_inputs('cc_set_property') if cc_set_property == "bk_service_status": bk_service_status = cc_format_prop_data(executor, 'set', 'bk_service_status', parent_data.get_one_of_inputs('language'), supplier_account) if not bk_service_status['result']: data.set_outputs('ex_data', bk_service_status['message']) return False cc_set_prop_value = bk_service_status['data'].get(data.get_one_of_inputs('cc_set_prop_value')) if not cc_set_prop_value: data.set_outputs('ex_data', _(u"服务状态校验失败,请重试并修改为正确的服务状态")) return False elif cc_set_property == "bk_set_env": bk_set_env = cc_format_prop_data(executor, 'set', 'bk_set_env', parent_data.get_one_of_inputs('language'), supplier_account) if not bk_set_env['result']: data.set_outputs('ex_data', bk_set_env['message']) return False cc_set_prop_value = bk_set_env['data'].get(data.get_one_of_inputs('cc_set_prop_value')) if not cc_set_prop_value: data.set_outputs('ex_data', _(u"环境类型校验失败,请重试并修改为正确的环境类型")) return False else: cc_set_prop_value = data.get_one_of_inputs('cc_set_prop_value') for set_id in cc_set_select: cc_kwargs = { "bk_biz_id": biz_cc_id, "bk_supplier_account": supplier_account, "bk_set_id": set_id, "data": { cc_set_property: cc_set_prop_value } } cc_result = client.cc.update_set(cc_kwargs) if not cc_result['result']: message = cc_handle_api_error('cc.update_set', cc_kwargs, cc_result['message']) data.set_outputs('ex_data', message) return False return True
def execute(self, data, parent_data): executor = parent_data.get_one_of_inputs('executor') biz_cc_id = parent_data.get_one_of_inputs('biz_cc_id') supplier_account = parent_data.get_one_of_inputs('biz_supplier_account') client = get_client_by_user(executor) if parent_data.get_one_of_inputs('language'): setattr(client, 'language', parent_data.get_one_of_inputs('language')) translation.activate(parent_data.get_one_of_inputs('language')) cc_set_parent_select = cc_format_tree_mode_id(data.get_one_of_inputs('cc_set_parent_select')) cc_set_info = data.get_one_of_inputs('cc_set_info') cc_kwargs = { 'bk_biz_id': biz_cc_id, 'bk_supplier_account': supplier_account, 'data': {} } for parent_id in cc_set_parent_select: cc_kwargs['data']['bk_parent_id'] = parent_id for set_params in cc_set_info: for key, value in set_params.items(): if value: if key == "bk_set_env": bk_set_env = cc_format_prop_data(executor, 'set', 'bk_set_env', parent_data.get_one_of_inputs('language'), supplier_account) if not bk_set_env['result']: data.set_outputs('ex_data', bk_set_env['message']) return False value = bk_set_env['data'].get(value) if not value: data.set_outputs('ex_data', _(u"环境类型校验失败,请重试并修改为正确的环境类型")) return False elif key == "bk_service_status": bk_service_status = cc_format_prop_data(executor, 'set', 'bk_service_status', parent_data.get_one_of_inputs('language'), supplier_account) if not bk_service_status['result']: data.set_outputs('ex_data', bk_service_status['message']) return False value = bk_service_status['data'].get(value) if not value: data.set_outputs('ex_data', _(u"服务状态校验失败,请重试并修改为正确的服务状态")) return False cc_kwargs['data'][key] = value cc_result = client.cc.create_set(cc_kwargs) if not cc_result['result']: message = cc_handle_api_error('cc.create_set', cc_kwargs, cc_result['message']) data.set_outputs('ex_data', message) return False return True
def execute(self, data, parent_data): executor = parent_data.get_one_of_inputs('executor') biz_cc_id = parent_data.get_one_of_inputs('biz_cc_id') client = get_client_by_user(executor) if parent_data.get_one_of_inputs('language'): setattr(client, 'language', parent_data.get_one_of_inputs('language')) translation.activate(parent_data.get_one_of_inputs('language')) original_ip_list = data.get_one_of_inputs('job_ip_list') ip_info = cc_get_ips_info_by_str(username=executor, biz_cc_id=biz_cc_id, ip_str=original_ip_list, use_cache=False) ip_list = [{ 'ip': _ip['InnerIP'], 'bk_cloud_id': _ip['Source'] } for _ip in ip_info['ip_result']] job_kwargs = { 'bk_biz_id': biz_cc_id, 'script_timeout': data.get_one_of_inputs('job_script_timeout'), 'account': data.get_one_of_inputs('job_account'), 'ip_list': ip_list, 'bk_callback_url': get_node_callback_url(self.id) } script_param = data.get_one_of_inputs('job_script_param') if script_param: job_kwargs.update({ 'script_param': base64.b64encode(script_param.encode('utf-8')) }) script_source = data.get_one_of_inputs('job_script_source') if script_source in ["general", "public"]: job_kwargs.update({ "script_id": data.get_one_of_inputs('job_script_list_%s' % script_source) }) else: job_kwargs.update({ 'script_type': data.get_one_of_inputs('job_script_type'), 'script_content': base64.b64encode( data.get_one_of_inputs('job_content').encode('utf-8')), }) job_result = client.job.fast_execute_script(job_kwargs) LOGGER.info('job_result: {result}, job_kwargs: {kwargs}'.format( result=job_result, kwargs=job_kwargs)) if job_result['result']: job_instance_id = job_result['data']['job_instance_id'] data.outputs.job_inst_id = job_instance_id data.outputs.job_inst_name = job_result['data'][ 'job_instance_name'] data.outputs.job_inst_url = get_job_instance_url( parent_data.inputs.biz_cc_id, job_instance_id) data.outputs.client = client return True else: data.outputs.ex_data = '%s, invalid ip: %s' % ( job_result['message'], ','.join(ip_info['invalid_ip'])) return False
def execute(self, data, parent_data): executor = parent_data.get_one_of_inputs('executor') biz_cc_id = parent_data.get_one_of_inputs('biz_cc_id') supplier_account = parent_data.get_one_of_inputs('biz_supplier_account') client = get_client_by_user(executor) if parent_data.get_one_of_inputs('language'): setattr(client, 'language', parent_data.get_one_of_inputs('language')) translation.activate(parent_data.get_one_of_inputs('language')) cc_hosts = data.get_one_of_inputs('cc_host_replace_detail') # 查询主机可编辑属性 search_attr_kwargs = { 'bk_obj_id': 'host', 'bk_supplier_account': supplier_account } search_attr_result = client.cc.search_object_attribute(search_attr_kwargs) if not search_attr_result['result']: message = handle_api_error(__group_name__, 'cc.search_object_attribute', search_attr_kwargs, search_attr_result['message']) logger.error(message) data.outputs.ex_data = message return False editable_attrs = [] for item in search_attr_result['data']: if item['editable']: editable_attrs.append(item['bk_property_id']) # 拉取所有主机信息 search_kwargs = { 'bk_biz_id': biz_cc_id, 'bk_supplier_account': supplier_account, 'condition': [{ 'bk_obj_id': 'module', 'fields': ['bk_module_id'], 'condition': [] }] } fault_replace_ip_map = {} for item in cc_hosts: fault_replace_ip_map[''.join(get_ip_by_regex(item['cc_fault_ip']))] = ''.join( get_ip_by_regex(item['cc_new_ip'])) all_hosts = [] all_hosts.extend(fault_replace_ip_map.keys()) all_hosts.extend(fault_replace_ip_map.values()) search_kwargs['ip'] = { 'data': all_hosts, 'exact': 1, 'flag': 'bk_host_innerip' } hosts_result = client.cc.search_host(search_kwargs) if not hosts_result['result']: message = handle_api_error(__group_name__, 'cc.search_host', search_attr_kwargs, hosts_result['message']) logger.error(message) data.outputs.ex_data = message return False # 更新替换机信息 batch_update_kwargs = { 'bk_obj_id': 'host', 'bk_supplier_account': supplier_account, 'update': [] } host_dict = {host_info['host']['bk_host_innerip']: host_info['host'] for host_info in hosts_result['data']['info']} host_id_to_ip = {host_info['host']['bk_host_id']: host_info['host']['bk_host_innerip'] for host_info in hosts_result['data']['info']} fault_replace_id_map = {} for fault_ip, new_ip in fault_replace_ip_map.items(): fault_host = host_dict.get(fault_ip) new_host = host_dict.get(new_ip) if not fault_host: data.outputs.ex_data = _(u"无法查询到 %s 机器信息,请确认该机器是否在当前业务下" % fault_host) return False if not new_host: data.outputs.ex_data = _(u"无法查询到 %s 机器信息,请确认该机器是否在当前业务下" % new_host) return False update_item = { 'datas': {}, 'inst_id': new_host['bk_host_id'] } for attr in [attr for attr in editable_attrs if attr in fault_host]: update_item['datas'][attr] = fault_host[attr] batch_update_kwargs['update'].append(update_item) fault_replace_id_map[fault_host['bk_host_id']] = new_host['bk_host_id'] update_result = client.cc.batch_update_inst(batch_update_kwargs) if not update_result['result']: message = handle_api_error(__group_name__, 'cc.batch_update_inst', batch_update_kwargs, update_result['message']) logger.error(message) data.outputs.ex_data = message return False # 将主机上交至故障机模块 fault_transfer_kwargs = { 'bk_supplier_account': supplier_account, 'bk_biz_id': biz_cc_id, 'bk_host_id': list(fault_replace_id_map.keys()) } fault_transfer_result = client.cc.transfer_host_to_faultmodule(fault_transfer_kwargs) if not fault_transfer_result['result']: message = handle_api_error(__group_name__, 'cc.transfer_host_to_faultmodule', fault_transfer_kwargs, fault_transfer_result['message']) data.set_outputs('ex_data', message) return False # 转移主机模块 transfer_kwargs_list = [] for host_info in hosts_result['data']['info']: new_host_id = fault_replace_id_map.get(host_info['host']['bk_host_id']) if new_host_id: transfer_kwargs_list.append({ 'bk_biz_id': biz_cc_id, 'bk_supplier_account': supplier_account, 'bk_host_id': [new_host_id], 'bk_module_id': [module_info['bk_module_id'] for module_info in host_info['module']], 'is_increment': True }) success = [] for kwargs in transfer_kwargs_list: transfer_result = client.cc.transfer_host_module(kwargs) if not transfer_result['result']: message = handle_api_error(__group_name__, 'cc.transfer_host_module', kwargs, transfer_result['message']) logger.error(message) data.outputs.ex_data = '%s, %s'.format(_(u"成功替换的机器: %s") % success) return False success.append(host_id_to_ip[kwargs['bk_host_id'][0]])