Search in sources :

Example 11 with VMSnapshotTO

use of com.cloud.agent.api.VMSnapshotTO in project cloudstack by apache.

the class DefaultVMSnapshotStrategy 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 {
        VMSnapshotVO snapshot = vmSnapshotDao.findById(vmSnapshotVO.getId());
        List<VolumeObjectTO> volumeTOs = vmSnapshotHelper.getVolumeTOList(userVm.getId());
        String vmInstanceName = userVm.getInstanceName();
        VMSnapshotTO parent = vmSnapshotHelper.getSnapshotWithParents(snapshot).getParent();
        VMSnapshotTO vmSnapshotTO = new VMSnapshotTO(snapshot.getId(), snapshot.getName(), snapshot.getType(), snapshot.getCreated().getTime(), snapshot.getDescription(), snapshot.getCurrent(), parent, true);
        Long hostId = vmSnapshotHelper.pickRunningHost(vmSnapshot.getVmId());
        GuestOSVO guestOS = guestOSDao.findById(userVm.getGuestOSId());
        RevertToVMSnapshotCommand revertToSnapshotCommand = new RevertToVMSnapshotCommand(vmInstanceName, userVm.getUuid(), vmSnapshotTO, volumeTOs, guestOS.getDisplayName());
        HostVO host = hostDao.findById(hostId);
        GuestOSHypervisorVO guestOsMapping = guestOsHypervisorDao.findByOsIdAndHypervisor(guestOS.getId(), host.getHypervisorType().toString(), host.getHypervisorVersion());
        if (guestOsMapping == null) {
            revertToSnapshotCommand.setPlatformEmulator(null);
        } else {
            revertToSnapshotCommand.setPlatformEmulator(guestOsMapping.getGuestOsName());
        }
        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 (OperationTimedoutException e) {
        s_logger.debug("Failed to revert vm snapshot", e);
        throw new CloudRuntimeException(e.getMessage());
    } catch (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 (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.exception.OperationTimedoutException) RevertToVMSnapshotAnswer(com.cloud.agent.api.RevertToVMSnapshotAnswer) RevertToVMSnapshotCommand(com.cloud.agent.api.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.agent.api.VMSnapshotTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO)

Example 12 with VMSnapshotTO

use of com.cloud.agent.api.VMSnapshotTO in project cloudstack by apache.

the class VMSnapshotHelperImpl method getSnapshotWithParents.

@Override
public VMSnapshotTO getSnapshotWithParents(VMSnapshotVO snapshot) {
    Map<Long, VMSnapshotVO> snapshotMap = new HashMap<Long, VMSnapshotVO>();
    List<VMSnapshotVO> allSnapshots = _vmSnapshotDao.findByVm(snapshot.getVmId());
    for (VMSnapshotVO vmSnapshotVO : allSnapshots) {
        snapshotMap.put(vmSnapshotVO.getId(), vmSnapshotVO);
    }
    VMSnapshotTO currentTO = convert2VMSnapshotTO(snapshot);
    VMSnapshotTO result = currentTO;
    VMSnapshotVO current = snapshot;
    while (current.getParent() != null) {
        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)

Example 13 with VMSnapshotTO

use of com.cloud.agent.api.VMSnapshotTO in project cloudstack by apache.

the class NotAValidCommand method testCreateVMSnapshotCommand.

@Test
public void testCreateVMSnapshotCommand() {
    final Connection conn = Mockito.mock(Connection.class);
    final VMSnapshotTO snapshotTO = Mockito.mock(VMSnapshotTO.class);
    final List<VolumeObjectTO> volumeTOs = new ArrayList<VolumeObjectTO>();
    final CreateVMSnapshotCommand vmSnapshot = new CreateVMSnapshotCommand("Test", "uuid", snapshotTO, volumeTOs, "Debian");
    final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
    assertNotNull(wrapper);
    when(citrixResourceBase.getConnection()).thenReturn(conn);
    final Answer answer = wrapper.execute(vmSnapshot, citrixResourceBase);
    verify(citrixResourceBase, times(1)).getConnection();
    assertTrue(answer.getResult());
}
Also used : RebootAnswer(com.cloud.agent.api.RebootAnswer) CreateAnswer(com.cloud.agent.api.storage.CreateAnswer) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) VMSnapshotTO(com.cloud.agent.api.VMSnapshotTO) Connection(com.xensource.xenapi.Connection) ArrayList(java.util.ArrayList) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) CreateVMSnapshotCommand(com.cloud.agent.api.CreateVMSnapshotCommand) Test(org.junit.Test)

Aggregations

VMSnapshotTO (com.cloud.agent.api.VMSnapshotTO)13 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)11 GuestOSVO (com.cloud.storage.GuestOSVO)7 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)6 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)6 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)6 VMSnapshotVO (com.cloud.vm.snapshot.VMSnapshotVO)6 ArrayList (java.util.ArrayList)6 Answer (com.cloud.agent.api.Answer)5 HostVO (com.cloud.host.HostVO)5 GuestOSHypervisorVO (com.cloud.storage.GuestOSHypervisorVO)5 Test (org.junit.Test)5 UserVmVO (com.cloud.vm.UserVmVO)4 CreateVMSnapshotAnswer (com.cloud.agent.api.CreateVMSnapshotAnswer)3 CreateVMSnapshotCommand (com.cloud.agent.api.CreateVMSnapshotCommand)3 RebootAnswer (com.cloud.agent.api.RebootAnswer)3 RevertToVMSnapshotAnswer (com.cloud.agent.api.RevertToVMSnapshotAnswer)3 CreateAnswer (com.cloud.agent.api.storage.CreateAnswer)3 NoTransitionException (com.cloud.utils.fsm.NoTransitionException)3 Connection (com.xensource.xenapi.Connection)3