Search in sources :

Example 16 with TemplateFormatInfo

use of com.cloud.legacymodel.storage.TemplateFormatInfo in project cosmic by MissionCriticalCloud.

the class TemplateLocation method load.

public boolean load() throws IOException {
    try (final FileInputStream strm = new FileInputStream(this._file)) {
        this._props.load(strm);
    } catch (final IOException e) {
        s_logger.warn("Unable to load the template properties", e);
    }
    for (final ImageFormat format : ImageFormat.values()) {
        final String ext = this._props.getProperty(format.toString().toLowerCase());
        if (ext != null) {
            final TemplateFormatInfo info = new TemplateFormatInfo();
            info.format = format;
            info.filename = this._props.getProperty(format.toString().toLowerCase() + ".filename");
            if (info.filename == null) {
                continue;
            }
            info.size = NumbersUtil.parseLong(this._props.getProperty(format.toString().toLowerCase() + ".size"), -1);
            this._props.setProperty("physicalSize", Long.toString(info.size));
            info.virtualSize = NumbersUtil.parseLong(this._props.getProperty(format.toString().toLowerCase() + ".virtualsize"), -1);
            this._formats.add(info);
            if (!checkFormatValidity(info)) {
                this._isCorrupted = true;
                s_logger.warn("Cleaning up inconsistent information for " + format);
            }
        }
    }
    if (this._props.getProperty("uniquename") == null || this._props.getProperty("virtualsize") == null) {
        return false;
    }
    return (this._formats.size() > 0);
}
Also used : TemplateFormatInfo(com.cloud.legacymodel.storage.TemplateFormatInfo) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ImageFormat(com.cloud.model.enumeration.ImageFormat)

Example 17 with TemplateFormatInfo

use of com.cloud.legacymodel.storage.TemplateFormatInfo in project cosmic by MissionCriticalCloud.

the class LibvirtCreatePrivateTemplateFromVolumeCommandWrapper method execute.

