Search in sources :

Example 16 with VMSnapshotTO

use of com.cloud.agent.api.VMSnapshotTO in project cosmic by MissionCriticalCloud.

the class DefaultVMSnapshotStrategy method takeVMSnapshot.

@Override
public VMSnapshot takeVMSnapshot(final VMSnapshot vmSnapshot) {
    final Long hostId = vmSnapshotHelper.pickRunningHost(vmSnapshot.getVmId());
    final UserVm userVm = userVmDao.findById(vmSnapshot.getVmId());
    final VMSnapshotVO vmSnapshotVO = (VMSnapshotVO) vmSnapshot;
    try {
        vmSnapshotHelper.vmSnapshotStateTransitTo(vmSnapshotVO, VMSnapshot.Event.CreateRequested);
    } catch (final NoTransitionException e) {
        throw new CloudRuntimeException(e.getMessage());
    }
    CreateVMSnapshotAnswer answer = null;
    boolean result = false;
    try {
        final GuestOSVO guestOS = guestOSDao.findById(userVm.getGuestOSId());
        final List<VolumeObjectTO> volumeTOs = vmSnapshotHelper.getVolumeTOList(userVm.getId());
        VMSnapshotTO current = null;
        final VMSnapshotVO currentSnapshot = vmSnapshotDao.findCurrentSnapshotByVmId(userVm.getId());
        if (currentSnapshot != null) {
            current = vmSnapshotHelper.getSnapshotWithParents(currentSnapshot);
        }
        final VMSnapshotOptions options = ((VMSnapshotVO) vmSnapshot).getOptions();
        boolean quiescevm = true;
        if (options != null) {
            quiescevm = options.needQuiesceVM();
        }
        final VMSnapshotTO target = new VMSnapshotTO(vmSnapshot.getId(), vmSnapshot.getName(), vmSnapshot.getType(), null, vmSnapshot.getDescription(), false, current, quiescevm);
        if (current == null) {
            vmSnapshotVO.setParent(null);
        } else {
            vmSnapshotVO.setParent(current.getId());
        }
        final HostVO host = hostDao.findById(hostId);
        final GuestOSHypervisorVO guestOsMapping = guestOsHypervisorDao.findByOsIdAndHypervisor(guestOS.getId(), host.getHypervisorType().toString(), host.getHypervisorVersion());
        final CreateVMSnapshotCommand ccmd = new CreateVMSnapshotCommand(userVm.getInstanceName(), userVm.getUuid(), target, volumeTOs, guestOS.getDisplayName());
        if (guestOsMapping == null) {
            ccmd.setPlatformEmulator(null);
        } else {
            ccmd.setPlatformEmulator(guestOsMapping.getGuestOsName());
        }
        ccmd.setWait(_wait);
        answer = (CreateVMSnapshotAnswer) agentMgr.send(hostId, ccmd);
        if (answer != null && answer.getResult()) {
            processAnswer(vmSnapshotVO, userVm, answer, hostId);
            s_logger.debug("Create vm snapshot " + vmSnapshot.getName() + " succeeded for vm: " + userVm.getInstanceName());
            result = true;
            return vmSnapshot;
        } else {
            String errMsg = "Creating VM snapshot: " + vmSnapshot.getName() + " failed";
            if (answer != null && answer.getDetails() != null) {
                errMsg = errMsg + " due to " + answer.getDetails();
            }
            s_logger.error(errMsg);
            throw new CloudRuntimeException(errMsg);
        }
    } catch (final OperationTimedoutException e) {
        s_logger.debug("Creating VM snapshot: " + vmSnapshot.getName() + " failed: " + e.toString());
        throw new CloudRuntimeException("Creating VM snapshot: " + vmSnapshot.getName() + " failed: " + e.toString());
    } catch (final AgentUnavailableException e) {
        s_logger.debug("Creating VM snapshot: " + vmSnapshot.getName() + " failed", e);
        throw new CloudRuntimeException("Creating VM snapshot: " + vmSnapshot.getName() + " failed: " + e.toString());
    } finally {
        if (!result) {
            try {
                vmSnapshotHelper.vmSnapshotStateTransitTo(vmSnapshot, VMSnapshot.Event.OperationFailed);
            } catch (final NoTransitionException e1) {
                s_logger.error("Cannot set vm snapshot state due to: " + e1.getMessage());
            }
        }
    }
}
Also used : OperationTimedoutException(com.cloud.exception.OperationTimedoutException) VMSnapshotOptions(com.cloud.engine.subsystem.api.storage.VMSnapshotOptions) GuestOSVO(com.cloud.storage.GuestOSVO) CreateVMSnapshotCommand(com.cloud.agent.api.CreateVMSnapshotCommand) HostVO(com.cloud.host.HostVO) GuestOSHypervisorVO(com.cloud.storage.GuestOSHypervisorVO) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) UserVm(com.cloud.uservm.UserVm) CreateVMSnapshotAnswer(com.cloud.agent.api.CreateVMSnapshotAnswer) VMSnapshotTO(com.cloud.agent.api.VMSnapshotTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO)

