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());
}
}
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());
}
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();
}
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;
}
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);
}
Aggregations