Search in sources :

Example 11 with FormatInfo

use of com.cloud.storage.template.Processor.FormatInfo in project cloudstack by apache.

the class DownloadManagerImpl method postProcessAfterDownloadComplete.

private String postProcessAfterDownloadComplete(DownloadJob dnld, String resourcePath, String templateName, TemplateLocation loc) {
    Iterator<Processor> en = _processors.values().iterator();
    while (en.hasNext()) {
        Processor processor = en.next();
        FormatInfo info;
        try {
            info = processor.process(resourcePath, null, templateName, this._processTimeout);
        } catch (InternalErrorException e) {
            LOGGER.error("Template process exception ", e);
            return e.toString();
        }
        if (info != null) {
            if (!loc.addFormat(info)) {
                loc.purge();
                return "Unable to install due to invalid file format";
            }
            dnld.setTemplatesize(info.virtualSize);
            dnld.setTemplatePhysicalSize(info.size);
            if (info.ovfInformationTO != null) {
                dnld.setOvfInformationTO(info.ovfInformationTO);
            }
            break;
        }
    }
    if (!loc.save()) {
        LOGGER.warn("Cleaning up because we're unable to save the formats");
        loc.purge();
    }
    return null;
}
Also used : VhdProcessor(com.cloud.storage.template.VhdProcessor) OVAProcessor(com.cloud.storage.template.OVAProcessor) QCOW2Processor(com.cloud.storage.template.QCOW2Processor) TARProcessor(com.cloud.storage.template.TARProcessor) IsoProcessor(com.cloud.storage.template.IsoProcessor) Processor(com.cloud.storage.template.Processor) VmdkProcessor(com.cloud.storage.template.VmdkProcessor) RawImageProcessor(com.cloud.storage.template.RawImageProcessor) InternalErrorException(com.cloud.exception.InternalErrorException) FormatInfo(com.cloud.storage.template.Processor.FormatInfo)

Example 12 with FormatInfo

use of com.cloud.storage.template.Processor.FormatInfo in project cosmic by MissionCriticalCloud.

the class TemplateLocation method load.

public boolean load() throws IOException {
    try (FileInputStream strm = new FileInputStream(_file)) {
        _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 = _props.getProperty(format.getFileExtension());
        if (ext != null) {
            final FormatInfo info = new FormatInfo();
            info.format = format;
            info.filename = _props.getProperty(format.getFileExtension() + ".filename");
            if (info.filename == null) {
                continue;
            }
            info.size = NumbersUtil.parseLong(_props.getProperty(format.getFileExtension() + ".size"), -1);
            _props.setProperty("physicalSize", Long.toString(info.size));
            info.virtualSize = NumbersUtil.parseLong(_props.getProperty(format.getFileExtension() + ".virtualsize"), -1);
            _formats.add(info);
            if (!checkFormatValidity(info)) {
                _isCorrupted = true;
                s_logger.warn("Cleaning up inconsistent information for " + format);
            }
        }
    }
    if (_props.getProperty("uniquename") == null || _props.getProperty("virtualsize") == null) {
        return false;
    }
    return (_formats.size() > 0);
}
Also used : IOException(java.io.IOException) FormatInfo(com.cloud.storage.template.Processor.FormatInfo) FileInputStream(java.io.FileInputStream) ImageFormat(com.cloud.storage.Storage.ImageFormat)

Example 13 with FormatInfo

use of com.cloud.storage.template.Processor.FormatInfo in project cosmic by MissionCriticalCloud.

the class TemplateLocation method save.

public boolean save() {
    for (final FormatInfo info : _formats) {
        _props.setProperty(info.format.getFileExtension(), "true");
        _props.setProperty(info.format.getFileExtension() + ".filename", info.filename);
        _props.setProperty(info.format.getFileExtension() + ".size", Long.toString(info.size));
        _props.setProperty(info.format.getFileExtension() + ".virtualsize", Long.toString(info.virtualSize));
    }
    try (FileOutputStream strm = new FileOutputStream(_file)) {
        _props.store(strm, "");
    } catch (final IOException e) {
        s_logger.warn("Unable to save the template properties ", e);
        return false;
    }
    return true;
}
Also used : FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FormatInfo(com.cloud.storage.template.Processor.FormatInfo)

Example 14 with FormatInfo

