Search in sources :

Example 1 with OVFHelper

use of com.cloud.agent.api.storage.OVFHelper in project cloudstack by apache.

the class NfsSecondaryStorageResource method execute.

public Answer execute(CreateDatadiskTemplateCommand cmd) {
    TemplateObjectTO diskTemplate = new TemplateObjectTO();
    TemplateObjectTO dataDiskTemplate = (TemplateObjectTO) cmd.getDataDiskTemplate();
    DataStoreTO dataStore = dataDiskTemplate.getDataStore();
    if (!(dataStore instanceof NfsTO)) {
        return new CreateDatadiskTemplateAnswer("Unsupported protocol");
    }
    NfsTO nfsImageStore = (NfsTO) dataStore;
    String secondaryStorageUrl = nfsImageStore.getUrl();
    assert (secondaryStorageUrl != null);
    try {
        String secondaryMountPoint = getRootDir(secondaryStorageUrl, _nfsVersion);
        long templateId = dataDiskTemplate.getId();
        String templateUniqueName = dataDiskTemplate.getUniqueName();
        String origDisk = cmd.getPath();
        long virtualSize = dataDiskTemplate.getSize();
        String diskName = origDisk.substring((origDisk.lastIndexOf(File.separator)) + 1);
        long physicalSize = new File(origDisk).length();
        String newTmplDir = getTemplateRelativeDirInSecStorage(dataDiskTemplate.getAccountId(), dataDiskTemplate.getId());
        String newTmplDirAbsolute = secondaryMountPoint + File.separator + newTmplDir;
        String ovfFilePath = getOVFFilePath(origDisk);
        if (!cmd.getBootable()) {
            // Create folder to hold datadisk template
            synchronized (newTmplDir.intern()) {
                Script command = new Script("mkdir", _timeout, s_logger);
                command.add("-p");
                command.add(newTmplDirAbsolute);
                String result = command.execute();
                if (result != null) {
                    String msg = "Unable to prepare template directory: " + newTmplDir + ", storage: " + secondaryStorageUrl + ", error msg: " + result;
                    s_logger.error(msg);
                    throw new Exception(msg);
                }
            }
            // Move Datadisk VMDK from parent template folder to Datadisk template folder
            synchronized (origDisk.intern()) {
                Script command = new Script("mv", _timeout, s_logger);
                command.add(origDisk);
                command.add(newTmplDirAbsolute);
                String result = command.execute();
                if (result != null) {
                    String msg = "Unable to copy VMDK from parent template folder to datadisk template folder" + ", error msg: " + result;
                    s_logger.error(msg);
                    throw new Exception(msg);
                }
                command = new Script("cp", _timeout, s_logger);
                command.add(ovfFilePath + ORIGINAL_FILE_EXTENSION);
                command.add(newTmplDirAbsolute);
                result = command.execute();
                if (result != null) {
                    String msg = "Unable to copy VMDK from parent template folder to datadisk template folder" + ", error msg: " + result;
                    s_logger.error(msg);
                    throw new Exception(msg);
                }
            }
        }
        // Create OVF for the disk
        String newOvfFilePath = newTmplDirAbsolute + File.separator + ovfFilePath.substring(ovfFilePath.lastIndexOf(File.separator) + 1);
        OVFHelper ovfHelper = new OVFHelper();
        ovfHelper.rewriteOVFFileForSingleDisk(ovfFilePath + ORIGINAL_FILE_EXTENSION, newOvfFilePath, diskName);
        postCreatePrivateTemplate(newTmplDirAbsolute, templateId, templateUniqueName, physicalSize, virtualSize);
        writeMetaOvaForTemplate(newTmplDirAbsolute, ovfFilePath.substring(ovfFilePath.lastIndexOf(File.separator) + 1), diskName, templateUniqueName, physicalSize);
        diskTemplate.setId(templateId);
        if (diskName.endsWith("iso")) {
            diskTemplate.setPath(newTmplDir + File.separator + diskName);
        } else {
            diskTemplate.setPath(newTmplDir + File.separator + templateUniqueName + ".ova");
        }
        diskTemplate.setSize(virtualSize);
        diskTemplate.setPhysicalSize(physicalSize);
    } catch (Exception e) {
        String msg = "Create Datadisk template failed due to " + e.getMessage();
        s_logger.error(msg, e);
        return new CreateDatadiskTemplateAnswer(msg);
    }
    return new CreateDatadiskTemplateAnswer(diskTemplate);
}
Also used : Script(com.cloud.utils.script.Script) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) CreateDatadiskTemplateAnswer(com.cloud.agent.api.storage.CreateDatadiskTemplateAnswer) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) OVFHelper(com.cloud.agent.api.storage.OVFHelper) NfsTO(com.cloud.agent.api.to.NfsTO) File(java.io.File) S3Utils.putFile(com.cloud.utils.storage.S3.S3Utils.putFile) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException)

