Search in sources :

Example 1 with VMSnapshot

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

the class VMSnapshotStrategyTest method testCreateVMSnapshot.

@Test
public void testCreateVMSnapshot() throws AgentUnavailableException, OperationTimedoutException {
    Long hostId = 1L;
    Long vmId = 1L;
    Long guestOsId = 1L;
    HypervisorType hypervisorType = HypervisorType.Any;
    String hypervisorVersion = "default";
    String guestOsName = "Other";
    List<VolumeObjectTO> volumeObjectTOs = new ArrayList<VolumeObjectTO>();
    VMSnapshotVO vmSnapshot = Mockito.mock(VMSnapshotVO.class);
    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);
    GuestOSVO guestOSVO = Mockito.mock(GuestOSVO.class);
    Mockito.when(guestOSDao.findById(Matchers.anyLong())).thenReturn(guestOSVO);
    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);
    Mockito.when(agentMgr.send(Matchers.anyLong(), Matchers.any(Command.class))).thenReturn(null);
    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.takeVMSnapshot(vmSnapshot);
    } catch (CloudRuntimeException e1) {
        e = e1;
    }
    assertNotNull(e);
    CreateVMSnapshotAnswer answer = Mockito.mock(CreateVMSnapshotAnswer.class);
    Mockito.when(answer.getResult()).thenReturn(true);
    Mockito.when(agentMgr.send(Matchers.anyLong(), Matchers.any(Command.class))).thenReturn(answer);
    Mockito.when(vmSnapshotDao.findById(Matchers.anyLong())).thenReturn(vmSnapshot);
    VMSnapshot snapshot = null;
    snapshot = vmSnapshotStrategy.takeVMSnapshot(vmSnapshot);
    assertNotNull(snapshot);
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) ArrayList(java.util.ArrayList) GuestOSVO(com.cloud.storage.GuestOSVO) VMSnapshot(com.cloud.vm.snapshot.VMSnapshot) 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) CreateVMSnapshotAnswer(com.cloud.agent.api.CreateVMSnapshotAnswer) Command(com.cloud.agent.api.Command) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) Test(org.junit.Test)

Example 2 with VMSnapshot

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

the class VMSnapshotStrategyTest method testCreateVMSnapshot.

@Test
public void testCreateVMSnapshot() throws AgentUnavailableException, OperationTimedoutException {
    Long hostId = 1L;
    Long vmId = 1L;
    Long guestOsId = 1L;
    HypervisorType hypervisorType = HypervisorType.Any;
    String hypervisorVersion = "default";
    String guestOsName = "Other";
    List<VolumeObjectTO> volumeObjectTOs = new ArrayList<VolumeObjectTO>();
    VMSnapshotVO vmSnapshot = Mockito.mock(VMSnapshotVO.class);
    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);
    GuestOSVO guestOSVO = Mockito.mock(GuestOSVO.class);
    Mockito.when(guestOSDao.findById(Matchers.anyLong())).thenReturn(guestOSVO);
    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);
    Mockito.when(agentMgr.send(Matchers.anyLong(), Matchers.any(Command.class))).thenReturn(null);
    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.takeVMSnapshot(vmSnapshot);
    } catch (CloudRuntimeException e1) {
        e = e1;
    }
    assertNotNull(e);
    CreateVMSnapshotAnswer answer = Mockito.mock(CreateVMSnapshotAnswer.class);
    Mockito.when(answer.getResult()).thenReturn(true);
    Mockito.when(agentMgr.send(Matchers.anyLong(), Matchers.any(Command.class))).thenReturn(answer);
    Mockito.when(vmSnapshotDao.findById(Matchers.anyLong())).thenReturn(vmSnapshot);
    VMSnapshot snapshot = null;
    snapshot = vmSnapshotStrategy.takeVMSnapshot(vmSnapshot);
    assertNotNull(snapshot);
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) ArrayList(java.util.ArrayList) GuestOSVO(com.cloud.storage.GuestOSVO) VMSnapshot(com.cloud.vm.snapshot.VMSnapshot) 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) CreateVMSnapshotAnswer(com.cloud.agent.api.CreateVMSnapshotAnswer) Command(com.cloud.agent.api.Command) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) Test(org.junit.Test)

Example 3 with VMSnapshot

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

the class CreateSnapshotFromVMSnapshotCmd method getVmId.

private Long getVmId() {
    VMSnapshot vmsnapshot = _entityMgr.findById(VMSnapshot.class, getVMSnapshotId());
    if (vmsnapshot == null) {
        throw new InvalidParameterValueException("Unable to find vm snapshot by id=" + getVMSnapshotId());
    }
    UserVm vm = _entityMgr.findById(UserVm.class, vmsnapshot.getVmId());
    if (vm == null) {
        throw new InvalidParameterValueException("Unable to find vm by vm snapshot id=" + getVMSnapshotId());
    }
    return vm.getId();
}
Also used : UserVm(com.cloud.uservm.UserVm) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) VMSnapshot(com.cloud.vm.snapshot.VMSnapshot)

Example 4 with VMSnapshot

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

the class StorageSystemSnapshotStrategy method takeSnapshot.

