Create a private shared network for Rally
[functest.git] / utils / clean_openstack.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 #
13 # Author:
14 #    jose.lausuch@ericsson.com
15 #
16 #
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
21 #
22
23 import os
24 import time
25 import yaml
26
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
31
32 import functest.utils.functest_logger as ft_logger
33 import functest.utils.openstack_utils as os_utils
34
35
36 """ logging configuration """
37 logger = ft_logger.Logger("clean_openstack").getLogger()
38
39 REPO_PATH = os.environ['repos_dir'] + '/functest/'
40
41 DEFAULTS_FILE = '/home/opnfv/functest/conf/os_defaults.yaml'
42
43 try:
44     with open(DEFAULTS_FILE) as f:
45         defaults_yaml = yaml.safe_load(f)
46 except Exception, e:
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)
50     exit(0)
51
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')
61
62
63 def separator():
64     logger.info("-------------------------------------------")
65
66
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.")
72         return
73
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!")
81         else:
82             logger.error("There has been a problem removing the "
83                          "instance %s..." % instance_id)
84
85     timeout = 50
86     while timeout > 0:
87         instances = os_utils.get_instances(nova_client)
88         if instances is None or len(instances) == 0:
89             break
90         else:
91             logger.debug("Waiting for instances to be terminated...")
92             timeout -= 1
93             time.sleep(1)
94
95
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.")
101         return
102
103     for image in images:
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!")
112             else:
113                 logger.error("There has been a problem removing the"
114                              "image %s..." % image_id)
115         else:
116             logger.debug("   > this is a default image and will "
117                          "NOT be deleted.")
118
119
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.")
125         return
126
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!")
135             else:
136                 logger.debug("Trying forced removal...")
137                 if os_utils.delete_volume(cinder_client,
138                                           volume_id,
139                                           forced=True):
140                     logger.debug("  > Done!")
141                 else:
142                     logger.error("There has been a problem removing the "
143                                  "volume %s..." % volume_id)
144         else:
145             logger.debug("   > this is a default volume and will "
146                          "NOT be deleted.")
147
148
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.")
154         return
155
156     init_len = len(floatingips)
157     deleted = 0
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!")
166                 deleted += 1
167             else:
168                 logger.error("There has been a problem removing the "
169                              "floating IP %s..." % fip_id)
170         else:
171             logger.debug("   > this is a default floating IP and will "
172                          "NOT be deleted.")
173
174     timeout = 50
175     while timeout > 0:
176         floatingips = os_utils.get_floating_ips(nova_client)
177         if floatingips is None or len(floatingips) == (init_len - deleted):
178             break
179         else:
180             logger.debug("Waiting for floating ips to be released...")
181             timeout -= 1
182             time.sleep(1)
183
184
185 def remove_networks(neutron_client):
186     logger.info("Removing Neutron objects")
187     network_ids = []
188     networks = os_utils.get_network_list(neutron_client)
189     if networks is None:
190         logger.debug("There are no networks in the deployment. ")
191     else:
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 "
199                              "NOT be deleted.")
200             elif network['router:external'] is True:
201                 logger.debug("   > this is an external network and will "
202                              "NOT be deleted.")
203             else:
204                 logger.debug("   > this network will be deleted.")
205                 network_ids.append(net_id)
206
207     # delete ports
208     ports = os_utils.get_port_list(neutron_client)
209     if ports is None:
210         logger.debug("There are no ports in the deployment. ")
211     else:
212         remove_ports(neutron_client, ports, network_ids)
213
214     # remove routers
215     routers = os_utils.get_router_list(neutron_client)
216     if routers is None:
217         logger.debug("There are no routers in the deployment. ")
218     else:
219         remove_routers(neutron_client, routers)
220
221     # remove networks
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!")
227             else:
228                 logger.error("There has been a problem removing the "
229                              "network %s..." % net_id)
230
231
232 def remove_ports(neutron_client, ports, network_ids):
233     for port in ports:
234         if port['network_id'] in network_ids:
235             port_id = port['id']
236             try:
237                 subnet_id = port['fixed_ips'][0]['subnet_id']
238             except:
239                 logger.info("  > WARNING: Port %s does not contain 'fixed_ips'"
240                             % port_id)
241                 print port
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!")
247                 else:
248                     logger.error("There has been a problem removing the "
249                                  "port %s ..." % port_id)
250                     force_remove_port(neutron_client, port_id)
251
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!")
259                 else:
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)
264             else:
265                 force_remove_port(neutron_client, port_id)
266
267
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!")
275     else:
276         logger.error("There has been a problem removing the port %s..."
277                      % port_id)
278
279
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,
286                                                              router_id))
287             if router['external_gateway_info'] is not None:
288                 logger.debug("Router has gateway to external network."
289                              "Removing link...")
290                 if os_utils.remove_gateway_router(neutron_client, router_id):
291                     logger.debug("  > Done!")
292                 else:
293                     logger.error("There has been a problem removing "
294                                  "the gateway...")
295             else:
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!")
302             else:
303                 logger.error("There has been a problem removing the "
304                              "router '%s'(%s)..." % (router_name, router_id))
305
306
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.")
312         return
313
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!")
322             else:
323                 logger.error("There has been a problem removing the "
324                              "security group %s..." % secgroup_id)
325         else:
326             logger.debug("   > this is a default security group and will NOT "
327                          "be deleted.")
328
329
330 def remove_users(keystone_client):
331     logger.info("Removing Users...")
332     users = os_utils.get_users(keystone_client)
333     if users is None:
334         logger.debug("There are no users in the deployment. ")
335         return
336
337     for user in users:
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!")
345             else:
346                 logger.error("There has been a problem removing the "
347                              "user '%s'(%s)..." % (user_name, user_id))
348         else:
349             logger.debug("   > this is a default user and will "
350                          "NOT be deleted.")
351
352
353 def remove_tenants(keystone_client):
354     logger.info("Removing Tenants...")
355     tenants = os_utils.get_tenants(keystone_client)
356     if tenants is None:
357         logger.debug("There are no tenants in the deployment. ")
358         return
359
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!")
368             else:
369                 logger.error("There has been a problem removing the "
370                              "tenant '%s'(%s)..." % (tenant_name, tenant_id))
371         else:
372             logger.debug("   > this is a default tenant and will "
373                          "NOT be deleted.")
374
375
376 def main():
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)
382
383     creds_neutron = os_utils.get_credentials("neutron")
384     neutron_client = neutronclient.Client(**creds_neutron)
385
386     creds_keystone = os_utils.get_credentials("keystone")
387     keystone_client = keystoneclient.Client(**creds_keystone)
388
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")
396
397     if not os_utils.check_credentials():
398         logger.error("Please source the openrc credentials and run "
399                      "the script again.")
400         exit(-1)
401
402     remove_instances(nova_client)
403     separator()
404     remove_images(nova_client)
405     separator()
406     remove_volumes(cinder_client)
407     separator()
408     remove_floatingips(nova_client)
409     separator()
410     remove_networks(neutron_client)
411     separator()
412     remove_security_groups(neutron_client)
413     separator()
414     remove_users(keystone_client)
415     separator()
416     remove_tenants(keystone_client)
417     separator()
418
419
420 if __name__ == '__main__':
421     main()