Search in sources :

Example 56 with Snapshot

use of org.ovirt.engine.core.common.businessentities.Snapshot in project ovirt-engine by oVirt.

the class RestoreAllSnapshotsCommand method restoreSnapshotAndRemoveObsoleteSnapshots.

/**
 * Restore the snapshot - if it is not the active snapshot, then the VM configuration will be restored.<br>
 * Additionally, remove all obsolete snapshots (The one after stateless, or the preview chain which was not chosen).
 */
protected void restoreSnapshotAndRemoveObsoleteSnapshots(Snapshot targetSnapshot) {
    Guid activeSnapshotId = snapshotDao.getId(getVmId(), SnapshotType.ACTIVE);
    List<DiskImage> imagesFromActiveSnapshot = diskImageDao.getAllSnapshotsForVmSnapshot(activeSnapshotId);
    Snapshot previewedSnapshot = snapshotDao.get(getVmId(), SnapshotType.PREVIEW);
    if (previewedSnapshot != null) {
        VM vmFromConf = snapshotVmConfigurationHelper.getVmFromConfiguration(previewedSnapshot.getVmConfiguration(), previewedSnapshot.getVmId(), previewedSnapshot.getId());
        List<DiskImage> previewedImagesFromDB = diskImageDao.getAllSnapshotsForVmSnapshot(previewedSnapshot.getId());
        imagesFromPreviewSnapshot.addAll(ImagesHandler.imagesIntersection(vmFromConf.getImages(), previewedImagesFromDB));
    }
    List<DiskImage> intersection = ImagesHandler.imagesIntersection(imagesFromActiveSnapshot, imagesFromPreviewSnapshot);
    switch(targetSnapshot.getType()) {
        case PREVIEW:
            snapshotDao.updateStatus(snapshotDao.getId(getVmId(), SnapshotType.REGULAR, SnapshotStatus.IN_PREVIEW), SnapshotStatus.OK);
            getParameters().setImages((List<DiskImage>) CollectionUtils.union(imagesFromPreviewSnapshot, intersection));
            imagesFromPreviewSnapshot.forEach(image -> {
                if (image.getDiskStorageType() != DiskStorageType.CINDER) {
                    imagesToRestore.add(image);
                } else {
                    List<DiskImage> cinderDiskFromPreviewSnapshot = intersection.stream().filter(diskImage -> diskImage.getId().equals(image.getId())).collect(Collectors.toList());
                    if (!cinderDiskFromPreviewSnapshot.isEmpty()) {
                        imagesToRestore.add(cinderDiskFromPreviewSnapshot.get(0));
                    }
                }
            });
            updateSnapshotIdForSkipRestoreImages(ImagesHandler.imagesSubtract(imagesFromActiveSnapshot, imagesToRestore), targetSnapshot.getId());
            restoreConfiguration(targetSnapshot);
            break;
        case STATELESS:
            imagesToRestore = getParameters().getImages();
            restoreConfiguration(targetSnapshot);
            break;
        case REGULAR:
            prepareToDeletePreviewBranch(imagesFromActiveSnapshot);
            // Set the active snapshot's images as target images for restore, because they are what we keep.
            getParameters().setImages(imagesFromActiveSnapshot);
            imagesFromActiveSnapshot.forEach(image -> {
                List<DiskImage> cinderDiskFromPreviewSnapshot = imagesFromPreviewSnapshot.stream().filter(diskImage -> diskImage.getId().equals(image.getId())).collect(Collectors.toList());
                if (!cinderDiskFromPreviewSnapshot.isEmpty()) {
                    if (image.getDiskStorageType() == DiskStorageType.IMAGE) {
                        imagesToRestore.add(image);
                    } else if (image.getDiskStorageType() == DiskStorageType.CINDER) {
                        CinderDisk cinderVolume = getInitialCinderVolumeToDelete(image);
                        if (cinderVolume != null) {
                            imagesToRestore.add(cinderVolume);
                        }
                    }
                }
            });
            updateSnapshotIdForSkipRestoreImages(ImagesHandler.imagesSubtract(imagesFromActiveSnapshot, imagesToRestore), activeSnapshotId);
            break;
        default:
            throw new EngineException(EngineError.ENGINE, "No support for restoring to snapshot type: " + targetSnapshot.getType());
    }
}
Also used : LockMessagesMatchUtil(org.ovirt.engine.core.bll.LockMessagesMatchUtil) CinderDisk(org.ovirt.engine.core.common.businessentities.storage.CinderDisk) EngineException(org.ovirt.engine.core.common.errors.EngineException) SnapshotDao(org.ovirt.engine.core.dao.SnapshotDao) VmDynamicDao(org.ovirt.engine.core.dao.VmDynamicDao) ActionReturnValue(org.ovirt.engine.core.common.action.ActionReturnValue) RestoreAllSnapshotsParameters(org.ovirt.engine.core.common.action.RestoreAllSnapshotsParameters) VmStaticDao(org.ovirt.engine.core.dao.VmStaticDao) CommandContext(org.ovirt.engine.core.bll.context.CommandContext) ActionType(org.ovirt.engine.core.common.action.ActionType) Future(java.util.concurrent.Future) DisksFilter(org.ovirt.engine.core.bll.storage.disk.image.DisksFilter) Map(java.util.Map) Snapshot(org.ovirt.engine.core.common.businessentities.Snapshot) Instance(javax.enterprise.inject.Instance) DiskImagesValidator(org.ovirt.engine.core.bll.validator.storage.DiskImagesValidator) DiskStorageType(org.ovirt.engine.core.common.businessentities.storage.DiskStorageType) EngineMessage(org.ovirt.engine.core.common.errors.EngineMessage) Set(java.util.Set) Collectors(java.util.stream.Collectors) ConcurrentChildCommandsExecutionCallback(org.ovirt.engine.core.bll.ConcurrentChildCommandsExecutionCallback) SnapshotType(org.ovirt.engine.core.common.businessentities.Snapshot.SnapshotType) Objects(java.util.Objects) ONLY_NOT_SHAREABLE(org.ovirt.engine.core.bll.storage.disk.image.DisksFilter.ONLY_NOT_SHAREABLE) List(java.util.List) VmValidator(org.ovirt.engine.core.bll.validator.VmValidator) Optional(java.util.Optional) MultipleStorageDomainsValidator(org.ovirt.engine.core.bll.validator.storage.MultipleStorageDomainsValidator) AuditLogType(org.ovirt.engine.core.common.AuditLogType) VdcObjectType(org.ovirt.engine.core.common.VdcObjectType) QuotaConsumptionParameter(org.ovirt.engine.core.bll.quota.QuotaConsumptionParameter) VmLeaseVDSParameters(org.ovirt.engine.core.common.vdscommands.VmLeaseVDSParameters) QuotaStorageDependent(org.ovirt.engine.core.bll.quota.QuotaStorageDependent) EntityInfo(org.ovirt.engine.core.common.asynctasks.EntityInfo) LockProperties(org.ovirt.engine.core.common.action.LockProperties) Guid(org.ovirt.engine.core.compat.Guid) RestoreAllCinderSnapshotsParameters(org.ovirt.engine.core.common.action.RestoreAllCinderSnapshotsParameters) SnapshotActionEnum(org.ovirt.engine.core.common.businessentities.SnapshotActionEnum) DiskImage(org.ovirt.engine.core.common.businessentities.storage.DiskImage) OvfUtils(org.ovirt.engine.core.utils.OvfUtils) ArrayList(java.util.ArrayList) CommandCoordinatorUtil(org.ovirt.engine.core.bll.tasks.CommandCoordinatorUtil) ImageStatus(org.ovirt.engine.core.common.businessentities.storage.ImageStatus) HashSet(java.util.HashSet) Inject(javax.inject.Inject) CollectionUtils(org.apache.commons.collections.CollectionUtils) DiskImageDao(org.ovirt.engine.core.dao.DiskImageDao) ONLY_ACTIVE(org.ovirt.engine.core.bll.storage.disk.image.DisksFilter.ONLY_ACTIVE) VmCommand(org.ovirt.engine.core.bll.VmCommand) Pair(org.ovirt.engine.core.common.utils.Pair) RestoreFromSnapshotParameters(org.ovirt.engine.core.common.action.RestoreFromSnapshotParameters) ImagesHandler(org.ovirt.engine.core.bll.storage.disk.image.ImagesHandler) StoragePoolValidator(org.ovirt.engine.core.bll.validator.storage.StoragePoolValidator) SnapshotStatus(org.ovirt.engine.core.common.businessentities.Snapshot.SnapshotStatus) LockingGroup(org.ovirt.engine.core.common.locks.LockingGroup) EndProcedure(org.ovirt.engine.core.common.action.ActionParametersBase.EndProcedure) ImagesContainterParametersBase(org.ovirt.engine.core.common.action.ImagesContainterParametersBase) VDSReturnValue(org.ovirt.engine.core.common.vdscommands.VDSReturnValue) Scope(org.ovirt.engine.core.common.action.LockProperties.Scope) Typed(javax.enterprise.inject.Typed) QuotaStorageConsumptionParameter(org.ovirt.engine.core.bll.quota.QuotaStorageConsumptionParameter) EngineError(org.ovirt.engine.core.common.errors.EngineError) ExecutionException(java.util.concurrent.ExecutionException) VM(org.ovirt.engine.core.common.businessentities.VM) CommandCallback(org.ovirt.engine.core.bll.tasks.interfaces.CommandCallback) PermissionSubject(org.ovirt.engine.core.bll.utils.PermissionSubject) RemoveImageParameters(org.ovirt.engine.core.common.action.RemoveImageParameters) VDSCommandType(org.ovirt.engine.core.common.vdscommands.VDSCommandType) Collections(java.util.Collections) VmInterfaceManager(org.ovirt.engine.core.bll.network.VmInterfaceManager) ImageDao(org.ovirt.engine.core.dao.ImageDao) Snapshot(org.ovirt.engine.core.common.businessentities.Snapshot) VM(org.ovirt.engine.core.common.businessentities.VM) EngineException(org.ovirt.engine.core.common.errors.EngineException) Guid(org.ovirt.engine.core.compat.Guid) CinderDisk(org.ovirt.engine.core.common.businessentities.storage.CinderDisk) DiskImage(org.ovirt.engine.core.common.businessentities.storage.DiskImage)