Example 17 with VMSnapshotTO

use of com.cloud.agent.api.VMSnapshotTO in project cosmic by MissionCriticalCloud.

the class VMSnapshotStrategyTest method testRevertSnapshot.

@Test
public void testRevertSnapshot() throws AgentUnavailableException, OperationTimedoutException {
    final Long hostId = 1L;
    final Long vmId = 1L;
    final Long guestOsId = 1L;
    final HypervisorType hypervisorType = HypervisorType.Any;
    final String hypervisorVersion = "default";
    final String guestOsName = "Other";
    final List<VolumeObjectTO> volumeObjectTOs = new ArrayList<>();
    final VMSnapshotVO vmSnapshot = Mockito.mock(VMSnapshotVO.class);
    final UserVmVO userVmVO = Mockito.mock(UserVmVO.class);
    Mockito.when(userVmVO.getGuestOSId()).thenReturn(guestOsId);
    Mockito.when(vmSnapshot.getVmId()).thenReturn(vmId);
    Mockito.when(vmSnapshotHelper.pickRunningHost(Matchers.anyLong())).thenReturn(hostId);
    Mockito.when(vmSnapshotHelper.getVolumeTOList(Matchers.anyLong())).thenReturn(volumeObjectTOs);
    Mockito.when(userVmDao.findById(Matchers.anyLong())).thenReturn(userVmVO);
    final GuestOSVO guestOSVO = Mockito.mock(GuestOSVO.class);
    Mockito.when(guestOSDao.findById(Matchers.anyLong())).thenReturn(guestOSVO);
    final GuestOSHypervisorVO guestOSHypervisorVO = Mockito.mock(GuestOSHypervisorVO.class);
    Mockito.when(guestOSHypervisorVO.getGuestOsName()).thenReturn(guestOsName);
    Mockito.when(guestOsHypervisorDao.findById(Matchers.anyLong())).thenReturn(guestOSHypervisorVO);
    Mockito.when(guestOsHypervisorDao.findByOsIdAndHypervisor(Matchers.anyLong(), Matchers.anyString(), Matchers.anyString())).thenReturn(guestOSHypervisorVO);
    final VMSnapshotTO vmSnapshotTO = Mockito.mock(VMSnapshotTO.class);
    Mockito.when(vmSnapshotHelper.getSnapshotWithParents(Matchers.any(VMSnapshotVO.class))).thenReturn(vmSnapshotTO);
    Mockito.when(vmSnapshotDao.findById(Matchers.anyLong())).thenReturn(vmSnapshot);
    Mockito.when(vmSnapshot.getId()).thenReturn(1L);
    Mockito.when(vmSnapshot.getCreated()).thenReturn(new Date());
    Mockito.when(agentMgr.send(Matchers.anyLong(), Matchers.any(Command.class))).thenReturn(null);
    final HostVO hostVO = Mockito.mock(HostVO.class);
    Mockito.when(hostDao.findById(Matchers.anyLong())).thenReturn(hostVO);
    Mockito.when(hostVO.getHypervisorType()).thenReturn(hypervisorType);
    Mockito.when(hostVO.getHypervisorVersion()).thenReturn(hypervisorVersion);
    Exception e = null;
    try {
        vmSnapshotStrategy.revertVMSnapshot(vmSnapshot);
    } catch (final CloudRuntimeException e1) {
        e = e1;
    }
    assertNotNull(e);
    final RevertToVMSnapshotAnswer answer = Mockito.mock(RevertToVMSnapshotAnswer.class);
    Mockito.when(answer.getResult()).thenReturn(Boolean.TRUE);
    Mockito.when(agentMgr.send(Matchers.anyLong(), Matchers.any(Command.class))).thenReturn(answer);
    final boolean result = vmSnapshotStrategy.revertVMSnapshot(vmSnapshot);
    assertTrue(result);
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) RevertToVMSnapshotAnswer(com.cloud.agent.api.RevertToVMSnapshotAnswer) ArrayList(java.util.ArrayList) GuestOSVO(com.cloud.storage.GuestOSVO) Date(java.util.Date) HostVO(com.cloud.host.HostVO) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) GuestOSHypervisorVO(com.cloud.storage.GuestOSHypervisorVO) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) VMSnapshotTO(com.cloud.agent.api.VMSnapshotTO) Command(com.cloud.agent.api.Command) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO) Test(org.junit.Test)