Example 2 with OVFHelper

use of com.cloud.agent.api.storage.OVFHelper in project cloudstack by apache.

the class NfsSecondaryStorageResource method execute.

public Answer execute(GetDatadisksCommand cmd) {
    DataTO srcData = cmd.getData();
    String configurationId = cmd.getConfigurationId();
    TemplateObjectTO template = (TemplateObjectTO) srcData;
    DataStoreTO srcStore = srcData.getDataStore();
    if (!(srcStore instanceof NfsTO)) {
        return new CreateDatadiskTemplateAnswer("Unsupported protocol");
    }
    NfsTO nfsImageStore = (NfsTO) srcStore;
    String secondaryStorageUrl = nfsImageStore.getUrl();
    assert (secondaryStorageUrl != null);
    String templateUrl = secondaryStorageUrl + File.separator + srcData.getPath();
    Pair<String, String> templateInfo = decodeTemplateRelativePathAndNameFromUrl(secondaryStorageUrl, templateUrl, template.getName());
    String templateRelativeFolderPath = templateInfo.first();
    try {
        String secondaryMountPoint = getRootDir(secondaryStorageUrl, _nfsVersion);
        s_logger.info("MDOVE Secondary storage mount point: " + secondaryMountPoint);
        String srcOVAFileName = getTemplateOnSecStorageFilePath(secondaryMountPoint, templateRelativeFolderPath, templateInfo.second(), ImageFormat.OVA.getFileExtension());
        String ovfFilePath = getOVFFilePath(srcOVAFileName);
        if (ovfFilePath == null) {
            Script command = new Script("tar", 0, s_logger);
            command.add("--no-same-owner");
            command.add("--no-same-permissions");
            command.add("-xf", srcOVAFileName);
            command.setWorkDir(secondaryMountPoint + File.separator + templateRelativeFolderPath);
            s_logger.info("Executing command: " + command.toString());
            String result = command.execute();
            if (result != null) {
                String msg = "Unable to unpack snapshot OVA file at: " + srcOVAFileName;
                s_logger.error(msg);
                throw new Exception(msg);
            }
            command = new Script("chmod", 0, s_logger);
            command.add("-R");
            command.add("666", secondaryMountPoint + File.separator + templateRelativeFolderPath);
            result = command.execute();
            if (result != null) {
                s_logger.warn("Unable to set permissions for " + secondaryMountPoint + File.separator + templateRelativeFolderPath + " due to " + result);
            }
        }
        Script command = new Script("cp", _timeout, s_logger);
        command.add(ovfFilePath);
        command.add(ovfFilePath + ORIGINAL_FILE_EXTENSION);
        String result = command.execute();
        if (result != null) {
            String msg = "Unable to rename original OVF, error msg: " + result;
            s_logger.error(msg);
        }
        s_logger.debug("Reading OVF " + ovfFilePath + " to retrive the number of disks present in OVA");
        OVFHelper ovfHelper = new OVFHelper();
        List<DatadiskTO> disks = ovfHelper.getOVFVolumeInfoFromFile(ovfFilePath, configurationId);
        return new GetDatadisksAnswer(disks);
    } catch (Exception e) {
        String msg = "Get Datadisk Template Count failed due to " + e.getMessage();
        s_logger.error(msg, e);
        return new GetDatadisksAnswer(msg);
    }
}
Also used : Script(com.cloud.utils.script.Script) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) GetDatadisksAnswer(com.cloud.agent.api.storage.GetDatadisksAnswer) NfsTO(com.cloud.agent.api.to.NfsTO) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) DataTO(com.cloud.agent.api.to.DataTO) DatadiskTO(com.cloud.agent.api.to.DatadiskTO) CreateDatadiskTemplateAnswer(com.cloud.agent.api.storage.CreateDatadiskTemplateAnswer) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) OVFHelper(com.cloud.agent.api.storage.OVFHelper)

Example 3 with OVFHelper

use of com.cloud.agent.api.storage.OVFHelper in project cloudstack by apache.

the class OVAProcessor method validateOva.

/**
 * side effect; properties are added to the info
 *
 * @throws InternalErrorException on an invalid ova contents
 */