Example 57 with Snapshot

use of org.ovirt.engine.core.common.businessentities.Snapshot in project ovirt-engine by oVirt.

the class RemoveSnapshotCommand method initializeObjectState.

private void initializeObjectState() {
    if (StringUtils.isEmpty(getSnapshotName())) {
        Snapshot snapshot = snapshotDao.get(getParameters().getSnapshotId());
        if (snapshot != null) {
            setSnapshotName(snapshot.getDescription());
            getParameters().setUseCinderCommandCallback(!DisksFilter.filterCinderDisks(getSourceImages()).isEmpty());
        }
    }
    setStoragePoolId(getVm().getStoragePoolId());
}
Also used : Snapshot(org.ovirt.engine.core.common.businessentities.Snapshot)

Example 58 with Snapshot

use of org.ovirt.engine.core.common.businessentities.Snapshot in project ovirt-engine by oVirt.

the class RemoveSnapshotCommand method endVmCommand.

@Override
protected void endVmCommand() {
    initializeObjectState();
    if (getParameters().getTaskGroupSuccess()) {
        snapshotDao.remove(getParameters().getSnapshotId());
    } else {
        List<String> failedToRemoveDisks = new ArrayList<>();
        Snapshot snapshot = snapshotDao.get(getParameters().getSnapshotId());
        for (ActionParametersBase parameters : getParameters().getImagesParameters()) {
            ImagesContainterParametersBase imagesParams = parameters instanceof ImagesContainterParametersBase ? (ImagesContainterParametersBase) parameters : null;
            if (imagesParams == null) {
                // instances of ImagesContainterParametersBase objects.
                continue;
            }
            if (imagesParams.getTaskGroupSuccess()) {
                snapshot = imagesHandler.prepareSnapshotConfigWithoutImageSingleImage(snapshot, imagesParams.getImageId(), ovfManager);
            } else {
                log.error("Could not delete image '{}' from snapshot '{}'", imagesParams.getImageId(), getParameters().getSnapshotId());
                DiskImage diskImage = diskImageDao.getSnapshotById(imagesParams.getImageId());
                failedToRemoveDisks.add(diskImage.getDiskAlias());
            }
        }
        // Remove memory volume and update the dao.
        // Note: on failure, we can treat memory volume deletion as deleting an image
        // and remove it from the snapshot entity (rollback isn't applicable).
        snapshot.setMemoryDiskId(null);
        snapshot.setMetadataDiskId(null);
        snapshotDao.update(snapshot);
        if (!failedToRemoveDisks.isEmpty()) {
            addCustomValue("DiskAliases", StringUtils.join(failedToRemoveDisks, ", "));
            auditLogDirector.log(this, AuditLogType.USER_REMOVE_SNAPSHOT_FINISHED_FAILURE_PARTIAL_SNAPSHOT);
        }
        snapshotDao.updateStatus(getParameters().getSnapshotId(), SnapshotStatus.OK);
    }
    super.endVmCommand();
}
Also used : Snapshot(org.ovirt.engine.core.common.businessentities.Snapshot) ImagesContainterParametersBase(org.ovirt.engine.core.common.action.ImagesContainterParametersBase) ArrayList(java.util.ArrayList) DiskImage(org.ovirt.engine.core.common.businessentities.storage.DiskImage) ActionParametersBase(org.ovirt.engine.core.common.action.ActionParametersBase)

