Dmitriy Kruglov | 090cb19 | 2023-07-04 22:10:50 +0200 | [diff] [blame] | 1 | import multiprocessing as mp |
| 2 | |
Dmitriy Kruglov | 6146cb4 | 2023-04-10 00:13:59 +0200 | [diff] [blame] | 3 | import connection as conn |
| 4 | from openstack.exceptions import ResourceFailure |
| 5 | from typing import Final |
| 6 | |
| 7 | |
| 8 | compute = conn.cloud.compute |
| 9 | network = conn.cloud.network |
| 10 | volume = conn.cloud.volume |
| 11 | |
| 12 | CLIENT_NAME_MASK: Final[str] = conn.FIO_CLIENT_NAME_MASK |
Dmitriy Kruglov | 090cb19 | 2023-07-04 22:10:50 +0200 | [diff] [blame] | 13 | AA_SERVER_GROUP_NAME: Final[str] = conn.FIO_AA_SERVER_GROUP_NAME |
Dmitriy Kruglov | 6146cb4 | 2023-04-10 00:13:59 +0200 | [diff] [blame] | 14 | FLAVOR_NAME: Final[str] = conn.FIO_FLAVOR_NAME |
| 15 | KEYPAIR_NAME: Final[str] = conn.FIO_KEYPAIR_NAME |
| 16 | SG_NAME: Final[str] = conn.FIO_SG_NAME |
| 17 | |
| 18 | ROUTER_NAME: Final[str] = conn.FIO_ROUTER_NAME |
| 19 | NET_NAME: Final[str] = conn.FIO_NET_NAME |
Dmitriy Kruglov | 090cb19 | 2023-07-04 22:10:50 +0200 | [diff] [blame] | 20 | CONCURRENCY: Final[int] = conn.CONCURRENCY |
| 21 | |
| 22 | |
| 23 | def delete_fio_client(vm_id: str) -> None: |
| 24 | vm = compute.get_server(vm_id) |
| 25 | attachments = compute.volume_attachments(vm) |
| 26 | # Delete fio volume attachment (and any other attachments |
| 27 | # that the VM could have) |
| 28 | # Delete the volume and the server |
| 29 | for att in attachments: |
| 30 | vol_id = att.volume_id |
| 31 | vol = volume.get_volume(vol_id) |
| 32 | try: |
| 33 | conn.detach_volume(vm, vol) |
| 34 | print( |
| 35 | f"'{vol.id}' volume has been detached from fio '{vm.name}'" |
| 36 | " server.") |
| 37 | conn.delete_volume(vol) |
| 38 | print(f"'{vol.id}' volume has been deleted.") |
| 39 | conn.delete_server(vm) |
| 40 | print(f"'{vm.name}' server has been deleted.") |
| 41 | except ResourceFailure as e: |
| 42 | print( |
| 43 | f"Cleanup of '{vm.id}' with volume '{vol.id}' attached " |
| 44 | f"failed with '{e.message}' error.") |
| 45 | conn.delete_volume(vol) |
Dmitriy Kruglov | 6146cb4 | 2023-04-10 00:13:59 +0200 | [diff] [blame] | 46 | |
| 47 | |
| 48 | if __name__ == "__main__": |
Dmitriy Kruglov | 090cb19 | 2023-07-04 22:10:50 +0200 | [diff] [blame] | 49 | # Find fio VMs |
| 50 | vms = list(compute.servers(name=CLIENT_NAME_MASK, details=False)) |
| 51 | |
| 52 | # Delete fio VMs in parallel in batches of CONCURRENCY size |
| 53 | with mp.Pool(processes=CONCURRENCY) as pool: |
| 54 | results = [pool.apply_async(delete_fio_client, (vm.id,)) for vm in vms] |
| 55 | # Waits for batch of fio VMs to be deleted |
| 56 | _ = [r.get() for r in results] |
Dmitriy Kruglov | 6146cb4 | 2023-04-10 00:13:59 +0200 | [diff] [blame] | 57 | |
| 58 | # Remove ports from fio router (including external GW) |
| 59 | router = network.find_router(ROUTER_NAME) |
| 60 | if router: |
| 61 | network.update_router(router.id, external_gateway_info={}) |
| 62 | print("Externa GW port has been deleted from fio router.") |
| 63 | router_ports = network.ports(device_id=router.id) |
| 64 | for p in router_ports: |
| 65 | network.remove_interface_from_router(router.id, port_id=p.id) |
| 66 | print(f"'{p.id}' port has been deleted from fio router.") |
| 67 | |
| 68 | # Delete fio network topology |
| 69 | net = network.find_network(NET_NAME) |
| 70 | if net: |
| 71 | network.delete_network(net.id) |
| 72 | print(f"fio '{net.id}' network has been deleted.") |
| 73 | if router: |
| 74 | network.delete_router(router.id) |
| 75 | print(f"fio '{router.id}' router has been deleted.") |
| 76 | |
| 77 | # Delete fio flavor |
| 78 | flavor = compute.find_flavor(FLAVOR_NAME) |
| 79 | if flavor: |
| 80 | compute.delete_flavor(flavor.id) |
| 81 | print(f"fio '{flavor.id}' flavor has been deleted.") |
| 82 | |
| 83 | # # Delete fio keypair |
| 84 | kp = compute.find_keypair(KEYPAIR_NAME) |
| 85 | if kp: |
| 86 | compute.delete_keypair(kp) |
| 87 | print(f"fio '{kp.id}' keypair has been deleted.") |
| 88 | |
| 89 | # Delete fio security group |
| 90 | sg = network.find_security_group(SG_NAME) |
| 91 | if sg: |
| 92 | network.delete_security_group(sg) |
| 93 | print(f"fio '{sg.id}' security group has been deleted.") |
Dmitriy Kruglov | 090cb19 | 2023-07-04 22:10:50 +0200 | [diff] [blame] | 94 | |
| 95 | # Delete fio server group |
| 96 | server_group = compute.find_server_group( |
| 97 | AA_SERVER_GROUP_NAME, all_projects=True) |
| 98 | if server_group: |
| 99 | compute.delete_server_group(server_group) |
| 100 | print(f"fio '{server_group.name}' server group has been deleted.") |