@@ -97,7 +97,7 @@ def publish_templates():
9797 sys .exit (1 )
9898
9999
100- def deploy_and_test_pattern (stack_prefix , pattern_config , admin_email , template_url ):
100+ def deploy_test_and_cleanup_pattern (stack_prefix , pattern_config , admin_email , template_url ):
101101 """Deploy and test a specific IDP pattern"""
102102 pattern_name = pattern_config ["name" ]
103103 pattern_id = pattern_config ["id" ]
@@ -195,28 +195,35 @@ def deploy_and_test_pattern(stack_prefix, pattern_config, admin_email, template_
195195 print (
196196 f"[{ pattern_name } ] ✅ Found expected verification string: '{ verify_string } '"
197197 )
198- return {
198+
199+ success_result = {
199200 "stack_name" : stack_name ,
200201 "pattern_name" : pattern_name ,
201202 "success" : True ,
202203 }
203204
204205 except Exception as e :
205206 print (f"[{ pattern_name } ] ❌ Failed to validate result content: { e } " )
206- return {
207+ success_result = {
207208 "stack_name" : stack_name ,
208209 "pattern_name" : pattern_name ,
209210 "success" : False ,
210211 }
211212
212213 except Exception as e :
213214 print (f"[{ pattern_name } ] ❌ Testing failed: { e } " )
214- return {
215+ success_result = {
215216 "stack_name" : stack_name ,
216217 "pattern_name" : pattern_name ,
217218 "success" : False ,
218219 }
219220
221+ # Always cleanup the stack regardless of success/failure
222+ finally :
223+ cleanup_stack (stack_name , pattern_name )
224+
225+ return success_result
226+
220227
221228def cleanup_stack (stack_name , pattern_name ):
222229 """Clean up a deployed stack"""
@@ -274,16 +281,15 @@ def main():
274281 # Step 1: Publish templates to S3
275282 template_url = publish_templates ()
276283
277- deployed_stacks = []
278284 all_success = True
279285
280- # Step 2: Deploy and test patterns concurrently
286+ # Step 2: Deploy, test, and cleanup patterns concurrently
281287 print ("🚀 Starting concurrent deployment of all patterns..." )
282288 with ThreadPoolExecutor (max_workers = len (DEPLOY_PATTERNS )) as executor :
283289 # Submit all deployment tasks
284290 future_to_pattern = {
285291 executor .submit (
286- deploy_and_test_pattern ,
292+ deploy_test_and_cleanup_pattern ,
287293 stack_prefix ,
288294 pattern_config ,
289295 admin_email ,
@@ -292,33 +298,22 @@ def main():
292298 for pattern_config in DEPLOY_PATTERNS
293299 }
294300
295- # Collect results as they complete
301+ # Collect results as they complete (cleanup happens within each pattern)
296302 for future in as_completed (future_to_pattern ):
297303 pattern_config = future_to_pattern [future ]
298304 try :
299305 result = future .result ()
300- deployed_stacks .append (result )
301306 if not result ["success" ]:
302307 all_success = False
303308 print (f"[{ pattern_config ['name' ]} ] ❌ Failed" )
304309 else :
305310 print (f"[{ pattern_config ['name' ]} ] ✅ Success" )
311+
306312 except Exception as e :
307313 print (f"[{ pattern_config ['name' ]} ] ❌ Exception: { e } " )
308314 all_success = False
309315
310- # Step 3: Cleanup all stacks concurrently
311- print ("🧹 Starting concurrent cleanup of all stacks..." )
312- with ThreadPoolExecutor (max_workers = len (deployed_stacks )) as executor :
313- cleanup_futures = [
314- executor .submit (cleanup_stack , result ["stack_name" ], result ["pattern_name" ])
315- for result in deployed_stacks
316- ]
317-
318- # Wait for all cleanups to complete
319- for future in as_completed (cleanup_futures ):
320- future .result () # Wait for completion
321-
316+ # Check final status after all cleanups are done
322317 if all_success :
323318 print ("🎉 All pattern deployments completed successfully!" )
324319 sys .exit (0 )
0 commit comments