Example 18 with VMSnapshotTO

use of com.cloud.agent.api.VMSnapshotTO in project cosmic by MissionCriticalCloud.

the class VMSnapshotManagerImpl method getSnapshotWithParents.

private VMSnapshotTO getSnapshotWithParents(final VMSnapshotVO snapshot) {
    final Map<Long, VMSnapshotVO> snapshotMap = new HashMap<>();
    final List<VMSnapshotVO> allSnapshots = _vmSnapshotDao.findByVm(snapshot.getVmId());
    for (final VMSnapshotVO vmSnapshotVO : allSnapshots) {
        snapshotMap.put(vmSnapshotVO.getId(), vmSnapshotVO);
    }
    VMSnapshotTO currentTO = convert2VMSnapshotTO(snapshot);
    final VMSnapshotTO result = currentTO;
    VMSnapshotVO current = snapshot;
    while (current.getParent() != null) {
        final VMSnapshotVO parent = snapshotMap.get(current.getParent());
        if (parent == null) {
            break;
        }
        currentTO.setParent(convert2VMSnapshotTO(parent));
        current = snapshotMap.get(current.getParent());
        currentTO = currentTO.getParent();
    }
    return result;
}
Also used : VMSnapshotTO(com.cloud.agent.api.VMSnapshotTO) HashMap(java.util.HashMap)

Example 19 with VMSnapshotTO

use of com.cloud.agent.api.VMSnapshotTO in project cosmic by MissionCriticalCloud.

the class VMSnapshotManagerImpl method createRestoreCommand.

