Search in sources :

Example 1 with TemplateObjectTO

use of com.cloud.legacymodel.to.TemplateObjectTO in project cosmic by MissionCriticalCloud.

the class NfsSecondaryStorageResource method postProcessing.

protected CopyCmdAnswer postProcessing(final File destFile, final String downloadPath, final String destPath, final DataTO srcData, final DataTO destData) throws ConfigurationException {
    if (destData.getObjectType() == DataObjectType.SNAPSHOT) {
        final SnapshotObjectTO snapshot = new SnapshotObjectTO();
        snapshot.setPath(destPath + File.separator + destFile.getName());
        final CopyCmdAnswer answer = new CopyCmdAnswer(snapshot);
        return answer;
    }
    // do post processing to unzip the file if it is compressed
    final String scriptsDir = "scripts/storage/secondary";
    final String createTmpltScr = Script.findScript(scriptsDir, "createtmplt.sh");
    if (createTmpltScr == null) {
        throw new ConfigurationException("Unable to find createtmplt.sh");
    }
    s_logger.info("createtmplt.sh found in " + createTmpltScr);
    final String createVolScr = Script.findScript(scriptsDir, "createvolume.sh");
    if (createVolScr == null) {
        throw new ConfigurationException("Unable to find createvolume.sh");
    }
    s_logger.info("createvolume.sh found in " + createVolScr);
    final String script = srcData.getObjectType() == DataObjectType.TEMPLATE ? createTmpltScr : createVolScr;
    final int installTimeoutPerGig = 180 * 60 * 1000;
    long imgSizeGigs = (long) Math.ceil(destFile.length() * 1.0d / (1024 * 1024 * 1024));
    // add one just in case
    imgSizeGigs++;
    final long timeout = imgSizeGigs * installTimeoutPerGig;
    final String origPath = destFile.getAbsolutePath();
    String extension = null;
    if (srcData.getObjectType() == DataObjectType.TEMPLATE) {
        extension = ((TemplateObjectTO) srcData).getFormat().toString().toLowerCase();
    } else if (srcData.getObjectType() == DataObjectType.VOLUME) {
        extension = ((VolumeObjectTO) srcData).getFormat().toString().toLowerCase();
    }
    final String templateName = UUID.randomUUID().toString();
    final String templateFilename = templateName + "." + extension;
    final Script scr = new Script(script, timeout, s_logger);
    // not used for now
    scr.add("-s", Long.toString(imgSizeGigs));
    scr.add("-n", templateFilename);
    scr.add("-t", downloadPath);
    // this is the temporary
    scr.add("-f", origPath);
    // template file downloaded
    final String result;
    result = scr.execute();
    if (result != null) {
        // script execution failure
        throw new CloudRuntimeException("Failed to run script " + script);
    }
    final String finalFileName = templateFilename;
    final String finalDownloadPath = destPath + File.separator + templateFilename;
    // compute the size of
    final long size = this._storage.getSize(downloadPath + File.separator + templateFilename);
    DataTO newDestTO = null;
    if (destData.getObjectType() == DataObjectType.TEMPLATE) {
        final TemplateObjectTO newTemplTO = new TemplateObjectTO();
        newTemplTO.setPath(finalDownloadPath);
        newTemplTO.setName(finalFileName);
        newTemplTO.setSize(size);
        newTemplTO.setPhysicalSize(size);
        newDestTO = newTemplTO;
    } else {
        final VolumeObjectTO newVolTO = new VolumeObjectTO();
        newVolTO.setPath(finalDownloadPath);
        newVolTO.setName(finalFileName);
        newVolTO.setSize(size);
        newDestTO = newVolTO;
    }
    return new CopyCmdAnswer(newDestTO);
}
Also used : SnapshotObjectTO(com.cloud.legacymodel.to.SnapshotObjectTO) Script(com.cloud.utils.script.Script) DataTO(com.cloud.legacymodel.to.DataTO) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO) TemplateObjectTO(com.cloud.legacymodel.to.TemplateObjectTO) CopyCmdAnswer(com.cloud.legacymodel.communication.answer.CopyCmdAnswer)

Example 2 with TemplateObjectTO

use of com.cloud.legacymodel.to.TemplateObjectTO in project cosmic by MissionCriticalCloud.

the class TemplateServiceImpl method handleTemplateSync.

