Search in sources :

Example 1 with MigrateDiskInfo

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

the class LibvirtMigrateCommandWrapper method replaceStorage.

/**
 * Pass in a list of the disks to update in the XML (xmlDesc). Each disk passed in needs to have a serial number. If any disk's serial number in the
 * list does not match a disk in the XML, an exception should be thrown.
 * In addition to the serial number, each disk in the list needs the following info:
 * <ul>
 *  <li>The value of the 'type' of the disk (ex. file, block)
 *  <li>The value of the 'type' of the driver of the disk (ex. qcow2, raw)
 *  <li>The source of the disk needs an attribute that is either 'file' or 'dev' as well as its corresponding value.
 * </ul>
 */
protected String replaceStorage(String xmlDesc, Map<String, MigrateCommand.MigrateDiskInfo> migrateStorage, boolean migrateStorageManaged) throws IOException, ParserConfigurationException, SAXException, TransformerException {
    InputStream in = IOUtils.toInputStream(xmlDesc);
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(in);
    // Get the root element
    Node domainNode = doc.getFirstChild();
    NodeList domainChildNodes = domainNode.getChildNodes();
    for (int i = 0; i < domainChildNodes.getLength(); i++) {
        Node domainChildNode = domainChildNodes.item(i);
        if ("devices".equals(domainChildNode.getNodeName())) {
            NodeList devicesChildNodes = domainChildNode.getChildNodes();
            for (int x = 0; x < devicesChildNodes.getLength(); x++) {
                Node deviceChildNode = devicesChildNodes.item(x);
                if ("disk".equals(deviceChildNode.getNodeName())) {
                    Node diskNode = deviceChildNode;
                    String sourceText = getSourceText(diskNode);
                    String path = getPathFromSourceText(migrateStorage.keySet(), sourceText);
                    if (path != null) {
                        MigrateCommand.MigrateDiskInfo migrateDiskInfo = migrateStorage.get(path);
                        NamedNodeMap diskNodeAttributes = diskNode.getAttributes();
                        Node diskNodeAttribute = diskNodeAttributes.getNamedItem("type");
                        diskNodeAttribute.setTextContent(migrateDiskInfo.getDiskType().toString());
                        NodeList diskChildNodes = diskNode.getChildNodes();
                        for (int z = 0; z < diskChildNodes.getLength(); z++) {
                            Node diskChildNode = diskChildNodes.item(z);
                            if (migrateStorageManaged && "driver".equals(diskChildNode.getNodeName())) {
                                Node driverNode = diskChildNode;
                                NamedNodeMap driverNodeAttributes = driverNode.getAttributes();
                                Node driverNodeAttribute = driverNodeAttributes.getNamedItem("type");
                                driverNodeAttribute.setTextContent(migrateDiskInfo.getDriverType().toString());
                            } else if ("source".equals(diskChildNode.getNodeName())) {
                                diskNode.removeChild(diskChildNode);
                                Element newChildSourceNode = doc.createElement("source");
                                newChildSourceNode.setAttribute(migrateDiskInfo.getSource().toString(), migrateDiskInfo.getSourceText());
                                diskNode.appendChild(newChildSourceNode);
                            } else if (migrateStorageManaged && "auth".equals(diskChildNode.getNodeName())) {
                                diskNode.removeChild(diskChildNode);
                            }
                        }
                    }
                }
            }
        }
    }
    return getXml(doc);
}
Also used : MigrateDiskInfo(com.cloud.agent.api.MigrateCommand.MigrateDiskInfo) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NamedNodeMap(org.w3c.dom.NamedNodeMap) DocumentBuilder(javax.xml.parsers.DocumentBuilder) InputStream(java.io.InputStream) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) MigrateCommand(com.cloud.agent.api.MigrateCommand)

Example 2 with MigrateDiskInfo

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

the class LibvirtMigrateCommandWrapperTest method testReplaceStorage.

@Test
public void testReplaceStorage() throws Exception {
    Map<String, MigrateDiskInfo> mapMigrateStorage = new HashMap<String, MigrateDiskInfo>();
    MigrateDiskInfo diskInfo = new MigrateDiskInfo("123456", DiskType.BLOCK, DriverType.RAW, Source.FILE, "sourctest");
    mapMigrateStorage.put("/mnt/812ea6a3-7ad0-30f4-9cab-01e3f2985b98/4650a2f7-fce5-48e2-beaa-bcdf063194e6", diskInfo);
    final String result = libvirtMigrateCmdWrapper.replaceStorage(fullfile, mapMigrateStorage, true);
    InputStream in = IOUtils.toInputStream(result);
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(in);
    assertXpath(doc, "/domain/devices/disk/iotune/write_iops_sec", "500");
    assertXpath(doc, "/domain/devices/disk/@type", "block");
    assertXpath(doc, "/domain/devices/disk/driver/@type", "raw");
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) HashMap(java.util.HashMap) DocumentBuilder(javax.xml.parsers.DocumentBuilder) InputStream(java.io.InputStream) MigrateDiskInfo(com.cloud.agent.api.MigrateCommand.MigrateDiskInfo) Document(org.w3c.dom.Document) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with MigrateDiskInfo

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

the class LibvirtMigrateCommandWrapperTest method configureAndVerifyTestSearchDiskDefOnMigrateDiskInfoList.