@Override
@DB
public SnapshotInfo takeSnapshot(SnapshotInfo snapshotInfo) {
    VolumeInfo volumeInfo = snapshotInfo.getBaseVolume();
    verifyFormat(volumeInfo);
    verifyLocationType(snapshotInfo);
    final boolean canStorageSystemCreateVolumeFromSnapshot = canStorageSystemCreateVolumeFromSnapshot(volumeInfo.getPoolId());
    final boolean computeClusterSupportsVolumeClone;
    // only XenServer, VMware and KVM are currently supported
    if (volumeInfo.getFormat() == ImageFormat.VHD) {
        HostVO hostVO = getHost(volumeInfo.getId());
        computeClusterSupportsVolumeClone = clusterDao.getSupportsResigning(hostVO.getClusterId());
    } else if (volumeInfo.getFormat() == ImageFormat.OVA || volumeInfo.getFormat() == ImageFormat.QCOW2 || volumeInfo.getFormat() == ImageFormat.RAW) {
        computeClusterSupportsVolumeClone = true;
    } else {
        throw new CloudRuntimeException("Unsupported format");
    }
    SnapshotVO snapshotVO = snapshotDao.acquireInLockTable(snapshotInfo.getId());
    if (snapshotVO == null) {
        throw new CloudRuntimeException("Failed to acquire lock on the following snapshot: " + snapshotInfo.getId());
    }
    VMSnapshot vmSnapshot = null;
    if (ImageFormat.OVA.equals(volumeInfo.getFormat())) {
        setVmdk(snapshotInfo, volumeInfo);
        vmSnapshot = takeHypervisorSnapshot(volumeInfo);
    }
    SnapshotResult result = null;
    SnapshotInfo snapshotOnPrimary;
    try {
        volumeInfo.stateTransit(Volume.Event.SnapshotRequested);
        if (canStorageSystemCreateVolumeFromSnapshot && computeClusterSupportsVolumeClone) {
            SnapshotDetailsVO snapshotDetail = new SnapshotDetailsVO(snapshotInfo.getId(), "takeSnapshot", Boolean.TRUE.toString(), false);
            snapshotDetailsDao.persist(snapshotDetail);
        }
        result = snapshotSvr.takeSnapshot(snapshotInfo);
        if (result.isFailed()) {
            s_logger.debug("Failed to take a snapshot: " + result.getResult());
            throw new CloudRuntimeException(result.getResult());
        }
        if (!canStorageSystemCreateVolumeFromSnapshot || !computeClusterSupportsVolumeClone) {
            performSnapshotAndCopyOnHostSide(volumeInfo, snapshotInfo);
        }
        snapshotOnPrimary = result.getSnapshot();
    } finally {
        if (result != null && result.isSuccess()) {
            volumeInfo.stateTransit(Volume.Event.OperationSucceeded);
        } else {
            volumeInfo.stateTransit(Volume.Event.OperationFailed);
        }
        if (ImageFormat.OVA.equals(volumeInfo.getFormat())) {
            if (vmSnapshot != null) {
                deleteHypervisorSnapshot(vmSnapshot);
            }
        }
    }
    snapshotDao.releaseFromLockTable(snapshotInfo.getId());
    return snapshotOnPrimary;
}
Also used : SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) SnapshotVO(com.cloud.storage.SnapshotVO) SnapshotResult(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotResult) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) SnapshotDetailsVO(com.cloud.storage.dao.SnapshotDetailsVO) VMSnapshot(com.cloud.vm.snapshot.VMSnapshot) HostVO(com.cloud.host.HostVO) DB(com.cloud.utils.db.DB)

Example 5 with VMSnapshot

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

the class DefaultVMSnapshotStrategy method publishUsageEvent.

private void publishUsageEvent(String type, VMSnapshot vmSnapshot, UserVm userVm, VolumeObjectTO volumeTo) {
    VolumeVO volume = volumeDao.findById(volumeTo.getId());
    Long diskOfferingId = volume.getDiskOfferingId();
    Long offeringId = null;
    if (diskOfferingId != null) {
        DiskOfferingVO offering = diskOfferingDao.findById(diskOfferingId);
        if (offering != null && !offering.isComputeOnly()) {
            offeringId = offering.getId();
        }
    }
    Map<String, String> details = new HashMap<>();
    if (vmSnapshot != null) {
        details.put(UsageEventVO.DynamicParameters.vmSnapshotId.name(), String.valueOf(vmSnapshot.getId()));
    }
    // save volume's id into templateId field
    UsageEventUtils.publishUsageEvent(// save volume's id into templateId field
    type, // save volume's id into templateId field
    vmSnapshot.getAccountId(), // save volume's id into templateId field
    userVm.getDataCenterId(), // save volume's id into templateId field
    userVm.getId(), // save volume's id into templateId field
    vmSnapshot.getName(), // save volume's id into templateId field
    offeringId, // save volume's id into templateId field
    volume.getId(), volumeTo.getSize(), VMSnapshot.class.getName(), vmSnapshot.getUuid(), details);
}
Also used : VolumeVO(com.cloud.storage.VolumeVO) HashMap(java.util.HashMap) DiskOfferingVO(com.cloud.storage.DiskOfferingVO) VMSnapshot(com.cloud.vm.snapshot.VMSnapshot)

Aggregations

VMSnapshot (com.cloud.vm.snapshot.VMSnapshot)25 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)7 VMSnapshotVO (com.cloud.vm.snapshot.VMSnapshotVO)7 UserVm (com.cloud.uservm.UserVm)6 ArrayList (java.util.ArrayList)6 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)5 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)5 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)5 UserVmVO (com.cloud.vm.UserVmVO)5 RevertToVMSnapshotAnswer (com.cloud.agent.api.RevertToVMSnapshotAnswer)4 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)4 Project (com.cloud.projects.Project)4 VolumeVO (com.cloud.storage.VolumeVO)4 HashMap (java.util.HashMap)4 VMSnapshotResponse (com.cloud.api.response.VMSnapshotResponse)3 HostVO (com.cloud.host.HostVO)3 VolumeObjectTO (com.cloud.storage.to.VolumeObjectTO)3 Account (com.cloud.user.Account)3 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)3 PowerState (com.cloud.vm.VirtualMachine.PowerState)3