Search in sources :

Example 6 with OVAProcessor

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

the class DownloadManagerImpl method gatherTemplateInfo.

@Override
public Map<String, TemplateProp> gatherTemplateInfo(String rootDir) {
    Map<String, TemplateProp> result = new HashMap<String, TemplateProp>();
    String templateDir = rootDir + File.separator + _templateDir;
    if (!_storage.exists(templateDir)) {
        _storage.mkdirs(templateDir);
    }
    List<String> publicTmplts = listTemplates(templateDir);
    for (String tmplt : publicTmplts) {
        String path = tmplt.substring(0, tmplt.lastIndexOf(File.separator));
        TemplateLocation loc = new TemplateLocation(_storage, path);
        try {
            if (!loc.load()) {
                s_logger.warn("Post download installation was not completed for " + path);
                // loc.purge();
                _storage.cleanup(path, templateDir);
                continue;
            }
        } catch (IOException e) {
            s_logger.warn("Unable to load template location " + path, e);
            continue;
        }
        TemplateProp tInfo = loc.getTemplateInfo();
        if ((tInfo.getSize() == tInfo.getPhysicalSize()) && (tInfo.getInstallPath().endsWith(ImageFormat.OVA.getFileExtension()))) {
            try {
                Processor processor = _processors.get("OVA Processor");
                OVAProcessor vmdkProcessor = (OVAProcessor) processor;
                long vSize = vmdkProcessor.getTemplateVirtualSize(path, tInfo.getInstallPath().substring(tInfo.getInstallPath().lastIndexOf(File.separator) + 1));
                tInfo.setSize(vSize);
                loc.updateVirtualSize(vSize);
                loc.save();
            } catch (Exception e) {
                s_logger.error("Unable to get the virtual size of the template: " + tInfo.getInstallPath() + " due to " + e.getMessage());
            }
        }
        result.put(tInfo.getTemplateName(), tInfo);
        s_logger.debug("Added template name: " + tInfo.getTemplateName() + ", path: " + tmplt);
    }
    /*
        for (String tmplt : isoTmplts) {
            String tmp[];
            tmp = tmplt.split("/");
            String tmpltName = tmp[tmp.length - 2];
            tmplt = tmplt.substring(tmplt.lastIndexOf("iso/"));
            TemplateInfo tInfo = new TemplateInfo(tmpltName, tmplt, false);
            s_logger.debug("Added iso template name: " + tmpltName + ", path: " + tmplt);
            result.put(tmpltName, tInfo);
        }
         */
    return result;
}
Also used : TemplateProp(com.cloud.storage.template.TemplateProp) 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) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) OVAProcessor(com.cloud.storage.template.OVAProcessor) TemplateLocation(com.cloud.storage.template.TemplateLocation) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException)

Example 7 with OVAProcessor

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

the class VmwareStorageManagerImpl method createTemplateFromSnapshot.

