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