def cleanup(self, context, instance, network_info, block_device_info=None, destroy_disks=True, migrate_data=None, destroy_vifs=True):
    self._undefine_domain(instance)
    if destroy_vifs:
        self._unplug_vifs(instance, network_info, True)
    retry = True
    while retry:
        try:
            self.firewall_driver.unfilter_instance(instance, network_info=network_info)
        except libvirt.libvirtError as e:
            try:
                state = self.get_info(instance)['state']
            except exception.InstanceNotFound:
                state = power_state.SHUTDOWN
            if (state != power_state.SHUTDOWN):
                LOG.warn(_LW('Instance may be still running, destroy it again.'), instance=instance)
                self._destroy(instance)
            else:
                retry = False
                errcode = e.get_error_code()
                LOG.exception(_LE('Error from libvirt during unfilter. Code=%(errcode)s Error=%(e)s'), {'errcode': errcode, 'e': e}, instance=instance)
                reason = 'Error unfiltering instance.'
                raise exception.InstanceTerminationFailure(reason=reason)
        except Exception:
            retry = False
            raise
        else:
            retry = False
    block_device_mapping = driver.block_device_info_get_mapping(block_device_info)
    for vol in block_device_mapping:
        connection_info = vol['connection_info']
        disk_dev = vol['mount_device'].rpartition('/')[2]
        if (('data' in connection_info) and ('volume_id' in connection_info['data'])):
            volume_id = connection_info['data']['volume_id']
            encryption = encryptors.get_encryption_metadata(context, self._volume_api, volume_id, connection_info)
            if encryption:
                encryptor = self._get_volume_encryptor(connection_info, encryption)
                encryptor.detach_volume(**encryption)
        try:
            self._disconnect_volume(connection_info, disk_dev)
        except Exception as exc:
            with excutils.save_and_reraise_exception() as ctxt:
                if destroy_disks:
                    ctxt.reraise = False
                    LOG.warn(_LW('Ignoring Volume Error on vol %(vol_id)s during delete %(exc)s'), {'vol_id': vol.get('volume_id'), 'exc': exc}, instance=instance)
    if (destroy_disks or (migrate_data and migrate_data.get('is_shared_block_storage', False))):
        self._delete_instance_files(instance)
    if destroy_disks:
        self._cleanup_lvm(instance)
        if (CONF.libvirt.images_type == 'rbd'):
            self._cleanup_rbd(instance)
def cleanup(self, context, instance, network_info, block_device_info=None, destroy_disks=True, migrate_data=None, destroy_vifs=True):
    self._undefine_domain(instance)
    if destroy_vifs:
        self._unplug_vifs(instance, network_info, True)
    retry = True
    while retry:
        try:
            self.firewall_driver.unfilter_instance(instance, network_info=network_info)
        except libvirt.libvirtError as e:
            try:
                state = self.get_info(instance)['state']
            except exception.InstanceNotFound:
                state = power_state.SHUTDOWN
            if (state != power_state.SHUTDOWN):
                LOG.warn(_LW('Instance may be still running, destroy it again.'), instance=instance)
                self._destroy(instance)
            else:
                retry = False
                errcode = e.get_error_code()
                LOG.exception(_LE('Error from libvirt during unfilter. Code=%(errcode)s Error=%(e)s'), {'errcode': errcode, 'e': e}, instance=instance)
                reason = 'Error unfiltering instance.'
                raise exception.InstanceTerminationFailure(reason=reason)
        except Exception:
            retry = False
            raise
        else:
            retry = False
    block_device_mapping = driver.block_device_info_get_mapping(block_device_info)
    for vol in block_device_mapping:
        connection_info = vol['connection_info']
        disk_dev = vol['mount_device'].rpartition('/')[2]
        if (('data' in connection_info) and ('volume_id' in connection_info['data'])):
            volume_id = connection_info['data']['volume_id']
            encryption = encryptors.get_encryption_metadata(context, self._volume_api, volume_id, connection_info)
            if encryption:
                encryptor = self._get_volume_encryptor(connection_info, encryption)
                encryptor.detach_volume(**encryption)
        try:
            self._disconnect_volume(connection_info, disk_dev)
        except Exception as exc:
            with excutils.save_and_reraise_exception() as ctxt:
                if destroy_disks:
                    ctxt.reraise = False
                    LOG.warn(_LW('Ignoring Volume Error on vol %(vol_id)s during delete %(exc)s'), {'vol_id': vol.get('volume_id'), 'exc': exc}, instance=instance)
    if (destroy_disks or (migrate_data and migrate_data.get('is_shared_block_storage', False))):
        self._delete_instance_files(instance)
    if destroy_disks:
        self._cleanup_lvm(instance)
        if (CONF.libvirt.images_type == 'rbd'):
            self._cleanup_rbd(instance)