Merge "test-requirements.txt is added for unit test."
[functest.git] / functest / utils / openstack_clean.py
1 #!/usr/bin/env python
2 #
3 # Description:
4 #   Cleans possible leftovers after running functest tests:
5 #       - Nova instances
6 #       - Glance images
7 #       - Cinder volumes
8 #       - Floating IPs
9 #       - Neutron networks, subnets and ports
10 #       - Routers
11 #       - Users and tenants
12 #       - Tacker VNFDs and VNFs
13 #       - Tacker SFCs and SFC classifiers
14 #
15 # Author:
16 #    jose.lausuch@ericsson.com
17 #
18 #
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
23 #
24
25 import time
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
29 import yaml
30 import functest.utils.functest_constants as ft_constants
31
32 logger = ft_logger.Logger("openstack_clean").getLogger()
33
34 OS_SNAPSHOT_FILE = ft_constants.OPENSTACK_SNAPSHOT_FILE
35
36
37 def separator():
38     logger.debug("-------------------------------------------")
39
40
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.")
46         return
47
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.")
57             else:
58                 logger.error("There has been a problem removing the "
59                              "instance %s..." % instance_id)
60         else:
61             logger.debug("   > this is a default instance and will "
62                          "NOT be deleted.")
63
64     timeout = 50
65     while timeout > 0:
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...")
71                 timeout -= 1
72                 time.sleep(1)
73                 continue
74         break
75
76
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.")
82         return
83
84     for image in images:
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!")
94             else:
95                 logger.error("There has been a problem removing the"
96                              "image %s..." % image_id)
97         else:
98             logger.debug("   > this is a default image and will "
99                          "NOT be deleted.")
100
101
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.")
107         return
108
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!")
118             else:
119                 logger.debug("Trying forced removal...")
120                 if os_utils.delete_volume(cinder_client,
121                                           volume_id,
122                                           forced=True):
123                     logger.debug("  > Done!")
124                 else:
125                     logger.error("There has been a problem removing the "
126                                  "volume %s..." % volume_id)
127         else:
128             logger.debug("   > this is a default volume and will "
129                          "NOT be deleted.")
130
131
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.")
137         return
138
139     init_len = len(floatingips)
140     deleted = 0
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!")
150                 deleted += 1
151             else:
152                 logger.error("There has been a problem removing the "
153                              "floating IP %s..." % fip_id)
154         else:
155             logger.debug("   > this is a default floating IP and will "
156                          "NOT be deleted.")
157
158     timeout = 50
159     while timeout > 0:
160         floatingips = os_utils.get_floating_ips(nova_client)
161         if floatingips is None or len(floatingips) == (init_len - deleted):
162             break
163         else:
164             logger.debug("Waiting for floating ips to be released...")
165             timeout -= 1
166             time.sleep(1)
167
168
169 def remove_networks(neutron_client, default_networks, default_routers):
170     logger.debug("Removing Neutron objects")
171     network_ids = []
172     networks = os_utils.get_network_list(neutron_client)
173     if networks is None:
174         logger.debug("There are no networks in the deployment. ")
175     else:
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 "
184                              "NOT be deleted.")
185             elif network['router:external'] is True:
186                 logger.debug("   > this is an external network and will "
187                              "NOT be deleted.")
188             else:
189                 logger.debug("   > this network will be deleted.")
190                 network_ids.append(net_id)
191
192     # delete ports
193     ports = os_utils.get_port_list(neutron_client)
194     if ports is None:
195         logger.debug("There are no ports in the deployment. ")
196     else:
197         remove_ports(neutron_client, ports, network_ids)
198
199     # remove routers
200     routers = os_utils.get_router_list(neutron_client)
201     if routers is None:
202         logger.debug("There are no routers in the deployment. ")
203     else:
204         remove_routers(neutron_client, routers, default_routers)
205
206     # trozet: wait for Neutron to auto-cleanup HA networks when HA router is
207     #  deleted
208     time.sleep(5)
209
210     # remove networks
211     if network_ids is not None:
212         for net_id in network_ids:
213             networks = os_utils.get_network_list(neutron_client)
214             if networks is None:
215                 logger.debug("No networks left to remove")
216                 break
217             elif not any(network['id'] == net_id for network in networks):
218                 logger.debug("Network %s has already been removed" % net_id)
219                 continue
220             logger.debug("Removing network %s ..." % net_id)
221             if os_utils.delete_neutron_net(neutron_client, net_id):
222                 logger.debug("  > Done!")
223             else:
224                 logger.error("There has been a problem removing the "
225                              "network %s..." % net_id)
226
227
228 def remove_ports(neutron_client, ports, network_ids):
229     for port in ports:
230         if port['network_id'] in network_ids:
231             port_id = port['id']
232             try:
233                 subnet_id = port['fixed_ips'][0]['subnet_id']
234             except:
235                 logger.debug("  > WARNING: Port %s does not contain fixed_ips"
236                              % port_id)
237                 logger.info(port)
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!")
243                 else:
244                     logger.error("There has been a problem removing the "
245                                  "port %s ..." % port_id)
246                     force_remove_port(neutron_client, port_id)
247
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!")
255                 else:
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)
260             else:
261                 force_remove_port(neutron_client, port_id)
262
263
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!")
271     else:
272         logger.error("There has been a problem removing the port %s..."
273                      % port_id)
274
275
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,
283                                                              router_id))
284             if router['external_gateway_info'] is not None:
285                 logger.debug("Router has gateway to external network."
286                              "Removing link...")
287                 if os_utils.remove_gateway_router(neutron_client, router_id):
288                     logger.debug("  > Done!")
289                 else:
290                     logger.error("There has been a problem removing "
291                                  "the gateway...")
292             else:
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!")
299             else:
300                 logger.error("There has been a problem removing the "
301                              "router '%s'(%s)..." % (router_name, router_id))
302
303
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.")
309         return
310
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!")
319             else:
320                 logger.error("There has been a problem removing the "
321                              "security group %s..." % secgroup_id)
322         else:
323             logger.debug("   > this is a default security group and will NOT "
324                          "be deleted.")
325
326
327 def remove_users(keystone_client, default_users):
328     logger.debug("Removing Users...")
329     users = os_utils.get_users(keystone_client)
330     if users is None:
331         logger.debug("There are no users in the deployment. ")
332         return
333
334     for user in users:
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!")
343             else:
344                 logger.error("There has been a problem removing the "
345                              "user '%s'(%s)..." % (user_name, user_id))
346         else:
347             logger.debug("   > this is a default user and will "
348                          "NOT be deleted.")
349
350
351 def remove_tenants(keystone_client, default_tenants):
352     logger.debug("Removing Tenants...")
353     tenants = os_utils.get_tenants(keystone_client)
354     if tenants is None:
355         logger.debug("There are no tenants in the deployment. ")
356         return
357
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!")
367             else:
368                 logger.error("There has been a problem removing the "
369                              "tenant '%s'(%s)..." % (tenant_name, tenant_id))
370         else:
371             logger.debug("   > this is a default tenant and will "
372                          "NOT be deleted.")
373
374
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']
378     if vnfds is None:
379         logger.debug("There are no Tacker VNFDs in the deployment. ")
380         return
381
382     for vnfd in vnfds:
383         vnfd_name = vnfd['name']
384         vnfd_id = vnfd['id']
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!")
392             else:
393                 logger.error("There has been a problem removing the "
394                              "VNFD '%s'(%s)..." % (vnfd_name, vnfd_id))
395         else:
396             logger.debug("   > this is a default VNFD and will "
397                          "NOT be deleted.")
398
399
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']
403     if vnfs is None:
404         logger.debug("There are no Tacker VNFs in the deployment. ")
405         return
406
407     for vnf in vnfs:
408         vnf_name = vnf['name']
409         vnf_id = vnf['id']
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!")
417             else:
418                 logger.error("There has been a problem removing the "
419                              "VNF '%s'(%s)..." % (vnf_name, vnf_id))
420         else:
421             logger.debug("   > this is a default VNF and will "
422                          "NOT be deleted.")
423
424
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']
428     if sfcs is None:
429         logger.debug("There are no Tacker SFCs in the deployment. ")
430         return
431
432     for sfc in sfcs:
433         sfc_name = sfc['name']
434         sfc_id = sfc['id']
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!")
442             else:
443                 logger.error("There has been a problem removing the "
444                              "SFC '%s'(%s)..." % (sfc_name, sfc_id))
445         else:
446             logger.debug("   > this is a default SFC and will "
447                          "NOT be deleted.")
448
449
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']
454     if sfc_clfs is None:
455         logger.debug("There are no Tacker SFC classifiers in the deployment.")
456         return
457
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!")
469             else:
470                 logger.error("There has been a problem removing the "
471                              "SFC classifier '%s'(%s)..."
472                              % (sfc_clf_name, sfc_clf_id))
473         else:
474             logger.debug("   > this is a default SFC classifier and will "
475                          "NOT be deleted.")
476
477
478 def main():
479     logger.info("Cleaning OpenStack resources...")
480
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()
486
487     try:
488         with open(OS_SNAPSHOT_FILE) as f:
489             snapshot_yaml = yaml.safe_load(f)
490     except Exception:
491         logger.info("The file %s does not exist. The OpenStack snapshot must"
492                     " be created first. Aborting cleanup." % OS_SNAPSHOT_FILE)
493         exit(0)
494
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')
508
509     if not os_utils.check_credentials():
510         logger.error("Please source the openrc credentials and run "
511                      "the script again.")
512         exit(-1)
513
514     remove_instances(nova_client, default_instances)
515     separator()
516     remove_images(nova_client, default_images)
517     separator()
518     remove_volumes(cinder_client, default_volumes)
519     separator()
520     remove_floatingips(nova_client, default_floatingips)
521     separator()
522     remove_networks(neutron_client, default_networks, default_routers)
523     separator()
524     remove_security_groups(neutron_client, default_security_groups)
525     separator()
526     remove_users(keystone_client, default_users)
527     separator()
528     remove_tenants(keystone_client, default_tenants)
529     separator()
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)
533     separator()
534     remove_tacker_sfcs(tacker_client, default_sfcs)
535     separator()
536     remove_tacker_vnfs(tacker_client, default_vnfs)
537     separator()
538     remove_tacker_vnfds(tacker_client, default_vnfds)
539     separator()
540
541
542 if __name__ == '__main__':
543     main()