@Override
public Answer execute(final CreatePrivateTemplateFromVolumeCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final String secondaryStorageUrl = command.getSecondaryStorageUrl();
    KvmStoragePool secondaryStorage = null;
    KvmStoragePool primary = null;
    final KvmStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
    try {
        final String templateFolder = command.getAccountId() + File.separator + command.getTemplateId() + File.separator;
        final String templateInstallFolder = "/template/tmpl/" + templateFolder;
        secondaryStorage = storagePoolMgr.getStoragePoolByUri(secondaryStorageUrl);
        try {
            primary = storagePoolMgr.getStoragePool(command.getPool().getType(), command.getPrimaryStoragePoolNameLabel());
        } catch (final CloudRuntimeException e) {
            if (e.getMessage().contains("not found")) {
                primary = storagePoolMgr.createStoragePool(command.getPool().getUuid(), command.getPool().getHost(), command.getPool().getPort(), command.getPool().getPath(), command.getPool().getUserInfo(), command.getPool().getType());
            } else {
                return new CreatePrivateTemplateAnswer(command, false, e.getMessage());
            }
        }
        final KvmPhysicalDisk disk = primary.getPhysicalDisk(command.getVolumePath());
        final String tmpltPath = secondaryStorage.getLocalPath() + File.separator + templateInstallFolder;
        final StorageLayer storage = libvirtComputingResource.getStorage();
        storage.mkdirs(tmpltPath);
        if (primary.getType() != StoragePoolType.RBD) {
            final String createTmplPath = libvirtComputingResource.createTmplPath();
            final int cmdsTimeout = libvirtComputingResource.getCmdsTimeout();
            final Script scriptCommand = new Script(createTmplPath, cmdsTimeout, s_logger);
            scriptCommand.add("-f", disk.getPath());
            scriptCommand.add("-t", tmpltPath);
            scriptCommand.add("-n", command.getUniqueName() + ".qcow2");
            final String result = scriptCommand.execute();
            if (result != null) {
                s_logger.debug("failed to create template: " + result);
                return new CreatePrivateTemplateAnswer(command, false, result);
            }
        } else {
            s_logger.debug("Converting RBD disk " + disk.getPath() + " into template " + command.getUniqueName());
            final QemuImgFile srcFile = new QemuImgFile(KvmPhysicalDisk.rbdStringBuilder(primary.getSourceHost(), primary.getSourcePort(), primary.getAuthUserName(), primary.getAuthSecret(), disk.getPath()));
            srcFile.setFormat(PhysicalDiskFormat.RAW);
            final QemuImgFile destFile = new QemuImgFile(tmpltPath + "/" + command.getUniqueName() + ".qcow2");
            destFile.setFormat(PhysicalDiskFormat.QCOW2);
            final QemuImg q = new QemuImg(0);
            try {
                q.convert(srcFile, destFile);
            } catch (final QemuImgException e) {
                s_logger.error("Failed to create new template while converting " + srcFile.getFileName() + " to " + destFile.getFileName() + " the error was: " + e.getMessage());
            }
            final File templateProp = new File(tmpltPath + "/template.properties");
            if (!templateProp.exists()) {
                templateProp.createNewFile();
            }
            String templateContent = "filename=" + command.getUniqueName() + ".qcow2" + System.getProperty("line.separator");
            final DateFormat dateFormat = new SimpleDateFormat("MM_dd_yyyy");
            final Date date = new Date();
            templateContent += "snapshot.name=" + dateFormat.format(date) + System.getProperty("line.separator");
            try (final FileOutputStream templFo = new FileOutputStream(templateProp)) {
                templFo.write(templateContent.getBytes("UTF-8"));
                templFo.flush();
            } catch (final IOException ex) {
                s_logger.error("CreatePrivateTemplateAnswer:Exception:" + ex.getMessage());
            }
        }
        final Map<String, Object> params = new HashMap<>();
        params.put(StorageLayer.InstanceConfigKey, storage);
        final Processor qcow2Processor = new QCOW2Processor();
        qcow2Processor.configure("QCOW2 Processor", params);
        final TemplateFormatInfo info = qcow2Processor.process(tmpltPath, null, command.getUniqueName());
        final TemplateLocation loc = new TemplateLocation(storage, tmpltPath);
        loc.create(1, true, command.getUniqueName());
        loc.addFormat(info);
        loc.save();
        return new CreatePrivateTemplateAnswer(command, true, null, templateInstallFolder + command.getUniqueName() + ".qcow2", info.virtualSize, info.size, command.getUniqueName(), ImageFormat.QCOW2);
    } catch (final InternalErrorException e) {
        return new CreatePrivateTemplateAnswer(command, false, e.toString());
    } catch (final IOException e) {
        return new CreatePrivateTemplateAnswer(command, false, e.toString());
    } catch (final ConfigurationException e) {
        return new CreatePrivateTemplateAnswer(command, false, e.toString());
    } catch (final CloudRuntimeException e) {
        return new CreatePrivateTemplateAnswer(command, false, e.toString());
    } finally {
        if (secondaryStorage != null) {
            storagePoolMgr.deleteStoragePool(secondaryStorage.getType(), secondaryStorage.getUuid());
        }
    }
}
Also used : KvmStoragePool(com.cloud.agent.resource.kvm.storage.KvmStoragePool) StorageLayer(com.cloud.utils.storage.StorageLayer) QCOW2Processor(com.cloud.common.storageprocessor.QCOW2Processor) Processor(com.cloud.common.storageprocessor.Processor) HashMap(java.util.HashMap) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) TemplateLocation(com.cloud.common.storageprocessor.TemplateLocation) QemuImgException(com.cloud.agent.resource.kvm.storage.utils.QemuImgException) KvmStoragePoolManager(com.cloud.agent.resource.kvm.storage.KvmStoragePoolManager) KvmPhysicalDisk(com.cloud.agent.resource.kvm.storage.KvmPhysicalDisk) Script(com.cloud.utils.script.Script) IOException(java.io.IOException) InternalErrorException(com.cloud.legacymodel.exceptions.InternalErrorException) Date(java.util.Date) QemuImg(com.cloud.agent.resource.kvm.storage.utils.QemuImg) QCOW2Processor(com.cloud.common.storageprocessor.QCOW2Processor) QemuImgFile(com.cloud.agent.resource.kvm.storage.utils.QemuImgFile) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) FileOutputStream(java.io.FileOutputStream) TemplateFormatInfo(com.cloud.legacymodel.storage.TemplateFormatInfo) CreatePrivateTemplateAnswer(com.cloud.legacymodel.communication.answer.CreatePrivateTemplateAnswer) File(java.io.File) QemuImgFile(com.cloud.agent.resource.kvm.storage.utils.QemuImgFile) SimpleDateFormat(java.text.SimpleDateFormat)

