Search in sources :

Example 1 with CreateDatadiskTemplateAnswer

use of com.cloud.agent.api.storage.CreateDatadiskTemplateAnswer 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 CreateDatadiskTemplateAnswer

use of com.cloud.agent.api.storage.CreateDatadiskTemplateAnswer 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 CreateDatadiskTemplateAnswer

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

the class TemplateObject method processEvent.

@Override
public void processEvent(ObjectInDataStoreStateMachine.Event event, Answer answer) {
    try {
        if (getDataStore().getRole() == DataStoreRole.Primary) {
            if (answer instanceof CopyCmdAnswer) {
                CopyCmdAnswer cpyAnswer = (CopyCmdAnswer) answer;
                TemplateObjectTO newTemplate = (TemplateObjectTO) cpyAnswer.getNewData();
                String deployAsIsConfiguration = newTemplate.getDeployAsIsConfiguration();
                VMTemplateStoragePoolVO templatePoolRef = templatePoolDao.findByPoolTemplate(getDataStore().getId(), getId(), deployAsIsConfiguration);
                templatePoolRef.setDownloadPercent(100);
                setTemplateSizeIfNeeded(newTemplate, templatePoolRef);
                templatePoolRef.setDownloadState(Status.DOWNLOADED);
                setDownloadPathIfNeeded(newTemplate, templatePoolRef);
                setInstallPathIfNeeded(newTemplate, templatePoolRef);
                templatePoolDao.update(templatePoolRef.getId(), templatePoolRef);
            }
        } else if (getDataStore().getRole() == DataStoreRole.Image || getDataStore().getRole() == DataStoreRole.ImageCache) {
            if (answer instanceof CopyCmdAnswer) {
                CopyCmdAnswer cpyAnswer = (CopyCmdAnswer) answer;
                TemplateObjectTO newTemplate = (TemplateObjectTO) cpyAnswer.getNewData();
                TemplateDataStoreVO templateStoreRef = templateStoreDao.findByStoreTemplate(getDataStore().getId(), getId());
                if (newTemplate.getPath() != null) {
                    templateStoreRef.setInstallPath(newTemplate.getPath());
                }
                templateStoreRef.setDownloadPercent(100);
                templateStoreRef.setDownloadState(Status.DOWNLOADED);
                templateStoreRef.setSize(newTemplate.getSize());
                if (newTemplate.getPhysicalSize() != null) {
                    templateStoreRef.setPhysicalSize(newTemplate.getPhysicalSize());
                }
                templateStoreDao.update(templateStoreRef.getId(), templateStoreRef);
                if (getDataStore().getRole() == DataStoreRole.Image) {
                    VMTemplateVO templateVO = imageDao.findById(getId());
                    if (newTemplate.getFormat() != null) {
                        templateVO.setFormat(newTemplate.getFormat());
                    }
                    if (newTemplate.getName() != null) {
                        // For template created from snapshot, template name is determine by resource code.
                        templateVO.setUniqueName(newTemplate.getName());
                    }
                    if (newTemplate.getHypervisorType() != null) {
                        templateVO.setHypervisorType(newTemplate.getHypervisorType());
                    }
                    templateVO.setSize(newTemplate.getSize());
                    imageDao.update(templateVO.getId(), templateVO);
                }
            } else if (answer instanceof CreateDatadiskTemplateAnswer) {
                CreateDatadiskTemplateAnswer createAnswer = (CreateDatadiskTemplateAnswer) answer;
                TemplateObjectTO dataDiskTemplate = createAnswer.getDataDiskTemplate();
                TemplateDataStoreVO templateStoreRef = templateStoreDao.findByStoreTemplate(getDataStore().getId(), dataDiskTemplate.getId());
                templateStoreRef.setInstallPath(dataDiskTemplate.getPath());
                templateStoreRef.setDownloadPercent(100);
                templateStoreRef.setDownloadState(Status.DOWNLOADED);
                templateStoreRef.setSize(dataDiskTemplate.getSize());
                templateStoreRef.setPhysicalSize(dataDiskTemplate.getPhysicalSize());
                templateStoreDao.update(templateStoreRef.getId(), templateStoreRef);
            }
        }
        objectInStoreMgr.update(this, event);
    } catch (NoTransitionException e) {
        s_logger.debug("failed to update state", e);
        throw new CloudRuntimeException("Failed to update state" + e.toString());
    } catch (Exception ex) {
        s_logger.debug("failed to process event and answer", ex);
        objectInStoreMgr.delete(this);
        throw new CloudRuntimeException("Failed to process event", ex);
    } finally {
        // in case of OperationFailed, expunge the entry
        if (event == ObjectInDataStoreStateMachine.Event.OperationFailed) {
            objectInStoreMgr.deleteIfNotReady(this);
        }
    }
}
Also used : VMTemplateStoragePoolVO(com.cloud.storage.VMTemplateStoragePoolVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) VMTemplateVO(com.cloud.storage.VMTemplateVO) CreateDatadiskTemplateAnswer(com.cloud.agent.api.storage.CreateDatadiskTemplateAnswer) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) TemplateDataStoreVO(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException)

Aggregations

CreateDatadiskTemplateAnswer (com.cloud.agent.api.storage.CreateDatadiskTemplateAnswer)3 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)3 TemplateObjectTO (org.apache.cloudstack.storage.to.TemplateObjectTO)3 OVFHelper (com.cloud.agent.api.storage.OVFHelper)2 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)2 NfsTO (com.cloud.agent.api.to.NfsTO)2 InternalErrorException (com.cloud.exception.InternalErrorException)2 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)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 GetDatadisksAnswer (com.cloud.agent.api.storage.GetDatadisksAnswer)1 DataTO (com.cloud.agent.api.to.DataTO)1 DatadiskTO (com.cloud.agent.api.to.DatadiskTO)1 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)1 VMTemplateStoragePoolVO (com.cloud.storage.VMTemplateStoragePoolVO)1 VMTemplateVO (com.cloud.storage.VMTemplateVO)1