Search in sources :

Example 16 with Pool

use of com.xensource.xenapi.Pool in project cloudstack by apache.

the class CitrixCreateVMSnapshotCommandWrapper method execute.

@Override
public Answer execute(final CreateVMSnapshotCommand command, final CitrixResourceBase citrixResourceBase) {
    final String vmName = command.getVmName();
    final String vmSnapshotName = command.getTarget().getSnapshotName();
    final List<VolumeObjectTO> listVolumeTo = command.getVolumeTOs();
    VmPowerState vmState = VmPowerState.HALTED;
    final String guestOSType = command.getGuestOSType();
    final String platformEmulator = command.getPlatformEmulator();
    final boolean snapshotMemory = command.getTarget().getType() == VMSnapshot.Type.DiskAndMemory;
    final long timeout = command.getWait();
    final Connection conn = citrixResourceBase.getConnection();
    VM vm = null;
    VM vmSnapshot = null;
    boolean success = false;
    try {
        // check if VM snapshot already exists
        final Set<VM> vmSnapshots = VM.getByNameLabel(conn, command.getTarget().getSnapshotName());
        if (vmSnapshots == null || vmSnapshots.size() > 0) {
            return new CreateVMSnapshotAnswer(command, command.getTarget(), command.getVolumeTOs());
        }
        // check if there is already a task for this VM snapshot
        Task task = null;
        Set<Task> tasks = Task.getByNameLabel(conn, "Async.VM.snapshot");
        if (tasks == null) {
            tasks = new LinkedHashSet<>();
        }
        final Set<Task> tasksByName = Task.getByNameLabel(conn, "Async.VM.checkpoint");
        if (tasksByName != null) {
            tasks.addAll(tasksByName);
        }
        for (final Task taskItem : tasks) {
            if (taskItem.getOtherConfig(conn).containsKey("CS_VM_SNAPSHOT_KEY")) {
                final String vmSnapshotTaskName = taskItem.getOtherConfig(conn).get("CS_VM_SNAPSHOT_KEY");
                if (vmSnapshotTaskName != null && vmSnapshotTaskName.equals(command.getTarget().getSnapshotName())) {
                    task = taskItem;
                }
            }
        }
        // create a new task if there is no existing task for this VM snapshot
        if (task == null) {
            try {
                vm = citrixResourceBase.getVM(conn, vmName);
                vmState = vm.getPowerState(conn);
            } catch (final Exception e) {
                if (!snapshotMemory) {
                    vm = citrixResourceBase.createWorkingVM(conn, vmName, guestOSType, platformEmulator, listVolumeTo);
                }
            }
            if (vm == null) {
                return new CreateVMSnapshotAnswer(command, false, "Creating VM Snapshot Failed due to can not find vm: " + vmName);
            }
            // call Xenserver API
            if (!snapshotMemory) {
                task = vm.snapshotAsync(conn, vmSnapshotName);
            } else {
                final Set<VBD> vbds = vm.getVBDs(conn);
                final Pool pool = Pool.getByUuid(conn, citrixResourceBase.getHost().getPool());
                for (final VBD vbd : vbds) {
                    final VBD.Record vbdr = vbd.getRecord(conn);
                    if (vbdr.userdevice.equals("0")) {
                        final VDI vdi = vbdr.VDI;
                        final SR sr = vdi.getSR(conn);
                        // store memory image on the same SR with ROOT volume
                        pool.setSuspendImageSR(conn, sr);
                    }
                }
                task = vm.checkpointAsync(conn, vmSnapshotName);
            }
            task.addToOtherConfig(conn, "CS_VM_SNAPSHOT_KEY", vmSnapshotName);
        }
        citrixResourceBase.waitForTask(conn, task, 1000, timeout * 1000);
        citrixResourceBase.checkForSuccess(conn, task);
        final String result = task.getResult(conn);
        // extract VM snapshot ref from result
        final String ref = result.substring("<value>".length(), result.length() - "</value>".length());
        vmSnapshot = Types.toVM(ref);
        try {
            Thread.sleep(5000);
        } catch (final InterruptedException ex) {
        }
        // calculate used capacity for this VM snapshot
        for (final VolumeObjectTO volumeTo : command.getVolumeTOs()) {
            try {
                final long size = citrixResourceBase.getVMSnapshotChainSize(conn, volumeTo, command.getVmName(), vmSnapshotName);
                volumeTo.setSize(size);
            } catch (final CloudRuntimeException cre) {
            }
        }
        success = true;
        return new CreateVMSnapshotAnswer(command, command.getTarget(), command.getVolumeTOs());
    } catch (final Exception e) {
        String msg = "";
        if (e instanceof Types.BadAsyncResult) {
            final String licenseKeyWord = "LICENCE_RESTRICTION";
            final Types.BadAsyncResult errorResult = (Types.BadAsyncResult) e;
            if (errorResult.shortDescription != null && errorResult.shortDescription.contains(licenseKeyWord)) {
                msg = licenseKeyWord;
            }
        } else {
            msg = e.toString();
        }
        s_logger.warn("Creating VM Snapshot " + command.getTarget().getSnapshotName() + " failed due to: " + msg, e);
        return new CreateVMSnapshotAnswer(command, false, msg);
    } finally {
        try {
            if (!success) {
                if (vmSnapshot != null) {
                    s_logger.debug("Delete exsisting VM Snapshot " + vmSnapshotName + " after making VolumeTO failed");
                    final Set<VBD> vbds = vmSnapshot.getVBDs(conn);
                    for (final VBD vbd : vbds) {
                        final VBD.Record vbdr = vbd.getRecord(conn);
                        if (vbdr.type == Types.VbdType.DISK) {
                            final VDI vdi = vbdr.VDI;
                            vdi.destroy(conn);
                        }
                    }
                    vmSnapshot.destroy(conn);
                }
            }
            if (vmState == VmPowerState.HALTED) {
                if (vm != null) {
                    vm.destroy(conn);
                }
            }
        } catch (final Exception e2) {
            s_logger.error("delete snapshot error due to " + e2.getMessage());
        }
    }
}
Also used : Types(com.xensource.xenapi.Types) Task(com.xensource.xenapi.Task) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) VBD(com.xensource.xenapi.VBD) Pool(com.xensource.xenapi.Pool) VDI(com.xensource.xenapi.VDI) SR(com.xensource.xenapi.SR) Connection(com.xensource.xenapi.Connection) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CreateVMSnapshotAnswer(com.cloud.agent.api.CreateVMSnapshotAnswer) VM(com.xensource.xenapi.VM) VmPowerState(com.xensource.xenapi.Types.VmPowerState)