Example 18 with TemplateFormatInfo

use of com.cloud.legacymodel.storage.TemplateFormatInfo in project cosmic by MissionCriticalCloud.

the class LibvirtCreatePrivateTemplateFromSnapshotCommandWrapper method execute.

@Override
public Answer execute(final CreatePrivateTemplateFromSnapshotCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
    final String templateFolder = command.getAccountId() + File.separator + command.getNewTemplateId();
    final String templateInstallFolder = "template/tmpl/" + templateFolder;
    final String tmplName = libvirtUtilitiesHelper.generateUuidName();
    final String tmplFileName = tmplName + ".qcow2";
    KvmStoragePool secondaryPool = null;
    KvmStoragePool snapshotPool = null;
    final KvmStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
    try {
        String snapshotPath = command.getSnapshotUuid();
        final int index = snapshotPath.lastIndexOf("/");
        snapshotPath = snapshotPath.substring(0, index);
        snapshotPool = storagePoolMgr.getStoragePoolByUri(command.getSecondaryStorageUrl() + snapshotPath);
        secondaryPool = storagePoolMgr.getStoragePoolByUri(command.getSecondaryStorageUrl());
        final KvmPhysicalDisk snapshot = snapshotPool.getPhysicalDisk(command.getSnapshotName());
        final String templatePath = secondaryPool.getLocalPath() + File.separator + templateInstallFolder;
        final StorageLayer storage = libvirtComputingResource.getStorage();
        storage.mkdirs(templatePath);
        final String tmplPath = templateInstallFolder + File.separator + tmplFileName;
        final String createTmplPath = libvirtComputingResource.createTmplPath();
        final int cmdsTimeout = libvirtComputingResource.getCmdsTimeout();
        final Script scriptCommand = new Script(createTmplPath, cmdsTimeout, s_logger);
        scriptCommand.add("-t", templatePath);
        scriptCommand.add("-n", tmplFileName);
        scriptCommand.add("-f", snapshot.getPath());
        scriptCommand.execute();
        final Processor qcow2Processor = libvirtUtilitiesHelper.buildQcow2Processor(storage);
        final TemplateFormatInfo info = qcow2Processor.process(templatePath, null, tmplName);
        final TemplateLocation loc = libvirtUtilitiesHelper.buildTemplateLocation(storage, templatePath);
        loc.create(1, true, tmplName);
        loc.addFormat(info);
        loc.save();
        return new CreatePrivateTemplateAnswer(command, true, "", tmplPath, info.virtualSize, info.size, tmplName, info.format);
    } catch (final ConfigurationException e) {
        return new CreatePrivateTemplateAnswer(command, false, e.getMessage());
    } catch (final InternalErrorException e) {
        return new CreatePrivateTemplateAnswer(command, false, e.getMessage());
    } catch (final IOException e) {
        return new CreatePrivateTemplateAnswer(command, false, e.getMessage());
    } catch (final CloudRuntimeException e) {
        return new CreatePrivateTemplateAnswer(command, false, e.getMessage());
    } finally {
        if (secondaryPool != null) {
            storagePoolMgr.deleteStoragePool(secondaryPool.getType(), secondaryPool.getUuid());
        }
        if (snapshotPool != null) {
            storagePoolMgr.deleteStoragePool(snapshotPool.getType(), snapshotPool.getUuid());
        }
    }
}
Also used : Script(com.cloud.utils.script.Script) KvmStoragePool(com.cloud.agent.resource.kvm.storage.KvmStoragePool) StorageLayer(com.cloud.utils.storage.StorageLayer) Processor(com.cloud.common.storageprocessor.Processor) InternalErrorException(com.cloud.legacymodel.exceptions.InternalErrorException) IOException(java.io.IOException) ConfigurationException(javax.naming.ConfigurationException) TemplateLocation(com.cloud.common.storageprocessor.TemplateLocation) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) KvmStoragePoolManager(com.cloud.agent.resource.kvm.storage.KvmStoragePoolManager) TemplateFormatInfo(com.cloud.legacymodel.storage.TemplateFormatInfo) CreatePrivateTemplateAnswer(com.cloud.legacymodel.communication.answer.CreatePrivateTemplateAnswer) KvmPhysicalDisk(com.cloud.agent.resource.kvm.storage.KvmPhysicalDisk)

