Search in sources :

Example 1 with VMSnapshotTO

use of com.cloud.legacymodel.to.VMSnapshotTO in project cosmic by MissionCriticalCloud.

the class VMSnapshotStrategyTest method testDeleteVMSnapshot.

@Test
public void testDeleteVMSnapshot() 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.deleteVMSnapshot(vmSnapshot);
    } catch (final CloudRuntimeException e1) {
        e = e1;
    }
    assertNotNull(e);
    final DeleteVMSnapshotAnswer answer = Mockito.mock(DeleteVMSnapshotAnswer.class);
    Mockito.when(answer.getResult()).thenReturn(true);
    Mockito.when(agentMgr.send(Matchers.anyLong(), Matchers.any(Command.class))).thenReturn(answer);
    final boolean result = vmSnapshotStrategy.deleteVMSnapshot(vmSnapshot);
    assertTrue(result);
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) ArrayList(java.util.ArrayList) GuestOSVO(com.cloud.storage.GuestOSVO) Date(java.util.Date) HostVO(com.cloud.host.HostVO) AgentUnavailableException(com.cloud.legacymodel.exceptions.AgentUnavailableException) OperationTimedoutException(com.cloud.legacymodel.exceptions.OperationTimedoutException) IOException(java.io.IOException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) HypervisorType(com.cloud.model.enumeration.HypervisorType) GuestOSHypervisorVO(com.cloud.storage.GuestOSHypervisorVO) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) VMSnapshotTO(com.cloud.legacymodel.to.VMSnapshotTO) Command(com.cloud.legacymodel.communication.command.Command) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO) DeleteVMSnapshotAnswer(com.cloud.legacymodel.communication.answer.DeleteVMSnapshotAnswer) Test(org.junit.Test)

Example 2 with VMSnapshotTO

use of com.cloud.legacymodel.to.VMSnapshotTO in project cosmic by MissionCriticalCloud.

the class DefaultVMSnapshotStrategy method deleteVMSnapshot.

@Override
public boolean deleteVMSnapshot(final VMSnapshot vmSnapshot) {
    final UserVmVO userVm = userVmDao.findById(vmSnapshot.getVmId());
    final VMSnapshotVO vmSnapshotVO = (VMSnapshotVO) vmSnapshot;
    try {
        vmSnapshotHelper.vmSnapshotStateTransitTo(vmSnapshot, VMSnapshot.Event.ExpungeRequested);
    } catch (final NoTransitionException e) {
        s_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 {
        final Long hostId = vmSnapshotHelper.pickRunningHost(vmSnapshot.getVmId());
        final List<VolumeObjectTO> volumeTOs = vmSnapshotHelper.getVolumeTOList(vmSnapshot.getVmId());
        final String vmInstanceName = userVm.getInstanceName();
        final VMSnapshotTO parent = vmSnapshotHelper.getSnapshotWithParents(vmSnapshotVO).getParent();
        final VMSnapshotTO vmSnapshotTO = new VMSnapshotTO(vmSnapshot.getId(), vmSnapshot.getName(), vmSnapshot.getType(), vmSnapshot.getCreated().getTime(), vmSnapshot.getDescription(), vmSnapshot.getCurrent(), parent, true);
        final GuestOSVO guestOS = guestOSDao.findById(userVm.getGuestOSId());
        final DeleteVMSnapshotCommand deleteSnapshotCommand = new DeleteVMSnapshotCommand(vmInstanceName, vmSnapshotTO, volumeTOs, guestOS.getDisplayName());
        final Answer answer = agentMgr.send(hostId, deleteSnapshotCommand);
        if (answer != null && answer.getResult()) {
            processAnswer(vmSnapshotVO, userVm, answer, hostId);
            return true;
        } else {
            final String errMsg = (answer == null) ? null : answer.getDetails();
            s_logger.error("Delete vm snapshot " + vmSnapshot.getName() + " of vm " + userVm.getInstanceName() + " failed due to " + errMsg);
            throw new CloudRuntimeException("Delete vm snapshot " + vmSnapshot.getName() + " of vm " + userVm.getInstanceName() + " failed due to " + errMsg);
        }
    } catch (final OperationTimedoutException | AgentUnavailableException e) {
        throw new CloudRuntimeException("Delete vm snapshot " + vmSnapshot.getName() + " of vm " + userVm.getInstanceName() + " failed due to " + e.getMessage());
    }
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) OperationTimedoutException(com.cloud.legacymodel.exceptions.OperationTimedoutException) GuestOSVO(com.cloud.storage.GuestOSVO) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) DeleteVMSnapshotAnswer(com.cloud.legacymodel.communication.answer.DeleteVMSnapshotAnswer) RevertToVMSnapshotAnswer(com.cloud.legacymodel.communication.answer.RevertToVMSnapshotAnswer) CreateVMSnapshotAnswer(com.cloud.legacymodel.communication.answer.CreateVMSnapshotAnswer) Answer(com.cloud.legacymodel.communication.answer.Answer) VMSnapshotTO(com.cloud.legacymodel.to.VMSnapshotTO) DeleteVMSnapshotCommand(com.cloud.legacymodel.communication.command.DeleteVMSnapshotCommand) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) AgentUnavailableException(com.cloud.legacymodel.exceptions.AgentUnavailableException) NoTransitionException(com.cloud.legacymodel.exceptions.NoTransitionException) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO)