use of com.cloud.storage.template.Processor.FormatInfo 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 FormatInfo 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.hypervisor.kvm.storage.KvmStoragePool) StorageLayer(com.cloud.storage.StorageLayer) Processor(com.cloud.storage.template.Processor) InternalErrorException(com.cloud.exception.InternalErrorException) IOException(java.io.IOException) ConfigurationException(javax.naming.ConfigurationException) TemplateLocation(com.cloud.storage.template.TemplateLocation) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) KvmStoragePoolManager(com.cloud.hypervisor.kvm.storage.KvmStoragePoolManager) CreatePrivateTemplateAnswer(com.cloud.agent.api.storage.CreatePrivateTemplateAnswer) KvmPhysicalDisk(com.cloud.hypervisor.kvm.storage.KvmPhysicalDisk) FormatInfo(com.cloud.storage.template.Processor.FormatInfo)

Example 15 with FormatInfo

use of com.cloud.storage.template.Processor.FormatInfo in project cloudstack by apache.

the class TemplateLocation method load.

public boolean load() throws IOException {
    try (FileInputStream strm = new FileInputStream(_file)) {
        _props.load(strm);
    } catch (IOException e) {
        s_logger.warn("Unable to load the template properties for '" + _file + "': ", e);
    }
    for (ImageFormat format : ImageFormat.values()) {
        String currentExtension = format.getFileExtension();
        String ext = _props.getProperty(currentExtension);
        if (ext != null) {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("File extension '" + currentExtension + "' was found in '" + _file + "'.");
            }
            FormatInfo info = new FormatInfo();
            info.format = format;
            info.filename = _props.getProperty(currentExtension + ".filename");
            if (info.filename == null) {
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("Property '" + currentExtension + ".filename' was not found in '" + _file + "'. Current format is ignored.");
                }
                continue;
            }
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Property '" + currentExtension + ".filename' was found in '" + _file + "'. Current format will be parsed.");
            }
            info.size = NumbersUtil.parseLong(_props.getProperty(currentExtension + ".size"), -1);
            _props.setProperty("physicalSize", Long.toString(info.size));
            info.virtualSize = NumbersUtil.parseLong(_props.getProperty(currentExtension + ".virtualsize"), -1);
            _formats.add(info);
            if (!checkFormatValidity(info)) {
                _isCorrupted = true;
                s_logger.warn("Cleaning up inconsistent information for " + format);
            }
        } else {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Format extension '" + currentExtension + "' wasn't found in '" + _file + "'.");
            }
        }
    }
    if (_props.getProperty("uniquename") == null || _props.getProperty("virtualsize") == null) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Property 'uniquename' or 'virtualsize' weren't found in '" + _file + "'. Loading failed.");
        }
        return false;
    }
    return (_formats.size() > 0);
}
Also used : IOException(java.io.IOException) FormatInfo(com.cloud.storage.template.Processor.FormatInfo) FileInputStream(java.io.FileInputStream) ImageFormat(com.cloud.storage.Storage.ImageFormat)

Aggregations

FormatInfo (com.cloud.storage.template.Processor.FormatInfo)28 InternalErrorException (com.cloud.exception.InternalErrorException)22 Processor (com.cloud.storage.template.Processor)21 IOException (java.io.IOException)21 TemplateLocation (com.cloud.storage.template.TemplateLocation)20 ConfigurationException (javax.naming.ConfigurationException)17 Script (com.cloud.utils.script.Script)14 QCOW2Processor (com.cloud.storage.template.QCOW2Processor)13 StorageLayer (com.cloud.storage.StorageLayer)10 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)9 File (java.io.File)9 HashMap (java.util.HashMap)9 FileOutputStream (java.io.FileOutputStream)7 Answer (com.cloud.agent.api.Answer)6 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)6 CreatePrivateTemplateFromSnapshotCommand (com.cloud.agent.api.CreatePrivateTemplateFromSnapshotCommand)6 CreatePrivateTemplateAnswer (com.cloud.agent.api.storage.CreatePrivateTemplateAnswer)6 RawImageProcessor (com.cloud.storage.template.RawImageProcessor)6 TARProcessor (com.cloud.storage.template.TARProcessor)6 VhdProcessor (com.cloud.storage.template.VhdProcessor)6