private Ternary<String, Long, Long> createTemplateFromSnapshot(long accountId, long templateId, String templateUniqueName, String secStorageUrl, long volumeId, String backedUpSnapshotUuid, Integer nfsVersion) throws Exception {
    String secondaryMountPoint = _mountService.getMountPoint(secStorageUrl, nfsVersion);
    String installPath = getTemplateRelativeDirInSecStorage(accountId, templateId);
    String installFullPath = secondaryMountPoint + "/" + installPath;
    //Note: volss for tmpl
    String installFullOVAName = installFullPath + "/" + templateUniqueName + ".ova";
    String snapshotRoot = secondaryMountPoint + "/" + getSnapshotRelativeDirInSecStorage(accountId, volumeId);
    String snapshotFullOVAName = snapshotRoot + "/" + backedUpSnapshotUuid + ".ova";
    String snapshotFullOvfName = snapshotRoot + "/" + backedUpSnapshotUuid + ".ovf";
    String result;
    Script command;
    String templateVMDKName = "";
    //String snapshotFullVMDKName = snapshotRoot + "/";
    // the backedUpSnapshotUuid field currently has the format: uuid/uuid. so we need to extract the uuid out
    String backupSSUuid = backedUpSnapshotUuid.substring(0, backedUpSnapshotUuid.indexOf('/'));
    String snapshotFullVMDKName = snapshotRoot + "/" + backupSSUuid + "/";
    synchronized (installPath.intern()) {
        command = new Script(false, "mkdir", _timeout, s_logger);
        command.add("-p");
        command.add(installFullPath);
        result = command.execute();
        if (result != null) {
            String msg = "unable to prepare template directory: " + installPath + ", storage: " + secStorageUrl + ", error msg: " + result;
            s_logger.error(msg);
            throw new Exception(msg);
        }
    }
    try {
        if (new File(snapshotFullOVAName).exists()) {
            command = new Script(false, "cp", _timeout, s_logger);
            command.add(snapshotFullOVAName);
            command.add(installFullOVAName);
            result = command.execute();
            if (result != null) {
                String msg = "unable to copy snapshot " + snapshotFullOVAName + " to " + installFullPath;
                s_logger.error(msg);
                throw new Exception(msg);
            }
            // untar OVA file at template directory
            command = new Script("tar", 0, s_logger);
            command.add("--no-same-owner");
            command.add("-xf", installFullOVAName);
            command.setWorkDir(installFullPath);
            s_logger.info("Executing command: " + command.toString());
            result = command.execute();
            if (result != null) {
                String msg = "unable to untar snapshot " + snapshotFullOVAName + " to " + installFullPath;
                s_logger.error(msg);
                throw new Exception(msg);
            }
        } else {
            // there is no ova file, only ovf originally;
            if (new File(snapshotFullOvfName).exists()) {
                command = new Script(false, "cp", _timeout, s_logger);
                command.add(snapshotFullOvfName);
                //command.add(installFullOvfName);
                command.add(installFullPath);
                result = command.execute();
                if (result != null) {
                    String msg = "unable to copy snapshot " + snapshotFullOvfName + " to " + installFullPath;
                    s_logger.error(msg);
                    throw new Exception(msg);
                }
                s_logger.info("vmdkfile parent dir: " + snapshotFullVMDKName);
                File snapshotdir = new File(snapshotFullVMDKName);
                // File snapshotdir = new File(snapshotRoot);
                File[] ssfiles = snapshotdir.listFiles();
                // List<String> filenames = new ArrayList<String>();
                for (int i = 0; i < ssfiles.length; i++) {
                    String vmdkfile = ssfiles[i].getName();
                    s_logger.info("vmdk file name: " + vmdkfile);
                    if (vmdkfile.toLowerCase().startsWith(backupSSUuid) && vmdkfile.toLowerCase().endsWith(".vmdk")) {
                        snapshotFullVMDKName += vmdkfile;
                        templateVMDKName += vmdkfile;
                        break;
                    }
                }
                if (snapshotFullVMDKName != null) {
                    command = new Script(false, "cp", _timeout, s_logger);
                    command.add(snapshotFullVMDKName);
                    command.add(installFullPath);
                    result = command.execute();
                    s_logger.info("Copy VMDK file: " + snapshotFullVMDKName);
                    if (result != null) {
                        String msg = "unable to copy snapshot vmdk file " + snapshotFullVMDKName + " to " + installFullPath;
                        s_logger.error(msg);
                        throw new Exception(msg);
                    }
                }
            } else {
                String msg = "unable to find any snapshot ova/ovf files" + snapshotFullOVAName + " to " + installFullPath;
                s_logger.error(msg);
                throw new Exception(msg);
            }
        }
        long physicalSize = new File(installFullPath + "/" + templateVMDKName).length();
        OVAProcessor processor = new OVAProcessor();
        // long physicalSize = new File(installFullPath + "/" + templateUniqueName + ".ova").length();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(StorageLayer.InstanceConfigKey, _storage);
        processor.configure("OVA Processor", params);
        long virtualSize = processor.getTemplateVirtualSize(installFullPath, templateUniqueName);
        postCreatePrivateTemplate(installFullPath, templateId, templateUniqueName, physicalSize, virtualSize);
        writeMetaOvaForTemplate(installFullPath, backedUpSnapshotUuid + ".ovf", templateVMDKName, templateUniqueName, physicalSize);
        return new Ternary<String, Long, Long>(installPath + "/" + templateUniqueName + ".ova", physicalSize, virtualSize);
    } catch (Exception e) {
        // TODO, clean up left over files
        throw e;
    }
}
Also used : Script(com.cloud.utils.script.Script) OVAProcessor(com.cloud.storage.template.OVAProcessor) HashMap(java.util.HashMap) Ternary(com.cloud.utils.Ternary) File(java.io.File) RemoteException(java.rmi.RemoteException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 8 with OVAProcessor

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

the class DownloadManagerImpl method gatherVolumeInfo.

@Override
public Map<Long, TemplateProp> gatherVolumeInfo(String rootDir) {
    Map<Long, TemplateProp> result = new HashMap<Long, TemplateProp>();
    String volumeDir = rootDir + File.separator + _volumeDir;
    if (!_storage.exists(volumeDir)) {
        _storage.mkdirs(volumeDir);
    }
    List<String> vols = listVolumes(volumeDir);
    for (String vol : vols) {
        String path = vol.substring(0, vol.lastIndexOf(File.separator));
        TemplateLocation loc = new TemplateLocation(_storage, path);
        try {
            if (!loc.load()) {
                s_logger.warn("Post download installation was not completed for " + path);
                // loc.purge();
                _storage.cleanup(path, volumeDir);
                continue;
            }
        } catch (IOException e) {
            s_logger.warn("Unable to load volume location " + path, e);
            continue;
        }
        TemplateProp vInfo = loc.getTemplateInfo();
        if ((vInfo.getSize() == vInfo.getPhysicalSize()) && (vInfo.getInstallPath().endsWith(ImageFormat.OVA.getFileExtension()))) {
            try {
                Processor processor = _processors.get("OVA Processor");
                OVAProcessor vmdkProcessor = (OVAProcessor) processor;
                long vSize = vmdkProcessor.getTemplateVirtualSize(path, vInfo.getInstallPath().substring(vInfo.getInstallPath().lastIndexOf(File.separator) + 1));
                vInfo.setSize(vSize);
                loc.updateVirtualSize(vSize);
                loc.save();
            } catch (Exception e) {
                s_logger.error("Unable to get the virtual size of the volume: " + vInfo.getInstallPath() + " due to " + e.getMessage());
            }
        }
        result.put(vInfo.getId(), vInfo);
        s_logger.debug("Added volume name: " + vInfo.getTemplateName() + ", path: " + vol);
    }
    return result;
}
Also used : TemplateProp(com.cloud.storage.template.TemplateProp) 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) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) OVAProcessor(com.cloud.storage.template.OVAProcessor) TemplateLocation(com.cloud.storage.template.TemplateLocation) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException)

