blob: 715e01dd3309e314c860a26e896dfad3dacab8a2 [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 Zadorozhna1a7eb392023-10-02 23:44:41 +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:
34 conn.detach_volume(vm, vol)
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 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 Zadorozhna1a7eb392023-10-02 23:44:41 +020045 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={})
63 print("Externa GW port has been deleted from fio router.")
64 router_ports = network.ports(device_id=router.id)
65 for p in router_ports:
66 network.remove_interface_from_router(router.id, port_id=p.id)
67 print(f"'{p.id}' port has been deleted from fio router.")
68
69 # Delete fio network topology
70 net = network.find_network(NET_NAME)
71 if net:
72 network.delete_network(net.id)
73 print(f"fio '{net.id}' network has been deleted.")
74 if router:
75 network.delete_router(router.id)
76 print(f"fio '{router.id}' router has been deleted.")
77
78 # Delete fio flavor
79 flavor = compute.find_flavor(FLAVOR_NAME)
80 if flavor:
81 compute.delete_flavor(flavor.id)
82 print(f"fio '{flavor.id}' flavor has been deleted.")
83
84 # # Delete fio keypair
85 kp = compute.find_keypair(KEYPAIR_NAME)
86 if kp:
87 compute.delete_keypair(kp)
88 print(f"fio '{kp.id}' keypair has been deleted.")
89
90 # Delete fio security group
91 sg = network.find_security_group(SG_NAME)
92 if sg:
93 network.delete_security_group(sg)
94 print(f"fio '{sg.id}' security group has been deleted.")
Dmitriy Kruglov090cb192023-07-04 22:10:50 +020095
Ievgeniia Zadorozhna1a7eb392023-10-02 23:44:41 +020096 # Delete the orphan fio volumes which are not attached (if any)
97 volumes = volume.volumes(VOL_NAME_MASK)
98 for v in volumes:
99 if not v.attachments:
100 volume.delete_volume(v.id)
101 print(f"'{v.id}' volume has been deleted.")
102
Dmitriy Kruglov090cb192023-07-04 22:10:50 +0200103 # Delete fio server group
104 server_group = compute.find_server_group(
105 AA_SERVER_GROUP_NAME, all_projects=True)
106 if server_group:
107 compute.delete_server_group(server_group)
108 print(f"fio '{server_group.name}' server group has been deleted.")