@Override
public void handleTemplateSync(final DataStore store) {
    if (store == null) {
        s_logger.warn("Huh? image store is null");
        return;
    }
    final long storeId = store.getId();
    // add lock to make template sync for a data store only be done once
    final String lockString = "templatesync.storeId:" + storeId;
    final GlobalLock syncLock = GlobalLock.getInternLock(lockString);
    try {
        if (syncLock.lock(3)) {
            try {
                final Long zoneId = store.getScope().getScopeId();
                final Map<String, TemplateProp> templateInfos = listTemplate(store);
                if (templateInfos == null) {
                    return;
                }
                final Set<VMTemplateVO> toBeDownloaded = new HashSet<>();
                List<VMTemplateVO> allTemplates = null;
                if (zoneId == null) {
                    // region wide store
                    allTemplates = _templateDao.listByState(VirtualMachineTemplate.State.Active, VirtualMachineTemplate.State.NotUploaded, VirtualMachineTemplate.State.UploadInProgress);
                } else {
                    // zone wide store
                    allTemplates = _templateDao.listInZoneByState(zoneId, VirtualMachineTemplate.State.Active, VirtualMachineTemplate.State.NotUploaded, VirtualMachineTemplate.State.UploadInProgress);
                }
                final List<VMTemplateVO> rtngTmplts = _templateDao.listAllSystemVMTemplates();
                final List<VMTemplateVO> defaultBuiltin = _templateDao.listDefaultBuiltinTemplates();
                if (rtngTmplts != null) {
                    for (final VMTemplateVO rtngTmplt : rtngTmplts) {
                        if (!allTemplates.contains(rtngTmplt)) {
                            allTemplates.add(rtngTmplt);
                        }
                    }
                }
                if (defaultBuiltin != null) {
                    for (final VMTemplateVO builtinTmplt : defaultBuiltin) {
                        if (!allTemplates.contains(builtinTmplt)) {
                            allTemplates.add(builtinTmplt);
                        }
                    }
                }
                toBeDownloaded.addAll(allTemplates);
                final StateMachine2<VirtualMachineTemplate.State, VirtualMachineTemplate.Event, VirtualMachineTemplate> stateMachine = VirtualMachineTemplate.State.getStateMachine();
                for (final VMTemplateVO tmplt : allTemplates) {
                    final String uniqueName = tmplt.getUniqueName();
                    TemplateDataStoreVO tmpltStore = _vmTemplateStoreDao.findByStoreTemplate(storeId, tmplt.getId());
                    if (templateInfos.containsKey(uniqueName)) {
                        final TemplateProp tmpltInfo = templateInfos.remove(uniqueName);
                        toBeDownloaded.remove(tmplt);
                        if (tmpltStore != null) {
                            s_logger.info("Template Sync found " + uniqueName + " already in the image store");
                            if (tmpltStore.getDownloadState() != VMTemplateStatus.DOWNLOADED) {
                                tmpltStore.setErrorString("");
                            }
                            if (tmpltInfo.isCorrupted()) {
                                tmpltStore.setDownloadState(VMTemplateStatus.DOWNLOAD_ERROR);
                                String msg = "Template " + tmplt.getName() + ":" + tmplt.getId() + " is corrupted on secondary storage " + tmpltStore.getId();
                                tmpltStore.setErrorString(msg);
                                s_logger.info(msg);
                                _alertMgr.sendAlert(AlertManager.AlertType.ALERT_TYPE_UPLOAD_FAILED, zoneId, null, msg, msg);
                                if (tmplt.getState() == VirtualMachineTemplate.State.NotUploaded || tmplt.getState() == VirtualMachineTemplate.State.UploadInProgress) {
                                    s_logger.info("Template Sync found " + uniqueName + " on image store " + storeId + " uploaded using SSVM as corrupted, marking it as " + "failed");
                                    tmpltStore.setState(State.Failed);
                                    try {
                                        new StateMachine2Transitions(stateMachine).transitTo(tmplt, VirtualMachineTemplate.Event.OperationFailed, null, _templateDao);
                                    } catch (final NoTransitionException e) {
                                        s_logger.error("Unexpected state transition exception for template " + tmplt.getName() + ". Details: " + e.getMessage());
                                    }
                                } else if (tmplt.getUrl() == null) {
                                    msg = "Private template (" + tmplt + ") with install path " + tmpltInfo.getInstallPath() + " is corrupted, please check in image store: " + tmpltStore.getDataStoreId();
                                    s_logger.warn(msg);
                                } else {
                                    s_logger.info("Removing template_store_ref entry for corrupted template " + tmplt.getName());
                                    _vmTemplateStoreDao.remove(tmpltStore.getId());
                                    toBeDownloaded.add(tmplt);
                                }
                            } else {
                                tmpltStore.setDownloadPercent(100);
                                tmpltStore.setDownloadState(VMTemplateStatus.DOWNLOADED);
                                tmpltStore.setState(ObjectInDataStoreStateMachine.State.Ready);
                                tmpltStore.setInstallPath(tmpltInfo.getInstallPath());
                                tmpltStore.setSize(tmpltInfo.getSize());
                                tmpltStore.setPhysicalSize(tmpltInfo.getPhysicalSize());
                                tmpltStore.setLastUpdated(new Date());
                                // update size in vm_template table
                                final VMTemplateVO tmlpt = _templateDao.findById(tmplt.getId());
                                tmlpt.setSize(tmpltInfo.getSize());
                                _templateDao.update(tmplt.getId(), tmlpt);
                                if (tmplt.getState() == VirtualMachineTemplate.State.NotUploaded || tmplt.getState() == VirtualMachineTemplate.State.UploadInProgress) {
                                    try {
                                        new StateMachine2Transitions(stateMachine).transitTo(tmplt, VirtualMachineTemplate.Event.OperationSucceeded, null, _templateDao);
                                    } catch (final NoTransitionException e) {
                                        s_logger.error("Unexpected state transition exception for template " + tmplt.getName() + ". Details: " + e.getMessage());
                                    }
                                }
                                // which already got checked and incremented during createTemplate API call.
                                if (tmpltInfo.getSize() > 0 && tmplt.getAccountId() != Account.ACCOUNT_ID_SYSTEM && tmplt.getUrl() != null) {
                                    final long accountId = tmplt.getAccountId();
                                    try {
                                        _resourceLimitMgr.checkResourceLimit(_accountMgr.getAccount(accountId), Resource.ResourceType.secondary_storage, tmpltInfo.getSize() - UriUtils.getRemoteSize(tmplt.getUrl()));
                                    } catch (final ResourceAllocationException e) {
                                        s_logger.warn(e.getMessage());
                                        _alertMgr.sendAlert(AlertManager.AlertType.ALERT_TYPE_RESOURCE_LIMIT_EXCEEDED, zoneId, null, e.getMessage(), e.getMessage());
                                    } finally {
                                        _resourceLimitMgr.recalculateResourceCount(accountId, _accountMgr.getAccount(accountId).getDomainId(), Resource.ResourceType.secondary_storage.getOrdinal());
                                    }
                                }
                            }
                            _vmTemplateStoreDao.update(tmpltStore.getId(), tmpltStore);
                        } else {
                            tmpltStore = new TemplateDataStoreVO(storeId, tmplt.getId(), new Date(), 100, VMTemplateStatus.DOWNLOADED, null, null, null, tmpltInfo.getInstallPath(), tmplt.getUrl());
                            tmpltStore.setSize(tmpltInfo.getSize());
                            tmpltStore.setPhysicalSize(tmpltInfo.getPhysicalSize());
                            tmpltStore.setDataStoreRole(store.getRole());
                            _vmTemplateStoreDao.persist(tmpltStore);
                            // update size in vm_template table
                            final VMTemplateVO tmlpt = _templateDao.findById(tmplt.getId());
                            tmlpt.setSize(tmpltInfo.getSize());
                            _templateDao.update(tmplt.getId(), tmlpt);
                            associateTemplateToZone(tmplt.getId(), zoneId);
                        }
                    } else if (tmplt.getState() == VirtualMachineTemplate.State.NotUploaded || tmplt.getState() == VirtualMachineTemplate.State.UploadInProgress) {
                        s_logger.info("Template Sync did not find " + uniqueName + " on image store " + storeId + " uploaded using SSVM, marking it as failed");
                        toBeDownloaded.remove(tmplt);
                        tmpltStore.setDownloadState(VMTemplateStatus.DOWNLOAD_ERROR);
                        final String msg = "Template " + tmplt.getName() + ":" + tmplt.getId() + " is corrupted on secondary storage " + tmpltStore.getId();
                        tmpltStore.setErrorString(msg);
                        tmpltStore.setState(State.Failed);
                        _vmTemplateStoreDao.update(tmpltStore.getId(), tmpltStore);
                        try {
                            new StateMachine2Transitions(stateMachine).transitTo(tmplt, VirtualMachineTemplate.Event.OperationFailed, null, _templateDao);
                        } catch (final NoTransitionException e) {
                            s_logger.error("Unexpected state transition exception for template " + tmplt.getName() + ". Details: " + e.getMessage());
                        }
                    } else {
                        s_logger.info("Template Sync did not find " + uniqueName + " on image store " + storeId + ", may request download based on available hypervisor types");
                        if (tmpltStore != null) {
                            if (_storeMgr.isRegionStore(store) && tmpltStore.getDownloadState() == VMTemplateStatus.DOWNLOADED && tmpltStore.getState() == State.Ready && tmpltStore.getInstallPath() == null) {
                                s_logger.info("Keep fake entry in template store table for migration of previous NFS to object store");
                            } else if (tmpltStore.getState() == State.Ready && tmpltStore.getDownloadState() == VMTemplateStatus.DOWNLOADED) {
                                s_logger.info("Keep template " + uniqueName + " entry as state is Ready and downloaded. No need to download again.");
                            } else {
                                s_logger.info("Removing leftover template " + uniqueName + " entry from template store table");
                                // remove those leftover entries
                                _vmTemplateStoreDao.remove(tmpltStore.getId());
                            }
                        }
                    }
                }
                if (toBeDownloaded.size() > 0) {
                    /* Only download templates whose hypervirsor type is in the zone */
                    final List<HypervisorType> availHypers = _clusterDao.getAvailableHypervisorInZone(zoneId);
                    if (availHypers.isEmpty()) {
                        /*
                             * This is for cloudzone, local secondary storage resource
                             * started before cluster created
                             */
                        availHypers.add(HypervisorType.KVM);
                    }
                    // bug 9809: resume ISO
                    availHypers.add(HypervisorType.None);
                    // download.
                    for (final VMTemplateVO tmplt : toBeDownloaded) {
                        if (tmplt.getUrl() == null) {
                            // If url is null, skip downloading
                            s_logger.info("Skip downloading template " + tmplt.getUniqueName() + " since no url is specified.");
                            continue;
                        }
                        // means that this is a duplicate entry from migration of previous NFS to staging.
                        if (_storeMgr.isRegionStore(store)) {
                            final TemplateDataStoreVO tmpltStore = _vmTemplateStoreDao.findByStoreTemplate(storeId, tmplt.getId());
                            if (tmpltStore != null && tmpltStore.getDownloadState() == VMTemplateStatus.DOWNLOADED && tmpltStore.getState() == State.Ready && tmpltStore.getInstallPath() == null) {
                                s_logger.info("Skip sync template for migration of previous NFS to object store");
                                continue;
                            }
                        }
                        if (availHypers.contains(tmplt.getHypervisorType())) {
                            s_logger.info("Downloading template " + tmplt.getUniqueName() + " to image store " + store.getName());
                            associateTemplateToZone(tmplt.getId(), zoneId);
                            final TemplateInfo tmpl = _templateFactory.getTemplate(tmplt.getId(), DataStoreRole.Image);
                            createTemplateAsync(tmpl, store, null);
                        } else {
                            s_logger.info("Skip downloading of a template because it's not part of the supported hypervisors");
                        }
                    }
                }
                for (final String uniqueName : templateInfos.keySet()) {
                    final TemplateProp tInfo = templateInfos.get(uniqueName);
                    if (_tmpltMgr.templateIsDeleteable(tInfo.getId())) {
                        // we cannot directly call deleteTemplateSync here to reuse delete logic since in this case db does not have this template at all.
                        final TemplateObjectTO tmplTO = new TemplateObjectTO();
                        tmplTO.setDataStore(store.getTO());
                        tmplTO.setPath(tInfo.getInstallPath());
                        tmplTO.setId(tInfo.getId());
                        final DeleteCommand dtCommand = new DeleteCommand(tmplTO);
                        final EndPoint ep = _epSelector.select(store);
                        Answer answer = null;
                        if (ep == null) {
                            final String errMsg = "No remote endpoint to send command, check if host or ssvm is down?";
                            s_logger.error(errMsg);
                            answer = new Answer(dtCommand, false, errMsg);
                        } else {
                            answer = ep.sendMessage(dtCommand);
                        }
                        if (answer == null || !answer.getResult()) {
                            s_logger.info("Failed to deleted template at store: " + store.getName());
                        } else {
                            final String description = "Deleted template " + tInfo.getTemplateName() + " on secondary storage " + storeId;
                            s_logger.info(description);
                        }
                    }
                }
            } finally {
                syncLock.unlock();
            }
        } else {
            s_logger.info("Couldn't get global lock on " + lockString + ", another thread may be doing template sync on data store " + storeId + " now.");
        }
    } finally {
        syncLock.releaseRef();
    }
}
Also used : TemplateProp(com.cloud.legacymodel.storage.TemplateProp) VMTemplateVO(com.cloud.storage.VMTemplateVO) EndPoint(com.cloud.engine.subsystem.api.storage.EndPoint) GlobalLock(com.cloud.utils.db.GlobalLock) DeleteCommand(com.cloud.legacymodel.communication.command.DeleteCommand) StateMachine2Transitions(com.cloud.utils.fsm.StateMachine2Transitions) ResourceAllocationException(com.cloud.legacymodel.exceptions.ResourceAllocationException) HashSet(java.util.HashSet) VirtualMachineTemplate(com.cloud.legacymodel.storage.VirtualMachineTemplate) TemplateDataStoreVO(com.cloud.storage.datastore.db.TemplateDataStoreVO) Date(java.util.Date) HypervisorType(com.cloud.model.enumeration.HypervisorType) ListTemplateAnswer(com.cloud.legacymodel.communication.answer.ListTemplateAnswer) Answer(com.cloud.legacymodel.communication.answer.Answer) TemplateInfo(com.cloud.engine.subsystem.api.storage.TemplateInfo) State(com.cloud.legacymodel.storage.ObjectInDataStoreStateMachine.State) NoTransitionException(com.cloud.legacymodel.exceptions.NoTransitionException) Event(com.cloud.legacymodel.storage.ObjectInDataStoreStateMachine.Event) TemplateObjectTO(com.cloud.legacymodel.to.TemplateObjectTO)

