Search in sources :

Example 1 with CreateVMSnapshotAnswer

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

the class LibvirtCreateVMSnapshotCommandWrapper method execute.

@Override
public Answer execute(final CreateVMSnapshotCommand cmd, final LibvirtComputingResource libvirtComputingResource) {
    String vmName = cmd.getVmName();
    String vmSnapshotName = cmd.getTarget().getSnapshotName();
    Domain dm = null;
    try {
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        Connect conn = libvirtUtilitiesHelper.getConnection();
        dm = libvirtComputingResource.getDomain(conn, vmName);
        if (dm == null) {
            return new CreateVMSnapshotAnswer(cmd, false, "Create VM Snapshot Failed due to can not find vm: " + vmName);
        }
        DomainState domainState = dm.getInfo().state;
        if (domainState != DomainState.VIR_DOMAIN_RUNNING) {
            return new CreateVMSnapshotAnswer(cmd, false, "Create VM Snapshot Failed due to  vm is not running: " + vmName + " with domainState = " + domainState);
        }
        String vmSnapshotXML = "<domainsnapshot>" + "  <name>" + vmSnapshotName + "</name>" + "  <memory snapshot='internal' />" + "</domainsnapshot>";
        dm.snapshotCreateXML(vmSnapshotXML);
        return new CreateVMSnapshotAnswer(cmd, cmd.getTarget(), cmd.getVolumeTOs());
    } catch (LibvirtException e) {
        String msg = " Create VM snapshot failed due to " + e.toString();
        s_logger.warn(msg, e);
        return new CreateVMSnapshotAnswer(cmd, false, msg);
    } finally {
        if (dm != null) {
            try {
                dm.free();
            } catch (LibvirtException l) {
                s_logger.trace("Ignoring libvirt error.", l);
            }
            ;
        }
    }
}
Also used : CreateVMSnapshotAnswer(com.cloud.agent.api.CreateVMSnapshotAnswer) LibvirtException(org.libvirt.LibvirtException) DomainState(org.libvirt.DomainInfo.DomainState) Connect(org.libvirt.Connect) Domain(org.libvirt.Domain)

Example 2 with CreateVMSnapshotAnswer

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

the class VmwareStorageManagerImpl method execute.

@Override
public CreateVMSnapshotAnswer execute(VmwareHostService hostService, CreateVMSnapshotCommand cmd) {
    List<VolumeObjectTO> volumeTOs = cmd.getVolumeTOs();
    String vmName = cmd.getVmName();
    String vmSnapshotName = cmd.getTarget().getSnapshotName();
    String vmSnapshotDesc = cmd.getTarget().getDescription();
    boolean snapshotMemory = cmd.getTarget().getType() == VMSnapshot.Type.DiskAndMemory;
    boolean quiescevm = cmd.getTarget().getQuiescevm();
    VirtualMachineMO vmMo = null;
    VmwareContext context = hostService.getServiceContext(cmd);
    try {
        VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd);
        // wait if there are already VM snapshot task running
        ManagedObjectReference taskmgr = context.getServiceContent().getTaskManager();
        List<ManagedObjectReference> tasks = context.getVimClient().getDynamicProperty(taskmgr, "recentTask");
        for (ManagedObjectReference taskMor : tasks) {
            TaskInfo info = (TaskInfo) (context.getVimClient().getDynamicProperty(taskMor, "info"));
            if (info.getEntityName().equals(cmd.getVmName()) && StringUtils.isNotBlank(info.getName()) && info.getName().equalsIgnoreCase("CreateSnapshot_Task")) {
                if (!(info.getState().equals(TaskInfoState.SUCCESS) || info.getState().equals(TaskInfoState.ERROR))) {
                    s_logger.debug("There is already a VM snapshot task running, wait for it");
                    context.getVimClient().waitForTask(taskMor);
                }
            }
        }
        vmMo = hyperHost.findVmOnHyperHost(vmName);
        if (vmMo == null) {
            vmMo = hyperHost.findVmOnPeerHyperHost(vmName);
        }
        if (vmMo == null) {
            String msg = "Unable to find VM for CreateVMSnapshotCommand";
            s_logger.info(msg);
            return new CreateVMSnapshotAnswer(cmd, false, msg);
        } else {
            if (vmMo.getSnapshotMor(vmSnapshotName) != null) {
                s_logger.info("VM snapshot " + vmSnapshotName + " already exists");
            } else if (!vmMo.createSnapshot(vmSnapshotName, vmSnapshotDesc, snapshotMemory, quiescevm)) {
                return new CreateVMSnapshotAnswer(cmd, false, "Unable to create snapshot due to esxi internal failed");
            }
            Map<String, String> mapNewDisk = getNewDiskMap(vmMo);
            setVolumeToPathAndSize(volumeTOs, mapNewDisk, context, hyperHost, cmd.getVmName());
            return new CreateVMSnapshotAnswer(cmd, cmd.getTarget(), volumeTOs);
        }
    } catch (Exception e) {
        String msg = e.getMessage();
        s_logger.error("failed to create snapshot for vm:" + vmName + " due to " + msg);
        try {
            if (vmMo.getSnapshotMor(vmSnapshotName) != null) {
                vmMo.removeSnapshot(vmSnapshotName, false);
            }
        } catch (Exception e1) {
            s_logger.info("[ignored]" + "error during snapshot remove: " + e1.getLocalizedMessage());
        }
        return new CreateVMSnapshotAnswer(cmd, false, e.getMessage());
    }
}
Also used : VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) RemoteException(java.rmi.RemoteException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) TaskInfo(com.vmware.vim25.TaskInfo) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) CreateVMSnapshotAnswer(com.cloud.agent.api.CreateVMSnapshotAnswer) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 3 with CreateVMSnapshotAnswer

