38
38
BOLD_W = '\x1b [1;37m' # white
39
39
RESET = '\x1b [0m'
40
40
41
+ INFRA_PREFIX = 'apim-infra-'
42
+ SAMPLE_PREFIX = 'apim-sample-'
43
+
41
44
# Thread colors for parallel operations
42
45
THREAD_COLORS = [BOLD_B , BOLD_G , BOLD_Y , BOLD_C , BOLD_M , BOLD_W ]
43
46
@@ -418,7 +421,7 @@ def _get_current_index(self) -> int | None:
418
421
int | None: The index if it exists, None otherwise.
419
422
"""
420
423
421
- prefix = f'apim-infra- { self .deployment .value } '
424
+ prefix = f'{ INFRA_PREFIX } { self .deployment .value } '
422
425
423
426
if self .rg_name == prefix :
424
427
return None
@@ -600,8 +603,8 @@ def _find_infrastructure_instances(self, infrastructure: INFRASTRUCTURE) -> list
600
603
601
604
for rg_name in rg_names :
602
605
# Parse the resource group name to extract the index
603
- # Expected format: apim-infra-{ infrastructure}-{index} or apim-infra- {infrastructure}
604
- prefix = f'apim-infra- { infrastructure .value } '
606
+ # Expected format: {INFRA_PREFIX}{ infrastructure}-{index} or {INFRA_PREFIX} {infrastructure}
607
+ prefix = f'{ INFRA_PREFIX } { infrastructure .value } '
605
608
606
609
if rg_name == prefix :
607
610
# No index
@@ -722,7 +725,7 @@ def _cleanup_resources(deployment_name: str, rg_name: str) -> None:
722
725
print_info (f'Resource group : { rg_name } ' )
723
726
724
727
# Show the deployment details
725
- output = run (f'az deployment group show --name { deployment_name } -g { rg_name } -o json' , 'Deployment retrieved' , 'Failed to retrieve the deployment' , print_command_to_run = False )
728
+ output = run (f'az deployment group show --name { deployment_name } -g { rg_name } -o json' , 'Deployment retrieved' , 'Failed to retrieve the deployment' , print_command_to_run = False , print_errors = False )
726
729
727
730
if output .success and output .json_data :
728
731
# Delete and purge CognitiveService accounts
@@ -780,7 +783,6 @@ def _cleanup_resources(deployment_name: str, rg_name: str) -> None:
780
783
781
784
except Exception as e :
782
785
print (f'An error occurred during cleanup: { e } ' )
783
- traceback .print_exc ()
784
786
785
787
def _print_log (message : str , prefix : str = '' , color : str = '' , output : str = '' , duration : str = '' , show_time : bool = False , blank_above : bool = False , blank_below : bool = False , wrap_lines : bool = False ) -> None :
786
788
"""
@@ -1380,16 +1382,13 @@ def _cleanup_resources_thread_safe(deployment_name: str, rg_name: str, thread_pr
1380
1382
# Create a modified version of _cleanup_resources that uses thread-safe printing
1381
1383
_cleanup_resources_with_thread_safe_printing (deployment_name , rg_name , thread_prefix , thread_color )
1382
1384
1383
- with _print_lock :
1384
- _print_log (f"{ thread_prefix } Completed cleanup for resource group: { rg_name } " , '👉🏽 ' , thread_color )
1385
-
1385
+ # Don't print completion message here - it will be printed in the main function with progress
1386
1386
return True , ""
1387
1387
1388
1388
except Exception as e :
1389
1389
error_msg = f'An error occurred during cleanup of { rg_name } : { str (e )} '
1390
1390
with _print_lock :
1391
1391
_print_log (f"{ thread_prefix } { error_msg } " , '⛔ ' , BOLD_R , show_time = True )
1392
- traceback .print_exc ()
1393
1392
return False , error_msg
1394
1393
1395
1394
@@ -1413,7 +1412,7 @@ def _cleanup_resources_with_thread_safe_printing(deployment_name: str, rg_name:
1413
1412
_print_log (f"{ thread_prefix } Resource group : { rg_name } " , '👉🏽 ' , thread_color )
1414
1413
1415
1414
# Show the deployment details
1416
- output = run (f'az deployment group show --name { deployment_name } -g { rg_name } -o json' , 'Deployment retrieved' , 'Failed to retrieve the deployment' , print_command_to_run = False )
1415
+ output = run (f'az deployment group show --name { deployment_name } -g { rg_name } -o json' , 'Deployment retrieved' , 'Failed to retrieve the deployment' , print_command_to_run = False , print_errors = False )
1417
1416
1418
1417
if output .success and output .json_data :
1419
1418
# Delete and purge CognitiveService accounts
@@ -1530,7 +1529,8 @@ def cleanup_infra_deployments(deployment: INFRASTRUCTURE, indexes: int | list[in
1530
1529
})
1531
1530
1532
1531
# Execute cleanup tasks in parallel
1533
- with ThreadPoolExecutor (max_workers = max_workers ) as executor :
1532
+ print_info (f'Executing cleanup tasks in parallel with up to { max_workers } workers.' , False )
1533
+ with ThreadPoolExecutor (max_workers = max_workers ) as executor :
1534
1534
# Submit all tasks
1535
1535
future_to_task = {
1536
1536
executor .submit (
@@ -1551,20 +1551,35 @@ def cleanup_infra_deployments(deployment: INFRASTRUCTURE, indexes: int | list[in
1551
1551
task = future_to_task [future ]
1552
1552
try :
1553
1553
success , error_msg = future .result ()
1554
- completed_count += 1
1555
1554
1556
1555
if success :
1556
+ completed_count += 1
1557
+ # Calculate status counts
1558
+ total_count = len (indexes_list )
1559
+ in_progress_count = total_count - completed_count - failed_count
1560
+
1557
1561
with _print_lock :
1558
- print_ok (f"Completed cleanup for { deployment .value } -{ task ['index' ]} ({ completed_count } /{ len (indexes_list )} )" )
1562
+ print_ok (f"Completed cleanup for resource group: { task ['rg_name' ]} " , blank_above = False )
1563
+ print_info (f"📊 Progress: { completed_count } /{ total_count } completed, { failed_count } failed, { in_progress_count } remaining" )
1559
1564
else :
1560
1565
failed_count += 1
1566
+ # Calculate status counts
1567
+ total_count = len (indexes_list )
1568
+ in_progress_count = total_count - completed_count - failed_count
1569
+
1561
1570
with _print_lock :
1562
1571
print_error (f"❌ Failed cleanup for { deployment .value } -{ task ['index' ]} : { error_msg } " )
1572
+ print_info (f"📊 Progress: { completed_count } /{ total_count } completed, { failed_count } failed, { in_progress_count } remaining" )
1563
1573
1564
1574
except Exception as e :
1565
1575
failed_count += 1
1576
+ # Calculate status counts
1577
+ total_count = len (indexes_list )
1578
+ in_progress_count = total_count - completed_count - failed_count
1579
+
1566
1580
with _print_lock :
1567
1581
print_error (f"❌ Exception during cleanup for { deployment .value } -{ task ['index' ]} : { str (e )} " )
1582
+ print_info (f"📊 Progress: { completed_count } /{ total_count } completed, { failed_count } failed, { in_progress_count } remaining" )
1568
1583
1569
1584
# Final summary
1570
1585
if failed_count == 0 :
@@ -1789,7 +1804,7 @@ def get_infra_rg_name(deployment_name: INFRASTRUCTURE, index: int | None = None)
1789
1804
str: The generated resource group name.
1790
1805
"""
1791
1806
1792
- rg_name = f'apim-infra- { deployment_name .value } '
1807
+ rg_name = f'{ INFRA_PREFIX } { deployment_name .value } '
1793
1808
1794
1809
if index is not None :
1795
1810
rg_name = f'{ rg_name } -{ index } '
@@ -1808,7 +1823,7 @@ def get_rg_name(deployment_name: str, index: int | None = None) -> str:
1808
1823
str: The generated resource group name.
1809
1824
"""
1810
1825
1811
- rg_name = f'apim-sample- { deployment_name } '
1826
+ rg_name = f'{ SAMPLE_PREFIX } { deployment_name } '
1812
1827
1813
1828
if index is not None :
1814
1829
rg_name = f'{ rg_name } -{ str (index )} '
0 commit comments