Example 3 with TemplateObjectTO

use of com.cloud.legacymodel.to.TemplateObjectTO in project cosmic by MissionCriticalCloud.

the class CitrixResourceBase method mount.

protected VDI mount(final Connection conn, final String vmName, final DiskTO volume) throws XmlRpcException, XenAPIException {
    final DataTO data = volume.getData();
    final VolumeType type = volume.getType();
    if (type == VolumeType.ISO) {
        final TemplateObjectTO iso = (TemplateObjectTO) data;
        final DataStoreTO store = iso.getDataStore();
        if (store == null) {
            // It's a fake iso
            return null;
        }
        // corer case, xenserver pv driver iso
        final String templateName = iso.getName();
        if (templateName.startsWith("xs-tools")) {
            try {
                final Set<VDI> vdis = VDI.getByNameLabel(conn, templateName);
                if (vdis.isEmpty()) {
                    throw new CloudRuntimeException("Could not find ISO with URL: " + templateName);
                }
                return vdis.iterator().next();
            } catch (final XenAPIException e) {
                throw new CloudRuntimeException("Unable to get pv iso: " + templateName + " due to " + e.toString());
            } catch (final Exception e) {
                throw new CloudRuntimeException("Unable to get pv iso: " + templateName + " due to " + e.toString());
            }
        }
        if (!(store instanceof NfsTO)) {
            throw new CloudRuntimeException("only support mount iso on nfs");
        }
        final NfsTO nfsStore = (NfsTO) store;
        final String isoPath = nfsStore.getUrl() + File.separator + iso.getPath();
        final int index = isoPath.lastIndexOf("/");
        final String mountpoint = isoPath.substring(0, index);
        final URI uri;
        try {
            uri = new URI(mountpoint);
        } catch (final URISyntaxException e) {
            throw new CloudRuntimeException("Incorrect uri " + mountpoint, e);
        }
        final SR isoSr = createIsoSRbyURI(conn, uri, vmName, false);
        final String isoname = isoPath.substring(index + 1);
        final VDI isoVdi = getVDIbyLocationandSR(conn, isoname, isoSr);
        if (isoVdi == null) {
            throw new CloudRuntimeException("Unable to find ISO " + isoPath);
        }
        return isoVdi;
    } else {
        final VolumeObjectTO vol = (VolumeObjectTO) data;
        return VDI.getByUuid(conn, vol.getPath());
    }
}
Also used : DataStoreTO(com.cloud.legacymodel.to.DataStoreTO) XenAPIException(com.xensource.xenapi.Types.XenAPIException) URISyntaxException(java.net.URISyntaxException) NfsTO(com.cloud.legacymodel.to.NfsTO) URI(java.net.URI) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) SAXException(org.xml.sax.SAXException) ConfigurationException(javax.naming.ConfigurationException) MalformedURLException(java.net.MalformedURLException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DataTO(com.cloud.legacymodel.to.DataTO) VolumeType(com.cloud.model.enumeration.VolumeType) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO) VDI(com.xensource.xenapi.VDI) TemplateObjectTO(com.cloud.legacymodel.to.TemplateObjectTO) SR(com.xensource.xenapi.SR)

