Search in sources :

Example 31 with VMSnapshotVO

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

the class DefaultVMSnapshotStrategy method finalizeDelete.

protected void finalizeDelete(VMSnapshotVO vmSnapshot, List<VolumeObjectTO> volumeTOs) {
    // update volumes path
    updateVolumePath(volumeTOs);
    // update children's parent snapshots
    List<VMSnapshotVO> children = vmSnapshotDao.listByParent(vmSnapshot.getId());
    for (VMSnapshotVO child : children) {
        child.setParent(vmSnapshot.getParent());
        vmSnapshotDao.persist(child);
    }
    // update current snapshot
    VMSnapshotVO current = vmSnapshotDao.findCurrentSnapshotByVmId(vmSnapshot.getVmId());
    if (current != null && current.getId() == vmSnapshot.getId() && vmSnapshot.getParent() != null) {
        VMSnapshotVO parent = vmSnapshotDao.findById(vmSnapshot.getParent());
        parent.setCurrent(true);
        vmSnapshotDao.persist(parent);
    }
    vmSnapshot.setCurrent(false);
    vmSnapshotDao.persist(vmSnapshot);
}
Also used : VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO)

Example 32 with VMSnapshotVO

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

the class DefaultVMSnapshotStrategy method finalizeCreate.

protected void finalizeCreate(VMSnapshotVO vmSnapshot, List<VolumeObjectTO> volumeTOs) {
    // update volumes path
    updateVolumePath(volumeTOs);
    vmSnapshot.setCurrent(true);
    // change current snapshot
    if (vmSnapshot.getParent() != null) {
        VMSnapshotVO previousCurrent = vmSnapshotDao.findById(vmSnapshot.getParent());
        previousCurrent.setCurrent(false);
        vmSnapshotDao.persist(previousCurrent);
    }
    vmSnapshotDao.persist(vmSnapshot);
}
Also used : VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO)

Example 33 with VMSnapshotVO

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

the class UserVmManagerTest method testRestoreVMF4.

// Test restoreVM on providing new template Id, when VM is in running state
@Test
public void testRestoreVMF4() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException {
    doReturn(VirtualMachine.State.Running).when(_vmMock).getState();
    when(_vmDao.findById(anyLong())).thenReturn(_vmMock);
    when(_volsDao.findByInstanceAndType(314L, Volume.Type.ROOT)).thenReturn(_rootVols);
    doReturn(false).when(_rootVols).isEmpty();
    when(_rootVols.get(eq(0))).thenReturn(_volumeMock);
    doReturn(3L).when(_volumeMock).getTemplateId();
    doReturn(ImageFormat.VHD).when(_templateMock).getFormat();
    when(_templateDao.findById(anyLong())).thenReturn(_templateMock);
    doNothing().when(_accountMgr).checkAccess(_account, null, true, _templateMock);
    when(_storageMgr.allocateDuplicateVolume(_volumeMock, 14L)).thenReturn(_volumeMock);
    when(_templateMock.getGuestOSId()).thenReturn(5L);
    doNothing().when(_vmMock).setGuestOSId(anyLong());
    doNothing().when(_vmMock).setTemplateId(3L);
    when(_vmDao.update(314L, _vmMock)).thenReturn(true);
    when(_storageMgr.allocateDuplicateVolume(_volumeMock, null)).thenReturn(_volumeMock);
    doNothing().when(_volsDao).attachVolume(anyLong(), anyLong(), anyLong());
    when(_volumeMock.getId()).thenReturn(3L);
    doNothing().when(_volsDao).detachVolume(anyLong());
    List<VMSnapshotVO> mockList = mock(List.class);
    when(_vmSnapshotDao.findByVm(anyLong())).thenReturn(mockList);
    when(mockList.size()).thenReturn(0);
    when(_templateMock.getUuid()).thenReturn("b1a3626e-72e0-4697-8c7c-a110940cc55d");
    Account account = new AccountVO("testaccount", 1L, "networkdomain", (short) 0, "uuid");
    UserVO user = new UserVO(1, "testuser", "password", "firstname", "lastName", "email", "timezone", UUID.randomUUID().toString(), User.Source.UNKNOWN);
    StoragePoolVO storagePool = new StoragePoolVO();
    storagePool.setManaged(false);
    when(_storagePoolDao.findById(anyLong())).thenReturn(storagePool);
    CallContext.register(user, account);
    try {
        _userVmMgr.restoreVMInternal(_account, _vmMock, 14L);
    } finally {
        CallContext.unregister();
    }
}
Also used : VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) Account(com.cloud.user.Account) UserVO(com.cloud.user.UserVO) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) AccountVO(com.cloud.user.AccountVO) Test(org.junit.Test)

Example 34 with VMSnapshotVO

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

the class UserVmManagerTest method testRestoreVMF5.