Aggregations

Pool (com.xensource.xenapi.Pool)16 Connection (com.xensource.xenapi.Connection)14 XenAPIException (com.xensource.xenapi.Types.XenAPIException)13 XmlRpcException (org.apache.xmlrpc.XmlRpcException)13 Host (com.xensource.xenapi.Host)12 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)9 Types (com.xensource.xenapi.Types)6 StartupCommand (com.cloud.agent.api.StartupCommand)4 HashMap (java.util.HashMap)4 Answer (com.cloud.agent.api.Answer)2 ClusterVMMetaDataSyncAnswer (com.cloud.agent.api.ClusterVMMetaDataSyncAnswer)2 ClusterVMMetaDataSyncCommand (com.cloud.agent.api.ClusterVMMetaDataSyncCommand)2 CreateVMSnapshotAnswer (com.cloud.agent.api.CreateVMSnapshotAnswer)2 RebootAnswer (com.cloud.agent.api.RebootAnswer)2 SetupAnswer (com.cloud.agent.api.SetupAnswer)2 StartupRoutingCommand (com.cloud.agent.api.StartupRoutingCommand)2 StartupStorageCommand (com.cloud.agent.api.StartupStorageCommand)2 CreateAnswer (com.cloud.agent.api.storage.CreateAnswer)2 ClusterVO (com.cloud.dc.ClusterVO)2 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)2