Example 3 with VMSnapshotTO

use of com.cloud.legacymodel.to.VMSnapshotTO in project cosmic by MissionCriticalCloud.

the class DefaultVMSnapshotStrategy method revertVMSnapshot.

@Override
public boolean revertVMSnapshot(final VMSnapshot vmSnapshot) {
    final VMSnapshotVO vmSnapshotVO = (VMSnapshotVO) vmSnapshot;
    final UserVmVO userVm = userVmDao.findById(vmSnapshot.getVmId());
    try {
        vmSnapshotHelper.vmSnapshotStateTransitTo(vmSnapshotVO, VMSnapshot.Event.RevertRequested);
    } catch (final NoTransitionException e) {
        throw new CloudRuntimeException(e.getMessage());
    }
    boolean result = false;
    try {
        final VMSnapshotVO snapshot = vmSnapshotDao.findById(vmSnapshotVO.getId());
        final List<VolumeObjectTO> volumeTOs = vmSnapshotHelper.getVolumeTOList(userVm.getId());
        final String vmInstanceName = userVm.getInstanceName();
        final VMSnapshotTO parent = vmSnapshotHelper.getSnapshotWithParents(snapshot).getParent();
        final VMSnapshotTO vmSnapshotTO = new VMSnapshotTO(snapshot.getId(), snapshot.getName(), snapshot.getType(), snapshot.getCreated().getTime(), snapshot.getDescription(), snapshot.getCurrent(), parent, true);
        final Long hostId = vmSnapshotHelper.pickRunningHost(vmSnapshot.getVmId());
        final GuestOSVO guestOS = guestOSDao.findById(userVm.getGuestOSId());
        final RevertToVMSnapshotCommand revertToSnapshotCommand = new RevertToVMSnapshotCommand(vmInstanceName, userVm.getUuid(), vmSnapshotTO, volumeTOs, guestOS.getDisplayName());
        final HostVO host = hostDao.findById(hostId);
        final GuestOSHypervisorVO guestOsMapping = guestOsHypervisorDao.findByOsIdAndHypervisor(guestOS.getId(), host.getHypervisorType().toString(), host.getHypervisorVersion());
        if (guestOsMapping == null) {
            revertToSnapshotCommand.setPlatformEmulator(null);
        } else {
            revertToSnapshotCommand.setPlatformEmulator(guestOsMapping.getGuestOsName());
        }
        final RevertToVMSnapshotAnswer answer = (RevertToVMSnapshotAnswer) agentMgr.send(hostId, revertToSnapshotCommand);
        if (answer != null && answer.getResult()) {
            processAnswer(vmSnapshotVO, userVm, answer, hostId);
            result = true;
        } else {
            String errMsg = "Revert VM: " + userVm.getInstanceName() + " to snapshot: " + vmSnapshotVO.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("Failed to revert vm snapshot", e);
        throw new CloudRuntimeException(e.getMessage());
    } catch (final AgentUnavailableException e) {
        s_logger.debug("Failed to revert vm snapshot", e);
        throw new CloudRuntimeException(e.getMessage());
    } 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());
            }
        }
    }
    return result;
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) OperationTimedoutException(com.cloud.legacymodel.exceptions.OperationTimedoutException) RevertToVMSnapshotAnswer(com.cloud.legacymodel.communication.answer.RevertToVMSnapshotAnswer) RevertToVMSnapshotCommand(com.cloud.legacymodel.communication.command.RevertToVMSnapshotCommand) GuestOSVO(com.cloud.storage.GuestOSVO) HostVO(com.cloud.host.HostVO) GuestOSHypervisorVO(com.cloud.storage.GuestOSHypervisorVO) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) VMSnapshotTO(com.cloud.legacymodel.to.VMSnapshotTO) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) AgentUnavailableException(com.cloud.legacymodel.exceptions.AgentUnavailableException) NoTransitionException(com.cloud.legacymodel.exceptions.NoTransitionException) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO)

