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')) cc_hosts = data.get_one_of_inputs('cc_host_replace_detail') # 克隆属性 clone_kwargs = {"bk_biz_id": biz_cc_id} fault_machine = [] for item in cc_hosts: clone_kwargs.update( {"bk_org_ip": "".join(get_ip_by_regex(item['cc_fault_ip']))}) clone_kwargs.update( {"bk_dst_ip": "".join(get_ip_by_regex(item['cc_new_ip']))}) # 克隆主机属性会自动把主机移动到对应模块 clone_result = client.cc.clone_host_property(clone_kwargs) if not clone_result['result']: message = cc_handle_api_error('cc.clone_host_property', clone_kwargs, clone_result['message']) data.set_outputs('ex_data', message) return False fault_machine.append("".join(get_ip_by_regex(item['cc_fault_ip']))) # 将故障机上交至故障机模块 host_result = cc_get_host_id_by_innerip(executor, biz_cc_id, fault_machine) if not host_result['result']: data.set_outputs('ex_data', host_result['message']) return False to_fault_module_kwargs = { "bk_biz_id": biz_cc_id, "bk_host_id": [int(host_id) for host_id in host_result['data']], } cc_result = client.cc.transfer_host_to_faultmodule( to_fault_module_kwargs) if cc_result['result']: return True else: message = cc_handle_api_error('cc.transfer_host_to_faultmodule', to_fault_module_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_params = { "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_resourcemodule( transfer_params) if transfer_result['result']: return True else: message = cc_handle_api_error( 'cc.transfer_host_to_resource_module', transfer_params, transfer_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 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 get_value(self): var_ip_picker = self.value username = self.pipeline_data['executor'] biz_cc_id = self.pipeline_data['biz_cc_id'] produce_method = var_ip_picker['var_ip_method'] if produce_method == 'custom': custom_value = var_ip_picker['var_ip_custom_value'] data = cc_get_ips_info_by_str(username, biz_cc_id, custom_value) ip_list = data['ip_result'] data = ','.join([ip['InnerIP'] for ip in ip_list]) else: select_data = var_ip_picker['var_ip_tree'] select_ip = map(lambda x: get_ip_by_regex(x)[0], filter(lambda x: get_ip_by_regex(x), select_data)) data = ','.join(list(set(select_ip))) return data
def execute(self, data, parent_data): executor = parent_data.get_one_of_inputs('executor') 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')) biz_cc_id = data.get_one_of_inputs('biz_cc_id', parent_data.inputs.biz_cc_id) supplier_account = supplier_account_for_business(biz_cc_id) # 查询主机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) self.logger.error(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')) 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_ip) return False if not new_host: data.outputs.ex_data = _(u"无法查询到 %s 机器信息,请确认该机器是否在当前业务下" % new_ip) 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 = u"{msg}\n{success}".format( msg=message, success=_(u"成功替换的机器: %s") % ','.join(success)) return False success.append(host_id_to_ip[kwargs['bk_host_id'][0]])
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 cc_get_ips_info_by_str(username, biz_cc_id, ip_str, use_cache=True): """ @summary: 从ip_str中匹配出IP信息 @param username @param biz_cc_id @param ip_str @param use_cache @note: 需要兼容的ip_str格式有 1: IP,IP 这种纯IP格式需要保证IP在业务中唯一,否则报错(需要注意一个IP 属于多个集群的情况,要根据平台和IP共同判断是否是同一个IP) 2: 集群名称|模块名称|IP,集群名称|模块名称|IP 这种格式可以唯一定位到一 个IP(如果业务把相同IP放到同一模块,还是有问题) 3: 平台ID:IP,平台ID:IP 这种格式可以唯一定位到一个IP,主要是兼容Job组件 传参需要和获取Job作业模板步骤参数 @return: {'result': True or False, 'data': [{'InnerIP': ,'HostID': , 'Source': , 'SetID': , 'SetName': , 'ModuleID': , 'ModuleName': },{}]} """ plat_ip_reg = re.compile(r'\d+:' + ip_re) set_module_ip_reg = re.compile( ur'[\u4e00-\u9fa5\w]+\|[\u4e00-\u9fa5\w]+\|' + ip_re ) # 中文字符或者其他字符 ip_input_list = get_ip_by_regex(ip_str) ip_result = [] # 如果是格式2,可以返回IP的集群、模块、平台信息 if set_module_ip_reg.match(ip_str): set_module_ip = [] for match in set_module_ip_reg.finditer(ip_str): set_module_ip.append(match.group()) ip_list = cc_get_ip_list_by_biz_and_user(username=username, biz_cc_id=biz_cc_id, use_cache=use_cache) for ip_info in ip_list: set_dict = {s['bk_set_id']: s for s in ip_info['set']} for module in ip_info['module']: if '%s|%s|%s' % (set_dict[module['bk_set_id']]['bk_set_name'], module['bk_module_name'], ip_info['host']['bk_host_innerip']) in set_module_ip: ip_result.append({'InnerIP': ip_info['host']['bk_host_innerip'], 'HostID': ip_info['host']['bk_host_id'], 'Source': ip_info['host']['bk_cloud_id'][0]['id'], 'SetID': module['bk_set_id'], 'SetName': set_dict[module['bk_set_id']]['bk_set_name'], 'ModuleID': module['bk_module_id'], 'ModuleName': module['bk_module_name'], }) # 如果是格式3,返回IP的平台信息 elif plat_ip_reg.match(ip_str): plat_ip = [] for match in plat_ip_reg.finditer(ip_str): plat_ip.append(match.group()) ip_list = cc_get_ip_list_by_biz_and_user(username=username, biz_cc_id=biz_cc_id, use_cache=use_cache) for ip_info in ip_list: if '%s:%s' % (ip_info['host']['bk_cloud_id'][0]['id'], ip_info['host']['bk_host_innerip']) in plat_ip: ip_result.append({'InnerIP': ip_info['host']['bk_host_innerip'], 'HostID': ip_info['host']['bk_host_id'], 'Source': ip_info['host']['bk_cloud_id'][0]['id'], }) else: ip = [] for match in ip_pattern.finditer(ip_str): ip.append(match.group()) ip_list = cc_get_ip_list_by_biz_and_user(username=username, biz_cc_id=biz_cc_id, use_cache=use_cache) host_id_list = [] for ip_info in ip_list: if ip_info['host']['bk_host_innerip'] in ip and ip_info['host']['bk_host_id'] not in host_id_list: ip_result.append({'InnerIP': ip_info['host']['bk_host_innerip'], 'HostID': ip_info['host']['bk_host_id'], 'Source': ip_info['host']['bk_cloud_id'][0]['id'], }) host_id_list.append(ip_info['host']['bk_host_id']) valid_ip = [ip_info['InnerIP'] for ip_info in ip_result] invalid_ip = list(set(ip_input_list) - set(valid_ip)) result = { 'result': True, 'ip_result': ip_result, 'ip_count': len(ip_result), 'invalid_ip': invalid_ip, } return result
def test_get_ip_by_regex(self): self.assertEqual(utils.get_ip_by_regex('1.1.1.1,2.2.2.2,3.3.3.3'), ['1.1.1.1', '2.2.2.2', '3.3.3.3'])
def execute(self, data, parent_data): executor = parent_data.inputs.executor client = get_client_by_user(executor) bk_biz_id = data.inputs.biz_cc_id bk_cloud_id = data.inputs.nodeman_bk_cloud_id node_type = data.inputs.nodeman_node_type op_type = data.inputs.nodeman_op_type nodeman_hosts = data.inputs.nodeman_hosts auth_type_dict = {'PASSWORD': '******', 'KEY': 'key'} hosts = [] for host in nodeman_hosts: conn_ips = get_ip_by_regex(host['conn_ips']) if len(conn_ips) == 0: data.set_outputs('ex_data', _(u'conn_ips 为空或输入格式错误')) return False try: login_ip = get_ip_by_regex(host['login_ip'])[0] except IndexError: data.set_outputs('ex_data', _(u' login_ip为空或输入格式错误')) return False try: data_ip = get_ip_by_regex(host['data_ip'])[0] except IndexError: data.set_outputs('ex_data', _(u'data_ip 为空或输入格式错误')) return False try: cascade_ip = get_ip_by_regex(host['cascade_ip'])[0] except IndexError: data.set_outputs('ex_data', _(u'cascade_ip 为空或输入格式错误')) return False one = { 'login_ip': login_ip, 'data_ip': data_ip, 'cascade_ip': cascade_ip, 'os_type': host['os_type'], 'has_cygwin': host['has_cygwin'], 'port': host['port'], 'account': host['account'], 'auth_type': host['auth_type'], 'password': host['password'], 'key': host['key'], } auth_type = host['auth_type'] value = host[auth_type.lower()] try: value = rsa_decrypt_password(value, settings.RSA_PRIV_KEY) except Exception: # password is not encrypted pass value = nodeman_rsa_encrypt(value) one.update({auth_type_dict[auth_type]: value}) for conn_ip in conn_ips: dict_temp = {'conn_ips': conn_ip} dict_temp.update(one) hosts.append(dict_temp) agent_kwargs = { 'bk_biz_id': bk_biz_id, 'bk_cloud_id': bk_cloud_id, 'node_type': node_type, 'op_type': op_type, 'creator': executor, 'hosts': hosts } agent_result = client.nodeman.create_task(agent_kwargs) LOGGER.info( 'nodeman created task result: {result}, api_kwargs: {kwargs}'. format(result=agent_result, kwargs=agent_kwargs)) if agent_result['result']: data.set_outputs('job_id', agent_result['data']['hosts'][0]['job_id']) return True else: message = u"create agent install task failed: %s" % agent_result[ 'message'] data.set_outputs('ex_data', message) return False