blob: f403823a6b9bddeffcf90773311d7680e4a5c98e [file] [log] [blame]
Dmitriy Kruglov090cb192023-07-04 22:10:50 +02001import multiprocessing as mp
2
Dmitriy Kruglov6146cb42023-04-10 00:13:59 +02003import connection as conn
4from openstack.exceptions import ResourceFailure
5from typing import Final
6
7
8compute = conn.cloud.compute
9network = conn.cloud.network
10volume = conn.cloud.volume
11
12CLIENT_NAME_MASK: Final[str] = conn.FIO_CLIENT_NAME_MASK
Dmitriy Kruglov090cb192023-07-04 22:10:50 +020013AA_SERVER_GROUP_NAME: Final[str] = conn.FIO_AA_SERVER_GROUP_NAME
Dmitriy Kruglov6146cb42023-04-10 00:13:59 +020014FLAVOR_NAME: Final[str] = conn.FIO_FLAVOR_NAME
15KEYPAIR_NAME: Final[str] = conn.FIO_KEYPAIR_NAME
16SG_NAME: Final[str] = conn.FIO_SG_NAME
Ievgeniia Zadorozhna24378502023-10-02 16:38:28 +020017VOL_NAME_MASK: Final[str] = conn.FIO_VOL_NAME_MASK
Dmitriy Kruglov6146cb42023-04-10 00:13:59 +020018
19ROUTER_NAME: Final[str] = conn.FIO_ROUTER_NAME
20NET_NAME: Final[str] = conn.FIO_NET_NAME
Dmitriy Kruglov090cb192023-07-04 22:10:50 +020021CONCURRENCY: Final[int] = conn.CONCURRENCY
22
23
24def 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 Zadorozhnaa33b29a2023-08-29 18:19:47 +030034 conn.detach_volume(att, vm, vol)
Dmitriy Kruglov090cb192023-07-04 22:10:50 +020035 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 Kruglov090cb192023-07-04 22:10:50 +020040 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 Zadorozhnaa33b29a2023-08-29 18:19:47 +030045 conn.delete_server(vm)
46 print(f"'{vm.name}' server has been deleted.")
Dmitriy Kruglov6146cb42023-04-10 00:13:59 +020047
48
49if __name__ == "__main__":
Dmitriy Kruglov090cb192023-07-04 22:10:50 +020050 # 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 Kruglov6146cb42023-04-10 00:13:59 +020058
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 Zadorozhnaa33b29a2023-08-29 18:19:47 +030063 print("External GW port has been deleted from fio router.")
Dmitriy Kruglov6146cb42023-04-10 00:13:59 +020064 router_ports = network.ports(device_id=router.id)
65 for p in router_ports:
Ievgeniia Zadorozhnaa33b29a2023-08-29 18:19:47 +030066 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 Kruglov6146cb42023-04-10 00:13:59 +020069
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 Kruglov090cb192023-07-04 22:10:50 +020096
Ievgeniia Zadorozhna24378502023-10-02 16:38:28 +020097 # 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 Kruglov090cb192023-07-04 22:10:50 +0200104 # Delete fio server group
Ievgeniia Zadorozhnaa33b29a2023-08-29 18:19:47 +0300105 server_group = conn.find_server_group(AA_SERVER_GROUP_NAME)
Dmitriy Kruglov090cb192023-07-04 22:10:50 +0200106 if server_group:
107 compute.delete_server_group(server_group)
108 print(f"fio '{server_group.name}' server group has been deleted.")