private void validateOva(String templateFileFullPath, FormatInfo info) throws InternalErrorException {
    String ovfFilePath = getOVFFilePath(templateFileFullPath);
    OVFHelper ovfHelper = new OVFHelper();
    Document doc = ovfHelper.getOvfParser().parseOVFFile(ovfFilePath);
    OVFInformationTO ovfInformationTO = createOvfInformationTO(ovfHelper, doc, ovfFilePath);
    info.ovfInformationTO = ovfInformationTO;
}
Also used : OVFInformationTO(com.cloud.agent.api.to.OVFInformationTO) OVFHelper(com.cloud.agent.api.storage.OVFHelper) Document(org.w3c.dom.Document)

Example 4 with OVFHelper

use of com.cloud.agent.api.storage.OVFHelper in project cloudstack by apache.

the class OVAProcessor method getTemplateVirtualSize.

/**
 * gets the virtual size from the OVF file meta data.
 *
 * @return the accumulative virtual size of the disk definitions in the OVF
 * @throws InternalErrorException
 */
public long getTemplateVirtualSize(String templatePath, String templateName) throws InternalErrorException {
    long virtualSize = 0;
    String templateFileFullPath = templatePath.endsWith(File.separator) ? templatePath : templatePath + File.separator;
    templateFileFullPath += templateName.endsWith(ImageFormat.OVA.getFileExtension()) ? templateName : templateName + "." + ImageFormat.OVA.getFileExtension();
    String ovfFileName = getOVFFilePath(templateFileFullPath);
    OVFHelper ovfHelper = new OVFHelper();
    if (ovfFileName == null) {
        String msg = "Unable to locate OVF file in template package directory: " + templatePath;
        LOGGER.error(msg);
        throw new InternalErrorException(msg);
    }
    try {
        Document ovfDoc = ovfHelper.getOvfParser().parseOVFFile(ovfFileName);
        NodeList diskElements = ovfHelper.getOvfParser().getElementsFromOVFDocument(ovfDoc, "Disk");
        for (int i = 0; i < diskElements.getLength(); i++) {
            Element disk = (Element) diskElements.item(i);
            String diskSizeValue = disk.getAttribute("ovf:capacity");
            long diskSize = 1;
            try {
                diskSize = Long.parseLong(diskSizeValue);
            } catch (NumberFormatException e) {
                // ASSUMEably the diskSize contains a property for replacement
                LOGGER.warn(String.format("the disksize for disk %s is not a valid number: %s", disk.getAttribute("diskId"), diskSizeValue));
            // TODO parse the property to get any value can not be done at registration time
            // and will have to be done at deploytime, so for orchestration purposes
            // we now assume, a value of one
            }
            String allocationUnits = disk.getAttribute("ovf:capacityAllocationUnits");
            diskSize = OVFHelper.getDiskVirtualSize(diskSize, allocationUnits, ovfFileName);
            virtualSize += diskSize;
        }
        return virtualSize;
    } catch (InternalErrorException | NumberFormatException e) {
        String msg = "getTemplateVirtualSize: Unable to parse OVF XML document " + templatePath + " to get the virtual disk " + templateName + " size due to " + e;
        LOGGER.error(msg);
        throw new InternalErrorException(msg);
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) OVFHelper(com.cloud.agent.api.storage.OVFHelper) InternalErrorException(com.cloud.exception.InternalErrorException) Document(org.w3c.dom.Document)

Aggregations

OVFHelper (com.cloud.agent.api.storage.OVFHelper)4 InternalErrorException (com.cloud.exception.InternalErrorException)3 CreateDatadiskTemplateAnswer (com.cloud.agent.api.storage.CreateDatadiskTemplateAnswer)2 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)2 NfsTO (com.cloud.agent.api.to.NfsTO)2 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 Script (com.cloud.utils.script.Script)2 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 UnknownHostException (java.net.UnknownHostException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 ConfigurationException (javax.naming.ConfigurationException)2 TemplateObjectTO (org.apache.cloudstack.storage.to.TemplateObjectTO)2 Document (org.w3c.dom.Document)2 GetDatadisksAnswer (com.cloud.agent.api.storage.GetDatadisksAnswer)1 DataTO (com.cloud.agent.api.to.DataTO)1 DatadiskTO (com.cloud.agent.api.to.DatadiskTO)1 OVFInformationTO (com.cloud.agent.api.to.OVFInformationTO)1 S3Utils.putFile (com.cloud.utils.storage.S3.S3Utils.putFile)1