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
23 """ logging configuration """
27 from cinderclient import client as cinderclient
28 import functest.utils.functest_logger as ft_logger
29 import functest.utils.functest_utils as ft_utils
30 import functest.utils.openstack_utils as os_utils
31 from keystoneclient.v2_0 import client as keystoneclient
32 from neutronclient.v2_0 import client as neutronclient
33 from novaclient import client as novaclient
37 logger = ft_logger.Logger("openstack_clean").getLogger()
39 OS_SNAPSHOT_FILE = ft_utils.get_parameter_from_yaml(
40 "general.openstack.snapshot_file")
44 logger.info("-------------------------------------------")
47 def remove_instances(nova_client, default_instances):
48 logger.info("Removing Nova instances...")
49 instances = os_utils.get_instances(nova_client)
50 if instances is None or len(instances) == 0:
51 logger.debug("No instances found.")
54 for instance in instances:
55 instance_name = getattr(instance, 'name')
56 instance_id = getattr(instance, 'id')
57 logger.debug("'%s', ID=%s " % (instance_name, instance_id))
58 if instance_id not in default_instances:
59 logger.debug("Removing instance '%s' ..." % instance_id)
60 if os_utils.delete_instance(nova_client, instance_id):
61 logger.debug(" > Request sent.")
63 logger.error("There has been a problem removing the "
64 "instance %s..." % instance_id)
66 logger.debug(" > this is a default instance and will "
71 instances = os_utils.get_instances(nova_client)
72 for instance in instances:
73 instance_id = getattr(instance, 'id')
74 if instance_id not in default_instances:
75 logger.debug("Waiting for instances to be terminated...")
82 def remove_images(nova_client, default_images):
83 logger.info("Removing Glance images...")
84 images = os_utils.get_images(nova_client)
85 if images is None or len(images) == 0:
86 logger.debug("No images found.")
90 image_name = getattr(image, 'name')
91 image_id = getattr(image, 'id')
92 logger.debug("'%s', ID=%s " % (image_name, image_id))
93 if image_id not in default_images:
94 logger.debug("Removing image '%s', ID=%s ..."
95 % (image_name, image_id))
96 if os_utils.delete_glance_image(nova_client, image_id):
97 logger.debug(" > Done!")
99 logger.error("There has been a problem removing the"
100 "image %s..." % image_id)
102 logger.debug(" > this is a default image and will "
106 def remove_volumes(cinder_client, default_volumes):
107 logger.info("Removing Cinder volumes...")
108 volumes = os_utils.get_volumes(cinder_client)
109 if volumes is None or len(volumes) == 0:
110 logger.debug("No volumes found.")
113 for volume in volumes:
114 volume_id = getattr(volume, 'id')
115 volume_name = getattr(volume, 'display_name')
116 logger.debug("'%s', ID=%s " % (volume_name, volume_id))
117 if volume_id not in default_volumes:
118 logger.debug("Removing cinder volume %s ..." % volume_id)
119 if os_utils.delete_volume(cinder_client, volume_id):
120 logger.debug(" > Done!")
122 logger.debug("Trying forced removal...")
123 if os_utils.delete_volume(cinder_client,
126 logger.debug(" > Done!")
128 logger.error("There has been a problem removing the "
129 "volume %s..." % volume_id)
131 logger.debug(" > this is a default volume and will "
135 def remove_floatingips(nova_client, default_floatingips):
136 logger.info("Removing floating IPs...")
137 floatingips = os_utils.get_floating_ips(nova_client)
138 if floatingips is None or len(floatingips) == 0:
139 logger.debug("No floating IPs found.")
142 init_len = len(floatingips)
144 for fip in floatingips:
145 fip_id = getattr(fip, 'id')
146 fip_ip = getattr(fip, 'ip')
147 logger.debug("'%s', ID=%s " % (fip_ip, fip_id))
148 if fip_id not in default_floatingips:
149 logger.debug("Removing floating IP %s ..." % fip_id)
150 if os_utils.delete_floating_ip(nova_client, fip_id):
151 logger.debug(" > Done!")
154 logger.error("There has been a problem removing the "
155 "floating IP %s..." % fip_id)
157 logger.debug(" > this is a default floating IP and will "
162 floatingips = os_utils.get_floating_ips(nova_client)
163 if floatingips is None or len(floatingips) == (init_len - deleted):
166 logger.debug("Waiting for floating ips to be released...")
171 def remove_networks(neutron_client, default_networks, default_routers):
172 logger.info("Removing Neutron objects")
174 networks = os_utils.get_network_list(neutron_client)
176 logger.debug("There are no networks in the deployment. ")
178 logger.debug("Existing networks:")
179 for network in networks:
180 net_id = network['id']
181 net_name = network['name']
182 logger.debug(" '%s', ID=%s " % (net_name, net_id))
183 if net_id in default_networks:
184 logger.debug(" > this is a default network and will "
186 elif network['router:external'] is True:
187 logger.debug(" > this is an external network and will "
190 logger.debug(" > this network will be deleted.")
191 network_ids.append(net_id)
194 ports = os_utils.get_port_list(neutron_client)
196 logger.debug("There are no ports in the deployment. ")
198 remove_ports(neutron_client, ports, network_ids)
201 routers = os_utils.get_router_list(neutron_client)
203 logger.debug("There are no routers in the deployment. ")
205 remove_routers(neutron_client, routers, default_routers)
208 if network_ids is not None:
209 for net_id in network_ids:
210 logger.debug("Removing network %s ..." % net_id)
211 if os_utils.delete_neutron_net(neutron_client, net_id):
212 logger.debug(" > Done!")
214 logger.error("There has been a problem removing the "
215 "network %s..." % net_id)
218 def remove_ports(neutron_client, ports, network_ids):
220 if port['network_id'] in network_ids:
223 subnet_id = port['fixed_ips'][0]['subnet_id']
225 logger.info(" > WARNING: Port %s does not contain 'fixed_ips'"
228 router_id = port['device_id']
229 if len(port['fixed_ips']) == 0 and router_id == '':
230 logger.debug("Removing port %s ..." % port_id)
231 if (os_utils.delete_neutron_port(neutron_client, port_id)):
232 logger.debug(" > Done!")
234 logger.error("There has been a problem removing the "
235 "port %s ..." % port_id)
236 force_remove_port(neutron_client, port_id)
238 elif port['device_owner'] == 'network:router_interface':
239 logger.debug("Detaching port %s (subnet %s) from router %s ..."
240 % (port_id, subnet_id, router_id))
241 if os_utils.remove_interface_router(
242 neutron_client, router_id, subnet_id):
243 time.sleep(5) # leave 5 seconds to detach
244 logger.debug(" > Done!")
246 logger.error("There has been a problem removing the "
247 "interface %s from router %s..."
248 % (subnet_id, router_id))
249 force_remove_port(neutron_client, port_id)
251 force_remove_port(neutron_client, port_id)
254 def force_remove_port(neutron_client, port_id):
255 logger.debug("Clearing device_owner for port %s ..." % port_id)
256 os_utils.update_neutron_port(neutron_client, port_id,
257 device_owner='clear')
258 logger.debug("Removing port %s ..." % port_id)
259 if os_utils.delete_neutron_port(neutron_client, port_id):
260 logger.debug(" > Done!")
262 logger.error("There has been a problem removing the port %s..."
266 def remove_routers(neutron_client, routers, default_routers):
267 for router in routers:
268 router_id = router['id']
269 router_name = router['name']
270 if router_id not in default_routers:
271 logger.debug("Checking '%s' with ID=(%s) ..." % (router_name,
273 if router['external_gateway_info'] is not None:
274 logger.debug("Router has gateway to external network."
276 if os_utils.remove_gateway_router(neutron_client, router_id):
277 logger.debug(" > Done!")
279 logger.error("There has been a problem removing "
282 logger.debug("Router is not connected to anything."
283 "Ready to remove...")
284 logger.debug("Removing router %s(%s) ..."
285 % (router_name, router_id))
286 if os_utils.delete_neutron_router(neutron_client, router_id):
287 logger.debug(" > Done!")
289 logger.error("There has been a problem removing the "
290 "router '%s'(%s)..." % (router_name, router_id))
293 def remove_security_groups(neutron_client, default_security_groups):
294 logger.info("Removing Security groups...")
295 secgroups = os_utils.get_security_groups(neutron_client)
296 if secgroups is None or len(secgroups) == 0:
297 logger.debug("No security groups found.")
300 for secgroup in secgroups:
301 secgroup_name = secgroup['name']
302 secgroup_id = secgroup['id']
303 logger.debug("'%s', ID=%s " % (secgroup_name, secgroup_id))
304 if secgroup_id not in default_security_groups:
305 logger.debug(" Removing '%s'..." % secgroup_name)
306 if os_utils.delete_security_group(neutron_client, secgroup_id):
307 logger.debug(" > Done!")
309 logger.error("There has been a problem removing the "
310 "security group %s..." % secgroup_id)
312 logger.debug(" > this is a default security group and will NOT "
316 def remove_users(keystone_client, default_users):
317 logger.info("Removing Users...")
318 users = os_utils.get_users(keystone_client)
320 logger.debug("There are no users in the deployment. ")
324 user_name = getattr(user, 'name')
325 user_id = getattr(user, 'id')
326 logger.debug("'%s', ID=%s " % (user_name, user_id))
327 if user_id not in default_users:
328 logger.debug(" Removing '%s'..." % user_name)
329 if os_utils.delete_user(keystone_client, user_id):
330 logger.debug(" > Done!")
332 logger.error("There has been a problem removing the "
333 "user '%s'(%s)..." % (user_name, user_id))
335 logger.debug(" > this is a default user and will "
339 def remove_tenants(keystone_client, default_tenants):
340 logger.info("Removing Tenants...")
341 tenants = os_utils.get_tenants(keystone_client)
343 logger.debug("There are no tenants in the deployment. ")
346 for tenant in tenants:
347 tenant_name = getattr(tenant, 'name')
348 tenant_id = getattr(tenant, 'id')
349 logger.debug("'%s', ID=%s " % (tenant_name, tenant_id))
350 if tenant_id not in default_tenants:
351 logger.debug(" Removing '%s'..." % tenant_name)
352 if os_utils.delete_tenant(keystone_client, tenant_id):
353 logger.debug(" > Done!")
355 logger.error("There has been a problem removing the "
356 "tenant '%s'(%s)..." % (tenant_name, tenant_id))
358 logger.debug(" > this is a default tenant and will "
363 logger.info("+++++++++++++++++++++++++++++++")
364 logger.info("Cleaning OpenStack resources...")
365 logger.info("+++++++++++++++++++++++++++++++")
368 with open(OS_SNAPSHOT_FILE) as f:
369 snapshot_yaml = yaml.safe_load(f)
371 logger.info("The file %s does not exist. The OpenStack snapshot must"
372 " be created first. Aborting cleanup." % OS_SNAPSHOT_FILE)
375 default_images = snapshot_yaml.get('images')
376 default_instances = snapshot_yaml.get('instances')
377 default_volumes = snapshot_yaml.get('volumes')
378 default_networks = snapshot_yaml.get('networks')
379 default_routers = snapshot_yaml.get('routers')
380 default_security_groups = snapshot_yaml.get('secgroups')
381 default_floatingips = snapshot_yaml.get('floatingips')
382 default_users = snapshot_yaml.get('users')
383 default_tenants = snapshot_yaml.get('tenants')
385 creds_nova = os_utils.get_credentials("nova")
386 nova_client = novaclient.Client('2', **creds_nova)
388 creds_neutron = os_utils.get_credentials("neutron")
389 neutron_client = neutronclient.Client(**creds_neutron)
391 creds_keystone = os_utils.get_credentials("keystone")
392 keystone_client = keystoneclient.Client(**creds_keystone)
394 creds_cinder = os_utils.get_credentials("cinder")
395 # cinder_client = cinderclient.Client(**creds_cinder)
396 cinder_client = cinderclient.Client('1', creds_cinder['username'],
397 creds_cinder['api_key'],
398 creds_cinder['project_id'],
399 creds_cinder['auth_url'],
400 service_type="volume")
402 if not os_utils.check_credentials():
403 logger.error("Please source the openrc credentials and run "
407 remove_instances(nova_client, default_instances)
409 remove_images(nova_client, default_images)
411 remove_volumes(cinder_client, default_volumes)
413 remove_floatingips(nova_client, default_floatingips)
415 remove_networks(neutron_client, default_networks, default_routers)
417 remove_security_groups(neutron_client, default_security_groups)
419 remove_users(keystone_client, default_users)
421 remove_tenants(keystone_client, default_tenants)
425 if __name__ == '__main__':