@Override
public RestoreVMSnapshotCommand createRestoreCommand(final UserVmVO userVm, final List<VMSnapshotVO> vmSnapshotVOs) {
    if (!HypervisorType.KVM.equals(userVm.getHypervisorType())) {
        return null;
    }
    final List<VMSnapshotTO> snapshots = new ArrayList<>();
    final Map<Long, VMSnapshotTO> snapshotAndParents = new HashMap<>();
    for (final VMSnapshotVO vmSnapshotVO : vmSnapshotVOs) {
        if (vmSnapshotVO.getType() == VMSnapshot.Type.DiskAndMemory) {
            final VMSnapshotVO snapshot = _vmSnapshotDao.findById(vmSnapshotVO.getId());
            final VMSnapshotTO parent = getSnapshotWithParents(snapshot).getParent();
            final VMSnapshotOptions options = snapshot.getOptions();
            boolean quiescevm = true;
            if (options != null) {
                quiescevm = options.needQuiesceVM();
            }
            final VMSnapshotTO vmSnapshotTO = new VMSnapshotTO(snapshot.getId(), snapshot.getName(), snapshot.getType(), snapshot.getCreated().getTime(), snapshot.getDescription(), snapshot.getCurrent(), parent, quiescevm);
            snapshots.add(vmSnapshotTO);
            snapshotAndParents.put(vmSnapshotVO.getId(), parent);
        }
    }
    if (snapshotAndParents.isEmpty()) {
        return null;
    }
    // prepare RestoreVMSnapshotCommand
    final String vmInstanceName = userVm.getInstanceName();
    final List<VolumeObjectTO> volumeTOs = getVolumeTOList(userVm.getId());
    final GuestOSVO guestOS = _guestOSDao.findById(userVm.getGuestOSId());
    final RestoreVMSnapshotCommand restoreSnapshotCommand = new RestoreVMSnapshotCommand(vmInstanceName, null, volumeTOs, guestOS.getDisplayName());
    restoreSnapshotCommand.setSnapshots(snapshots);
    restoreSnapshotCommand.setSnapshotAndParents(snapshotAndParents);
    return restoreSnapshotCommand;
}
Also used : HashMap(java.util.HashMap) VMSnapshotOptions(com.cloud.engine.subsystem.api.storage.VMSnapshotOptions) ArrayList(java.util.ArrayList) GuestOSVO(com.cloud.storage.GuestOSVO) RestoreVMSnapshotCommand(com.cloud.agent.api.RestoreVMSnapshotCommand) VMSnapshotTO(com.cloud.agent.api.VMSnapshotTO) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO)

Example 20 with VMSnapshotTO

use of com.cloud.agent.api.VMSnapshotTO in project cosmic by MissionCriticalCloud.

the class VMSnapshotHelperImpl method getSnapshotWithParents.

@Override
public VMSnapshotTO getSnapshotWithParents(final VMSnapshotVO snapshot) {
    final Map<Long, VMSnapshotVO> snapshotMap = new HashMap<>();
    final List<VMSnapshotVO> allSnapshots = _vmSnapshotDao.findByVm(snapshot.getVmId());
    for (final VMSnapshotVO vmSnapshotVO : allSnapshots) {
        snapshotMap.put(vmSnapshotVO.getId(), vmSnapshotVO);
    }
    VMSnapshotTO currentTO = convert2VMSnapshotTO(snapshot);
    final VMSnapshotTO result = currentTO;
    VMSnapshotVO current = snapshot;
    while (current.getParent() != null) {
        final VMSnapshotVO parent = snapshotMap.get(current.getParent());
        if (parent == null) {
            break;
        }
        currentTO.setParent(convert2VMSnapshotTO(parent));
        current = snapshotMap.get(current.getParent());
        currentTO = currentTO.getParent();
    }
    return result;
}
Also used : VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) VMSnapshotTO(com.cloud.agent.api.VMSnapshotTO) HashMap(java.util.HashMap)

Aggregations

VMSnapshotTO (com.cloud.agent.api.VMSnapshotTO)28 GuestOSVO (com.cloud.storage.GuestOSVO)15 VMSnapshotVO (com.cloud.vm.snapshot.VMSnapshotVO)15 ArrayList (java.util.ArrayList)15 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)14 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)14 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)13 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)13 Test (org.junit.Test)12 HostVO (com.cloud.host.HostVO)11 GuestOSHypervisorVO (com.cloud.storage.GuestOSHypervisorVO)11 VolumeObjectTO (com.cloud.storage.to.VolumeObjectTO)10 UserVmVO (com.cloud.vm.UserVmVO)10 Answer (com.cloud.agent.api.Answer)9 RevertToVMSnapshotAnswer (com.cloud.agent.api.RevertToVMSnapshotAnswer)7 NoTransitionException (com.cloud.utils.fsm.NoTransitionException)7 HashMap (java.util.HashMap)7 Command (com.cloud.agent.api.Command)6 RebootAnswer (com.cloud.agent.api.RebootAnswer)6 CreateAnswer (com.cloud.agent.api.storage.CreateAnswer)6