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("-d", "--debug", help="Debug mode", action="store_true")
31 args = parser.parse_args()
34 """ logging configuration """
35 logger = logging.getLogger('clean_openstack')
36 logger.setLevel(logging.DEBUG)
38 ch = logging.StreamHandler()
40 ch.setLevel(logging.DEBUG)
42 ch.setLevel(logging.INFO)
44 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
45 ch.setFormatter(formatter)
48 REPO_PATH=os.environ['repos_dir']+'/functest/'
49 if not os.path.exists(REPO_PATH):
50 logger.error("Functest repository directory not found '%s'" % REPO_PATH)
52 sys.path.append(REPO_PATH + "testcases/")
55 default_images = ['TestVM']
56 default_networks = ['net04', 'net04_ext', 'functest-net']
57 default_routers = ['router04']
58 default_users = ["heat", "heat-cfn", "cinder", "nova", "swift", "glance",
59 "neutron", "admin", "fuel_stats_user", "quantum", "heat-cfn_heat",
60 "ceilometer", "cinder_cinderv2", "demo"]
61 default_tenants = ["admin", "services", "service", "demo"]
62 default_security_groups = ['default']
65 logger.debug("-------------------------------------------")
67 def remove_instances(nova_client):
68 logger.info("Removing Nova instances...")
69 instances = functest_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 ..." % (instance_name,instance_id))
78 if functest_utils.delete_instance(nova_client, instance_id):
79 logger.debug(" > Done!")
81 logger.info(" > ERROR: There has been a problem removing the "
82 "instance %s..." % instance_id)
86 instances = functest_utils.get_instances(nova_client)
87 if instances is None or len(instances) == 0:
90 logger.debug("Waiting for instances to be terminated...")
95 def remove_images(nova_client):
96 logger.info("Removing Glance images...")
97 images = functest_utils.get_images(nova_client)
98 if images is None or len(images) == 0:
99 logger.debug("No images found.")
103 image_name = getattr(image, 'name')
104 image_id = getattr(image, 'id')
105 logger.debug("'%s', ID=%s " %(image_name,image_id))
106 if image_name not in default_images:
107 logger.debug("Removing image '%s', ID=%s ..." % (image_name,image_id))
108 if functest_utils.delete_glance_image(nova_client, image_id):
109 logger.debug(" > Done!")
111 logger.info(" > ERROR: There has been a problem removing the"
112 "image %s..." % image_id)
114 logger.debug(" > this is a default image and will NOT be deleted.")
117 def remove_volumes(cinder_client):
118 logger.info("Removing Cinder volumes...")
119 volumes = functest_utils.get_volumes(cinder_client)
120 if volumes is None or len(volumes) == 0:
121 logger.debug("No volumes found.")
124 for volume in volumes:
125 volume_id = getattr(volume, 'id')
126 logger.debug("Removing cinder volume %s ..." % volume_id)
127 if functest_utils.delete_volume(cinder_client, volume_id):
128 logger.debug(" > Done!")
130 logger.info(" > ERROR: There has been a problem removing the "
131 "volume %s..." % volume_id)
134 def remove_floatingips(nova_client):
135 logger.info("Removing floating IPs...")
136 floatingips = functest_utils.get_floating_ips(nova_client)
137 if floatingips is None or len(floatingips) == 0:
138 logger.debug("No floating IPs found.")
141 for fip in floatingips:
142 fip_id = getattr(fip, 'id')
143 logger.debug("Removing floating IP %s ..." % fip_id)
144 if functest_utils.delete_floating_ip(nova_client, fip_id):
145 logger.debug(" > Done!")
147 logger.info(" > ERROR: There has been a problem removing the "
148 "floating IP %s..." % fip_id)
152 floatingips = functest_utils.get_floating_ips(nova_client)
153 if floatingips is None or len(floatingips) == 0:
156 logger.debug("Waiting for floating ips to be released...")
161 def remove_networks(neutron_client):
162 logger.info("Removing Neutron objects")
164 networks = functest_utils.get_network_list(neutron_client)
166 logger.debug("There are no networks in the deployment. ")
169 logger.debug("Existing networks:")
170 for network in networks:
171 net_id = network['id']
172 net_name = network['name']
173 logger.debug(" '%s', ID=%s " %(net_name,net_id))
174 if net_name not in default_networks:
175 logger.debug(" > this is not a default network and will be deleted.")
176 network_ids.append(net_id)
178 logger.debug(" > this is a default network and will NOT be deleted.")
181 #remove interfaces router and delete ports
182 ports = functest_utils.get_port_list(neutron_client)
184 logger.debug("There are no ports in the deployment. ")
187 #debug information (to be removed when it works many times in a row)
191 if port['network_id'] in network_ids:
194 subnet_id = port['fixed_ips'][0]['subnet_id']
196 logger.info(" > WARNING: Port %s does not contain 'fixed_ips'" % port_id)
199 router_id = port['device_id']
200 if len(port['fixed_ips']) == 0 and router_id == '':
201 logger.debug("Removing port %s ..." % port_id)
202 if functest_utils.delete_neutron_port(neutron_client, port_id):
203 logger.debug(" > Done!")
205 logger.info(" > ERROR: There has been a problem removing the "
206 "port %s ..." %port_id)
208 elif port['device_owner'] == 'network:router_interface':
209 logger.debug("Detaching port %s (subnet %s) from router %s ..."
210 % (port_id,subnet_id,router_id))
211 if functest_utils.remove_interface_router(neutron_client,
212 router_id, subnet_id):
213 time.sleep(5) # leave 5 seconds to detach before doing anything else
214 logger.debug(" > Done!")
216 logger.info(" > ERROR: There has been a problem removing the "
217 "interface %s from router %s..." %(subnet_id,router_id))
220 logger.debug("Removing port %s ..." % port_id)
221 if functest_utils.delete_neutron_port(neutron_client, port_id):
222 logger.debug(" > Done!")
224 logger.info(" > ERROR: There has been a problem removing the "
225 "port %s ..." %port_id)
229 routers = functest_utils.get_router_list(neutron_client)
231 logger.debug("There are no routers in the deployment. ")
233 for router in routers:
234 router_id = router['id']
235 router_name = router['name']
236 if router_name not in default_routers:
237 logger.debug("Checking '%s' with ID=(%s) ..." % (router_name,router_id))
238 if router['external_gateway_info'] != None:
239 logger.debug("Router has gateway to external network. Removing link...")
240 if functest_utils.remove_gateway_router(neutron_client, router_id):
241 logger.debug(" > Done!")
243 logger.info(" > ERROR: There has been a problem removing "
248 logger.debug("Router is not connected to anything. Ready to remove...")
249 logger.debug("Removing router %s(%s) ..." % (router_name,router_id))
250 if functest_utils.delete_neutron_router(neutron_client, router_id):
251 logger.debug(" > Done!")
253 logger.info(" > ERROR: There has been a problem removing the "
254 "router '%s'(%s)..." % (router_name,router_id))
258 for net_id in network_ids:
259 logger.debug("Removing network %s ..." % net_id)
260 if functest_utils.delete_neutron_net(neutron_client, net_id):
261 logger.debug(" > Done!")
263 logger.info(" > ERROR: There has been a problem removing the "
264 "network %s..." % net_id)
267 def remove_security_groups(neutron_client):
268 logger.info("Removing Security groups...")
269 secgroups = functest_utils.get_security_groups(neutron_client)
270 if secgroups is None or len(secgroups) == 0:
271 logger.debug("No security groups found.")
274 for secgroup in secgroups:
275 secgroup_name = secgroup['name']
276 secgroup_id = secgroup['id']
277 logger.debug("'%s', ID=%s " %(secgroup_name,secgroup_id))
278 if secgroup_name not in default_security_groups:
279 logger.debug(" Removing '%s'..." % secgroup_name)
280 if functest_utils.delete_security_group(neutron_client, secgroup_id):
281 logger.debug(" > Done!")
283 logger.info(" > ERROR: There has been a problem removing the "
284 "security group %s..." % secgroup_id)
286 logger.debug(" > this is a default security group and will NOT "
290 def remove_users(keystone_client):
291 logger.info("Removing Users...")
292 users = functest_utils.get_users(keystone_client)
294 logger.debug("There are no users in the deployment. ")
298 user_name = getattr(user, 'name')
299 user_id = getattr(user, 'id')
300 logger.debug("'%s', ID=%s " %(user_name,user_id))
301 if user_name not in default_users:
302 logger.debug(" Removing '%s'..." % user_name)
303 if functest_utils.delete_user(keystone_client,user_id):
304 logger.debug(" > Done!")
306 logger.info(" > ERROR: There has been a problem removing the "
307 "user '%s'(%s)..." % (user_name,user_id))
309 logger.debug(" > this is a default user and will NOT be deleted.")
312 def remove_tenants(keystone_client):
313 logger.info("Removing Tenants...")
314 tenants = functest_utils.get_tenants(keystone_client)
316 logger.debug("There are no tenants in the deployment. ")
319 for tenant in tenants:
320 tenant_name=getattr(tenant, 'name')
321 tenant_id = getattr(tenant, 'id')
322 logger.debug("'%s', ID=%s " %(tenant_name,tenant_id))
323 if tenant_name not in default_tenants:
324 logger.debug(" Removing '%s'..." % tenant_name)
325 if functest_utils.delete_tenant(keystone_client,tenant_id):
326 logger.debug(" > Done!")
328 logger.info(" > ERROR: There has been a problem removing the "
329 "tenant '%s'(%s)..." % (tenant_name,tenant_id))
331 logger.debug(" > this is a default tenant and will NOT be deleted.")
336 creds_nova = functest_utils.get_credentials("nova")
337 nova_client = novaclient.Client(**creds_nova)
339 creds_neutron = functest_utils.get_credentials("neutron")
340 neutron_client = neutronclient.Client(**creds_neutron)
342 creds_keystone = functest_utils.get_credentials("keystone")
343 keystone_client = keystoneclient.Client(**creds_keystone)
345 creds_cinder = functest_utils.get_credentials("cinder")
346 #cinder_client = cinderclient.Client(**creds_cinder)
347 cinder_client = cinderclient.Client('1',creds_cinder['username'],
348 creds_cinder['api_key'],
349 creds_cinder['project_id'],
350 creds_cinder['auth_url'],
351 service_type="volume")
353 if not functest_utils.check_credentials():
354 logger.error("Please source the openrc credentials and run the script again.")
358 remove_instances(nova_client)
360 remove_images(nova_client)
362 remove_volumes(cinder_client)
364 remove_floatingips(nova_client)
366 remove_networks(neutron_client)
368 remove_security_groups(neutron_client)
370 remove_users(keystone_client)
372 remove_tenants(keystone_client)
378 if __name__ == '__main__':