Example 9 with OVAProcessor

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

the class VmwareStorageProcessor method createTemplateFromVolume.

private Ternary<String, Long, Long> createTemplateFromVolume(VirtualMachineMO vmMo, String installPath, long templateId, String templateUniqueName, String secStorageUrl, String volumePath, String workerVmName, Integer nfsVersion) throws Exception {
    String secondaryMountPoint = mountService.getMountPoint(secStorageUrl, nfsVersion);
    String installFullPath = secondaryMountPoint + "/" + installPath;
    synchronized (installPath.intern()) {
        Script command = new Script(false, "mkdir", _timeout, s_logger);
        command.add("-p");
        command.add(installFullPath);
        String result = command.execute();
        if (result != null) {
            String msg = "unable to prepare template directory: " + installPath + ", storage: " + secStorageUrl + ", error msg: " + result;
            s_logger.error(msg);
            throw new Exception(msg);
        }
    }
    VirtualMachineMO clonedVm = null;
    try {
        Pair<VirtualDisk, String> volumeDeviceInfo = vmMo.getDiskDevice(volumePath);
        if (volumeDeviceInfo == null) {
            String msg = "Unable to find related disk device for volume. volume path: " + volumePath;
            s_logger.error(msg);
            throw new Exception(msg);
        }
        if (!vmMo.createSnapshot(templateUniqueName, "Temporary snapshot for template creation", false, false)) {
            String msg = "Unable to take snapshot for creating template from volume. volume path: " + volumePath;
            s_logger.error(msg);
            throw new Exception(msg);
        }
        // 4 MB is the minimum requirement for VM memory in VMware
        Pair<VirtualMachineMO, String[]> cloneResult = vmMo.cloneFromCurrentSnapshot(workerVmName, 0, 4, volumeDeviceInfo.second(), VmwareHelper.getDiskDeviceDatastore(volumeDeviceInfo.first()));
        clonedVm = cloneResult.first();
        clonedVm.exportVm(secondaryMountPoint + "/" + installPath, templateUniqueName, false, false);
        // Get VMDK filename
        String templateVMDKName = "";
        File[] files = new File(installFullPath).listFiles();
        if (files != null) {
            for (File file : files) {
                String fileName = file.getName();
                if (fileName.toLowerCase().startsWith(templateUniqueName) && fileName.toLowerCase().endsWith(".vmdk")) {
                    templateVMDKName += fileName;
                    break;
                }
            }
        }
        long physicalSize = new File(installFullPath + "/" + templateVMDKName).length();
        OVAProcessor processor = new OVAProcessor();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(StorageLayer.InstanceConfigKey, _storage);
        processor.configure("OVA Processor", params);
        long virtualSize = processor.getTemplateVirtualSize(installFullPath, templateUniqueName);
        postCreatePrivateTemplate(installFullPath, templateId, templateUniqueName, physicalSize, virtualSize);
        writeMetaOvaForTemplate(installFullPath, templateUniqueName + ".ovf", templateVMDKName, templateUniqueName, physicalSize);
        return new Ternary<String, Long, Long>(installPath + "/" + templateUniqueName + ".ova", physicalSize, virtualSize);
    } finally {
        if (clonedVm != null) {
            clonedVm.detachAllDisks();
            clonedVm.destroy();
        }
        vmMo.removeSnapshot(templateUniqueName, false);
    }
}
Also used : Script(com.cloud.utils.script.Script) OVAProcessor(com.cloud.storage.template.OVAProcessor) HashMap(java.util.HashMap) Ternary(com.cloud.utils.Ternary) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) RemoteException(java.rmi.RemoteException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VirtualDisk(com.vmware.vim25.VirtualDisk) DatastoreFile(com.cloud.hypervisor.vmware.mo.DatastoreFile) File(java.io.File)

Aggregations

OVAProcessor (com.cloud.storage.template.OVAProcessor)9 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)8 HashMap (java.util.HashMap)8 Script (com.cloud.utils.script.Script)5 RemoteException (java.rmi.RemoteException)5 Processor (com.cloud.storage.template.Processor)4 QCOW2Processor (com.cloud.storage.template.QCOW2Processor)4 RawImageProcessor (com.cloud.storage.template.RawImageProcessor)4 TARProcessor (com.cloud.storage.template.TARProcessor)4 VhdProcessor (com.cloud.storage.template.VhdProcessor)4 VmdkProcessor (com.cloud.storage.template.VmdkProcessor)4 Ternary (com.cloud.utils.Ternary)4 File (java.io.File)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 ConfigurationException (javax.naming.ConfigurationException)4 InternalErrorException (com.cloud.exception.InternalErrorException)3 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)3 IsoProcessor (com.cloud.storage.template.IsoProcessor)3 IOException (java.io.IOException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3