use of com.cloud.legacymodel.communication.answer.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);
StorageVol storageVol = null;
try {
storageVol = destinationPool.storageVolLookupByName(volumeTO.getPath());
} catch (final LibvirtException e) {
s_logger.debug("Could not find volume " + volumeTO.getPath() + ": " + e.getMessage());
}
if (storageVol == null) {
LibvirtStorageVolumeDef volumeDef = new LibvirtStorageVolumeDef(volumeTO.getPath(), volumeTO.getSize(), LibvirtStorageVolumeDef.VolumeFormat.getFormat(volumeTO.getImageFormat().name()), null, null, volumeTO.getStorageProvisioningType());
destinationPool.storageVolCreateXML(volumeDef.toString(), 0);
}
}
// 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, libvirtComputingResource.getMigrateSpeedAcrossCluster());
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);
}
}
use of com.cloud.legacymodel.communication.answer.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());
}
}
Aggregations