Search in sources :

Example 1 with VMSnapshotDetailsVO

use of com.cloud.vm.snapshot.VMSnapshotDetailsVO in project cloudstack by apache.

the class ScaleIOVMSnapshotStrategy method takeVMSnapshot.

@Override
public VMSnapshot takeVMSnapshot(VMSnapshot vmSnapshot) {
    UserVm userVm = userVmDao.findById(vmSnapshot.getVmId());
    VMSnapshotVO vmSnapshotVO = (VMSnapshotVO) vmSnapshot;
    try {
        vmSnapshotHelper.vmSnapshotStateTransitTo(vmSnapshotVO, VMSnapshot.Event.CreateRequested);
    } catch (NoTransitionException e) {
        throw new CloudRuntimeException(e.getMessage());
    }
    boolean result = false;
    try {
        Map<String, String> srcVolumeDestSnapshotMap = new HashMap<>();
        List<VolumeObjectTO> volumeTOs = vmSnapshotHelper.getVolumeTOList(userVm.getId());
        final Long storagePoolId = vmSnapshotHelper.getStoragePoolForVM(userVm.getId());
        StoragePoolVO storagePool = storagePoolDao.findById(storagePoolId);
        long prev_chain_size = 0;
        long virtual_size = 0;
        for (VolumeObjectTO volume : volumeTOs) {
            String volumeSnapshotName = String.format("%s-%s-%s-%s-%s", ScaleIOUtil.VMSNAPSHOT_PREFIX, vmSnapshotVO.getId(), volume.getId(), storagePool.getUuid().split("-")[0].substring(4), ManagementServerImpl.customCsIdentifier.value());
            srcVolumeDestSnapshotMap.put(ScaleIOUtil.getVolumePath(volume.getPath()), volumeSnapshotName);
            virtual_size += volume.getSize();
            VolumeVO volumeVO = volumeDao.findById(volume.getId());
            prev_chain_size += volumeVO.getVmSnapshotChainSize() == null ? 0 : volumeVO.getVmSnapshotChainSize();
        }
        VMSnapshotTO current = null;
        VMSnapshotVO currentSnapshot = vmSnapshotDao.findCurrentSnapshotByVmId(userVm.getId());
        if (currentSnapshot != null) {
            current = vmSnapshotHelper.getSnapshotWithParents(currentSnapshot);
        }
        if (current == null)
            vmSnapshotVO.setParent(null);
        else
            vmSnapshotVO.setParent(current.getId());
        try {
            final ScaleIOGatewayClient client = getScaleIOClient(storagePoolId);
            SnapshotGroup snapshotGroup = client.takeSnapshot(srcVolumeDestSnapshotMap);
            if (snapshotGroup == null) {
                throw new CloudRuntimeException("Failed to take VM snapshot on PowerFlex storage pool");
            }
            String snapshotGroupId = snapshotGroup.getSnapshotGroupId();
            List<String> volumeIds = snapshotGroup.getVolumeIds();
            if (volumeIds != null && !volumeIds.isEmpty()) {
                List<VMSnapshotDetailsVO> vmSnapshotDetails = new ArrayList<VMSnapshotDetailsVO>();
                vmSnapshotDetails.add(new VMSnapshotDetailsVO(vmSnapshot.getId(), "SnapshotGroupId", snapshotGroupId, false));
                for (int index = 0; index < volumeIds.size(); index++) {
                    String volumeSnapshotName = srcVolumeDestSnapshotMap.get(ScaleIOUtil.getVolumePath(volumeTOs.get(index).getPath()));
                    String pathWithScaleIOVolumeName = ScaleIOUtil.updatedPathWithVolumeName(volumeIds.get(index), volumeSnapshotName);
                    vmSnapshotDetails.add(new VMSnapshotDetailsVO(vmSnapshot.getId(), "Vol_" + volumeTOs.get(index).getId() + "_Snapshot", pathWithScaleIOVolumeName, false));
                }
                vmSnapshotDetailsDao.saveDetails(vmSnapshotDetails);
            }
            finalizeCreate(vmSnapshotVO, volumeTOs);
            result = true;
            LOGGER.debug("Create vm snapshot " + vmSnapshot.getName() + " succeeded for vm: " + userVm.getInstanceName());
            long new_chain_size = 0;
            for (VolumeObjectTO volumeTo : volumeTOs) {
                publishUsageEvent(EventTypes.EVENT_VM_SNAPSHOT_CREATE, vmSnapshot, userVm, volumeTo);
                new_chain_size += volumeTo.getSize();
            }
            publishUsageEvent(EventTypes.EVENT_VM_SNAPSHOT_ON_PRIMARY, vmSnapshot, userVm, new_chain_size - prev_chain_size, virtual_size);
            return vmSnapshot;
        } catch (Exception e) {
            String errMsg = "Unable to take vm snapshot due to: " + e.getMessage();
            LOGGER.warn(errMsg, e);
            throw new CloudRuntimeException(errMsg);
        }
    } finally {
        if (!result) {
            try {
                vmSnapshotHelper.vmSnapshotStateTransitTo(vmSnapshot, VMSnapshot.Event.OperationFailed);
                String subject = "Take snapshot failed for VM: " + userVm.getDisplayName();
                String message = "Snapshot operation failed for VM: " + userVm.getDisplayName() + ", Please check and delete if any stale volumes created with VM snapshot id: " + vmSnapshot.getVmId();
                alertManager.sendAlert(AlertManager.AlertType.ALERT_TYPE_VM_SNAPSHOT, userVm.getDataCenterId(), userVm.getPodIdToDeployIn(), subject, message);
            } catch (NoTransitionException e1) {
                LOGGER.error("Cannot set vm snapshot state due to: " + e1.getMessage());
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SnapshotGroup(org.apache.cloudstack.storage.datastore.api.SnapshotGroup) ConfigurationException(javax.naming.ConfigurationException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) UserVm(com.cloud.uservm.UserVm) VMSnapshotTO(com.cloud.agent.api.VMSnapshotTO) VolumeVO(com.cloud.storage.VolumeVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) ScaleIOGatewayClient(org.apache.cloudstack.storage.datastore.client.ScaleIOGatewayClient) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) VMSnapshotDetailsVO(com.cloud.vm.snapshot.VMSnapshotDetailsVO)

Example 2 with VMSnapshotDetailsVO

use of com.cloud.vm.snapshot.VMSnapshotDetailsVO in project cloudstack by apache.

the class VmSnapshotDaoTest method testVmSnapshotDetails.

@Test
public void testVmSnapshotDetails() {
    VMSnapshotDetailsVO detailsVO = new VMSnapshotDetailsVO(1L, "test", "foo", true);
    vmsnapshotDetailsDao.persist(detailsVO);
    Map<String, String> details = vmsnapshotDetailsDao.listDetailsKeyPairs(1L);
    Assert.assertTrue(details.containsKey("test"));
}
Also used : VMSnapshotDetailsVO(com.cloud.vm.snapshot.VMSnapshotDetailsVO) Test(org.junit.Test)

Example 3 with VMSnapshotDetailsVO

use of com.cloud.vm.snapshot.VMSnapshotDetailsVO in project cloudstack by apache.

the class ScaleIOVMSnapshotStrategy method deleteVMSnapshot.

@Override
public boolean deleteVMSnapshot(VMSnapshot vmSnapshot) {
    UserVmVO userVm = userVmDao.findById(vmSnapshot.getVmId());
    VMSnapshotVO vmSnapshotVO = (VMSnapshotVO) vmSnapshot;
    try {
        vmSnapshotHelper.vmSnapshotStateTransitTo(vmSnapshot, VMSnapshot.Event.ExpungeRequested);
    } catch (NoTransitionException e) {
        LOGGER.debug("Failed to change vm snapshot state with event ExpungeRequested");
        throw new CloudRuntimeException("Failed to change vm snapshot state with event ExpungeRequested: " + e.getMessage());
    }
    try {
        List<VolumeObjectTO> volumeTOs = vmSnapshotHelper.getVolumeTOList(vmSnapshot.getVmId());
        Long storagePoolId = vmSnapshotHelper.getStoragePoolForVM(userVm.getId());
        String systemId = storagePoolDetailsDao.findDetail(storagePoolId, ScaleIOGatewayClient.STORAGE_POOL_SYSTEM_ID).getValue();
        if (systemId == null) {
            throw new CloudRuntimeException("Failed to get the system id for PowerFlex storage pool for deleting VM snapshot: " + vmSnapshot.getName());
        }
        VMSnapshotDetailsVO vmSnapshotDetailsVO = vmSnapshotDetailsDao.findDetail(vmSnapshot.getId(), "SnapshotGroupId");
        if (vmSnapshotDetailsVO == null) {
            throw new CloudRuntimeException("Failed to get snapshot group id for the VM snapshot: " + vmSnapshot.getName());
        }
        String snapshotGroupId = vmSnapshotDetailsVO.getValue();
        final ScaleIOGatewayClient client = getScaleIOClient(storagePoolId);
        int volumesDeleted = client.deleteSnapshotGroup(systemId, snapshotGroupId);
        if (volumesDeleted <= 0) {
            throw new CloudRuntimeException("Failed to delete VM snapshot: " + vmSnapshot.getName());
        } else if (volumesDeleted != volumeTOs.size()) {
            LOGGER.warn("Unable to delete all volumes of the VM snapshot: " + vmSnapshot.getName());
        }
        finalizeDelete(vmSnapshotVO, volumeTOs);
        long full_chain_size = 0;
        for (VolumeObjectTO volumeTo : volumeTOs) {
            publishUsageEvent(EventTypes.EVENT_VM_SNAPSHOT_DELETE, vmSnapshot, userVm, volumeTo);
            full_chain_size += volumeTo.getSize();
        }
        publishUsageEvent(EventTypes.EVENT_VM_SNAPSHOT_OFF_PRIMARY, vmSnapshot, userVm, full_chain_size, 0L);
        return true;
    } catch (Exception e) {
        String errMsg = "Unable to delete vm snapshot: " + vmSnapshot.getName() + " of vm " + userVm.getInstanceName() + " due to " + e.getMessage();
        LOGGER.warn(errMsg, e);
        throw new CloudRuntimeException(errMsg);
    }
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) ConfigurationException(javax.naming.ConfigurationException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) ScaleIOGatewayClient(org.apache.cloudstack.storage.datastore.client.ScaleIOGatewayClient) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) VMSnapshotDetailsVO(com.cloud.vm.snapshot.VMSnapshotDetailsVO)

Example 4 with VMSnapshotDetailsVO

use of com.cloud.vm.snapshot.VMSnapshotDetailsVO in project cloudstack by apache.

the class ScaleIOVMSnapshotStrategy method revertVMSnapshot.

@Override
public boolean revertVMSnapshot(VMSnapshot vmSnapshot) {
    VMSnapshotVO vmSnapshotVO = (VMSnapshotVO) vmSnapshot;
    UserVmVO userVm = userVmDao.findById(vmSnapshot.getVmId());
    try {
        vmSnapshotHelper.vmSnapshotStateTransitTo(vmSnapshotVO, VMSnapshot.Event.RevertRequested);
    } catch (NoTransitionException e) {
        throw new CloudRuntimeException(e.getMessage());
    }
    boolean result = false;
    try {
        List<VolumeObjectTO> volumeTOs = vmSnapshotHelper.getVolumeTOList(userVm.getId());
        Long storagePoolId = vmSnapshotHelper.getStoragePoolForVM(userVm.getId());
        Map<String, String> srcSnapshotDestVolumeMap = new HashMap<>();
        for (VolumeObjectTO volume : volumeTOs) {
            VMSnapshotDetailsVO vmSnapshotDetail = vmSnapshotDetailsDao.findDetail(vmSnapshotVO.getId(), "Vol_" + volume.getId() + "_Snapshot");
            String srcSnapshotVolumeId = ScaleIOUtil.getVolumePath(vmSnapshotDetail.getValue());
            String destVolumeId = ScaleIOUtil.getVolumePath(volume.getPath());
            srcSnapshotDestVolumeMap.put(srcSnapshotVolumeId, destVolumeId);
        }
        String systemId = storagePoolDetailsDao.findDetail(storagePoolId, ScaleIOGatewayClient.STORAGE_POOL_SYSTEM_ID).getValue();
        if (systemId == null) {
            throw new CloudRuntimeException("Failed to get the system id for PowerFlex storage pool for reverting VM snapshot: " + vmSnapshot.getName());
        }
        final ScaleIOGatewayClient client = getScaleIOClient(storagePoolId);
        result = client.revertSnapshot(systemId, srcSnapshotDestVolumeMap);
        if (!result) {
            throw new CloudRuntimeException("Failed to revert VM snapshot on PowerFlex storage pool");
        }
        finalizeRevert(vmSnapshotVO, volumeTOs);
        result = true;
    } catch (Exception e) {
        String errMsg = "Revert VM: " + userVm.getInstanceName() + " to snapshot: " + vmSnapshotVO.getName() + " failed due to " + e.getMessage();
        LOGGER.error(errMsg, e);
        throw new CloudRuntimeException(errMsg);
    } finally {
        if (!result) {
            try {
                vmSnapshotHelper.vmSnapshotStateTransitTo(vmSnapshot, VMSnapshot.Event.OperationFailed);
            } catch (NoTransitionException e1) {
                LOGGER.error("Cannot set vm snapshot state due to: " + e1.getMessage());
            }
        }
    }
    return result;
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) HashMap(java.util.HashMap) ConfigurationException(javax.naming.ConfigurationException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) ScaleIOGatewayClient(org.apache.cloudstack.storage.datastore.client.ScaleIOGatewayClient) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) VMSnapshotDetailsVO(com.cloud.vm.snapshot.VMSnapshotDetailsVO)

Aggregations

VMSnapshotDetailsVO (com.cloud.vm.snapshot.VMSnapshotDetailsVO)4 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)3 NoTransitionException (com.cloud.utils.fsm.NoTransitionException)3 VMSnapshotVO (com.cloud.vm.snapshot.VMSnapshotVO)3 ConfigurationException (javax.naming.ConfigurationException)3 ScaleIOGatewayClient (org.apache.cloudstack.storage.datastore.client.ScaleIOGatewayClient)3 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)3 UserVmVO (com.cloud.vm.UserVmVO)2 HashMap (java.util.HashMap)2 VMSnapshotTO (com.cloud.agent.api.VMSnapshotTO)1 VolumeVO (com.cloud.storage.VolumeVO)1 UserVm (com.cloud.uservm.UserVm)1 ArrayList (java.util.ArrayList)1 SnapshotGroup (org.apache.cloudstack.storage.datastore.api.SnapshotGroup)1 StoragePoolVO (org.apache.cloudstack.storage.datastore.db.StoragePoolVO)1 Test (org.junit.Test)1