X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=functest%2Futils%2Fopenstack_clean.py;h=d7df8f848929e249f972b059a8e9436eccd08ca1;hb=1da439765b20bbc9a1f63d736f4c6284b654d50d;hp=0c3ae3e32e7032791bbe07e58c19b5a43e741707;hpb=c10898356f97b3f2e5c39928e4133570b75a52e1;p=functest.git diff --git a/functest/utils/openstack_clean.py b/functest/utils/openstack_clean.py old mode 100755 new mode 100644 index 0c3ae3e32..d7df8f848 --- a/functest/utils/openstack_clean.py +++ b/functest/utils/openstack_clean.py @@ -22,16 +22,16 @@ # http://www.apache.org/licenses/LICENSE-2.0 # +import logging import time -import functest.utils.functest_logger as ft_logger -import functest.utils.openstack_utils as os_utils -import functest.utils.openstack_tacker as os_tacker import yaml -import functest.utils.functest_constants as ft_constants -logger = ft_logger.Logger("openstack_clean").getLogger() +import functest.utils.openstack_utils as os_utils +from functest.utils.constants import CONST + +logger = logging.getLogger(__name__) -OS_SNAPSHOT_FILE = ft_constants.OPENSTACK_SNAPSHOT_FILE +OS_SNAPSHOT_FILE = CONST.openstack_snapshot_file def separator(): @@ -48,9 +48,14 @@ def remove_instances(nova_client, default_instances): for instance in instances: instance_name = getattr(instance, 'name') instance_id = getattr(instance, 'id') + instance_status = getattr(instance, 'status') + instance_state = getattr(instance, 'OS-EXT-STS:task_state') + logger.debug("'%s', ID=%s " % (instance_name, instance_id)) if (instance_id not in default_instances and - instance_name not in default_instances.values()): + instance_name not in default_instances.values() and + instance_status != 'DELETED' and + (instance_status != 'BUILD' or instance_state != 'deleting')): logger.debug("Removing instance '%s' ..." % instance_id) if os_utils.delete_instance(nova_client, instance_id): logger.debug(" > Request sent.") @@ -74,22 +79,25 @@ def remove_instances(nova_client, default_instances): break -def remove_images(nova_client, default_images): +def remove_images(glance_client, default_images): logger.debug("Removing Glance images...") - images = os_utils.get_images(nova_client) - if images is None or len(images) == 0: + images = os_utils.get_images(glance_client) + if images is None: + return -1 + images = {image.id: image.name for image in images} + if len(images) == 0: logger.debug("No images found.") return for image in images: - image_name = getattr(image, 'name') - image_id = getattr(image, 'id') + image_id = image + image_name = images.get(image_id) logger.debug("'%s', ID=%s " % (image_name, image_id)) if (image_id not in default_images and image_name not in default_images.values()): logger.debug("Removing image '%s', ID=%s ..." % (image_name, image_id)) - if os_utils.delete_glance_image(nova_client, image_id): + if os_utils.delete_glance_image(glance_client, image_id): logger.debug(" > Done!") else: logger.error("There has been a problem removing the" @@ -108,7 +116,7 @@ def remove_volumes(cinder_client, default_volumes): for volume in volumes: volume_id = getattr(volume, 'id') - volume_name = getattr(volume, 'display_name') + volume_name = getattr(volume, 'name') logger.debug("'%s', ID=%s " % (volume_name, volume_id)) if (volume_id not in default_volumes and volume_name not in default_volumes.values()): @@ -129,9 +137,9 @@ def remove_volumes(cinder_client, default_volumes): "NOT be deleted.") -def remove_floatingips(nova_client, default_floatingips): +def remove_floatingips(neutron_client, default_floatingips): logger.debug("Removing floating IPs...") - floatingips = os_utils.get_floating_ips(nova_client) + floatingips = os_utils.get_floating_ips(neutron_client) if floatingips is None or len(floatingips) == 0: logger.debug("No floating IPs found.") return @@ -139,13 +147,13 @@ def remove_floatingips(nova_client, default_floatingips): init_len = len(floatingips) deleted = 0 for fip in floatingips: - fip_id = getattr(fip, 'id') - fip_ip = getattr(fip, 'ip') + fip_id = fip['id'] + fip_ip = fip['floating_ip_address'] logger.debug("'%s', ID=%s " % (fip_ip, fip_id)) if (fip_id not in default_floatingips and fip_ip not in default_floatingips.values()): logger.debug("Removing floating IP %s ..." % fip_id) - if os_utils.delete_floating_ip(nova_client, fip_id): + if os_utils.delete_floating_ip(neutron_client, fip_id): logger.debug(" > Done!") deleted += 1 else: @@ -157,7 +165,7 @@ def remove_floatingips(nova_client, default_floatingips): timeout = 50 while timeout > 0: - floatingips = os_utils.get_floating_ips(nova_client) + floatingips = os_utils.get_floating_ips(neutron_client) if floatingips is None or len(floatingips) == (init_len - deleted): break else: @@ -372,117 +380,15 @@ def remove_tenants(keystone_client, default_tenants): "NOT be deleted.") -def remove_tacker_vnfds(tacker_client, default_vnfds): - logger.debug("Removing Tacker VNFDs...") - vnfds = os_tacker.list_vnfds(tacker_client, verbose=True)['vnfds'] - if vnfds is None: - logger.debug("There are no Tacker VNFDs in the deployment. ") - return - - for vnfd in vnfds: - vnfd_name = vnfd['name'] - vnfd_id = vnfd['id'] - logger.debug("'%s', ID=%s " % (vnfd_name, vnfd_id)) - if (vnfd_id not in default_vnfds and - vnfd_name not in default_vnfds.values()): - logger.debug(" Removing '%s'..." % vnfd_name) - deleted = os_tacker.delete_vnfd(tacker_client, vnfd_id=vnfd_id) - if deleted is not None: - logger.debug(" > Done!") - else: - logger.error("There has been a problem removing the " - "VNFD '%s'(%s)..." % (vnfd_name, vnfd_id)) - else: - logger.debug(" > this is a default VNFD and will " - "NOT be deleted.") - - -def remove_tacker_vnfs(tacker_client, default_vnfs): - logger.debug("Removing Tacker VNFs...") - vnfs = os_tacker.list_vnfs(tacker_client, verbose=True)['vnfs'] - if vnfs is None: - logger.debug("There are no Tacker VNFs in the deployment. ") - return - - for vnf in vnfs: - vnf_name = vnf['name'] - vnf_id = vnf['id'] - logger.debug("'%s', ID=%s " % (vnf_name, vnf_id)) - if (vnf_id not in default_vnfs and - vnf_name not in default_vnfs.values()): - logger.debug(" Removing '%s'..." % vnf_name) - deleted = os_tacker.delete_vnf(tacker_client, vnf_id=vnf_id) - if deleted is not None: - logger.debug(" > Done!") - else: - logger.error("There has been a problem removing the " - "VNF '%s'(%s)..." % (vnf_name, vnf_id)) - else: - logger.debug(" > this is a default VNF and will " - "NOT be deleted.") - - -def remove_tacker_sfcs(tacker_client, default_sfcs): - logger.debug("Removing Tacker SFCs...") - sfcs = os_tacker.list_sfcs(tacker_client, verbose=True)['sfcs'] - if sfcs is None: - logger.debug("There are no Tacker SFCs in the deployment. ") - return - - for sfc in sfcs: - sfc_name = sfc['name'] - sfc_id = sfc['id'] - logger.debug("'%s', ID=%s " % (sfc_name, sfc_id)) - if (sfc_id not in default_sfcs and - sfc_name not in default_sfcs.values()): - logger.debug(" Removing '%s'..." % sfc_name) - deleted = os_tacker.delete_sfc(tacker_client, sfc_id=sfc_id) - if deleted is not None: - logger.debug(" > Done!") - else: - logger.error("There has been a problem removing the " - "SFC '%s'(%s)..." % (sfc_name, sfc_id)) - else: - logger.debug(" > this is a default SFC and will " - "NOT be deleted.") - - -def remove_tacker_sfc_classifiers(tacker_client, default_sfc_classifiers): - logger.debug("Removing Tacker SFC classifiers...") - sfc_clfs = os_tacker.list_sfc_classifiers( - tacker_client, verbose=True)['sfc_classfiers'] - if sfc_clfs is None: - logger.debug("There are no Tacker SFC classifiers in the deployment.") - return - - for sfc_clf in sfc_clfs: - sfc_clf_name = sfc_clf['name'] - sfc_clf_id = sfc_clf['id'] - logger.debug("'%s', ID=%s " % (sfc_clf_name, sfc_clf_id)) - if (sfc_clf_id not in default_sfc_classifiers and - sfc_clf_name not in default_sfc_classifiers.values()): - logger.debug(" Removing '%s'..." % sfc_clf_name) - deleted = os_tacker.delete_sfc_classifier( - tacker_client, sfc_clf_id=sfc_clf_id) - if deleted is not None: - logger.debug(" > Done!") - else: - logger.error("There has been a problem removing the " - "SFC classifier '%s'(%s)..." - % (sfc_clf_name, sfc_clf_id)) - else: - logger.debug(" > this is a default SFC classifier and will " - "NOT be deleted.") - - def main(): + logging.basicConfig() logger.info("Cleaning OpenStack resources...") nova_client = os_utils.get_nova_client() neutron_client = os_utils.get_neutron_client() keystone_client = os_utils.get_keystone_client() cinder_client = os_utils.get_cinder_client() - tacker_client = os_tacker.get_tacker_client() + glance_client = os_utils.get_glance_client() try: with open(OS_SNAPSHOT_FILE) as f: @@ -490,7 +396,7 @@ def main(): except Exception: logger.info("The file %s does not exist. The OpenStack snapshot must" " be created first. Aborting cleanup." % OS_SNAPSHOT_FILE) - exit(0) + return 0 default_images = snapshot_yaml.get('images') default_instances = snapshot_yaml.get('instances') @@ -501,23 +407,19 @@ def main(): default_floatingips = snapshot_yaml.get('floatingips') default_users = snapshot_yaml.get('users') default_tenants = snapshot_yaml.get('tenants') - default_vnfds = snapshot_yaml.get('vnfds') - default_vnfs = snapshot_yaml.get('vnfs') - default_sfcs = snapshot_yaml.get('sfcs') - default_sfc_classifiers = snapshot_yaml.get('sfc_classifiers') if not os_utils.check_credentials(): logger.error("Please source the openrc credentials and run " "the script again.") - exit(-1) + return -1 remove_instances(nova_client, default_instances) separator() - remove_images(nova_client, default_images) + remove_images(glance_client, default_images) separator() remove_volumes(cinder_client, default_volumes) separator() - remove_floatingips(nova_client, default_floatingips) + remove_floatingips(neutron_client, default_floatingips) separator() remove_networks(neutron_client, default_networks, default_routers) separator() @@ -527,17 +429,4 @@ def main(): separator() remove_tenants(keystone_client, default_tenants) separator() - # Note: Delete in this order - # 1. Classifiers, 2. SFCs, 3. VNFs, 4. VNFDs - remove_tacker_sfc_classifiers(tacker_client, default_sfc_classifiers) - separator() - remove_tacker_sfcs(tacker_client, default_sfcs) - separator() - remove_tacker_vnfs(tacker_client, default_vnfs) - separator() - remove_tacker_vnfds(tacker_client, default_vnfds) - separator() - - -if __name__ == '__main__': - main() + return 0