4 # Cleans possible leftovers after running functest tests:
9 # - Neutron networks, subnets and ports
14 # jose.lausuch@ericsson.com
17 # All rights reserved. This program and the accompanying materials
18 # are made available under the terms of the Apache License, Version 2.0
19 # which accompanies this distribution, and is available at
20 # http://www.apache.org/licenses/LICENSE-2.0
24 import functest.utils.functest_logger as ft_logger
25 import functest.utils.functest_utils as ft_utils
26 import functest.utils.openstack_utils as os_utils
30 logger = ft_logger.Logger("openstack_clean").getLogger()
32 OS_SNAPSHOT_FILE = ft_utils.get_parameter_from_yaml(
33 "general.openstack.snapshot_file")
37 logger.debug("-------------------------------------------")
40 def remove_instances(nova_client, default_instances):
41 logger.debug("Removing Nova instances...")
42 instances = os_utils.get_instances(nova_client)
43 if instances is None or len(instances) == 0:
44 logger.debug("No instances found.")
47 for instance in instances:
48 instance_name = getattr(instance, 'name')
49 instance_id = getattr(instance, 'id')
50 logger.debug("'%s', ID=%s " % (instance_name, instance_id))
51 if (instance_id not in default_instances and
52 instance_name not in default_instances.values()):
53 logger.debug("Removing instance '%s' ..." % instance_id)
54 if os_utils.delete_instance(nova_client, instance_id):
55 logger.debug(" > Request sent.")
57 logger.error("There has been a problem removing the "
58 "instance %s..." % instance_id)
60 logger.debug(" > this is a default instance and will "
65 instances = os_utils.get_instances(nova_client)
66 for instance in instances:
67 instance_id = getattr(instance, 'id')
68 if instance_id not in default_instances:
69 logger.debug("Waiting for instances to be terminated...")
76 def remove_images(nova_client, default_images):
77 logger.debug("Removing Glance images...")
78 images = os_utils.get_images(nova_client)
79 if images is None or len(images) == 0:
80 logger.debug("No images found.")
84 image_name = getattr(image, 'name')
85 image_id = getattr(image, 'id')
86 logger.debug("'%s', ID=%s " % (image_name, image_id))
87 if (image_id not in default_images and
88 image_name not in default_images.values()):
89 logger.debug("Removing image '%s', ID=%s ..."
90 % (image_name, image_id))
91 if os_utils.delete_glance_image(nova_client, image_id):
92 logger.debug(" > Done!")
94 logger.error("There has been a problem removing the"
95 "image %s..." % image_id)
97 logger.debug(" > this is a default image and will "
101 def remove_volumes(cinder_client, default_volumes):
102 logger.debug("Removing Cinder volumes...")
103 volumes = os_utils.get_volumes(cinder_client)
104 if volumes is None or len(volumes) == 0:
105 logger.debug("No volumes found.")
108 for volume in volumes:
109 volume_id = getattr(volume, 'id')
110 volume_name = getattr(volume, 'display_name')
111 logger.debug("'%s', ID=%s " % (volume_name, volume_id))
112 if (volume_id not in default_volumes and
113 volume_name not in default_volumes.values()):
114 logger.debug("Removing cinder volume %s ..." % volume_id)
115 if os_utils.delete_volume(cinder_client, volume_id):
116 logger.debug(" > Done!")
118 logger.debug("Trying forced removal...")
119 if os_utils.delete_volume(cinder_client,
122 logger.debug(" > Done!")
124 logger.error("There has been a problem removing the "
125 "volume %s..." % volume_id)
127 logger.debug(" > this is a default volume and will "
131 def remove_floatingips(nova_client, default_floatingips):
132 logger.debug("Removing floating IPs...")
133 floatingips = os_utils.get_floating_ips(nova_client)
134 if floatingips is None or len(floatingips) == 0:
135 logger.debug("No floating IPs found.")
138 init_len = len(floatingips)
140 for fip in floatingips:
141 fip_id = getattr(fip, 'id')
142 fip_ip = getattr(fip, 'ip')
143 logger.debug("'%s', ID=%s " % (fip_ip, fip_id))
144 if (fip_id not in default_floatingips and
145 fip_ip not in default_floatingips.values()):
146 logger.debug("Removing floating IP %s ..." % fip_id)
147 if os_utils.delete_floating_ip(nova_client, fip_id):
148 logger.debug(" > Done!")
151 logger.error("There has been a problem removing the "
152 "floating IP %s..." % fip_id)
154 logger.debug(" > this is a default floating IP and will "
159 floatingips = os_utils.get_floating_ips(nova_client)
160 if floatingips is None or len(floatingips) == (init_len - deleted):
163 logger.debug("Waiting for floating ips to be released...")
168 def remove_networks(neutron_client, default_networks, default_routers):
169 logger.debug("Removing Neutron objects")
171 networks = os_utils.get_network_list(neutron_client)
173 logger.debug("There are no networks in the deployment. ")
175 logger.debug("Existing networks:")
176 for network in networks:
177 net_id = network['id']
178 net_name = network['name']
179 logger.debug(" '%s', ID=%s " % (net_name, net_id))
180 if (net_id in default_networks and
181 net_name in default_networks.values()):
182 logger.debug(" > this is a default network and will "
184 elif network['router:external'] is True:
185 logger.debug(" > this is an external network and will "
188 logger.debug(" > this network will be deleted.")
189 network_ids.append(net_id)
192 ports = os_utils.get_port_list(neutron_client)
194 logger.debug("There are no ports in the deployment. ")
196 remove_ports(neutron_client, ports, network_ids)
199 routers = os_utils.get_router_list(neutron_client)
201 logger.debug("There are no routers in the deployment. ")
203 remove_routers(neutron_client, routers, default_routers)
205 # trozet: wait for Neutron to auto-cleanup HA networks when HA router is
210 if network_ids is not None:
211 for net_id in network_ids:
212 networks = os_utils.get_network_list(neutron_client)
214 logger.debug("No networks left to remove")
216 elif not any(network['id'] == net_id for network in networks):
217 logger.debug("Network %s has already been removed" % net_id)
219 logger.debug("Removing network %s ..." % net_id)
220 if os_utils.delete_neutron_net(neutron_client, net_id):
221 logger.debug(" > Done!")
223 logger.error("There has been a problem removing the "
224 "network %s..." % net_id)
227 def remove_ports(neutron_client, ports, network_ids):
229 if port['network_id'] in network_ids:
232 subnet_id = port['fixed_ips'][0]['subnet_id']
234 logger.debug(" > WARNING: Port %s does not contain fixed_ips"
237 router_id = port['device_id']
238 if len(port['fixed_ips']) == 0 and router_id == '':
239 logger.debug("Removing port %s ..." % port_id)
240 if (os_utils.delete_neutron_port(neutron_client, port_id)):
241 logger.debug(" > Done!")
243 logger.error("There has been a problem removing the "
244 "port %s ..." % port_id)
245 force_remove_port(neutron_client, port_id)
247 elif port['device_owner'] == 'network:router_interface':
248 logger.debug("Detaching port %s (subnet %s) from router %s ..."
249 % (port_id, subnet_id, router_id))
250 if os_utils.remove_interface_router(
251 neutron_client, router_id, subnet_id):
252 time.sleep(5) # leave 5 seconds to detach
253 logger.debug(" > Done!")
255 logger.error("There has been a problem removing the "
256 "interface %s from router %s..."
257 % (subnet_id, router_id))
258 force_remove_port(neutron_client, port_id)
260 force_remove_port(neutron_client, port_id)
263 def force_remove_port(neutron_client, port_id):
264 logger.debug("Clearing device_owner for port %s ..." % port_id)
265 os_utils.update_neutron_port(neutron_client, port_id,
266 device_owner='clear')
267 logger.debug("Removing port %s ..." % port_id)
268 if os_utils.delete_neutron_port(neutron_client, port_id):
269 logger.debug(" > Done!")
271 logger.error("There has been a problem removing the port %s..."
275 def remove_routers(neutron_client, routers, default_routers):
276 for router in routers:
277 router_id = router['id']
278 router_name = router['name']
279 if (router_id not in default_routers and
280 router_name not in default_routers.values()):
281 logger.debug("Checking '%s' with ID=(%s) ..." % (router_name,
283 if router['external_gateway_info'] is not None:
284 logger.debug("Router has gateway to external network."
286 if os_utils.remove_gateway_router(neutron_client, router_id):
287 logger.debug(" > Done!")
289 logger.error("There has been a problem removing "
292 logger.debug("Router is not connected to anything."
293 "Ready to remove...")
294 logger.debug("Removing router %s(%s) ..."
295 % (router_name, router_id))
296 if os_utils.delete_neutron_router(neutron_client, router_id):
297 logger.debug(" > Done!")
299 logger.error("There has been a problem removing the "
300 "router '%s'(%s)..." % (router_name, router_id))
303 def remove_security_groups(neutron_client, default_security_groups):
304 logger.debug("Removing Security groups...")
305 secgroups = os_utils.get_security_groups(neutron_client)
306 if secgroups is None or len(secgroups) == 0:
307 logger.debug("No security groups found.")
310 for secgroup in secgroups:
311 secgroup_name = secgroup['name']
312 secgroup_id = secgroup['id']
313 logger.debug("'%s', ID=%s " % (secgroup_name, secgroup_id))
314 if secgroup_id not in default_security_groups:
315 logger.debug(" Removing '%s'..." % secgroup_name)
316 if os_utils.delete_security_group(neutron_client, secgroup_id):
317 logger.debug(" > Done!")
319 logger.error("There has been a problem removing the "
320 "security group %s..." % secgroup_id)
322 logger.debug(" > this is a default security group and will NOT "
326 def remove_users(keystone_client, default_users):
327 logger.debug("Removing Users...")
328 users = os_utils.get_users(keystone_client)
330 logger.debug("There are no users in the deployment. ")
334 user_name = getattr(user, 'name')
335 user_id = getattr(user, 'id')
336 logger.debug("'%s', ID=%s " % (user_name, user_id))
337 if (user_id not in default_users and
338 user_name not in default_users.values()):
339 logger.debug(" Removing '%s'..." % user_name)
340 if os_utils.delete_user(keystone_client, user_id):
341 logger.debug(" > Done!")
343 logger.error("There has been a problem removing the "
344 "user '%s'(%s)..." % (user_name, user_id))
346 logger.debug(" > this is a default user and will "
350 def remove_tenants(keystone_client, default_tenants):
351 logger.debug("Removing Tenants...")
352 tenants = os_utils.get_tenants(keystone_client)
354 logger.debug("There are no tenants in the deployment. ")
357 for tenant in tenants:
358 tenant_name = getattr(tenant, 'name')
359 tenant_id = getattr(tenant, 'id')
360 logger.debug("'%s', ID=%s " % (tenant_name, tenant_id))
361 if (tenant_id not in default_tenants and
362 tenant_name not in default_tenants.values()):
363 logger.debug(" Removing '%s'..." % tenant_name)
364 if os_utils.delete_tenant(keystone_client, tenant_id):
365 logger.debug(" > Done!")
367 logger.error("There has been a problem removing the "
368 "tenant '%s'(%s)..." % (tenant_name, tenant_id))
370 logger.debug(" > this is a default tenant and will "
375 logger.info("Cleaning OpenStack resources...")
377 nova_client = os_utils.get_nova_client()
378 neutron_client = os_utils.get_neutron_client()
379 keystone_client = os_utils.get_keystone_client()
380 cinder_client = os_utils.get_cinder_client()
383 with open(OS_SNAPSHOT_FILE) as f:
384 snapshot_yaml = yaml.safe_load(f)
386 logger.info("The file %s does not exist. The OpenStack snapshot must"
387 " be created first. Aborting cleanup." % OS_SNAPSHOT_FILE)
390 default_images = snapshot_yaml.get('images')
391 default_instances = snapshot_yaml.get('instances')
392 default_volumes = snapshot_yaml.get('volumes')
393 default_networks = snapshot_yaml.get('networks')
394 default_routers = snapshot_yaml.get('routers')
395 default_security_groups = snapshot_yaml.get('secgroups')
396 default_floatingips = snapshot_yaml.get('floatingips')
397 default_users = snapshot_yaml.get('users')
398 default_tenants = snapshot_yaml.get('tenants')
400 if not os_utils.check_credentials():
401 logger.error("Please source the openrc credentials and run "
405 remove_instances(nova_client, default_instances)
407 remove_images(nova_client, default_images)
409 remove_volumes(cinder_client, default_volumes)
411 remove_floatingips(nova_client, default_floatingips)
413 remove_networks(neutron_client, default_networks, default_routers)
415 remove_security_groups(neutron_client, default_security_groups)
417 remove_users(keystone_client, default_users)
419 remove_tenants(keystone_client, default_tenants)
423 if __name__ == '__main__':