Example 59 with Snapshot

use of org.ovirt.engine.core.common.businessentities.Snapshot in project ovirt-engine by oVirt.

the class SnapshotVmConfigurationHelper method getVmFromConfiguration.

/**
 * Creates a VM by a specified OVF string.
 * If configuration is not specified, creates a VM according to the snapshotId
 * (required for backwards compatibility - @see getVmWithoutConfiguration method).
 *
 * @param configuration The OVF String
 * @param vmId The VM ID
 * @param snapshotId The snapshot ID
 * @return a VM object based on the specified parameters.
 */
public VM getVmFromConfiguration(String configuration, Guid vmId, Guid snapshotId) {
    VM vm;
    if (configuration != null) {
        vm = getVmWithConfiguration(configuration, vmId);
        Snapshot snapshot = snapshotDao.get(snapshotId);
        if (snapshot != null && snapshot.getType() != Snapshot.SnapshotType.PREVIEW) {
            // No need to mark disks of 'PREVIEW' snapshot as illegal
            // as it represents previous 'Active VM' state and no operations
            // on disks can be done while previewing a snapshot.
            markImagesIllegalIfNotInDb(vm, snapshotId);
        }
    } else {
        vm = getVmWithoutConfiguration(vmId, snapshotId);
    }
    vmHandler.updateDisksForVm(vm, vm.getImages());
    return vm;
}
Also used : Snapshot(org.ovirt.engine.core.common.businessentities.Snapshot) VM(org.ovirt.engine.core.common.businessentities.VM)

