4 # Cleans possible leftovers after running functest tests:
9 # - Neutron networks, subnets and ports
14 # jose.lausuch@ericsson.com
24 import novaclient.v2.client as novaclient
25 from neutronclient.v2_0 import client as neutronclient
26 from keystoneclient.v2_0 import client as keystoneclient
27 from cinderclient import client as cinderclient
29 parser = argparse.ArgumentParser()
30 parser.add_argument("repo_path", help="Path to the repository")
31 parser.add_argument("-d", "--debug", help="Debug mode", action="store_true")
32 args = parser.parse_args()
35 """ logging configuration """
36 logger = logging.getLogger('clean_openstack')
37 logger.setLevel(logging.DEBUG)
39 ch = logging.StreamHandler()
41 ch.setLevel(logging.DEBUG)
43 ch.setLevel(logging.INFO)
45 sys.path.append(args.repo_path + "testcases/")
48 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
49 ch.setFormatter(formatter)
52 default_images = ['TestVM']
53 default_networks = ['net04', 'net04_ext', 'functest-net']
54 default_routers = ['router04']
55 default_users = ["heat", "heat-cfn", "cinder", "nova", "swift", "glance",
56 "neutron", "admin", "fuel_stats_user", "quantum", "heat-cfn_heat",
57 "ceilometer", "cinder_cinderv2"]
58 default_tenants = ["admin", "services","service"]
59 default_security_groups = ['default']
62 logger.debug("-------------------------------------------")
64 def remove_instances(nova_client):
65 logger.info("Removing Nova instances...")
66 instances = functest_utils.get_instances(nova_client)
67 if instances is None or len(instances) == 0:
68 logger.debug("No instances found.")
71 for instance in instances:
72 instance_name = getattr(instance, 'name')
73 instance_id = getattr(instance, 'id')
74 logger.debug("Removing instance '%s', ID=%s ..." % (instance_name,instance_id))
75 if functest_utils.delete_instance(nova_client, instance_id):
76 logger.debug(" > Done!")
78 logger.info(" > ERROR: There has been a problem removing the "
79 "instance %s..." % instance_id)
83 instances = functest_utils.get_instances(nova_client)
84 if instances is None or len(instances) == 0:
87 logger.debug("Waiting for instances to be terminated...")
92 def remove_images(nova_client):
93 logger.info("Removing Glance images...")
94 images = functest_utils.get_images(nova_client)
95 if images is None or len(images) == 0:
96 logger.debug("No images found.")
100 image_name = getattr(image, 'name')
101 image_id = getattr(image, 'id')
102 logger.debug("'%s', ID=%s " %(image_name,image_id))
103 if image_name not in default_images:
104 logger.debug("Removing image '%s', ID=%s ..." % (image_name,image_id))
105 if functest_utils.delete_glance_image(nova_client, image_id):
106 logger.debug(" > Done!")
108 logger.info(" > ERROR: There has been a problem removing the"
109 "image %s..." % image_id)
111 logger.debug(" > this is a default image and will NOT be deleted.")
114 def remove_volumes(cinder_client):
115 logger.info("Removing Cinder volumes...")
116 volumes = functest_utils.get_volumes(cinder_client)
117 if volumes is None or len(volumes) == 0:
118 logger.debug("No volumes found.")
121 for volume in volumes:
122 volume_id = getattr(volume, 'id')
123 logger.debug("Removing cinder volume %s ..." % volume_id)
124 if functest_utils.delete_volume(cinder_client, volume_id):
125 logger.debug(" > Done!")
127 logger.info(" > ERROR: There has been a problem removing the "
128 "volume %s..." % volume_id)
131 def remove_floatingips(nova_client):
132 logger.info("Removing floating IPs...")
133 floatingips = functest_utils.get_floating_ips(nova_client)
134 if floatingips is None or len(floatingips) == 0:
135 logger.debug("No floating IPs found.")
138 for fip in floatingips:
139 fip_id = getattr(fip, 'id')
140 logger.debug("Removing floating IP %s ..." % fip_id)
141 if functest_utils.delete_floating_ip(nova_client, fip_id):
142 logger.debug(" > Done!")
144 logger.info(" > ERROR: There has been a problem removing the "
145 "floating IP %s..." % fip_id)
148 def remove_networks(neutron_client):
149 logger.info("Removing Neutron objects")
151 networks = functest_utils.get_network_list(neutron_client)
153 logger.debug("There are no networks in the deployment. ")
156 logger.debug("Existing networks:")
157 for network in networks:
158 net_id = network['id']
159 net_name = network['name']
160 logger.debug(" '%s', ID=%s " %(net_name,net_id))
161 if net_name not in default_networks:
162 logger.debug(" > this is not a default network and will be deleted.")
163 network_ids.append(net_id)
165 logger.debug(" > this is a default network and will NOT be deleted.")
168 #remove interfaces router and delete ports
169 ports = functest_utils.get_port_list(neutron_client)
171 logger.debug("There are no ports in the deployment. ")
174 #debug information (to be removed when it works many times in a row)
178 if port['network_id'] in network_ids:
181 subnet_id = port['fixed_ips'][0]['subnet_id']
183 logger.info(" > WARNING: Port %s does not contain 'fixed_ips'" % port_id)
186 router_id = port['device_id']
187 if len(port['fixed_ips']) == 0 and router_id == '':
188 logger.debug("Removing port %s ..." % port_id)
189 if functest_utils.delete_neutron_port(neutron_client, port_id):
190 logger.debug(" > Done!")
192 logger.info(" > ERROR: There has been a problem removing the "
193 "port %s ..." %port_id)
195 elif port['device_owner'] == 'network:router_interface':
196 logger.debug("Detaching port %s (subnet %s) from router %s ..."
197 % (port_id,subnet_id,router_id))
198 if functest_utils.remove_interface_router(neutron_client,
199 router_id, subnet_id):
200 time.sleep(5) # leave 5 seconds to detach before doing anything else
201 logger.debug(" > Done!")
203 logger.info(" > ERROR: There has been a problem removing the "
204 "interface %s from router %s..." %(subnet_id,router_id))
207 logger.debug("Removing port %s ..." % port_id)
208 if functest_utils.delete_neutron_port(neutron_client, port_id):
209 logger.debug(" > Done!")
211 logger.info(" > ERROR: There has been a problem removing the "
212 "port %s ..." %port_id)
216 routers = functest_utils.get_router_list(neutron_client)
218 logger.debug("There are no routers in the deployment. ")
220 for router in routers:
221 router_id = router['id']
222 router_name = router['name']
223 if router_name not in default_routers:
224 logger.debug("Checking '%s' with ID=(%s) ..." % (router_name,router_id))
225 if router['external_gateway_info'] != None:
226 logger.debug("Router has gateway to external network. Removing link...")
227 if functest_utils.remove_gateway_router(neutron_client, router_id):
228 logger.debug(" > Done!")
230 logger.info(" > ERROR: There has been a problem removing "
235 logger.debug("Router is not connected to anything. Ready to remove...")
236 logger.debug("Removing router %s(%s) ..." % (router_name,router_id))
237 if functest_utils.delete_neutron_router(neutron_client, router_id):
238 logger.debug(" > Done!")
240 logger.info(" > ERROR: There has been a problem removing the "
241 "router '%s'(%s)..." % (router_name,router_id))
245 for net_id in network_ids:
246 logger.debug("Removing network %s ..." % net_id)
247 if functest_utils.delete_neutron_net(neutron_client, net_id):
248 logger.debug(" > Done!")
250 logger.info(" > ERROR: There has been a problem removing the "
251 "network %s..." % net_id)
254 def remove_security_groups(neutron_client):
255 logger.info("Removing Security groups...")
256 secgroups = functest_utils.get_security_groups(neutron_client)
257 if secgroups is None or len(secgroups) == 0:
258 logger.debug("No security groups found.")
261 for secgroup in secgroups:
262 secgroup_name = secgroup['name']
263 secgroup_id = secgroup['id']
264 logger.debug("'%s', ID=%s " %(secgroup_name,secgroup_id))
265 if secgroup_name not in default_security_groups:
266 logger.debug(" Removing '%s'..." % secgroup_name)
267 if functest_utils.delete_security_group(neutron_client, secgroup_id):
268 logger.debug(" > Done!")
270 logger.info(" > ERROR: There has been a problem removing the "
271 "security group %s..." % secgroup_id)
273 logger.debug(" > this is a default security group and will NOT "
277 def remove_users(keystone_client):
278 logger.info("Removing Users...")
279 users = functest_utils.get_users(keystone_client)
281 logger.debug("There are no users in the deployment. ")
285 user_name = getattr(user, 'name')
286 user_id = getattr(user, 'id')
287 logger.debug("'%s', ID=%s " %(user_name,user_id))
288 if user_name not in default_users:
289 logger.debug(" Removing '%s'..." % user_name)
290 if functest_utils.delete_user(keystone_client,user_id):
291 logger.debug(" > Done!")
293 logger.info(" > ERROR: There has been a problem removing the "
294 "user '%s'(%s)..." % (user_name,user_id))
296 logger.debug(" > this is a default user and will NOT be deleted.")
299 def remove_tenants(keystone_client):
300 logger.info("Removing Tenants...")
301 tenants = functest_utils.get_tenants(keystone_client)
303 logger.debug("There are no tenants in the deployment. ")
306 for tenant in tenants:
307 tenant_name=getattr(tenant, 'name')
308 tenant_id = getattr(tenant, 'id')
309 logger.debug("'%s', ID=%s " %(tenant_name,tenant_id))
310 if tenant_name not in default_tenants:
311 logger.debug(" Removing '%s'..." % tenant_name)
312 if functest_utils.delete_tenant(keystone_client,tenant_id):
313 logger.debug(" > Done!")
315 logger.info(" > ERROR: There has been a problem removing the "
316 "tenant '%s'(%s)..." % (tenant_name,tenant_id))
318 logger.debug(" > this is a default tenant and will NOT be deleted.")
323 creds_nova = functest_utils.get_credentials("nova")
324 nova_client = novaclient.Client(**creds_nova)
326 creds_neutron = functest_utils.get_credentials("neutron")
327 neutron_client = neutronclient.Client(**creds_neutron)
329 creds_keystone = functest_utils.get_credentials("keystone")
330 keystone_client = keystoneclient.Client(**creds_keystone)
332 creds_cinder = functest_utils.get_credentials("cinder")
333 #cinder_client = cinderclient.Client(**creds_cinder)
334 cinder_client = cinderclient.Client('1',creds_cinder['username'],
335 creds_cinder['api_key'],
336 creds_cinder['project_id'],
337 creds_cinder['auth_url'],
338 service_type="volume")
340 if not functest_utils.check_credentials():
341 logger.error("Please source the openrc credentials and run the script again.")
345 remove_instances(nova_client)
347 remove_images(nova_client)
349 remove_volumes(cinder_client)
351 remove_floatingips(nova_client)
353 remove_networks(neutron_client)
355 remove_security_groups(neutron_client)
357 remove_users(keystone_client)
359 remove_tenants(keystone_client)
365 if __name__ == '__main__':