@@ -99,6 +99,7 @@ def _list_resource_pool_hosts(start):
9999def _bulk_update_host (hosts , extra_fields ):
100100 update_fields = [
101101 "node_type" ,
102+ "bk_biz_id" ,
102103 "bk_cloud_id" ,
103104 "bk_host_name" ,
104105 "bk_addressing" ,
@@ -107,14 +108,16 @@ def _bulk_update_host(hosts, extra_fields):
107108 "inner_ipv6" ,
108109 "outer_ipv6" ,
109110 "bk_agent_id" ,
111+ "os_type" ,
110112 ] + extra_fields
111113 if hosts :
112114 models .Host .objects .bulk_update (hosts , fields = update_fields )
113115
114116
115- def _generate_host (biz_id , host , ap_id ):
116- os_type = tools .HostV2Tools .get_os_type (host )
117- cpu_arch = tools .HostV2Tools .get_cpu_arch (host )
117+ def _generate_host (biz_id , host , ap_id , is_os_type_priority = False , is_sync_cmdb_host_apply_cpu_arch = False ):
118+ os_type = tools .HostV2Tools .get_os_type (host , is_os_type_priority )
119+ cpu_arch = tools .HostV2Tools .get_cpu_arch (host , is_sync_cmdb_host_apply_cpu_arch , os_type = os_type )
120+
118121 host_data = models .Host (
119122 bk_host_id = host ["bk_host_id" ],
120123 bk_agent_id = host .get ("bk_agent_id" ),
@@ -192,16 +195,16 @@ def update_or_create_host_base(biz_id, task_id, cmdb_host_data):
192195 ).values_list ("bk_host_id" , flat = True )
193196 )
194197
195- need_delete_host_ids : typing .Set [int ] = set ()
196- need_create_host_without_biz : typing .List [typing .Dict ] = []
198+ is_sync_cmdb_host_apply_cpu_arch = tools .HostV2Tools .is_sync_cmdb_host_apply_cpu_arch ()
199+ is_os_type_priority = tools .HostV2Tools .is_os_type_priority ()
200+
197201 need_update_hosts : typing .List [models .Host ] = []
198- need_update_hosts_without_biz : typing .List [models .Host ] = []
199- need_update_hosts_without_os : typing .List [models .Host ] = []
200- need_update_hosts_without_biz_os : typing .List [models .Host ] = []
202+ need_update_hosts_with_arch : typing .List [models .Host ] = []
203+
201204 need_create_hosts : typing .List [models .Host ] = []
202- need_update_host_identity_objs : typing .List [models .IdentityData ] = []
203205 need_create_host_identity_objs : typing .List [models .IdentityData ] = []
204206 need_create_process_status_objs : typing .List [models .ProcessStatus ] = []
207+ need_update_host_identity_objs : typing .List [models .IdentityData ] = []
205208
206209 ap_id = constants .DEFAULT_AP_ID if models .AccessPoint .objects .count () > 1 else models .AccessPoint .objects .first ().id
207210
@@ -218,6 +221,7 @@ def update_or_create_host_base(biz_id, task_id, cmdb_host_data):
218221 host_params = {
219222 "bk_host_id" : host ["bk_host_id" ],
220223 "bk_agent_id" : host .get ("bk_agent_id" ),
224+ "bk_biz_id" : biz_id ,
221225 "bk_cloud_id" : host ["bk_cloud_id" ],
222226 "bk_host_name" : host .get ("bk_host_name" ),
223227 "bk_addressing" : host .get ("bk_addressing" ) or constants .CmdbAddressingType .STATIC .value ,
@@ -226,74 +230,47 @@ def update_or_create_host_base(biz_id, task_id, cmdb_host_data):
226230 "inner_ipv6" : (host .get ("bk_host_innerip_v6" ) or "" ).split ("," )[0 ],
227231 "outer_ipv6" : (host .get ("bk_host_outerip_v6" ) or "" ).split ("," )[0 ],
228232 }
229- if host ["bk_host_id" ] in exist_agent_host_ids :
230233
231- os_type = tools .HostV2Tools .get_os_type (host )
234+ if host ["bk_host_id" ] in exist_agent_host_ids :
235+ host_params ["os_type" ] = tools .HostV2Tools .get_os_type (host , is_os_type_priority )
232236 host_params ["node_type" ] = (constants .NodeType .PAGENT , constants .NodeType .AGENT )[
233237 host ["bk_cloud_id" ] == constants .DEFAULT_CLOUD
234238 ]
235-
236- if os_type and biz_id :
237- host_params ["bk_biz_id" ] = biz_id
238- host_params ["os_type" ] = os_type
239- need_update_hosts .append (models .Host (** host_params ))
240- elif biz_id :
241- host_params ["bk_biz_id" ] = biz_id
242- need_update_hosts_without_os .append (models .Host (** host_params ))
243- elif os_type :
244- host_params ["os_type" ] = os_type
245- need_update_hosts_without_biz .append (models .Host (** host_params ))
246- else :
247- need_update_hosts_without_biz_os .append (models .Host (** host_params ))
248239 elif host ["bk_host_id" ] in exist_proxy_host_ids :
249240 host_params ["os_type" ] = constants .OsType .LINUX
250241 host_params ["node_type" ] = constants .NodeType .PROXY
251- if biz_id :
252- host_params ["bk_biz_id" ] = biz_id
253- need_update_hosts .append (models .Host (** host_params ))
254- else :
255- need_update_hosts_without_biz .append (models .Host (** host_params ))
256242 else :
257- # 不是agent不是proxy的主机需要创建
258- if not biz_id :
259- need_create_host_without_biz .append (host )
243+ host_data , identify_data , process_status_data = _generate_host (
244+ biz_id ,
245+ host ,
246+ ap_id ,
247+ is_os_type_priority ,
248+ is_sync_cmdb_host_apply_cpu_arch ,
249+ )
250+ need_create_hosts .append (host_data )
251+ if identify_data .bk_host_id not in host_ids_in_exist_identity_data :
252+ need_create_host_identity_objs .append (identify_data )
260253 else :
261- host_data , identify_data , process_status_data = _generate_host (biz_id , host , ap_id )
262- need_create_hosts .append (host_data )
263- if identify_data .bk_host_id not in host_ids_in_exist_identity_data :
264- need_create_host_identity_objs .append (identify_data )
265- else :
266- need_update_host_identity_objs .append (identify_data )
267- if process_status_data .bk_host_id not in host_ids_in_exist_proc_statuses :
268- need_create_process_status_objs .append (process_status_data )
269-
270- if need_create_host_without_biz :
271- # 查询业务主机数据进行创建
272- find_host_biz_ids = [_host ["bk_host_id" ] for _host in need_create_host_without_biz ]
273- host_biz_relation = find_host_biz_relations (find_host_biz_ids )
274- # 查询不到业务需要删除
275- need_delete_host_ids = set (find_host_biz_ids ) - set (host_biz_relation .keys ())
276-
277- for need_create_host in need_create_host_without_biz :
278- if need_create_host ["bk_host_id" ] not in need_delete_host_ids :
279- host_data , identify_data , process_status_data = _generate_host (
280- host_biz_relation [need_create_host ["bk_host_id" ]],
281- need_create_host ,
282- ap_id ,
283- )
284- need_create_hosts .append (host_data )
285- if identify_data .bk_host_id not in host_ids_in_exist_identity_data :
286- need_create_host_identity_objs .append (identify_data )
287- else :
288- need_update_host_identity_objs .append (identify_data )
289- if process_status_data .bk_host_id not in host_ids_in_exist_proc_statuses :
290- need_create_process_status_objs .append (process_status_data )
254+ need_update_host_identity_objs .append (identify_data )
255+ if process_status_data .bk_host_id not in host_ids_in_exist_proc_statuses :
256+ need_create_process_status_objs .append (process_status_data )
257+ continue
258+
259+ cpu_arch = tools .HostV2Tools .get_cpu_arch (
260+ host ,
261+ is_sync_cmdb_host_apply_cpu_arch ,
262+ get_default = False ,
263+ )
264+ if is_sync_cmdb_host_apply_cpu_arch and cpu_arch :
265+ host_params ["cpu_arch" ] = cpu_arch
266+ need_update_hosts_with_arch .append (models .Host (** host_params ))
267+ continue
268+
269+ need_update_hosts .append (models .Host (** host_params ))
291270
292271 with transaction .atomic ():
293- _bulk_update_host (need_update_hosts , ["bk_biz_id" , "os_type" ])
294- _bulk_update_host (need_update_hosts_without_biz , ["os_type" ])
295- _bulk_update_host (need_update_hosts_without_os , ["bk_biz_id" ])
296- _bulk_update_host (need_update_hosts_without_biz_os , [])
272+ _bulk_update_host (need_update_hosts , [])
273+ _bulk_update_host (need_update_hosts_with_arch , ["cpu_arch" ])
297274
298275 if need_create_hosts :
299276 models .Host .objects .bulk_create (need_create_hosts , batch_size = 500 )
@@ -306,7 +283,7 @@ def update_or_create_host_base(biz_id, task_id, cmdb_host_data):
306283 if need_create_process_status_objs :
307284 models .ProcessStatus .objects .bulk_create (need_create_process_status_objs , batch_size = 500 )
308285
309- return bk_host_ids , list ( need_delete_host_ids )
286+ return bk_host_ids
310287
311288
312289def sync_biz_incremental_hosts (bk_biz_id : int , expected_bk_host_ids : typing .Iterable [int ]):
@@ -362,7 +339,7 @@ def _update_or_create_host(biz_id, start=0, task_id=None):
362339 f"host_count -> { host_count } , range -> { start } -{ start + constants .QUERY_CMDB_LIMIT } "
363340 )
364341
365- bk_host_ids , _ = update_or_create_host_base (biz_id , task_id , host_data )
342+ bk_host_ids = update_or_create_host_base (biz_id , task_id , host_data )
366343
367344 # 递归
368345 if host_count > start + constants .QUERY_CMDB_LIMIT :
0 commit comments