|
| 1 | +""" |
| 2 | +Delete old Azure Virtual Machines and related orphaned resources. |
| 3 | +
|
| 4 | +Run with the shell script: delete_old_azure_resources.sh |
| 5 | +""" |
| 6 | + |
| 7 | +import argparse |
| 8 | +import datetime |
| 9 | +import os |
| 10 | + |
| 11 | +from azure.identity import DefaultAzureCredential |
| 12 | +from azure.mgmt.compute import ComputeManagementClient |
| 13 | +from azure.mgmt.network import NetworkManagementClient |
| 14 | + |
| 15 | + |
| 16 | +def main(): |
| 17 | + # Parse args: |
| 18 | + parser = argparse.ArgumentParser() |
| 19 | + parser.add_argument("--dry-run", action="store_true") |
| 20 | + args = parser.parse_args() |
| 21 | + |
| 22 | + # Create clients: |
| 23 | + sub_id = os.getenv("AZURE_SUBSCRIPTION_ID") |
| 24 | + resource_group_name = os.getenv("AZURE_RESOURCE_GROUP") |
| 25 | + cmclient = ComputeManagementClient( |
| 26 | + credential=DefaultAzureCredential(), subscription_id=sub_id |
| 27 | + ) |
| 28 | + nmclient = NetworkManagementClient( |
| 29 | + credential=DefaultAzureCredential(), subscription_id=sub_id |
| 30 | + ) |
| 31 | + |
| 32 | + # Delete old Virtual Machines: |
| 33 | + vm_names = [] |
| 34 | + for vm in cmclient.virtual_machines.list(resource_group_name): |
| 35 | + try: |
| 36 | + now = datetime.datetime.now(tz=datetime.timezone.utc) |
| 37 | + delta = now - vm.time_created |
| 38 | + if delta < datetime.timedelta(hours=2): |
| 39 | + print( |
| 40 | + f"{vm.name} is less than 2 hours old. Age is: {delta} ... skipping" |
| 41 | + ) |
| 42 | + continue |
| 43 | + vm_names.append(vm.name) |
| 44 | + except Exception as e: |
| 45 | + print(f"Exception occurred: {e}") |
| 46 | + print(f"Detected old Virtual Machines: {vm_names}") |
| 47 | + if args.dry_run: |
| 48 | + print("Dry run detected. Not deleting.") |
| 49 | + else: |
| 50 | + for vm_name in vm_names: |
| 51 | + try: |
| 52 | + print(f"Deleting Virtual Machine '{vm_name}' ...") |
| 53 | + cmclient.virtual_machines.begin_delete( |
| 54 | + resource_group_name, vm_name |
| 55 | + ).result() |
| 56 | + print(f"Deleting Virtual Machine '{vm_name}' ... done") |
| 57 | + except Exception as e: |
| 58 | + print(f"Exception occurred: {e}") |
| 59 | + |
| 60 | + # Get list of all Virtual Machine names to detect orphaned resources: |
| 61 | + all_vm_names = [] # Example: `vmname-RUBY-10561` |
| 62 | + for vm in cmclient.virtual_machines.list(resource_group_name): |
| 63 | + all_vm_names.append(vm.name) |
| 64 | + |
| 65 | + # Delete orphaned NSGs: |
| 66 | + orphan_nsg_names = [] |
| 67 | + for nsg in nmclient.network_security_groups.list(resource_group_name): |
| 68 | + is_orphan = True |
| 69 | + for vm_name in all_vm_names: |
| 70 | + if vm_name + "-NSG" == nsg.name: |
| 71 | + is_orphan = False |
| 72 | + break |
| 73 | + if is_orphan: |
| 74 | + orphan_nsg_names.append(nsg.name) |
| 75 | + print(f"Detected orphaned NSGs: {orphan_nsg_names}") |
| 76 | + if args.dry_run: |
| 77 | + print("Dry run detected. Not deleting.") |
| 78 | + else: |
| 79 | + for nsg_name in orphan_nsg_names: |
| 80 | + try: |
| 81 | + print(f"Deleting orphaned NSG '{nsg_name}' ...") |
| 82 | + nmclient.network_security_groups.begin_delete( |
| 83 | + resource_group_name, nsg_name |
| 84 | + ).result() |
| 85 | + print(f"Deleting orphaned NSG '{nsg_name}' ... done") |
| 86 | + except Exception as e: |
| 87 | + print(f"Exception occurred: {e}") |
| 88 | + |
| 89 | + # Delete orphaned IPs: |
| 90 | + orphan_ip_names = [] |
| 91 | + for ip in nmclient.public_ip_addresses.list(resource_group_name): |
| 92 | + is_orphan = True |
| 93 | + for vm_name in all_vm_names: |
| 94 | + if vm_name + "-PUBLIC-IP" == ip.name: |
| 95 | + is_orphan = False |
| 96 | + break |
| 97 | + if is_orphan: |
| 98 | + orphan_ip_names.append(ip.name) |
| 99 | + print(f"Detected orphaned IPs: {orphan_ip_names}") |
| 100 | + if args.dry_run: |
| 101 | + print("Dry run detected. Not deleting.") |
| 102 | + else: |
| 103 | + for ip_name in orphan_ip_names: |
| 104 | + try: |
| 105 | + print(f"Deleting orphaned IP '{ip_name}' ...") |
| 106 | + nmclient.public_ip_addresses.begin_delete( |
| 107 | + resource_group_name, ip_name |
| 108 | + ).result() |
| 109 | + print(f"Deleting orphaned IP '{ip_name}' ... done") |
| 110 | + except Exception as e: |
| 111 | + print(f"Exception occurred: {e}") |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == "__main__": |
| 115 | + main() |
0 commit comments