Search in sources :

Example 1 with MigrateWithStorageAcrossClustersAnswer

use of com.cloud.agent.api.MigrateWithStorageAcrossClustersAnswer in project cosmic by MissionCriticalCloud.

the class LibvirtMigrateWithStorageAcrossClustersCommandWrapper method execute.

@Override
public Answer execute(final MigrateWithStorageAcrossClustersCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final VirtualMachineTO vm = command.getVirtualMachine();
    try {
        final LibvirtUtilitiesHelper utilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        final Connect sourceConnection = utilitiesHelper.getConnectionByVmName(vm.getName());
        final Connect destinationConnection = utilitiesHelper.retrieveQemuConnection("qemu+tcp://" + command.getDestinationIpAddress() + "/system");
        final Domain domain = sourceConnection.domainLookupByName(vm.getName());
        // VIR_DOMAIN_XML_MIGRATABLE = 8
        String domainXml = domain.getXMLDesc(8);
        final XPath xPath = XPathFactory.newInstance().newXPath();
        final XPathExpression expression = xPath.compile("/pool/target/path");
        final List<VolumeObjectTO> volumes = new ArrayList<>();
        for (final Pair<VolumeTO, StorageFilerTO> entry : command.getVolumeMapping()) {
            final VolumeTO volumeTO = entry.first();
            final StorageFilerTO storageFilerTO = entry.second();
            final StoragePool sourcePool = sourceConnection.storagePoolLookupByName(volumeTO.getPoolUuid());
            final String sourcePoolXml = sourcePool.getXMLDesc(0);
            final StoragePool destinationPool = destinationConnection.storagePoolLookupByName(storageFilerTO.getUuid());
            final String destinationPoolXml = destinationPool.getXMLDesc(0);
            final String sourcePath = expression.evaluate(new InputSource(new StringReader(sourcePoolXml)));
            final String sourceLocation = sourcePath + "/" + volumeTO.getPath();
            final String destinationPath = expression.evaluate(new InputSource(new StringReader(destinationPoolXml)));
            final String destinationLocation = destinationPath + "/" + volumeTO.getPath();
            domainXml = domainXml.replace(sourceLocation, destinationLocation);
            final VolumeObjectTO volumeObjectTO = new VolumeObjectTO();
            volumeObjectTO.setId(volumeTO.getId());
            volumeObjectTO.setPath(volumeTO.getPath());
            volumes.add(volumeObjectTO);
        }
        // VIR_MIGRATE_LIVE = 1
        // VIR_MIGRATE_UNDEFINE_SOURCE = 16
        // VIR_MIGRATE_NON_SHARED_DISK = 64
        domain.migrate(destinationConnection, 1 | 16 | 64, domainXml, vm.getName(), null, 0);
        return new MigrateWithStorageAcrossClustersAnswer(command, volumes);
    } catch (final Exception e) {
        s_logger.error("Migration of vm " + vm.getName() + " with storage failed due to " + e.toString(), e);
        return new MigrateWithStorageAcrossClustersAnswer(command, e);
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) InputSource(org.xml.sax.InputSource) StoragePool(org.libvirt.StoragePool) Connect(org.libvirt.Connect) ArrayList(java.util.ArrayList) StorageFilerTO(com.cloud.agent.api.to.StorageFilerTO) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) VolumeTO(com.cloud.agent.api.to.VolumeTO) MigrateWithStorageAcrossClustersAnswer(com.cloud.agent.api.MigrateWithStorageAcrossClustersAnswer) StringReader(java.io.StringReader) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO) Domain(org.libvirt.Domain)

Example 2 with MigrateWithStorageAcrossClustersAnswer

use of com.cloud.agent.api.MigrateWithStorageAcrossClustersAnswer in project cosmic by MissionCriticalCloud.

the class KVMStorageMotionStrategy method migrateVmWithVolumesAcrossCluster.

@Override
protected Answer migrateVmWithVolumesAcrossCluster(final VMInstanceVO instanceTo, final VirtualMachineTO vmTo, final Host srcHost, final Host destHost, final Map<VolumeInfo, DataStore> volumeToPoolMap) throws AgentUnavailableException {
    try {
        final List<Pair<VolumeTO, StorageFilerTO>> volumeMapping = buildVolumeMapping(volumeToPoolMap);
        final MigrateWithStorageAcrossClustersCommand command = new MigrateWithStorageAcrossClustersCommand(vmTo, volumeMapping, destHost.getPrivateIpAddress());
        final MigrateWithStorageAcrossClustersAnswer answer = (MigrateWithStorageAcrossClustersAnswer) agentMgr.send(srcHost.getId(), command);
        if (answer == null) {
            final String errorMessage = String.format("Error when trying to migrate VM %s (with storage) to host %s", instanceTo, destHost);
            s_logger.error(errorMessage);
            throw new CloudRuntimeException(errorMessage);
        } else if (!answer.getResult()) {
            final String errorMessage = String.format("Error when trying to migrate VM %s (with storage) to host %s => Details: %s", instanceTo, destHost, answer.getDetails());
            s_logger.error(errorMessage);
            throw new CloudRuntimeException(errorMessage);
        } else {
            // Update the volume details after migration.
            updateVolumePathsAfterMigration(volumeToPoolMap, answer.getVolumes());
        }
        return answer;
    } catch (final OperationTimedoutException e) {
        final String errorMessage = String.format("Operation timed out when attempting to migrate VM %s to host %s", instanceTo, destHost);
        s_logger.error(errorMessage, e);
        throw new AgentUnavailableException(errorMessage, srcHost.getId());
    }
}
Also used : OperationTimedoutException(com.cloud.exception.OperationTimedoutException) MigrateWithStorageAcrossClustersAnswer(com.cloud.agent.api.MigrateWithStorageAcrossClustersAnswer) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) MigrateWithStorageAcrossClustersCommand(com.cloud.agent.api.MigrateWithStorageAcrossClustersCommand) Pair(com.cloud.utils.Pair)

Aggregations

MigrateWithStorageAcrossClustersAnswer (com.cloud.agent.api.MigrateWithStorageAcrossClustersAnswer)2 MigrateWithStorageAcrossClustersCommand (com.cloud.agent.api.MigrateWithStorageAcrossClustersCommand)1 StorageFilerTO (com.cloud.agent.api.to.StorageFilerTO)1 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)1 VolumeTO (com.cloud.agent.api.to.VolumeTO)1 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)1 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)1 VolumeObjectTO (com.cloud.storage.to.VolumeObjectTO)1 Pair (com.cloud.utils.Pair)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 XPath (javax.xml.xpath.XPath)1 XPathExpression (javax.xml.xpath.XPathExpression)1 Connect (org.libvirt.Connect)1 Domain (org.libvirt.Domain)1 StoragePool (org.libvirt.StoragePool)1 InputSource (org.xml.sax.InputSource)1