Example 4 with TemplateObjectTO

use of com.cloud.legacymodel.to.TemplateObjectTO in project cosmic by MissionCriticalCloud.

the class KvmStorageProcessor method createTemplateFromVolume.

@Override
public Answer createTemplateFromVolume(final CopyCommand cmd) {
    final DataTO srcData = cmd.getSrcTO();
    final DataTO destData = cmd.getDestTO();
    final int wait = cmd.getWaitInMillSeconds();
    final TemplateObjectTO template = (TemplateObjectTO) destData;
    final DataStoreTO imageStore = template.getDataStore();
    final VolumeObjectTO volume = (VolumeObjectTO) srcData;
    final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) volume.getDataStore();
    if (!(imageStore instanceof NfsTO)) {
        return new CopyCmdAnswer("unsupported protocol");
    }
    final NfsTO nfsImageStore = (NfsTO) imageStore;
    KvmStoragePool secondaryStorage = null;
    final KvmStoragePool primary;
    try {
        final String templateFolder = template.getPath();
        secondaryStorage = this.storagePoolMgr.getStoragePoolByUri(nfsImageStore.getUrl());
        primary = this.storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid());
        final KvmPhysicalDisk disk = this.storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), volume.getPath());
        final String tmpltPath = secondaryStorage.getLocalPath() + File.separator + templateFolder;
        this.storageLayer.mkdirs(tmpltPath);
        final String templateName = UUID.randomUUID().toString();
        if (primary.getType() != StoragePoolType.RBD) {
            final Script command = new Script(this.createTmplPath, wait, this.logger);
            command.add("-f", disk.getPath());
            command.add("-t", tmpltPath);
            command.add("-n", templateName + ".qcow2");
            final String result = command.execute();
            if (result != null) {
                this.logger.debug("failed to create template: " + result);
                return new CopyCmdAnswer(result);
            }
        } else {
            this.logger.debug("Converting RBD disk " + disk.getPath() + " into template " + templateName);
            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 + "/" + templateName + ".qcow2");
            destFile.setFormat(PhysicalDiskFormat.QCOW2);
            final QemuImg q = new QemuImg(cmd.getWaitInMillSeconds());
            try {
                q.convert(srcFile, destFile);
            } catch (final QemuImgException e) {
                final String message = "Failed to create new template while converting " + srcFile.getFileName() + " to " + destFile.getFileName() + " the error was: " + e.getMessage();
                throw new QemuImgException(message);
            }
            final File templateProp = new File(tmpltPath + "/template.properties");
            if (!templateProp.exists()) {
                templateProp.createNewFile();
            }
            String templateContent = "filename=" + templateName + ".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());
                templFo.flush();
            } catch (final IOException e) {
                throw e;
            }
        }
        final Map<String, Object> params = new HashMap<>();
        params.put(StorageLayer.InstanceConfigKey, this.storageLayer);
        final Processor qcow2Processor = new QCOW2Processor();
        qcow2Processor.configure("QCOW2 Processor", params);
        final TemplateFormatInfo info = qcow2Processor.process(tmpltPath, null, templateName);
        final TemplateLocation loc = new TemplateLocation(this.storageLayer, tmpltPath);
        loc.create(1, true, templateName);
        loc.addFormat(info);
        loc.save();
        final TemplateObjectTO newTemplate = new TemplateObjectTO();
        newTemplate.setPath(templateFolder + File.separator + templateName + ".qcow2");
        newTemplate.setSize(info.virtualSize);
        newTemplate.setPhysicalSize(info.size);
        newTemplate.setFormat(ImageFormat.QCOW2);
        newTemplate.setName(templateName);
        return new CopyCmdAnswer(newTemplate);
    } catch (final QemuImgException e) {
        this.logger.error(e.getMessage());
        return new CopyCmdAnswer(e.toString());
    } catch (final Exception e) {
        this.logger.debug("Failed to createTemplateFromVolume: ", e);
        return new CopyCmdAnswer(e.toString());
    } finally {
        if (secondaryStorage != null) {
            secondaryStorage.delete();
        }
    }
}
Also used : QCOW2Processor(com.cloud.common.storageprocessor.QCOW2Processor) StorageProcessor(com.cloud.common.storageprocessor.resource.StorageProcessor) Processor(com.cloud.common.storageprocessor.Processor) HashMap(java.util.HashMap) DataTO(com.cloud.legacymodel.to.DataTO) TemplateLocation(com.cloud.common.storageprocessor.TemplateLocation) QemuImgException(com.cloud.agent.resource.kvm.storage.utils.QemuImgException) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO) Script(com.cloud.utils.script.Script) PrimaryDataStoreTO(com.cloud.legacymodel.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.legacymodel.to.DataStoreTO) IOException(java.io.IOException) NfsTO(com.cloud.legacymodel.to.NfsTO) Date(java.util.Date) LibvirtException(org.libvirt.LibvirtException) QemuImgException(com.cloud.agent.resource.kvm.storage.utils.QemuImgException) FileNotFoundException(java.io.FileNotFoundException) InternalErrorException(com.cloud.legacymodel.exceptions.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) IOException(java.io.IOException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) QemuImg(com.cloud.agent.resource.kvm.storage.utils.QemuImg) QCOW2Processor(com.cloud.common.storageprocessor.QCOW2Processor) PrimaryDataStoreTO(com.cloud.legacymodel.to.PrimaryDataStoreTO) QemuImgFile(com.cloud.agent.resource.kvm.storage.utils.QemuImgFile) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) FileOutputStream(java.io.FileOutputStream) TemplateFormatInfo(com.cloud.legacymodel.storage.TemplateFormatInfo) TemplateObjectTO(com.cloud.legacymodel.to.TemplateObjectTO) File(java.io.File) QemuImgFile(com.cloud.agent.resource.kvm.storage.utils.QemuImgFile) SimpleDateFormat(java.text.SimpleDateFormat) CopyCmdAnswer(com.cloud.legacymodel.communication.answer.CopyCmdAnswer)