private void configureAndVerifyTestSearchDiskDefOnMigrateDiskInfoList(String serialNumber, String diskPath, boolean isExpectedDiskInfoNull) {
    MigrateDiskInfo migrateDiskInfo = new MigrateDiskInfo(serialNumber, DiskType.FILE, DriverType.QCOW2, Source.FILE, "sourceText");
    List<MigrateDiskInfo> migrateDiskInfoList = new ArrayList<>();
    migrateDiskInfoList.add(migrateDiskInfo);
    DiskDef disk = new DiskDef();
    disk.setDiskPath(diskPath);
    MigrateDiskInfo returnedMigrateDiskInfo = libvirtMigrateCmdWrapper.searchDiskDefOnMigrateDiskInfoList(migrateDiskInfoList, disk);
    if (isExpectedDiskInfoNull)
        Assert.assertEquals(null, returnedMigrateDiskInfo);
    else
        Assert.assertEquals(migrateDiskInfo, returnedMigrateDiskInfo);
}
Also used : DiskDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef) ArrayList(java.util.ArrayList) MigrateDiskInfo(com.cloud.agent.api.MigrateCommand.MigrateDiskInfo)

Example 4 with MigrateDiskInfo

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

the class LibvirtMigrateCommandWrapperTest method createMigrateDiskInfo.

private MigrateDiskInfo createMigrateDiskInfo(boolean isSourceDiskOnStorageFileSystem) {
    MigrateDiskInfo migrateDiskInfo = new MigrateDiskInfo("serialNumber", DiskType.FILE, DriverType.QCOW2, Source.FILE, "sourceText");
    migrateDiskInfo.setSourceDiskOnStorageFileSystem(isSourceDiskOnStorageFileSystem);
    return migrateDiskInfo;
}
Also used : MigrateDiskInfo(com.cloud.agent.api.MigrateCommand.MigrateDiskInfo)

Example 5 with MigrateDiskInfo

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

the class LibvirtMigrateCommandWrapperTest method deleteOrDisconnectDisksOnSourcePoolTest.

@Test
public void deleteOrDisconnectDisksOnSourcePoolTest() {
    LibvirtMigrateCommandWrapper spyLibvirtMigrateCmdWrapper = PowerMockito.spy(libvirtMigrateCmdWrapper);
    Mockito.doNothing().when(spyLibvirtMigrateCmdWrapper).deleteLocalVolume("volPath");
    List<MigrateDiskInfo> migrateDiskInfoList = new ArrayList<>();
    MigrateDiskInfo migrateDiskInfo0 = createMigrateDiskInfo(true);
    MigrateDiskInfo migrateDiskInfo2 = createMigrateDiskInfo(false);
    List<DiskDef> disks = new ArrayList<>();
    DiskDef diskDef0 = new DiskDef();
    DiskDef diskDef1 = new DiskDef();
    DiskDef diskDef2 = new DiskDef();
    diskDef0.setDiskPath("volPath");
    disks.add(diskDef0);
    disks.add(diskDef1);
    disks.add(diskDef2);
    LibvirtComputingResource libvirtComputingResource = Mockito.spy(new LibvirtComputingResource());
    Mockito.doReturn(true).when(libvirtComputingResource).cleanupDisk(diskDef1);
    Mockito.doReturn(migrateDiskInfo0).when(spyLibvirtMigrateCmdWrapper).searchDiskDefOnMigrateDiskInfoList(migrateDiskInfoList, diskDef0);
    Mockito.doReturn(null).when(spyLibvirtMigrateCmdWrapper).searchDiskDefOnMigrateDiskInfoList(migrateDiskInfoList, diskDef1);
    Mockito.doReturn(migrateDiskInfo2).when(spyLibvirtMigrateCmdWrapper).searchDiskDefOnMigrateDiskInfoList(migrateDiskInfoList, diskDef2);
    spyLibvirtMigrateCmdWrapper.deleteOrDisconnectDisksOnSourcePool(libvirtComputingResource, migrateDiskInfoList, disks);
    InOrder inOrder = Mockito.inOrder(spyLibvirtMigrateCmdWrapper, libvirtComputingResource);
    inOrderVerifyDeleteOrDisconnect(inOrder, spyLibvirtMigrateCmdWrapper, libvirtComputingResource, migrateDiskInfoList, diskDef0, 1, 0);
    inOrderVerifyDeleteOrDisconnect(inOrder, spyLibvirtMigrateCmdWrapper, libvirtComputingResource, migrateDiskInfoList, diskDef1, 0, 1);
    inOrderVerifyDeleteOrDisconnect(inOrder, spyLibvirtMigrateCmdWrapper, libvirtComputingResource, migrateDiskInfoList, diskDef2, 0, 1);
}
Also used : DiskDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef) InOrder(org.mockito.InOrder) ArrayList(java.util.ArrayList) LibvirtComputingResource(com.cloud.hypervisor.kvm.resource.LibvirtComputingResource) MigrateDiskInfo(com.cloud.agent.api.MigrateCommand.MigrateDiskInfo) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

MigrateDiskInfo (com.cloud.agent.api.MigrateCommand.MigrateDiskInfo)7 DiskDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef)3 ArrayList (java.util.ArrayList)3 MigrateAnswer (com.cloud.agent.api.MigrateAnswer)2 MigrateCommand (com.cloud.agent.api.MigrateCommand)2 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)2 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 Answer (com.cloud.agent.api.Answer)1 ModifyTargetsAnswer (com.cloud.agent.api.ModifyTargetsAnswer)1 PrepareForMigrationCommand (com.cloud.agent.api.PrepareForMigrationCommand)1 CopyVolumeAnswer (com.cloud.agent.api.storage.CopyVolumeAnswer)1 MigrateVolumeAnswer (com.cloud.agent.api.storage.MigrateVolumeAnswer)1 DpdkTO (com.cloud.agent.api.to.DpdkTO)1 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)1 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)1 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)1