@@ -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"""
@@ -264,7 +271,7 @@ def main():
264271 """Main execution function"""
265272 print ("Starting CodeBuild deployment process..." )
266273
267- admin_email = get_env_var ("IDP_ADMIN_EMAIL" , "tanimath @amazon.com" )
274+ admin_email = get_env_var ("IDP_ADMIN_EMAIL" , "strahanr @amazon.com" )
268275 stack_prefix = generate_stack_prefix ()
269276
270277 print (f"Stack Prefix: { stack_prefix } " )
@@ -274,7 +281,6 @@ def main():
274281 # Step 1: Publish templates to S3
275282 template_url = publish_templates ()
276283
277- deployed_stacks = []
278284 all_success = True
279285
280286 # Step 2: Deploy, test, and cleanup patterns concurrently
@@ -283,7 +289,7 @@ def main():
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,35 +298,22 @@ def main():
292298 for pattern_config in DEPLOY_PATTERNS
293299 }
294300
295- # Collect results as they complete and cleanup successful deployments immediately
296- cleanup_futures = []
297- with ThreadPoolExecutor (max_workers = len (DEPLOY_PATTERNS )) as cleanup_executor :
298- for future in as_completed (future_to_pattern ):
299- pattern_config = future_to_pattern [future ]
300- try :
301- result = future .result ()
302- deployed_stacks .append (result )
303- if not result ["success" ]:
304- all_success = False
305- print (f"[{ pattern_config ['name' ]} ] ❌ Failed" )
306- else :
307- print (f"[{ pattern_config ['name' ]} ] ✅ Success" )
308-
309- # Start cleanup immediately for this stack
310- cleanup_future = cleanup_executor .submit (
311- cleanup_stack , result ["stack_name" ], result ["pattern_name" ]
312- )
313- cleanup_futures .append (cleanup_future )
314-
315- except Exception as e :
316- print (f"[{ pattern_config ['name' ]} ] ❌ Exception: { e } " )
301+ # Collect results as they complete (cleanup happens within each pattern)
302+ for future in as_completed (future_to_pattern ):
303+ pattern_config = future_to_pattern [future ]
304+ try :
305+ result = future .result ()
306+ if not result ["success" ]:
317307 all_success = False
308+ print (f"[{ pattern_config ['name' ]} ] ❌ Failed" )
309+ else :
310+ print (f"[{ pattern_config ['name' ]} ] ✅ Success" )
311+
312+ except Exception as e :
313+ print (f"[{ pattern_config ['name' ]} ] ❌ Exception: { e } " )
314+ all_success = False
318315
319- # Wait for all cleanups to complete
320- print ("🧹 Waiting for all cleanups to complete..." )
321- for future in as_completed (cleanup_futures ):
322- future .result () # Wait for completion
323-
316+ # Check final status after all cleanups are done
324317 if all_success :
325318 print ("🎉 All pattern deployments completed successfully!" )
326319 sys .exit (0 )
0 commit comments