use of com.cloud.agent.api.CreateVMSnapshotAnswer 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 4 with CreateVMSnapshotAnswer

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

the class HypervisorHelperImpl method quiesceVm.

@Override
public VMSnapshotTO quiesceVm(VirtualMachine virtualMachine) {
    String value = configurationDao.getValue("vmsnapshot.create.wait");
    int wait = NumbersUtil.parseInt(value, 1800);
    Long hostId = vmSnapshotHelper.pickRunningHost(virtualMachine.getId());
    VMSnapshotTO vmSnapshotTO = new VMSnapshotTO(1L, UUID.randomUUID().toString(), VMSnapshot.Type.Disk, null, null, false, null, true);
    GuestOSVO guestOS = guestOSDao.findById(virtualMachine.getGuestOSId());
    List<VolumeObjectTO> volumeTOs = vmSnapshotHelper.getVolumeTOList(virtualMachine.getId());
    CreateVMSnapshotCommand ccmd = new CreateVMSnapshotCommand(virtualMachine.getInstanceName(), virtualMachine.getUuid(), vmSnapshotTO, volumeTOs, guestOS.getDisplayName());
    HostVO host = hostDao.findById(hostId);
    GuestOSHypervisorVO guestOsMapping = guestOsHypervisorDao.findByOsIdAndHypervisor(guestOS.getId(), host.getHypervisorType().toString(), host.getHypervisorVersion());
    ccmd.setPlatformEmulator(guestOsMapping.getGuestOsName());
    ccmd.setWait(wait);
    try {
        Answer answer = agentMgr.send(hostId, ccmd);
        if (answer != null && answer.getResult()) {
            CreateVMSnapshotAnswer snapshotAnswer = (CreateVMSnapshotAnswer) answer;
            vmSnapshotTO.setVolumes(snapshotAnswer.getVolumeTOs());
        } else {
            String errMsg = (answer != null) ? answer.getDetails() : null;
            throw new CloudRuntimeException("Failed to quiesce vm, due to " + errMsg);
        }
    } catch (AgentUnavailableException e) {
        throw new CloudRuntimeException("Failed to quiesce vm", e);
    } catch (OperationTimedoutException e) {
        throw new CloudRuntimeException("Failed to quiesce vm", e);
    }
    return vmSnapshotTO;
}
Also used : OperationTimedoutException(com.cloud.exception.OperationTimedoutException) GuestOSVO(com.cloud.storage.GuestOSVO) CreateVMSnapshotCommand(com.cloud.agent.api.CreateVMSnapshotCommand) EndPoint(org.apache.cloudstack.engine.subsystem.api.storage.EndPoint) HostVO(com.cloud.host.HostVO) GuestOSHypervisorVO(com.cloud.storage.GuestOSHypervisorVO) CreateVMSnapshotAnswer(com.cloud.agent.api.CreateVMSnapshotAnswer) Answer(com.cloud.agent.api.Answer) IntroduceObjectAnswer(org.apache.cloudstack.storage.command.IntroduceObjectAnswer) VMSnapshotTO(com.cloud.agent.api.VMSnapshotTO) CreateVMSnapshotAnswer(com.cloud.agent.api.CreateVMSnapshotAnswer) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO)

