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
30 import functest.utils.openstack_utils as os_utils
31 from functest.utils.constants import CONST
33 logger = logging.getLogger(__name__)
35 OS_SNAPSHOT_FILE = CONST.openstack_snapshot_file
39 logger.debug("-------------------------------------------")
42 def remove_instances(nova_client, default_instances):
43 logger.debug("Removing Nova instances...")
44 instances = os_utils.get_instances(nova_client)
45 if instances is None or len(instances) == 0:
46 logger.debug("No instances found.")
49 for instance in instances:
50 instance_name = getattr(instance, 'name')
51 instance_id = getattr(instance, 'id')
52 instance_status = getattr(instance, 'status')
53 instance_state = getattr(instance, 'OS-EXT-STS:task_state')
55 logger.debug("'%s', ID=%s " % (instance_name, instance_id))
56 if (instance_id not in default_instances and
57 instance_name not in default_instances.values() and
58 instance_status != 'DELETED' and
59 (instance_status != 'BUILD' or instance_state != 'deleting')):
60 logger.debug("Removing instance '%s' ..." % instance_id)
61 if os_utils.delete_instance(nova_client, instance_id):
62 logger.debug(" > Request sent.")
64 logger.error("There has been a problem removing the "
65 "instance %s..." % instance_id)
67 logger.debug(" > this is a default instance and will "
72 instances = os_utils.get_instances(nova_client)
73 for instance in instances:
74 instance_id = getattr(instance, 'id')
75 if instance_id not in default_instances:
76 logger.debug("Waiting for instances to be terminated...")
83 def remove_images(glance_client, default_images):
84 logger.debug("Removing Glance images...")
85 images = os_utils.get_images(glance_client)
88 images = {image.id: image.name for image in images}
90 logger.debug("No images found.")
95 image_name = images.get(image_id)
96 logger.debug("'%s', ID=%s " % (image_name, image_id))
97 if (image_id not in default_images and
98 image_name not in default_images.values()):
99 logger.debug("Removing image '%s', ID=%s ..."
100 % (image_name, image_id))
101 if os_utils.delete_glance_image(glance_client, image_id):
102 logger.debug(" > Done!")
104 logger.error("There has been a problem removing the"
105 "image %s..." % image_id)
107 logger.debug(" > this is a default image and will "
111 def remove_volumes(cinder_client, default_volumes):
112 logger.debug("Removing Cinder volumes...")
113 volumes = os_utils.get_volumes(cinder_client)
114 if volumes is None or len(volumes) == 0:
115 logger.debug("No volumes found.")
118 for volume in volumes:
119 volume_id = getattr(volume, 'id')
120 volume_name = getattr(volume, 'name')
121 logger.debug("'%s', ID=%s " % (volume_name, volume_id))
122 if (volume_id not in default_volumes and
123 volume_name not in default_volumes.values()):
124 logger.debug("Removing cinder volume %s ..." % volume_id)
125 if os_utils.delete_volume(cinder_client, volume_id):
126 logger.debug(" > Done!")
128 logger.debug("Trying forced removal...")
129 if os_utils.delete_volume(cinder_client,
132 logger.debug(" > Done!")
134 logger.error("There has been a problem removing the "
135 "volume %s..." % volume_id)
137 logger.debug(" > this is a default volume and will "
141 def remove_floatingips(neutron_client, default_floatingips):
142 logger.debug("Removing floating IPs...")
143 floatingips = os_utils.get_floating_ips(neutron_client)
144 if floatingips is None or len(floatingips) == 0:
145 logger.debug("No floating IPs found.")
148 init_len = len(floatingips)
150 for fip in floatingips:
152 fip_ip = fip['floating_ip_address']
153 logger.debug("'%s', ID=%s " % (fip_ip, fip_id))
154 if (fip_id not in default_floatingips and
155 fip_ip not in default_floatingips.values()):
156 logger.debug("Removing floating IP %s ..." % fip_id)
157 if os_utils.delete_floating_ip(neutron_client, fip_id):
158 logger.debug(" > Done!")
161 logger.error("There has been a problem removing the "
162 "floating IP %s..." % fip_id)
164 logger.debug(" > this is a default floating IP and will "
169 floatingips = os_utils.get_floating_ips(neutron_client)
170 if floatingips is None or len(floatingips) == (init_len - deleted):
173 logger.debug("Waiting for floating ips to be released...")
178 def remove_networks(neutron_client, default_networks, default_routers):
179 logger.debug("Removing Neutron objects")
181 networks = os_utils.get_network_list(neutron_client)
183 logger.debug("There are no networks in the deployment. ")
185 logger.debug("Existing networks:")
186 for network in networks:
187 net_id = network['id']
188 net_name = network['name']
189 logger.debug(" '%s', ID=%s " % (net_name, net_id))
190 if (net_id in default_networks and
191 net_name in default_networks.values()):
192 logger.debug(" > this is a default network and will "
194 elif network['router:external'] is True:
195 logger.debug(" > this is an external network and will "
198 logger.debug(" > this network will be deleted.")
199 network_ids.append(net_id)
202 ports = os_utils.get_port_list(neutron_client)
204 logger.debug("There are no ports in the deployment. ")
206 remove_ports(neutron_client, ports, network_ids)
209 routers = os_utils.get_router_list(neutron_client)
211 logger.debug("There are no routers in the deployment. ")
213 remove_routers(neutron_client, routers, default_routers)
215 # trozet: wait for Neutron to auto-cleanup HA networks when HA router is
220 if network_ids is not None:
221 for net_id in network_ids:
222 networks = os_utils.get_network_list(neutron_client)
224 logger.debug("No networks left to remove")
226 elif not any(network['id'] == net_id for network in networks):
227 logger.debug("Network %s has already been removed" % net_id)
229 logger.debug("Removing network %s ..." % net_id)
230 if os_utils.delete_neutron_net(neutron_client, net_id):
231 logger.debug(" > Done!")
233 logger.error("There has been a problem removing the "
234 "network %s..." % net_id)
237 def remove_ports(neutron_client, ports, network_ids):
239 if port['network_id'] in network_ids:
242 subnet_id = port['fixed_ips'][0]['subnet_id']
244 logger.debug(" > WARNING: Port %s does not contain fixed_ips"
247 router_id = port['device_id']
248 if len(port['fixed_ips']) == 0 and router_id == '':
249 logger.debug("Removing port %s ..." % port_id)
250 if (os_utils.delete_neutron_port(neutron_client, port_id)):
251 logger.debug(" > Done!")
253 logger.error("There has been a problem removing the "
254 "port %s ..." % port_id)
255 force_remove_port(neutron_client, port_id)
257 elif port['device_owner'] == 'network:router_interface':
258 logger.debug("Detaching port %s (subnet %s) from router %s ..."
259 % (port_id, subnet_id, router_id))
260 if os_utils.remove_interface_router(
261 neutron_client, router_id, subnet_id):
262 time.sleep(5) # leave 5 seconds to detach
263 logger.debug(" > Done!")
265 logger.error("There has been a problem removing the "
266 "interface %s from router %s..."
267 % (subnet_id, router_id))
268 force_remove_port(neutron_client, port_id)
270 force_remove_port(neutron_client, port_id)
273 def force_remove_port(neutron_client, port_id):
274 logger.debug("Clearing device_owner for port %s ..." % port_id)
275 os_utils.update_neutron_port(neutron_client, port_id,
276 device_owner='clear')
277 logger.debug("Removing port %s ..." % port_id)
278 if os_utils.delete_neutron_port(neutron_client, port_id):
279 logger.debug(" > Done!")
281 logger.error("There has been a problem removing the port %s..."
285 def remove_routers(neutron_client, routers, default_routers):
286 for router in routers:
287 router_id = router['id']
288 router_name = router['name']
289 if (router_id not in default_routers and
290 router_name not in default_routers.values()):
291 logger.debug("Checking '%s' with ID=(%s) ..." % (router_name,
293 if router['external_gateway_info'] is not None:
294 logger.debug("Router has gateway to external network."
296 if os_utils.remove_gateway_router(neutron_client, router_id):
297 logger.debug(" > Done!")
299 logger.error("There has been a problem removing "
302 logger.debug("Router is not connected to anything."
303 "Ready to remove...")
304 logger.debug("Removing router %s(%s) ..."
305 % (router_name, router_id))
306 if os_utils.delete_neutron_router(neutron_client, router_id):
307 logger.debug(" > Done!")
309 logger.error("There has been a problem removing the "
310 "router '%s'(%s)..." % (router_name, router_id))
313 def remove_security_groups(neutron_client, default_security_groups):
314 logger.debug("Removing Security groups...")
315 secgroups = os_utils.get_security_groups(neutron_client)
316 if secgroups is None or len(secgroups) == 0:
317 logger.debug("No security groups found.")
320 for secgroup in secgroups:
321 secgroup_name = secgroup['name']
322 secgroup_id = secgroup['id']
323 logger.debug("'%s', ID=%s " % (secgroup_name, secgroup_id))
324 if secgroup_id not in default_security_groups:
325 logger.debug(" Removing '%s'..." % secgroup_name)
326 if os_utils.delete_security_group(neutron_client, secgroup_id):
327 logger.debug(" > Done!")
329 logger.error("There has been a problem removing the "
330 "security group %s..." % secgroup_id)
332 logger.debug(" > this is a default security group and will NOT "
336 def remove_users(keystone_client, default_users):
337 logger.debug("Removing Users...")
338 users = os_utils.get_users(keystone_client)
340 logger.debug("There are no users in the deployment. ")
344 user_name = getattr(user, 'name')
345 user_id = getattr(user, 'id')
346 logger.debug("'%s', ID=%s " % (user_name, user_id))
347 if (user_id not in default_users and
348 user_name not in default_users.values()):
349 logger.debug(" Removing '%s'..." % user_name)
350 if os_utils.delete_user(keystone_client, user_id):
351 logger.debug(" > Done!")
353 logger.error("There has been a problem removing the "
354 "user '%s'(%s)..." % (user_name, user_id))
356 logger.debug(" > this is a default user and will "
360 def remove_tenants(keystone_client, default_tenants):
361 logger.debug("Removing Tenants...")
362 tenants = os_utils.get_tenants(keystone_client)
364 logger.debug("There are no tenants in the deployment. ")
367 for tenant in tenants:
368 tenant_name = getattr(tenant, 'name')
369 tenant_id = getattr(tenant, 'id')
370 logger.debug("'%s', ID=%s " % (tenant_name, tenant_id))
371 if (tenant_id not in default_tenants and
372 tenant_name not in default_tenants.values()):
373 logger.debug(" Removing '%s'..." % tenant_name)
374 if os_utils.delete_tenant(keystone_client, tenant_id):
375 logger.debug(" > Done!")
377 logger.error("There has been a problem removing the "
378 "tenant '%s'(%s)..." % (tenant_name, tenant_id))
380 logger.debug(" > this is a default tenant and will "
385 logger.info("Cleaning OpenStack resources...")
387 nova_client = os_utils.get_nova_client()
388 neutron_client = os_utils.get_neutron_client()
389 keystone_client = os_utils.get_keystone_client()
390 cinder_client = os_utils.get_cinder_client()
391 glance_client = os_utils.get_glance_client()
394 with open(OS_SNAPSHOT_FILE) as f:
395 snapshot_yaml = yaml.safe_load(f)
397 logger.info("The file %s does not exist. The OpenStack snapshot must"
398 " be created first. Aborting cleanup." % OS_SNAPSHOT_FILE)
401 default_images = snapshot_yaml.get('images')
402 default_instances = snapshot_yaml.get('instances')
403 default_volumes = snapshot_yaml.get('volumes')
404 default_networks = snapshot_yaml.get('networks')
405 default_routers = snapshot_yaml.get('routers')
406 default_security_groups = snapshot_yaml.get('secgroups')
407 default_floatingips = snapshot_yaml.get('floatingips')
408 default_users = snapshot_yaml.get('users')
409 default_tenants = snapshot_yaml.get('tenants')
411 if not os_utils.check_credentials():
412 logger.error("Please source the openrc credentials and run "
416 remove_instances(nova_client, default_instances)
418 remove_images(glance_client, default_images)
420 remove_volumes(cinder_client, default_volumes)
422 remove_floatingips(neutron_client, default_floatingips)
424 remove_networks(neutron_client, default_networks, default_routers)
426 remove_security_groups(neutron_client, default_security_groups)
428 remove_users(keystone_client, default_users)
430 remove_tenants(keystone_client, default_tenants)
435 if __name__ == '__main__':
436 logging.basicConfig()