// Test restoreVM on providing new ISO Id, when VM(deployed using ISO) is in running state
@Test
public void testRestoreVMF5() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException {
    doReturn(VirtualMachine.State.Running).when(_vmMock).getState();
    when(_vmDao.findById(anyLong())).thenReturn(_vmMock);
    when(_volsDao.findByInstanceAndType(314L, Volume.Type.ROOT)).thenReturn(_rootVols);
    doReturn(false).when(_rootVols).isEmpty();
    when(_rootVols.get(eq(0))).thenReturn(_volumeMock);
    doReturn(null).when(_volumeMock).getTemplateId();
    doReturn(3L).when(_vmMock).getIsoId();
    doReturn(ImageFormat.ISO).when(_templateMock).getFormat();
    when(_templateDao.findById(anyLong())).thenReturn(_templateMock);
    doNothing().when(_accountMgr).checkAccess(_account, null, true, _templateMock);
    when(_storageMgr.allocateDuplicateVolume(_volumeMock, null)).thenReturn(_volumeMock);
    doNothing().when(_vmMock).setIsoId(14L);
    when(_templateMock.getGuestOSId()).thenReturn(5L);
    doNothing().when(_vmMock).setGuestOSId(anyLong());
    doNothing().when(_vmMock).setTemplateId(3L);
    when(_vmDao.update(314L, _vmMock)).thenReturn(true);
    when(_storageMgr.allocateDuplicateVolume(_volumeMock, null)).thenReturn(_volumeMock);
    doNothing().when(_volsDao).attachVolume(anyLong(), anyLong(), anyLong());
    when(_volumeMock.getId()).thenReturn(3L);
    doNothing().when(_volsDao).detachVolume(anyLong());
    List<VMSnapshotVO> mockList = mock(List.class);
    when(_vmSnapshotDao.findByVm(anyLong())).thenReturn(mockList);
    when(mockList.size()).thenReturn(0);
    when(_templateMock.getUuid()).thenReturn("b1a3626e-72e0-4697-8c7c-a110940cc55d");
    Account account = new AccountVO("testaccount", 1L, "networkdomain", (short) 0, "uuid");
    UserVO user = new UserVO(1, "testuser", "password", "firstname", "lastName", "email", "timezone", UUID.randomUUID().toString(), User.Source.UNKNOWN);
    StoragePoolVO storagePool = new StoragePoolVO();
    storagePool.setManaged(false);
    when(_storagePoolDao.findById(anyLong())).thenReturn(storagePool);
    CallContext.register(user, account);
    try {
        _userVmMgr.restoreVMInternal(_account, _vmMock, 14L);
    } finally {
        CallContext.unregister();
    }
    verify(_vmMock, times(1)).setIsoId(14L);
}
Also used : VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) Account(com.cloud.user.Account) UserVO(com.cloud.user.UserVO) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) AccountVO(com.cloud.user.AccountVO) Test(org.junit.Test)

Example 35 with VMSnapshotVO

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

the class StorageSystemSnapshotStrategy method takeHypervisorSnapshot.

private VMSnapshot takeHypervisorSnapshot(VolumeInfo volumeInfo) {
    VirtualMachine virtualMachine = volumeInfo.getAttachedVM();
    if (virtualMachine != null && VirtualMachine.State.Running.equals(virtualMachine.getState())) {
        String vmSnapshotName = UUID.randomUUID().toString().replace("-", "");
        VMSnapshotVO vmSnapshotVO = new VMSnapshotVO(virtualMachine.getAccountId(), virtualMachine.getDomainId(), virtualMachine.getId(), vmSnapshotName, vmSnapshotName, vmSnapshotName, virtualMachine.getServiceOfferingId(), VMSnapshot.Type.Disk, null);
        VMSnapshot vmSnapshot = vmSnapshotDao.persist(vmSnapshotVO);
        if (vmSnapshot == null) {
            throw new CloudRuntimeException("Unable to allocate a VM snapshot object");
        }
        vmSnapshot = vmSnapshotService.createVMSnapshot(virtualMachine.getId(), vmSnapshot.getId(), true);
        if (vmSnapshot == null) {
            throw new CloudRuntimeException("Unable to create a hypervisor-side snapshot");
        }
        try {
            Thread.sleep(60000);
        } catch (Exception ex) {
            s_logger.warn(ex.getMessage(), ex);
        }
        return vmSnapshot;
    }
    // We didn't need to take a hypervisor-side snapshot. Return 'null' to indicate this.
    return null;
}
Also used : VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VMSnapshot(com.cloud.vm.snapshot.VMSnapshot) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VirtualMachine(com.cloud.vm.VirtualMachine)

Aggregations

VMSnapshotVO (com.cloud.vm.snapshot.VMSnapshotVO)58 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)35 UserVmVO (com.cloud.vm.UserVmVO)27 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)20 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)20 Account (com.cloud.user.Account)17 HostVO (com.cloud.host.HostVO)16 VMSnapshotTO (com.cloud.agent.api.VMSnapshotTO)15 GuestOSVO (com.cloud.storage.GuestOSVO)15 Test (org.junit.Test)14 HypervisorType (com.cloud.hypervisor.Hypervisor.HypervisorType)13 GuestOSHypervisorVO (com.cloud.storage.GuestOSHypervisorVO)13 NoTransitionException (com.cloud.utils.fsm.NoTransitionException)13 ArrayList (java.util.ArrayList)13 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)12 VolumeVO (com.cloud.storage.VolumeVO)11 ConfigurationException (javax.naming.ConfigurationException)11 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)10 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)10 IOException (java.io.IOException)10