4 # Cleans possible leftovers after running functest tests:
9 # - Neutron networks, subnets and ports
12 # - Tacker VNFDs and VNFs
13 # - Tacker SFCs and SFC classifiers
16 # jose.lausuch@ericsson.com
19 # All rights reserved. This program and the accompanying materials
20 # are made available under the terms of the Apache License, Version 2.0
21 # which accompanies this distribution, and is available at
22 # http://www.apache.org/licenses/LICENSE-2.0
26 import functest.utils.functest_logger as ft_logger
27 import functest.utils.openstack_utils as os_utils
28 import functest.utils.openstack_tacker as os_tacker
30 import functest.utils.functest_constants as ft_constants
32 logger = ft_logger.Logger("openstack_clean").getLogger()
34 OS_SNAPSHOT_FILE = ft_constants.OPENSTACK_SNAPSHOT_FILE
38 logger.debug("-------------------------------------------")
41 def remove_instances(nova_client, default_instances):
42 logger.debug("Removing Nova instances...")
43 instances = os_utils.get_instances(nova_client)
44 if instances is None or len(instances) == 0:
45 logger.debug("No instances found.")
48 for instance in instances:
49 instance_name = getattr(instance, 'name')
50 instance_id = getattr(instance, 'id')
51 logger.debug("'%s', ID=%s " % (instance_name, instance_id))
52 if (instance_id not in default_instances and
53 instance_name not in default_instances.values()):
54 logger.debug("Removing instance '%s' ..." % instance_id)
55 if os_utils.delete_instance(nova_client, instance_id):
56 logger.debug(" > Request sent.")
58 logger.error("There has been a problem removing the "
59 "instance %s..." % instance_id)
61 logger.debug(" > this is a default instance and will "
66 instances = os_utils.get_instances(nova_client)
67 for instance in instances:
68 instance_id = getattr(instance, 'id')
69 if instance_id not in default_instances:
70 logger.debug("Waiting for instances to be terminated...")
77 def remove_images(nova_client, default_images):
78 logger.debug("Removing Glance images...")
79 images = os_utils.get_images(nova_client)
80 if images is None or len(images) == 0:
81 logger.debug("No images found.")
85 image_name = getattr(image, 'name')
86 image_id = getattr(image, 'id')
87 logger.debug("'%s', ID=%s " % (image_name, image_id))
88 if (image_id not in default_images and
89 image_name not in default_images.values()):
90 logger.debug("Removing image '%s', ID=%s ..."
91 % (image_name, image_id))
92 if os_utils.delete_glance_image(nova_client, image_id):
93 logger.debug(" > Done!")
95 logger.error("There has been a problem removing the"
96 "image %s..." % image_id)
98 logger.debug(" > this is a default image and will "
102 def remove_volumes(cinder_client, default_volumes):
103 logger.debug("Removing Cinder volumes...")
104 volumes = os_utils.get_volumes(cinder_client)
105 if volumes is None or len(volumes) == 0:
106 logger.debug("No volumes found.")
109 for volume in volumes:
110 volume_id = getattr(volume, 'id')
111 volume_name = getattr(volume, 'display_name')
112 logger.debug("'%s', ID=%s " % (volume_name, volume_id))
113 if (volume_id not in default_volumes and
114 volume_name not in default_volumes.values()):
115 logger.debug("Removing cinder volume %s ..." % volume_id)
116 if os_utils.delete_volume(cinder_client, volume_id):
117 logger.debug(" > Done!")
119 logger.debug("Trying forced removal...")
120 if os_utils.delete_volume(cinder_client,
123 logger.debug(" > Done!")
125 logger.error("There has been a problem removing the "
126 "volume %s..." % volume_id)
128 logger.debug(" > this is a default volume and will "
132 def remove_floatingips(nova_client, default_floatingips):
133 logger.debug("Removing floating IPs...")
134 floatingips = os_utils.get_floating_ips(nova_client)
135 if floatingips is None or len(floatingips) == 0:
136 logger.debug("No floating IPs found.")
139 init_len = len(floatingips)
141 for fip in floatingips:
142 fip_id = getattr(fip, 'id')
143 fip_ip = getattr(fip, 'ip')
144 logger.debug("'%s', ID=%s " % (fip_ip, fip_id))
145 if (fip_id not in default_floatingips and
146 fip_ip not in default_floatingips.values()):
147 logger.debug("Removing floating IP %s ..." % fip_id)
148 if os_utils.delete_floating_ip(nova_client, fip_id):
149 logger.debug(" > Done!")
152 logger.error("There has been a problem removing the "
153 "floating IP %s..." % fip_id)
155 logger.debug(" > this is a default floating IP and will "
160 floatingips = os_utils.get_floating_ips(nova_client)
161 if floatingips is None or len(floatingips) == (init_len - deleted):
164 logger.debug("Waiting for floating ips to be released...")
169 def remove_networks(neutron_client, default_networks, default_routers):
170 logger.debug("Removing Neutron objects")
172 networks = os_utils.get_network_list(neutron_client)
174 logger.debug("There are no networks in the deployment. ")
176 logger.debug("Existing networks:")
177 for network in networks:
178 net_id = network['id']
179 net_name = network['name']
180 logger.debug(" '%s', ID=%s " % (net_name, net_id))
181 if (net_id in default_networks and
182 net_name in default_networks.values()):
183 logger.debug(" > this is a default network and will "
185 elif network['router:external'] is True:
186 logger.debug(" > this is an external network and will "
189 logger.debug(" > this network will be deleted.")
190 network_ids.append(net_id)
193 ports = os_utils.get_port_list(neutron_client)
195 logger.debug("There are no ports in the deployment. ")
197 remove_ports(neutron_client, ports, network_ids)
200 routers = os_utils.get_router_list(neutron_client)
202 logger.debug("There are no routers in the deployment. ")
204 remove_routers(neutron_client, routers, default_routers)
206 # trozet: wait for Neutron to auto-cleanup HA networks when HA router is
211 if network_ids is not None:
212 for net_id in network_ids:
213 networks = os_utils.get_network_list(neutron_client)
215 logger.debug("No networks left to remove")
217 elif not any(network['id'] == net_id for network in networks):
218 logger.debug("Network %s has already been removed" % net_id)
220 logger.debug("Removing network %s ..." % net_id)
221 if os_utils.delete_neutron_net(neutron_client, net_id):
222 logger.debug(" > Done!")
224 logger.error("There has been a problem removing the "
225 "network %s..." % net_id)
228 def remove_ports(neutron_client, ports, network_ids):
230 if port['network_id'] in network_ids:
233 subnet_id = port['fixed_ips'][0]['subnet_id']
235 logger.debug(" > WARNING: Port %s does not contain fixed_ips"
238 router_id = port['device_id']
239 if len(port['fixed_ips']) == 0 and router_id == '':
240 logger.debug("Removing port %s ..." % port_id)
241 if (os_utils.delete_neutron_port(neutron_client, port_id)):
242 logger.debug(" > Done!")
244 logger.error("There has been a problem removing the "
245 "port %s ..." % port_id)
246 force_remove_port(neutron_client, port_id)
248 elif port['device_owner'] == 'network:router_interface':
249 logger.debug("Detaching port %s (subnet %s) from router %s ..."
250 % (port_id, subnet_id, router_id))
251 if os_utils.remove_interface_router(
252 neutron_client, router_id, subnet_id):
253 time.sleep(5) # leave 5 seconds to detach
254 logger.debug(" > Done!")
256 logger.error("There has been a problem removing the "
257 "interface %s from router %s..."
258 % (subnet_id, router_id))
259 force_remove_port(neutron_client, port_id)
261 force_remove_port(neutron_client, port_id)
264 def force_remove_port(neutron_client, port_id):
265 logger.debug("Clearing device_owner for port %s ..." % port_id)
266 os_utils.update_neutron_port(neutron_client, port_id,
267 device_owner='clear')
268 logger.debug("Removing port %s ..." % port_id)
269 if os_utils.delete_neutron_port(neutron_client, port_id):
270 logger.debug(" > Done!")
272 logger.error("There has been a problem removing the port %s..."
276 def remove_routers(neutron_client, routers, default_routers):
277 for router in routers:
278 router_id = router['id']
279 router_name = router['name']
280 if (router_id not in default_routers and
281 router_name not in default_routers.values()):
282 logger.debug("Checking '%s' with ID=(%s) ..." % (router_name,
284 if router['external_gateway_info'] is not None:
285 logger.debug("Router has gateway to external network."
287 if os_utils.remove_gateway_router(neutron_client, router_id):
288 logger.debug(" > Done!")
290 logger.error("There has been a problem removing "
293 logger.debug("Router is not connected to anything."
294 "Ready to remove...")
295 logger.debug("Removing router %s(%s) ..."
296 % (router_name, router_id))
297 if os_utils.delete_neutron_router(neutron_client, router_id):
298 logger.debug(" > Done!")
300 logger.error("There has been a problem removing the "
301 "router '%s'(%s)..." % (router_name, router_id))
304 def remove_security_groups(neutron_client, default_security_groups):
305 logger.debug("Removing Security groups...")
306 secgroups = os_utils.get_security_groups(neutron_client)
307 if secgroups is None or len(secgroups) == 0:
308 logger.debug("No security groups found.")
311 for secgroup in secgroups:
312 secgroup_name = secgroup['name']
313 secgroup_id = secgroup['id']
314 logger.debug("'%s', ID=%s " % (secgroup_name, secgroup_id))
315 if secgroup_id not in default_security_groups:
316 logger.debug(" Removing '%s'..." % secgroup_name)
317 if os_utils.delete_security_group(neutron_client, secgroup_id):
318 logger.debug(" > Done!")
320 logger.error("There has been a problem removing the "
321 "security group %s..." % secgroup_id)
323 logger.debug(" > this is a default security group and will NOT "
327 def remove_users(keystone_client, default_users):
328 logger.debug("Removing Users...")
329 users = os_utils.get_users(keystone_client)
331 logger.debug("There are no users in the deployment. ")
335 user_name = getattr(user, 'name')
336 user_id = getattr(user, 'id')
337 logger.debug("'%s', ID=%s " % (user_name, user_id))
338 if (user_id not in default_users and
339 user_name not in default_users.values()):
340 logger.debug(" Removing '%s'..." % user_name)
341 if os_utils.delete_user(keystone_client, user_id):
342 logger.debug(" > Done!")
344 logger.error("There has been a problem removing the "
345 "user '%s'(%s)..." % (user_name, user_id))
347 logger.debug(" > this is a default user and will "
351 def remove_tenants(keystone_client, default_tenants):
352 logger.debug("Removing Tenants...")
353 tenants = os_utils.get_tenants(keystone_client)
355 logger.debug("There are no tenants in the deployment. ")
358 for tenant in tenants:
359 tenant_name = getattr(tenant, 'name')
360 tenant_id = getattr(tenant, 'id')
361 logger.debug("'%s', ID=%s " % (tenant_name, tenant_id))
362 if (tenant_id not in default_tenants and
363 tenant_name not in default_tenants.values()):
364 logger.debug(" Removing '%s'..." % tenant_name)
365 if os_utils.delete_tenant(keystone_client, tenant_id):
366 logger.debug(" > Done!")
368 logger.error("There has been a problem removing the "
369 "tenant '%s'(%s)..." % (tenant_name, tenant_id))
371 logger.debug(" > this is a default tenant and will "
375 def remove_tacker_vnfds(tacker_client, default_vnfds):
376 logger.debug("Removing Tacker VNFDs...")
377 vnfds = os_tacker.list_vnfds(tacker_client, verbose=True)['vnfds']
379 logger.debug("There are no Tacker VNFDs in the deployment. ")
383 vnfd_name = vnfd['name']
385 logger.debug("'%s', ID=%s " % (vnfd_name, vnfd_id))
386 if (vnfd_id not in default_vnfds and
387 vnfd_name not in default_vnfds.values()):
388 logger.debug(" Removing '%s'..." % vnfd_name)
389 deleted = os_tacker.delete_vnfd(tacker_client, vnfd_id=vnfd_id)
390 if deleted is not None:
391 logger.debug(" > Done!")
393 logger.error("There has been a problem removing the "
394 "VNFD '%s'(%s)..." % (vnfd_name, vnfd_id))
396 logger.debug(" > this is a default VNFD and will "
400 def remove_tacker_vnfs(tacker_client, default_vnfs):
401 logger.debug("Removing Tacker VNFs...")
402 vnfs = os_tacker.list_vnfs(tacker_client, verbose=True)['vnfs']
404 logger.debug("There are no Tacker VNFs in the deployment. ")
408 vnf_name = vnf['name']
410 logger.debug("'%s', ID=%s " % (vnf_name, vnf_id))
411 if (vnf_id not in default_vnfs and
412 vnf_name not in default_vnfs.values()):
413 logger.debug(" Removing '%s'..." % vnf_name)
414 deleted = os_tacker.delete_vnf(tacker_client, vnf_id=vnf_id)
415 if deleted is not None:
416 logger.debug(" > Done!")
418 logger.error("There has been a problem removing the "
419 "VNF '%s'(%s)..." % (vnf_name, vnf_id))
421 logger.debug(" > this is a default VNF and will "
425 def remove_tacker_sfcs(tacker_client, default_sfcs):
426 logger.debug("Removing Tacker SFCs...")
427 sfcs = os_tacker.list_sfcs(tacker_client, verbose=True)['sfcs']
429 logger.debug("There are no Tacker SFCs in the deployment. ")
433 sfc_name = sfc['name']
435 logger.debug("'%s', ID=%s " % (sfc_name, sfc_id))
436 if (sfc_id not in default_sfcs and
437 sfc_name not in default_sfcs.values()):
438 logger.debug(" Removing '%s'..." % sfc_name)
439 deleted = os_tacker.delete_sfc(tacker_client, sfc_id=sfc_id)
440 if deleted is not None:
441 logger.debug(" > Done!")
443 logger.error("There has been a problem removing the "
444 "SFC '%s'(%s)..." % (sfc_name, sfc_id))
446 logger.debug(" > this is a default SFC and will "
450 def remove_tacker_sfc_classifiers(tacker_client, default_sfc_classifiers):
451 logger.debug("Removing Tacker SFC classifiers...")
452 sfc_clfs = os_tacker.list_sfc_classifiers(
453 tacker_client, verbose=True)['sfc_classfiers']
455 logger.debug("There are no Tacker SFC classifiers in the deployment.")
458 for sfc_clf in sfc_clfs:
459 sfc_clf_name = sfc_clf['name']
460 sfc_clf_id = sfc_clf['id']
461 logger.debug("'%s', ID=%s " % (sfc_clf_name, sfc_clf_id))
462 if (sfc_clf_id not in default_sfc_classifiers and
463 sfc_clf_name not in default_sfc_classifiers.values()):
464 logger.debug(" Removing '%s'..." % sfc_clf_name)
465 deleted = os_tacker.delete_sfc_classifier(
466 tacker_client, sfc_clf_id=sfc_clf_id)
467 if deleted is not None:
468 logger.debug(" > Done!")
470 logger.error("There has been a problem removing the "
471 "SFC classifier '%s'(%s)..."
472 % (sfc_clf_name, sfc_clf_id))
474 logger.debug(" > this is a default SFC classifier and will "
479 logger.info("Cleaning OpenStack resources...")
481 nova_client = os_utils.get_nova_client()
482 neutron_client = os_utils.get_neutron_client()
483 keystone_client = os_utils.get_keystone_client()
484 cinder_client = os_utils.get_cinder_client()
485 tacker_client = os_tacker.get_tacker_client()
488 with open(OS_SNAPSHOT_FILE) as f:
489 snapshot_yaml = yaml.safe_load(f)
491 logger.info("The file %s does not exist. The OpenStack snapshot must"
492 " be created first. Aborting cleanup." % OS_SNAPSHOT_FILE)
495 default_images = snapshot_yaml.get('images')
496 default_instances = snapshot_yaml.get('instances')
497 default_volumes = snapshot_yaml.get('volumes')
498 default_networks = snapshot_yaml.get('networks')
499 default_routers = snapshot_yaml.get('routers')
500 default_security_groups = snapshot_yaml.get('secgroups')
501 default_floatingips = snapshot_yaml.get('floatingips')
502 default_users = snapshot_yaml.get('users')
503 default_tenants = snapshot_yaml.get('tenants')
504 default_vnfds = snapshot_yaml.get('vnfds')
505 default_vnfs = snapshot_yaml.get('vnfs')
506 default_sfcs = snapshot_yaml.get('sfcs')
507 default_sfc_classifiers = snapshot_yaml.get('sfc_classifiers')
509 if not os_utils.check_credentials():
510 logger.error("Please source the openrc credentials and run "
514 remove_instances(nova_client, default_instances)
516 remove_images(nova_client, default_images)
518 remove_volumes(cinder_client, default_volumes)
520 remove_floatingips(nova_client, default_floatingips)
522 remove_networks(neutron_client, default_networks, default_routers)
524 remove_security_groups(neutron_client, default_security_groups)
526 remove_users(keystone_client, default_users)
528 remove_tenants(keystone_client, default_tenants)
530 # Note: Delete in this order
531 # 1. Classifiers, 2. SFCs, 3. VNFs, 4. VNFDs
532 remove_tacker_sfc_classifiers(tacker_client, default_sfc_classifiers)
534 remove_tacker_sfcs(tacker_client, default_sfcs)
536 remove_tacker_vnfs(tacker_client, default_vnfs)
538 remove_tacker_vnfds(tacker_client, default_vnfds)
542 if __name__ == '__main__':