Example 5 with TemplateObjectTO

use of com.cloud.legacymodel.to.TemplateObjectTO in project cosmic by MissionCriticalCloud.

the class KvmStorageProcessor method copyTemplateToPrimaryStorage.

@Override
public Answer copyTemplateToPrimaryStorage(final CopyCommand cmd) {
    final DataTO srcData = cmd.getSrcTO();
    final DataTO destData = cmd.getDestTO();
    final TemplateObjectTO template = (TemplateObjectTO) srcData;
    final DataStoreTO imageStore = template.getDataStore();
    final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) destData.getDataStore();
    if (!(imageStore instanceof NfsTO)) {
        return new CopyCmdAnswer("unsupported protocol");
    }
    final NfsTO nfsImageStore = (NfsTO) imageStore;
    final String tmplturl = nfsImageStore.getUrl() + File.separator + template.getPath();
    final int index = tmplturl.lastIndexOf("/");
    final String mountpoint = tmplturl.substring(0, index);
    String tmpltname = null;
    if (index < tmplturl.length() - 1) {
        tmpltname = tmplturl.substring(index + 1);
    }
    KvmPhysicalDisk tmplVol = null;
    KvmStoragePool secondaryPool = null;
    try {
        secondaryPool = this.storagePoolMgr.getStoragePoolByUri(mountpoint);
        /* Get template vol */
        if (tmpltname == null) {
            secondaryPool.refresh();
            final List<KvmPhysicalDisk> disks = secondaryPool.listPhysicalDisks();
            if (disks == null || disks.isEmpty()) {
                return new PrimaryStorageDownloadAnswer("Failed to get volumes from pool: " + secondaryPool.getUuid());
            }
            for (final KvmPhysicalDisk disk : disks) {
                if (disk.getName().endsWith("qcow2")) {
                    tmplVol = disk;
                    break;
                }
            }
        } else {
            tmplVol = secondaryPool.getPhysicalDisk(tmpltname);
        }
        if (tmplVol == null) {
            return new PrimaryStorageDownloadAnswer("Failed to get template from pool: " + secondaryPool.getUuid());
        }
        /* Copy volume to primary storage */
        this.logger.debug("Copying template to primary storage, template format is " + tmplVol.getFormat());
        final KvmStoragePool primaryPool = this.storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid());
        final KvmPhysicalDisk primaryVol;
        if (destData instanceof VolumeObjectTO) {
            final VolumeObjectTO volume = (VolumeObjectTO) destData;
            // rather than cloning on deploy
            if (volume.getSize() != null && volume.getSize() > tmplVol.getVirtualSize()) {
                this.logger.debug("Using configured size of " + volume.getSize());
                tmplVol.setSize(volume.getSize());
                tmplVol.setVirtualSize(volume.getSize());
            } else {
                this.logger.debug("Using template's size of " + tmplVol.getVirtualSize());
            }
            primaryVol = this.storagePoolMgr.copyPhysicalDisk(tmplVol, volume.getUuid(), primaryPool, cmd.getWaitInMillSeconds());
        } else if (destData instanceof TemplateObjectTO) {
            final TemplateObjectTO destTempl = (TemplateObjectTO) destData;
            primaryVol = this.storagePoolMgr.copyPhysicalDisk(tmplVol, destTempl.getUuid(), primaryPool, cmd.getWaitInMillSeconds());
        } else {
            primaryVol = this.storagePoolMgr.copyPhysicalDisk(tmplVol, UUID.randomUUID().toString(), primaryPool, cmd.getWaitInMillSeconds());
        }
        DataTO data = null;
        if (destData.getObjectType() == DataObjectType.TEMPLATE) {
            final TemplateObjectTO newTemplate = new TemplateObjectTO();
            newTemplate.setPath(primaryVol.getName());
            newTemplate.setSize(primaryVol.getSize());
            if (primaryPool.getType() == StoragePoolType.RBD) {
                newTemplate.setFormat(ImageFormat.RAW);
            } else {
                newTemplate.setFormat(ImageFormat.QCOW2);
            }
            data = newTemplate;
        } else if (destData.getObjectType() == DataObjectType.VOLUME) {
            final VolumeObjectTO volumeObjectTo = new VolumeObjectTO();
            volumeObjectTo.setPath(primaryVol.getName());
            volumeObjectTo.setSize(primaryVol.getSize());
            if (primaryVol.getFormat() == PhysicalDiskFormat.RAW) {
                volumeObjectTo.setFormat(ImageFormat.RAW);
            } else if (primaryVol.getFormat() == PhysicalDiskFormat.QCOW2) {
                volumeObjectTo.setFormat(ImageFormat.QCOW2);
            }
            data = volumeObjectTo;
        }
        return new CopyCmdAnswer(data);
    } catch (final CloudRuntimeException e) {
        return new CopyCmdAnswer(e.toString());
    } finally {
        try {
            if (secondaryPool != null) {
                secondaryPool.delete();
            }
        } catch (final Exception e) {
            this.logger.debug("Failed to clean up secondary storage", e);
        }
    }
}
Also used : PrimaryDataStoreTO(com.cloud.legacymodel.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.legacymodel.to.DataStoreTO) NfsTO(com.cloud.legacymodel.to.NfsTO) LibvirtException(org.libvirt.LibvirtException) QemuImgException(com.cloud.agent.resource.kvm.storage.utils.QemuImgException) FileNotFoundException(java.io.FileNotFoundException) InternalErrorException(com.cloud.legacymodel.exceptions.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) IOException(java.io.IOException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) PrimaryStorageDownloadAnswer(com.cloud.legacymodel.communication.answer.PrimaryStorageDownloadAnswer) DataTO(com.cloud.legacymodel.to.DataTO) PrimaryDataStoreTO(com.cloud.legacymodel.to.PrimaryDataStoreTO) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO) TemplateObjectTO(com.cloud.legacymodel.to.TemplateObjectTO) CopyCmdAnswer(com.cloud.legacymodel.communication.answer.CopyCmdAnswer)

