Search in sources :

Example 11 with VMSnapshot

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

the class CreateVMSnapshotCmd method execute.

@Override
public void execute() {
    CallContext.current().setEventDetails("VM Id: " + this._uuidMgr.getUuid(VirtualMachine.class, getVmId()));
    VMSnapshot result = _vmSnapshotService.createVMSnapshot(getVmId(), getEntityId(), getQuiescevm());
    if (result != null) {
        VMSnapshotResponse response = _responseGenerator.createVMSnapshotResponse(result);
        response.setResponseName(getCommandName());
        setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create vm snapshot due to an internal error creating snapshot for vm " + getVmId());
    }
}
Also used : ServerApiException(org.apache.cloudstack.api.ServerApiException) VMSnapshotResponse(org.apache.cloudstack.api.response.VMSnapshotResponse) VMSnapshot(com.cloud.vm.snapshot.VMSnapshot)

Example 12 with VMSnapshot

use of com.cloud.vm.snapshot.VMSnapshot in project cosmic by MissionCriticalCloud.

the class LibvirtRevertToVMSnapshotCommandWrapper method execute.

@Override
public Answer execute(final RevertToVMSnapshotCommand cmd, final LibvirtComputingResource libvirtComputingResource) {
    final String vmName = cmd.getVmName();
    final List<VolumeObjectTO> listVolumeTo = cmd.getVolumeTOs();
    final VMSnapshot.Type vmSnapshotType = cmd.getTarget().getType();
    final Boolean snapshotMemory = vmSnapshotType == VMSnapshot.Type.DiskAndMemory;
    PowerState vmState;
    Domain dm = null;
    try {
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        final Connect conn = libvirtUtilitiesHelper.getConnection();
        dm = libvirtComputingResource.getDomain(conn, vmName);
        if (dm == null) {
            return new RevertToVMSnapshotAnswer(cmd, false, "Revert to VM Snapshot Failed due to can not find vm: " + vmName);
        }
        final DomainSnapshot snapshot = dm.snapshotLookupByName(cmd.getTarget().getSnapshotName());
        if (snapshot == null) {
            return new RevertToVMSnapshotAnswer(cmd, false, "Cannot find vmSnapshot with name: " + cmd.getTarget().getSnapshotName());
        }
        dm.revertToSnapshot(snapshot);
        snapshot.free();
        if (!snapshotMemory) {
            dm.destroy();
            if (dm.isPersistent() == 1) {
                dm.undefine();
            }
            vmState = PowerState.PowerOff;
        } else {
            vmState = PowerState.PowerOn;
        }
        return new RevertToVMSnapshotAnswer(cmd, listVolumeTo, vmState);
    } catch (final LibvirtException e) {
        final String msg = " Revert to VM snapshot failed due to " + e.toString();
        s_logger.warn(msg, e);
        return new RevertToVMSnapshotAnswer(cmd, false, msg);
    } finally {
        if (dm != null) {
            try {
                dm.free();
            } catch (LibvirtException l) {
                s_logger.trace("Ignoring libvirt error.", l);
            }
            ;
        }
    }
}
Also used : RevertToVMSnapshotAnswer(com.cloud.agent.api.RevertToVMSnapshotAnswer) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) DomainSnapshot(org.libvirt.DomainSnapshot) VMSnapshot(com.cloud.vm.snapshot.VMSnapshot) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO) Domain(org.libvirt.Domain) PowerState(com.cloud.vm.VirtualMachine.PowerState)

Example 13 with VMSnapshot

use of com.cloud.vm.snapshot.VMSnapshot in project cosmic by MissionCriticalCloud.

the class CreateVMSnapshotCmd method execute.

@Override
public void execute() {
    CallContext.current().setEventDetails("VM Id: " + getVmId());
    final VMSnapshot result = _vmSnapshotService.createVMSnapshot(getVmId(), getEntityId(), isQuiescevm());
    if (result != null) {
        final VMSnapshotResponse response = _responseGenerator.createVMSnapshotResponse(result);
        response.setResponseName(getCommandName());
        setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create vm snapshot due to an internal error creating snapshot for vm " + getVmId());
    }
}
Also used : ServerApiException(com.cloud.api.ServerApiException) VMSnapshotResponse(com.cloud.api.response.VMSnapshotResponse) VMSnapshot(com.cloud.vm.snapshot.VMSnapshot)

Example 14 with VMSnapshot

use of com.cloud.vm.snapshot.VMSnapshot in project cosmic by MissionCriticalCloud.

the class VMSnapshotStrategyTest method testCreateVMSnapshot.

@Test
public void testCreateVMSnapshot() 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);
    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.takeVMSnapshot(vmSnapshot);
    } catch (final CloudRuntimeException e1) {
        e = e1;
    }
    assertNotNull(e);
    final 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(com.cloud.storage.to.VolumeObjectTO) Test(org.junit.Test)

Example 15 with VMSnapshot

use of com.cloud.vm.snapshot.VMSnapshot in project cosmic by MissionCriticalCloud.

the class ApiResponseHelper method createVMSnapshotResponse.

@Override
public VMSnapshotResponse createVMSnapshotResponse(final VMSnapshot vmSnapshot) {
    final VMSnapshotResponse vmSnapshotResponse = new VMSnapshotResponse();
    vmSnapshotResponse.setId(vmSnapshot.getUuid());
    vmSnapshotResponse.setName(vmSnapshot.getName());
    vmSnapshotResponse.setState(vmSnapshot.getState());
    vmSnapshotResponse.setCreated(vmSnapshot.getCreated());
    vmSnapshotResponse.setDescription(vmSnapshot.getDescription());
    vmSnapshotResponse.setDisplayName(vmSnapshot.getDisplayName());
    final UserVm vm = ApiDBUtils.findUserVmById(vmSnapshot.getVmId());
    if (vm != null) {
        vmSnapshotResponse.setVirtualMachineid(vm.getUuid());
    }
    if (vmSnapshot.getParent() != null) {
        final VMSnapshot vmSnapshotParent = ApiDBUtils.getVMSnapshotById(vmSnapshot.getParent());
        if (vmSnapshotParent != null) {
            vmSnapshotResponse.setParent(vmSnapshotParent.getUuid());
            vmSnapshotResponse.setParentName(vmSnapshotParent.getDisplayName());
        }
    }
    populateOwner(vmSnapshotResponse, vmSnapshot);
    final Project project = ApiDBUtils.findProjectByProjectAccountId(vmSnapshot.getAccountId());
    if (project != null) {
        vmSnapshotResponse.setProjectId(project.getUuid());
        vmSnapshotResponse.setProjectName(project.getName());
    }
    vmSnapshotResponse.setCurrent(vmSnapshot.getCurrent());
    vmSnapshotResponse.setType(vmSnapshot.getType().toString());
    vmSnapshotResponse.setObjectName("vmsnapshot");
    return vmSnapshotResponse;
}
Also used : Project(com.cloud.projects.Project) UserVm(com.cloud.uservm.UserVm) VMSnapshotResponse(com.cloud.api.response.VMSnapshotResponse) 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