2222from  typing  import  Optional , Tuple 
2323
2424from  elasticsearch  import  (
25-     AuthorizationException ,
2625    ConnectionError ,
2726    Elasticsearch ,
28-     NotFoundError ,
2927)
3028
3129SOURCE_DIR  =  Path (__file__ ).absolute ().parent .parent 
@@ -118,40 +116,15 @@ def wipe_cluster(client):
118116    except  ImportError :
119117        pass 
120118
121-     is_xpack  =  True 
122-     if  is_xpack :
123-         wipe_rollup_jobs (client )
124-         wait_for_pending_tasks (client , filter = "xpack/rollup/job" )
125-         wipe_slm_policies (client )
126- 
127-         # Searchable snapshot indices start in 7.8+ 
128-         if  es_version (client ) >=  (7 , 8 ):
129-             wipe_searchable_snapshot_indices (client )
130- 
131119    wipe_snapshots (client )
132-     if  is_xpack :
133-         wipe_data_streams (client )
120+     wipe_data_streams (client )
134121    wipe_indices (client )
135122
136-     if  is_xpack :
137-         wipe_xpack_templates (client )
138-     else :
139-         client .indices .delete_template (name = "*" )
140-         client .indices .delete_index_template (name = "*" )
141-         client .cluster .delete_component_template (name = "*" )
123+     client .indices .delete_template (name = "*" )
124+     client .indices .delete_index_template (name = "*" )
142125
143126    wipe_cluster_settings (client )
144127
145-     if  is_xpack :
146-         wipe_ilm_policies (client )
147-         wipe_auto_follow_patterns (client )
148-         wipe_tasks (client )
149-         wipe_node_shutdown_metadata (client )
150-         wait_for_pending_datafeeds_and_jobs (client )
151-         wipe_calendars (client )
152-         wipe_filters (client )
153-         wipe_transforms (client )
154- 
155128    wait_for_cluster_state_updates_to_finish (client )
156129    if  close_after_wipe :
157130        client .close ()
@@ -169,16 +142,6 @@ def wipe_cluster_settings(client):
169142        client .cluster .put_settings (body = new_settings )
170143
171144
172- def  wipe_rollup_jobs (client ):
173-     rollup_jobs  =  client .rollup .get_jobs (id = "_all" ).get ("jobs" , ())
174-     for  job  in  rollup_jobs :
175-         job_id  =  job ["config" ]["id" ]
176-         client .options (ignore_status = 404 ).rollup .stop_job (
177-             id = job_id , wait_for_completion = True 
178-         )
179-         client .options (ignore_status = 404 ).rollup .delete_job (id = job_id )
180- 
181- 
182145def  wipe_snapshots (client ):
183146    """Deletes all the snapshots and repositories from the cluster""" 
184147    in_progress_snapshots  =  []
@@ -223,259 +186,8 @@ def wipe_indices(client):
223186        )
224187
225188
226- def  wipe_searchable_snapshot_indices (client ):
227-     cluster_metadata  =  client .cluster .state (
228-         metric = "metadata" ,
229-         filter_path = "metadata.indices.*.settings.index.store.snapshot" ,
230-     )
231-     if  cluster_metadata :
232-         for  index  in  cluster_metadata ["metadata" ]["indices" ].keys ():
233-             client .indices .delete (index = index )
234- 
235- 
236- def  wipe_xpack_templates (client ):
237-     # Delete index templates (including legacy) 
238-     templates  =  [
239-         x .strip () for  x  in  client .cat .templates (h = "name" ).split ("\n " ) if  x .strip ()
240-     ]
241-     for  template  in  templates :
242-         if  is_xpack_template (template ):
243-             continue 
244-         try :
245-             client .indices .delete_template (name = template )
246-         except  NotFoundError  as  e :
247-             if  f"index_template [{ template }  ] missing"  in  str (e ):
248-                 client .indices .delete_index_template (name = template )
249- 
250-     # Delete component templates 
251-     templates  =  client .cluster .get_component_template ()["component_templates" ]
252-     templates_to_delete  =  [
253-         template ["name" ]
254-         for  template  in  templates 
255-         if  not  is_xpack_template (template ["name" ])
256-     ]
257-     if  templates_to_delete :
258-         client .cluster .delete_component_template (name = "," .join (templates_to_delete ))
259- 
260- 
261- def  wipe_ilm_policies (client ):
262-     for  policy  in  client .ilm .get_lifecycle ():
263-         if  (
264-             policy 
265-             not  in   {
266-                 "ilm-history-ilm-policy" ,
267-                 "slm-history-ilm-policy" ,
268-                 "watch-history-ilm-policy" ,
269-                 "watch-history-ilm-policy-16" ,
270-                 "ml-size-based-ilm-policy" ,
271-                 "logs" ,
272-                 "metrics" ,
273-                 "synthetics" ,
274-                 "7-days-default" ,
275-                 "30-days-default" ,
276-                 "90-days-default" ,
277-                 "180-days-default" ,
278-                 "365-days-default" ,
279-                 ".fleet-actions-results-ilm-policy" ,
280-                 ".deprecation-indexing-ilm-policy" ,
281-                 ".monitoring-8-ilm-policy" ,
282-             }
283-             and  "-history-ilm-polcy"  not  in   policy 
284-             and  "-meta-ilm-policy"  not  in   policy 
285-             and  "-data-ilm-policy"  not  in   policy 
286-             and  "@lifecycle"  not  in   policy 
287-         ):
288-             client .ilm .delete_lifecycle (name = policy )
289- 
290- 
291- def  wipe_slm_policies (client ):
292-     policies  =  client .slm .get_lifecycle ()
293-     for  policy  in  policies :
294-         if  policy  not  in   {"cloud-snapshot-policy" }:
295-             client .slm .delete_lifecycle (policy_id = policy )
296- 
297- 
298- def  wipe_auto_follow_patterns (client ):
299-     for  pattern  in  client .ccr .get_auto_follow_pattern ()["patterns" ]:
300-         client .ccr .delete_auto_follow_pattern (name = pattern ["name" ])
301- 
302- 
303- def  wipe_node_shutdown_metadata (client ):
304-     try :
305-         shutdown_status  =  client .shutdown .get_node ()
306-         # If response contains these two keys the feature flag isn't enabled 
307-         # on this cluster so skip this step now. 
308-         if  "_nodes"  in  shutdown_status  and  "cluster_name"  in  shutdown_status :
309-             return 
310- 
311-         for  shutdown_node  in  shutdown_status .get ("nodes" , []):
312-             node_id  =  shutdown_node ["node_id" ]
313-             client .shutdown .delete_node (node_id = node_id )
314- 
315-     # Elastic Cloud doesn't allow this so we skip. 
316-     except  AuthorizationException :
317-         pass 
318- 
319- 
320- def  wipe_tasks (client ):
321-     tasks  =  client .tasks .list ()
322-     for  node_name , node  in  tasks .get ("node" , {}).items ():
323-         for  task_id  in  node .get ("tasks" , ()):
324-             client .tasks .cancel (task_id = task_id , wait_for_completion = True )
325- 
326- 
327- def  wait_for_pending_tasks (client , filter , timeout = 30 ):
328-     end_time  =  time .time () +  timeout 
329-     while  time .time () <  end_time :
330-         tasks  =  client .cat .tasks (detailed = True ).split ("\n " )
331-         if  not  any (filter  in  task  for  task  in  tasks ):
332-             break 
333- 
334- 
335- def  wait_for_pending_datafeeds_and_jobs (client : Elasticsearch , timeout = 30 ):
336-     end_time  =  time .time () +  timeout 
337-     while  time .time () <  end_time :
338-         resp  =  client .ml .get_datafeeds (datafeed_id = "*" , allow_no_match = True )
339-         if  resp ["count" ] ==  0 :
340-             break 
341-         for  datafeed  in  resp ["datafeeds" ]:
342-             client .options (ignore_status = 404 ).ml .delete_datafeed (
343-                 datafeed_id = datafeed ["datafeed_id" ]
344-             )
345- 
346-     end_time  =  time .time () +  timeout 
347-     while  time .time () <  end_time :
348-         resp  =  client .ml .get_jobs (job_id = "*" , allow_no_match = True )
349-         if  resp ["count" ] ==  0 :
350-             break 
351-         for  job  in  resp ["jobs" ]:
352-             client .options (ignore_status = 404 ).ml .close_job (job_id = job ["job_id" ])
353-             client .options (ignore_status = 404 ).ml .delete_job (job_id = job ["job_id" ])
354- 
355-     end_time  =  time .time () +  timeout 
356-     while  time .time () <  end_time :
357-         resp  =  client .ml .get_data_frame_analytics (id = "*" )
358-         if  resp ["count" ] ==  0 :
359-             break 
360-         for  job  in  resp ["data_frame_analytics" ]:
361-             client .options (ignore_status = 404 ).ml .stop_data_frame_analytics (id = job ["id" ])
362-             client .options (ignore_status = 404 ).ml .delete_data_frame_analytics (
363-                 id = job ["id" ]
364-             )
365- 
366- 
367- def  wipe_filters (client : Elasticsearch , timeout = 30 ):
368-     end_time  =  time .time () +  timeout 
369-     while  time .time () <  end_time :
370-         resp  =  client .ml .get_filters (filter_id = "*" )
371-         if  resp ["count" ] ==  0 :
372-             break 
373-         for  filter  in  resp ["filters" ]:
374-             client .options (ignore_status = 404 ).ml .delete_filter (
375-                 filter_id = filter ["filter_id" ]
376-             )
377- 
378- 
379- def  wipe_calendars (client : Elasticsearch , timeout = 30 ):
380-     end_time  =  time .time () +  timeout 
381-     while  time .time () <  end_time :
382-         resp  =  client .ml .get_calendars (calendar_id = "*" )
383-         if  resp ["count" ] ==  0 :
384-             break 
385-         for  calendar  in  resp ["calendars" ]:
386-             client .options (ignore_status = 404 ).ml .delete_calendar (
387-                 calendar_id = calendar ["calendar_id" ]
388-             )
389- 
390- 
391- def  wipe_transforms (client : Elasticsearch , timeout = 30 ):
392-     end_time  =  time .time () +  timeout 
393-     while  time .time () <  end_time :
394-         resp  =  client .transform .get_transform (transform_id = "*" )
395-         if  resp ["count" ] ==  0 :
396-             break 
397-         for  trasnform  in  resp ["transforms" ]:
398-             client .options (ignore_status = 404 ).transform .stop_transform (
399-                 transform_id = trasnform ["id" ]
400-             )
401-             client .options (ignore_status = 404 ).transform .delete_transform (
402-                 transform_id = trasnform ["id" ]
403-             )
404- 
405- 
406189def  wait_for_cluster_state_updates_to_finish (client , timeout = 30 ):
407190    end_time  =  time .time () +  timeout 
408191    while  time .time () <  end_time :
409192        if  not  client .cluster .pending_tasks ().get ("tasks" , ()):
410193            break 
411- 
412- 
413- def  is_xpack_template (name ):
414-     if  name .startswith ("." ):
415-         return  True 
416-     elif  name .startswith ("behavioral_analytics-events" ):
417-         return  True 
418-     elif  name .startswith ("elastic-connectors-" ):
419-         return  True 
420-     elif  name .startswith ("entities_v1_" ):
421-         return  True 
422-     elif  name .endswith ("@ilm" ):
423-         return  True 
424-     elif  name .endswith ("@template" ):
425-         return  True 
426- 
427-     return  name  in  {
428-         "agentless" ,
429-         "agentless@mappings" ,
430-         "agentless@settings" ,
431-         "apm-10d@lifecycle" ,
432-         "apm-180d@lifecycle" ,
433-         "apm-390d@lifecycle" ,
434-         "apm-90d@lifecycle" ,
435-         "apm@mappings" ,
436-         "apm@settings" ,
437-         "data-streams-mappings" ,
438-         "data-streams@mappings" ,
439-         "elastic-connectors" ,
440-         "ecs@dynamic_templates" ,
441-         "ecs@mappings" ,
442-         "ilm-history-7" ,
443-         "kibana-reporting@settings" ,
444-         "logs" ,
445-         "logs-apm.error@mappings" ,
446-         "logs-apm@settings" ,
447-         "logs-mappings" ,
448-         "logs@mappings" ,
449-         "logs-settings" ,
450-         "logs@settings" ,
451-         "metrics" ,
452-         "metrics-apm@mappings" ,
453-         "metrics-apm.service_destination@mappings" ,
454-         "metrics-apm.service_summary@mappings" ,
455-         "metrics-apm.service_transaction@mappings" ,
456-         "metrics-apm@settings" ,
457-         "metrics-apm.transaction@mappings" ,
458-         "metrics-mappings" ,
459-         "metrics@mappings" ,
460-         "metrics-settings" ,
461-         "metrics@settings" ,
462-         "metrics-tsdb-settings" ,
463-         "metrics@tsdb-settings" ,
464-         "search-acl-filter" ,
465-         "synthetics" ,
466-         "synthetics-mappings" ,
467-         "synthetics@mappings" ,
468-         "synthetics-settings" ,
469-         "synthetics@settings" ,
470-         "traces-apm@mappings" ,
471-         "traces-apm.rum@mappings" ,
472-         "traces@mappings" ,
473-         "traces@settings" ,
474-         # otel 
475-         "metrics-otel@mappings" ,
476-         "semconv-resource-to-ecs@mappings" ,
477-         "traces-otel@mappings" ,
478-         "ecs-tsdb@mappings" ,
479-         "logs-otel@mappings" ,
480-         "otel@mappings" ,
481-     }
0 commit comments