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
27 from novaclient import client as novaclient
28 from neutronclient.v2_0 import client as neutronclient
29 from keystoneclient.v2_0 import client as keystoneclient
30 from cinderclient import client as cinderclient
32 import functest.utils.functest_logger as ft_logger
33 import functest.utils.openstack_utils as os_utils
36 """ logging configuration """
37 logger = ft_logger.Logger("clean_openstack").getLogger()
39 REPO_PATH = os.environ['repos_dir'] + '/functest/'
41 DEFAULTS_FILE = '/home/opnfv/functest/conf/os_defaults.yaml'
44 with open(DEFAULTS_FILE) as f:
45 defaults_yaml = yaml.safe_load(f)
47 logger.info("The file %s does not exist. Please run generate_defaults.py "
48 "to create the OpenStack defaults. "
49 "Aborting cleanup..." % DEFAULTS_FILE)
52 default_images = defaults_yaml.get('images')
53 default_instances = defaults_yaml.get('instances')
54 default_volumes = defaults_yaml.get('volumes')
55 default_networks = defaults_yaml.get('networks')
56 default_routers = defaults_yaml.get('routers')
57 default_security_groups = defaults_yaml.get('secgroups')
58 default_floatingips = defaults_yaml.get('floatingips')
59 default_users = defaults_yaml.get('users')
60 default_tenants = defaults_yaml.get('tenants')
64 logger.info("-------------------------------------------")
67 def remove_instances(nova_client):
68 logger.info("Removing Nova instances...")
69 instances = os_utils.get_instances(nova_client)
70 if instances is None or len(instances) == 0:
71 logger.debug("No instances found.")
74 for instance in instances:
75 instance_name = getattr(instance, 'name')
76 instance_id = getattr(instance, 'id')
77 logger.debug("Removing instance '%s', ID=%s ..."
78 % (instance_name, instance_id))
79 if os_utils.delete_instance(nova_client, instance_id):
80 logger.debug(" > Done!")
82 logger.error("There has been a problem removing the "
83 "instance %s..." % instance_id)
87 instances = os_utils.get_instances(nova_client)
88 if instances is None or len(instances) == 0:
91 logger.debug("Waiting for instances to be terminated...")
96 def remove_images(nova_client):
97 logger.info("Removing Glance images...")
98 images = os_utils.get_images(nova_client)
99 if images is None or len(images) == 0:
100 logger.debug("No images found.")
104 image_name = getattr(image, 'name')
105 image_id = getattr(image, 'id')
106 logger.debug("'%s', ID=%s " % (image_name, image_id))
107 if image_id not in default_images:
108 logger.debug("Removing image '%s', ID=%s ..."
109 % (image_name, image_id))
110 if os_utils.delete_glance_image(nova_client, image_id):
111 logger.debug(" > Done!")
113 logger.error("There has been a problem removing the"
114 "image %s..." % image_id)
116 logger.debug(" > this is a default image and will "
120 def remove_volumes(cinder_client):
121 logger.info("Removing Cinder volumes...")
122 volumes = os_utils.get_volumes(cinder_client)
123 if volumes is None or len(volumes) == 0:
124 logger.debug("No volumes found.")
127 for volume in volumes:
128 volume_id = getattr(volume, 'id')
129 volume_name = getattr(volume, 'display_name')
130 logger.debug("'%s', ID=%s " % (volume_name, volume_id))
131 if volume_id not in default_volumes:
132 logger.debug("Removing cinder volume %s ..." % volume_id)
133 if os_utils.delete_volume(cinder_client, volume_id):
134 logger.debug(" > Done!")
136 logger.debug("Trying forced removal...")
137 if os_utils.delete_volume(cinder_client,
140 logger.debug(" > Done!")
142 logger.error("There has been a problem removing the "
143 "volume %s..." % volume_id)
145 logger.debug(" > this is a default volume and will "
149 def remove_floatingips(nova_client):
150 logger.info("Removing floating IPs...")
151 floatingips = os_utils.get_floating_ips(nova_client)
152 if floatingips is None or len(floatingips) == 0:
153 logger.debug("No floating IPs found.")
156 init_len = len(floatingips)
158 for fip in floatingips:
159 fip_id = getattr(fip, 'id')
160 fip_ip = getattr(fip, 'ip')
161 logger.debug("'%s', ID=%s " % (fip_ip, fip_id))
162 if fip_id not in default_floatingips:
163 logger.debug("Removing floating IP %s ..." % fip_id)
164 if os_utils.delete_floating_ip(nova_client, fip_id):
165 logger.debug(" > Done!")
168 logger.error("There has been a problem removing the "
169 "floating IP %s..." % fip_id)
171 logger.debug(" > this is a default floating IP and will "
176 floatingips = os_utils.get_floating_ips(nova_client)
177 if floatingips is None or len(floatingips) == (init_len - deleted):
180 logger.debug("Waiting for floating ips to be released...")
185 def remove_networks(neutron_client):
186 logger.info("Removing Neutron objects")
188 networks = os_utils.get_network_list(neutron_client)
190 logger.debug("There are no networks in the deployment. ")
192 logger.debug("Existing networks:")
193 for network in networks:
194 net_id = network['id']
195 net_name = network['name']
196 logger.debug(" '%s', ID=%s " % (net_name, net_id))
197 if net_id in default_networks:
198 logger.debug(" > this is a default network and will "
200 elif network['router:external'] is True:
201 logger.debug(" > this is an external network and will "
204 logger.debug(" > this network will be deleted.")
205 network_ids.append(net_id)
208 ports = os_utils.get_port_list(neutron_client)
210 logger.debug("There are no ports in the deployment. ")
212 remove_ports(neutron_client, ports, network_ids)
215 routers = os_utils.get_router_list(neutron_client)
217 logger.debug("There are no routers in the deployment. ")
219 remove_routers(neutron_client, routers)
222 if network_ids is not None:
223 for net_id in network_ids:
224 logger.debug("Removing network %s ..." % net_id)
225 if os_utils.delete_neutron_net(neutron_client, net_id):
226 logger.debug(" > Done!")
228 logger.error("There has been a problem removing the "
229 "network %s..." % net_id)
232 def remove_ports(neutron_client, ports, network_ids):
234 if port['network_id'] in network_ids:
237 subnet_id = port['fixed_ips'][0]['subnet_id']
239 logger.info(" > WARNING: Port %s does not contain 'fixed_ips'"
242 router_id = port['device_id']
243 if len(port['fixed_ips']) == 0 and router_id == '':
244 logger.debug("Removing port %s ..." % port_id)
245 if (os_utils.delete_neutron_port(neutron_client, port_id)):
246 logger.debug(" > Done!")
248 logger.error("There has been a problem removing the "
249 "port %s ..." % port_id)
250 force_remove_port(neutron_client, port_id)
252 elif port['device_owner'] == 'network:router_interface':
253 logger.debug("Detaching port %s (subnet %s) from router %s ..."
254 % (port_id, subnet_id, router_id))
255 if os_utils.remove_interface_router(
256 neutron_client, router_id, subnet_id):
257 time.sleep(5) # leave 5 seconds to detach
258 logger.debug(" > Done!")
260 logger.error("There has been a problem removing the "
261 "interface %s from router %s..."
262 % (subnet_id, router_id))
263 force_remove_port(neutron_client, port_id)
265 force_remove_port(neutron_client, port_id)
268 def force_remove_port(neutron_client, port_id):
269 logger.debug("Clearing device_owner for port %s ..." % port_id)
270 os_utils.update_neutron_port(neutron_client, port_id,
271 device_owner='clear')
272 logger.debug("Removing port %s ..." % port_id)
273 if os_utils.delete_neutron_port(neutron_client, port_id):
274 logger.debug(" > Done!")
276 logger.error("There has been a problem removing the port %s..."
280 def remove_routers(neutron_client, routers):
281 for router in routers:
282 router_id = router['id']
283 router_name = router['name']
284 if router_id not in default_routers:
285 logger.debug("Checking '%s' with ID=(%s) ..." % (router_name,
287 if router['external_gateway_info'] is not None:
288 logger.debug("Router has gateway to external network."
290 if os_utils.remove_gateway_router(neutron_client, router_id):
291 logger.debug(" > Done!")
293 logger.error("There has been a problem removing "
296 logger.debug("Router is not connected to anything."
297 "Ready to remove...")
298 logger.debug("Removing router %s(%s) ..."
299 % (router_name, router_id))
300 if os_utils.delete_neutron_router(neutron_client, router_id):
301 logger.debug(" > Done!")
303 logger.error("There has been a problem removing the "
304 "router '%s'(%s)..." % (router_name, router_id))
307 def remove_security_groups(neutron_client):
308 logger.info("Removing Security groups...")
309 secgroups = os_utils.get_security_groups(neutron_client)
310 if secgroups is None or len(secgroups) == 0:
311 logger.debug("No security groups found.")
314 for secgroup in secgroups:
315 secgroup_name = secgroup['name']
316 secgroup_id = secgroup['id']
317 logger.debug("'%s', ID=%s " % (secgroup_name, secgroup_id))
318 if secgroup_id not in default_security_groups:
319 logger.debug(" Removing '%s'..." % secgroup_name)
320 if os_utils.delete_security_group(neutron_client, secgroup_id):
321 logger.debug(" > Done!")
323 logger.error("There has been a problem removing the "
324 "security group %s..." % secgroup_id)
326 logger.debug(" > this is a default security group and will NOT "
330 def remove_users(keystone_client):
331 logger.info("Removing Users...")
332 users = os_utils.get_users(keystone_client)
334 logger.debug("There are no users in the deployment. ")
338 user_name = getattr(user, 'name')
339 user_id = getattr(user, 'id')
340 logger.debug("'%s', ID=%s " % (user_name, user_id))
341 if user_id not in default_users:
342 logger.debug(" Removing '%s'..." % user_name)
343 if os_utils.delete_user(keystone_client, user_id):
344 logger.debug(" > Done!")
346 logger.error("There has been a problem removing the "
347 "user '%s'(%s)..." % (user_name, user_id))
349 logger.debug(" > this is a default user and will "
353 def remove_tenants(keystone_client):
354 logger.info("Removing Tenants...")
355 tenants = os_utils.get_tenants(keystone_client)
357 logger.debug("There are no tenants in the deployment. ")
360 for tenant in tenants:
361 tenant_name = getattr(tenant, 'name')
362 tenant_id = getattr(tenant, 'id')
363 logger.debug("'%s', ID=%s " % (tenant_name, tenant_id))
364 if tenant_id not in default_tenants:
365 logger.debug(" Removing '%s'..." % tenant_name)
366 if os_utils.delete_tenant(keystone_client, tenant_id):
367 logger.debug(" > Done!")
369 logger.error("There has been a problem removing the "
370 "tenant '%s'(%s)..." % (tenant_name, tenant_id))
372 logger.debug(" > this is a default tenant and will "
377 logger.info("+++++++++++++++++++++++++++++++")
378 logger.info("Cleaning OpenStack resources...")
379 logger.info("+++++++++++++++++++++++++++++++")
380 creds_nova = os_utils.get_credentials("nova")
381 nova_client = novaclient.Client('2', **creds_nova)
383 creds_neutron = os_utils.get_credentials("neutron")
384 neutron_client = neutronclient.Client(**creds_neutron)
386 creds_keystone = os_utils.get_credentials("keystone")
387 keystone_client = keystoneclient.Client(**creds_keystone)
389 creds_cinder = os_utils.get_credentials("cinder")
390 # cinder_client = cinderclient.Client(**creds_cinder)
391 cinder_client = cinderclient.Client('1', creds_cinder['username'],
392 creds_cinder['api_key'],
393 creds_cinder['project_id'],
394 creds_cinder['auth_url'],
395 service_type="volume")
397 if not os_utils.check_credentials():
398 logger.error("Please source the openrc credentials and run "
402 remove_instances(nova_client)
404 remove_images(nova_client)
406 remove_volumes(cinder_client)
408 remove_floatingips(nova_client)
410 remove_networks(neutron_client)
412 remove_security_groups(neutron_client)
414 remove_users(keystone_client)
416 remove_tenants(keystone_client)
420 if __name__ == '__main__':