Dmitriy Kruglov | 6146cb4 | 2023-04-10 00:13:59 +0200 | [diff] [blame^] | 1 | import connection as conn |
| 2 | from openstack.exceptions import ResourceFailure |
| 3 | from typing import Final |
| 4 | |
| 5 | |
| 6 | compute = conn.cloud.compute |
| 7 | network = conn.cloud.network |
| 8 | volume = conn.cloud.volume |
| 9 | |
| 10 | CLIENT_NAME_MASK: Final[str] = conn.FIO_CLIENT_NAME_MASK |
| 11 | FLAVOR_NAME: Final[str] = conn.FIO_FLAVOR_NAME |
| 12 | KEYPAIR_NAME: Final[str] = conn.FIO_KEYPAIR_NAME |
| 13 | SG_NAME: Final[str] = conn.FIO_SG_NAME |
| 14 | |
| 15 | ROUTER_NAME: Final[str] = conn.FIO_ROUTER_NAME |
| 16 | NET_NAME: Final[str] = conn.FIO_NET_NAME |
| 17 | |
| 18 | |
| 19 | if __name__ == "__main__": |
| 20 | # Find fio clients and server |
| 21 | vms = compute.servers(name=CLIENT_NAME_MASK) |
| 22 | for vm in vms: |
| 23 | attachments = compute.volume_attachments(vm) |
| 24 | # Delete fio volume attachment (and any other attachments |
| 25 | # that the VM could have) |
| 26 | # Delete the volume and the server |
| 27 | for att in attachments: |
| 28 | vol_id = att.volume_id |
| 29 | vol = volume.get_volume(vol_id) |
| 30 | try: |
| 31 | conn.detach_volume(vm, vol) |
| 32 | print( |
| 33 | f"'{vol.id}' volume has been detached from fio '{vm.name}'" |
| 34 | " server.") |
| 35 | conn.delete_volume(vol) |
| 36 | print(f"'{vol.id}' volume has been deleted.") |
| 37 | conn.delete_server(vm) |
| 38 | print(f"'{vm.name}' server has been deleted.") |
| 39 | except ResourceFailure as e: |
| 40 | print( |
| 41 | f"Cleanup of '{vm.id}' with volume '{vol.id}' attached " |
| 42 | f"failed with '{e.message}' error.") |
| 43 | conn.delete_volume(vol) |
| 44 | continue |
| 45 | |
| 46 | # Remove ports from fio router (including external GW) |
| 47 | router = network.find_router(ROUTER_NAME) |
| 48 | if router: |
| 49 | network.update_router(router.id, external_gateway_info={}) |
| 50 | print("Externa GW port has been deleted from fio router.") |
| 51 | router_ports = network.ports(device_id=router.id) |
| 52 | for p in router_ports: |
| 53 | network.remove_interface_from_router(router.id, port_id=p.id) |
| 54 | print(f"'{p.id}' port has been deleted from fio router.") |
| 55 | |
| 56 | # Delete fio network topology |
| 57 | net = network.find_network(NET_NAME) |
| 58 | if net: |
| 59 | network.delete_network(net.id) |
| 60 | print(f"fio '{net.id}' network has been deleted.") |
| 61 | if router: |
| 62 | network.delete_router(router.id) |
| 63 | print(f"fio '{router.id}' router has been deleted.") |
| 64 | |
| 65 | # Delete fio flavor |
| 66 | flavor = compute.find_flavor(FLAVOR_NAME) |
| 67 | if flavor: |
| 68 | compute.delete_flavor(flavor.id) |
| 69 | print(f"fio '{flavor.id}' flavor has been deleted.") |
| 70 | |
| 71 | # # Delete fio keypair |
| 72 | kp = compute.find_keypair(KEYPAIR_NAME) |
| 73 | if kp: |
| 74 | compute.delete_keypair(kp) |
| 75 | print(f"fio '{kp.id}' keypair has been deleted.") |
| 76 | |
| 77 | # Delete fio security group |
| 78 | sg = network.find_security_group(SG_NAME) |
| 79 | if sg: |
| 80 | network.delete_security_group(sg) |
| 81 | print(f"fio '{sg.id}' security group has been deleted.") |