Example 4 with VMSnapshotTO

use of com.cloud.legacymodel.to.VMSnapshotTO in project cosmic by MissionCriticalCloud.

the class NotAValidCommand method testDeleteVMSnapshotCommand.

@Test
public void testDeleteVMSnapshotCommand() {
    final Connection conn = Mockito.mock(Connection.class);
    final VMSnapshotTO snapshotTO = Mockito.mock(VMSnapshotTO.class);
    final List<VolumeObjectTO> volumeTOs = new ArrayList<>();
    final DeleteVMSnapshotCommand vmSnapshot = new DeleteVMSnapshotCommand("Test", snapshotTO, volumeTOs, "Debian");
    final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
    assertNotNull(wrapper);
    when(this.citrixResourceBase.getConnection()).thenReturn(conn);
    final Answer answer = wrapper.execute(vmSnapshot, this.citrixResourceBase);
    verify(this.citrixResourceBase, times(1)).getConnection();
    assertTrue(answer.getResult());
}
Also used : RebootAnswer(com.cloud.legacymodel.communication.answer.RebootAnswer) Answer(com.cloud.legacymodel.communication.answer.Answer) CreateAnswer(com.cloud.legacymodel.communication.answer.CreateAnswer) AttachAnswer(com.cloud.legacymodel.communication.answer.AttachAnswer) VMSnapshotTO(com.cloud.legacymodel.to.VMSnapshotTO) DeleteVMSnapshotCommand(com.cloud.legacymodel.communication.command.DeleteVMSnapshotCommand) Connection(com.xensource.xenapi.Connection) ArrayList(java.util.ArrayList) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with VMSnapshotTO

use of com.cloud.legacymodel.to.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.legacymodel.exceptions.OperationTimedoutException) VMSnapshotOptions(com.cloud.engine.subsystem.api.storage.VMSnapshotOptions) GuestOSVO(com.cloud.storage.GuestOSVO) CreateVMSnapshotCommand(com.cloud.legacymodel.communication.command.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.legacymodel.communication.answer.CreateVMSnapshotAnswer) VMSnapshotTO(com.cloud.legacymodel.to.VMSnapshotTO) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) AgentUnavailableException(com.cloud.legacymodel.exceptions.AgentUnavailableException) NoTransitionException(com.cloud.legacymodel.exceptions.NoTransitionException) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO)

Aggregations

VMSnapshotTO (com.cloud.legacymodel.to.VMSnapshotTO)12 VolumeObjectTO (com.cloud.legacymodel.to.VolumeObjectTO)10 GuestOSVO (com.cloud.storage.GuestOSVO)6 VMSnapshotVO (com.cloud.vm.snapshot.VMSnapshotVO)6 ArrayList (java.util.ArrayList)6 AgentUnavailableException (com.cloud.legacymodel.exceptions.AgentUnavailableException)5 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)5 OperationTimedoutException (com.cloud.legacymodel.exceptions.OperationTimedoutException)5 Test (org.junit.Test)5 HostVO (com.cloud.host.HostVO)4 Answer (com.cloud.legacymodel.communication.answer.Answer)4 GuestOSHypervisorVO (com.cloud.storage.GuestOSHypervisorVO)4 UserVmVO (com.cloud.vm.UserVmVO)4 AttachAnswer (com.cloud.legacymodel.communication.answer.AttachAnswer)3 CreateAnswer (com.cloud.legacymodel.communication.answer.CreateAnswer)3 RebootAnswer (com.cloud.legacymodel.communication.answer.RebootAnswer)3 RevertToVMSnapshotAnswer (com.cloud.legacymodel.communication.answer.RevertToVMSnapshotAnswer)3 NoTransitionException (com.cloud.legacymodel.exceptions.NoTransitionException)3 Connection (com.xensource.xenapi.Connection)3 HashMap (java.util.HashMap)3