@@ -116,8 +116,30 @@ def setup(client)
116116 return unless @setup
117117
118118 actions = @setup [ 'setup' ] . select { |action | action [ 'do' ] } . map { |action | Action . new ( action [ 'do' ] ) }
119+ count = 0
120+
119121 actions . each do |action |
120- action . execute ( client )
122+ begin
123+ action . execute ( client )
124+ rescue Elasticsearch ::Transport ::Transport ::Errors ::ServiceUnavailable => e
125+ # The action sometimes gets the cluster in a recovering state, so we
126+ # retry a few times and then raise an exception if it's still
127+ # happening
128+ count += 1
129+ raise e if count > 9
130+
131+ redo
132+ rescue Elasticsearch ::Transport ::Transport ::Errors ::BadRequest => e
133+ error = JSON . parse ( e . message . gsub ( /\[ [0-9]{3}\] / , '' ) ) [ 'error' ] [ 'root_cause' ] . first
134+ count += 1
135+
136+ logger = Logger . new ( $stdout)
137+ logger . error "#{ error [ 'type' ] } : #{ error [ 'reason' ] } "
138+ client . indices . delete ( index : error [ 'index' ] ) if error [ 'reason' ] =~ /index \[ .+\] already exists/
139+ raise e if count > 9
140+
141+ redo
142+ end
121143 end
122144 self
123145 end
@@ -164,26 +186,26 @@ def wipe_cluster(client)
164186 wait_for_pending_tasks ( client )
165187 clear_sml_policies ( client )
166188 end
167-
168189 clear_snapshots_and_repositories ( client )
169190 clear_datastreams ( client ) if xpack?
170191 clear_indices ( client )
171-
172192 if xpack?
193+ clear_datafeeds ( client )
173194 clear_templates_xpack ( client )
195+ clear_ml_jobs ( client )
174196 else
175197 client . indices . delete_template ( name : '*' )
176198 client . indices . delete_index_template ( name : '*' )
177199 client . cluster . delete_component_template ( name : '*' )
178200 end
179-
180201 clear_cluster_settings ( client )
181-
182202 return unless xpack?
183203
184204 clear_ilm_policies ( client )
185205 clear_auto_follow_patterns ( client )
186206 clear_tasks ( client )
207+ clear_indices ( client )
208+ clear_transforms ( client )
187209 end
188210
189211 def xpack?
@@ -355,16 +377,20 @@ def clear_transforms(client)
355377
356378 def clear_indices ( client )
357379 client . indices . delete ( index : '*' , expand_wildcards : 'all' , ignore : 404 )
358- end
359380
360- def clear_indices_xpack ( client )
361- indices = client . indices . get ( index : '_all' ) . keys . reject do |i |
362- i . start_with? ( '.security' ) || i . start_with? ( '.watches' )
381+ indices = client . indices . get ( index : '_all' , expand_wildcards : 'all' ) . keys . reject do |i |
382+ i . start_with? ( '.security' ) || i . start_with? ( '.watches' ) || i . start_with? ( '.ds-' )
363383 end
364384 indices . each do |index |
365385 client . indices . delete_alias ( index : index , name : '*' , ignore : 404 )
366386 client . indices . delete ( index : index , ignore : 404 )
367387 end
388+ rescue Elasticsearch ::Transport ::Transport ::Errors ::BadRequest => e
389+ raise e unless e . message . include? ( 'is the write index for data stream' )
390+
391+ error = JSON . parse ( e . message . gsub ( /\[ [0-9]{3}\] / , '' ) ) [ 'error' ] [ 'root_cause' ] . first
392+ logger = Logger . new ( $stdout)
393+ logger . error "#{ error [ 'type' ] } : #{ error [ 'reason' ] } "
368394 end
369395 end
370396 end
0 commit comments