Example 19 with TemplateFormatInfo

use of com.cloud.legacymodel.storage.TemplateFormatInfo in project cosmic by MissionCriticalCloud.

the class VhdProcessorTest method testProcess.

@Test
public void testProcess() throws Exception {
    final String templatePath = "/tmp";
    final String templateName = "template";
    final long virtualSize = 2000;
    final long actualSize = 1000;
    Mockito.when(this.mockStorageLayer.exists(Mockito.anyString())).thenReturn(true);
    final File mockFile = Mockito.mock(File.class);
    Mockito.when(this.mockStorageLayer.getFile(Mockito.anyString())).thenReturn(mockFile);
    Mockito.when(this.mockStorageLayer.getSize(Mockito.anyString())).thenReturn(actualSize);
    Mockito.doReturn(virtualSize).when(this.processor).getTemplateVirtualSize((File) Mockito.any());
    final TemplateFormatInfo info = this.processor.process(templatePath, null, templateName);
    Assert.assertEquals(ImageFormat.VHD, info.format);
    Assert.assertEquals(actualSize, info.size);
    Assert.assertEquals(virtualSize, info.virtualSize);
    Assert.assertEquals(templateName + ".vhd", info.filename);
}
Also used : TemplateFormatInfo(com.cloud.legacymodel.storage.TemplateFormatInfo) File(java.io.File) Test(org.junit.Test)

Aggregations

TemplateFormatInfo (com.cloud.legacymodel.storage.TemplateFormatInfo)19 InternalErrorException (com.cloud.legacymodel.exceptions.InternalErrorException)11 IOException (java.io.IOException)11 Processor (com.cloud.common.storageprocessor.Processor)9 TemplateLocation (com.cloud.common.storageprocessor.TemplateLocation)9 File (java.io.File)9 ConfigurationException (javax.naming.ConfigurationException)7 Script (com.cloud.utils.script.Script)6 KvmPhysicalDisk (com.cloud.agent.resource.kvm.storage.KvmPhysicalDisk)5 KvmStoragePool (com.cloud.agent.resource.kvm.storage.KvmStoragePool)5 KvmStoragePoolManager (com.cloud.agent.resource.kvm.storage.KvmStoragePoolManager)5 QCOW2Processor (com.cloud.common.storageprocessor.QCOW2Processor)5 StorageLayer (com.cloud.utils.storage.StorageLayer)5 Test (org.junit.Test)5 NfsStoragePool (com.cloud.agent.resource.kvm.ha.KvmHaBase.NfsStoragePool)3 LibvirtRequestWrapper (com.cloud.agent.resource.kvm.wrapper.LibvirtRequestWrapper)3 LibvirtUtilitiesHelper (com.cloud.agent.resource.kvm.wrapper.LibvirtUtilitiesHelper)3 RawImageProcessor (com.cloud.common.storageprocessor.RawImageProcessor)3 TARProcessor (com.cloud.common.storageprocessor.TARProcessor)3 VhdProcessor (com.cloud.common.storageprocessor.VhdProcessor)3