Example 60 with Snapshot

use of org.ovirt.engine.core.common.businessentities.Snapshot in project ovirt-engine by oVirt.

the class AddVmFromSnapshotCommandTest method canAddCloneVmFromSnapshotNoConfiguration.

@Test
public void canAddCloneVmFromSnapshotNoConfiguration() {
    cmd.getVm().setName("vm1");
    doReturn(null).when(cmd).getVmFromConfiguration();
    doReturn(ValidationResult.VALID).when(snapshotsValidator).vmNotDuringSnapshot(any());
    when(snapshotDao.get(SOURCE_SNAPSHOT_ID)).thenReturn(new Snapshot());
    ValidateTestUtils.runAndAssertValidateFailure(cmd, EngineMessage.ACTION_TYPE_FAILED_VM_SNAPSHOT_HAS_NO_CONFIGURATION);
}
Also used : Snapshot(org.ovirt.engine.core.common.businessentities.Snapshot) Test(org.junit.Test)

Aggregations

Snapshot (org.ovirt.engine.core.common.businessentities.Snapshot)96 DiskImage (org.ovirt.engine.core.common.businessentities.storage.DiskImage)33 VM (org.ovirt.engine.core.common.businessentities.VM)32 Guid (org.ovirt.engine.core.compat.Guid)30 Test (org.junit.Test)21 ArrayList (java.util.ArrayList)15 ActionReturnValue (org.ovirt.engine.core.common.action.ActionReturnValue)12 List (java.util.List)11 SnapshotStatus (org.ovirt.engine.core.common.businessentities.Snapshot.SnapshotStatus)9 Collections (java.util.Collections)8 Date (java.util.Date)8 Set (java.util.Set)8 Collectors (java.util.stream.Collectors)8 SnapshotType (org.ovirt.engine.core.common.businessentities.Snapshot.SnapshotType)8 EngineException (org.ovirt.engine.core.common.errors.EngineException)8 ActionType (org.ovirt.engine.core.common.action.ActionType)7 DiskStorageType (org.ovirt.engine.core.common.businessentities.storage.DiskStorageType)6 EngineMessage (org.ovirt.engine.core.common.errors.EngineMessage)6 HashMap (java.util.HashMap)5 HashSet (java.util.HashSet)5