Aggregations

TemplateObjectTO (com.cloud.legacymodel.to.TemplateObjectTO)24 CopyCmdAnswer (com.cloud.legacymodel.communication.answer.CopyCmdAnswer)17 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)17 NfsTO (com.cloud.legacymodel.to.NfsTO)14 InternalErrorException (com.cloud.legacymodel.exceptions.InternalErrorException)12 PrimaryDataStoreTO (com.cloud.legacymodel.to.PrimaryDataStoreTO)12 DataStoreTO (com.cloud.legacymodel.to.DataStoreTO)11 DataTO (com.cloud.legacymodel.to.DataTO)11 XenAPIException (com.xensource.xenapi.Types.XenAPIException)11 XmlRpcException (org.apache.xmlrpc.XmlRpcException)11 VDI (com.xensource.xenapi.VDI)10 Connection (com.xensource.xenapi.Connection)9 SR (com.xensource.xenapi.SR)9 URI (java.net.URI)8 DiskTO (com.cloud.legacymodel.to.DiskTO)6 VolumeObjectTO (com.cloud.legacymodel.to.VolumeObjectTO)6 ConfigurationException (javax.naming.ConfigurationException)6 IOException (java.io.IOException)5 Answer (com.cloud.legacymodel.communication.answer.Answer)4 AttachAnswer (com.cloud.legacymodel.communication.answer.AttachAnswer)4