Example 5 with CreateVMSnapshotAnswer

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

the class DefaultVMSnapshotStrategy method processAnswer.

@DB
protected void processAnswer(final VMSnapshotVO vmSnapshot, UserVm userVm, final Answer as, Long hostId) {
    try {
        Transaction.execute(new TransactionCallbackWithExceptionNoReturn<NoTransitionException>() {

            @Override
            public void doInTransactionWithoutResult(TransactionStatus status) throws NoTransitionException {
                if (as instanceof CreateVMSnapshotAnswer) {
                    CreateVMSnapshotAnswer answer = (CreateVMSnapshotAnswer) as;
                    finalizeCreate(vmSnapshot, answer.getVolumeTOs());
                    vmSnapshotHelper.vmSnapshotStateTransitTo(vmSnapshot, VMSnapshot.Event.OperationSucceeded);
                } else if (as instanceof RevertToVMSnapshotAnswer) {
                    RevertToVMSnapshotAnswer answer = (RevertToVMSnapshotAnswer) as;
                    finalizeRevert(vmSnapshot, answer.getVolumeTOs());
                    vmSnapshotHelper.vmSnapshotStateTransitTo(vmSnapshot, VMSnapshot.Event.OperationSucceeded);
                } else if (as instanceof DeleteVMSnapshotAnswer) {
                    DeleteVMSnapshotAnswer answer = (DeleteVMSnapshotAnswer) as;
                    finalizeDelete(vmSnapshot, answer.getVolumeTOs());
                    vmSnapshotDao.remove(vmSnapshot.getId());
                }
            }
        });
    } catch (Exception e) {
        String errMsg = "Error while process answer: " + as.getClass() + " due to " + e.getMessage();
        s_logger.error(errMsg, e);
        throw new CloudRuntimeException(errMsg);
    }
}
Also used : CreateVMSnapshotAnswer(com.cloud.agent.api.CreateVMSnapshotAnswer) RevertToVMSnapshotAnswer(com.cloud.agent.api.RevertToVMSnapshotAnswer) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) TransactionStatus(com.cloud.utils.db.TransactionStatus) DeleteVMSnapshotAnswer(com.cloud.agent.api.DeleteVMSnapshotAnswer) ConfigurationException(javax.naming.ConfigurationException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) DB(com.cloud.utils.db.DB)

Aggregations

CreateVMSnapshotAnswer (com.cloud.agent.api.CreateVMSnapshotAnswer)8 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)6 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)5 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)4 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)4 HostVO (com.cloud.host.HostVO)3 GuestOSHypervisorVO (com.cloud.storage.GuestOSHypervisorVO)3 GuestOSVO (com.cloud.storage.GuestOSVO)3 CreateVMSnapshotCommand (com.cloud.agent.api.CreateVMSnapshotCommand)2 VMSnapshotTO (com.cloud.agent.api.VMSnapshotTO)2 VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)2 NoTransitionException (com.cloud.utils.fsm.NoTransitionException)2 VMSnapshotVO (com.cloud.vm.snapshot.VMSnapshotVO)2 IOException (java.io.IOException)2 RemoteException (java.rmi.RemoteException)2 ConfigurationException (javax.naming.ConfigurationException)2 Answer (com.cloud.agent.api.Answer)1 Command (com.cloud.agent.api.Command)1 DeleteVMSnapshotAnswer (com.cloud.agent.api.DeleteVMSnapshotAnswer)1 RevertToVMSnapshotAnswer (com.cloud.agent.api.RevertToVMSnapshotAnswer)1