Search in sources :

Example 1 with CopyCmdAnswer

use of com.cloud.storage.command.CopyCmdAnswer 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().getFileExtension();
    } else if (srcData.getObjectType() == DataObjectType.VOLUME) {
        extension = ((VolumeObjectTO) srcData).getFormat().getFileExtension();
    }
    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 = _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.storage.to.SnapshotObjectTO) Script(com.cloud.utils.script.Script) DataTO(com.cloud.agent.api.to.DataTO) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO) TemplateObjectTO(com.cloud.storage.to.TemplateObjectTO) CopyCmdAnswer(com.cloud.storage.command.CopyCmdAnswer)

Example 2 with CopyCmdAnswer

use of com.cloud.storage.command.CopyCmdAnswer in project cosmic by MissionCriticalCloud.

the class SnapshotObject method processEvent.

@Override
public void processEvent(final ObjectInDataStoreStateMachine.Event event, final Answer answer) {
    try {
        final SnapshotDataStoreVO snapshotStore = snapshotStoreDao.findByStoreSnapshot(getDataStore().getRole(), getDataStore().getId(), getId());
        if (answer instanceof CreateObjectAnswer) {
            final SnapshotObjectTO snapshotTO = (SnapshotObjectTO) ((CreateObjectAnswer) answer).getData();
            snapshotStore.setInstallPath(snapshotTO.getPath());
            snapshotStoreDao.update(snapshotStore.getId(), snapshotStore);
        } else if (answer instanceof CopyCmdAnswer) {
            final SnapshotObjectTO snapshotTO = (SnapshotObjectTO) ((CopyCmdAnswer) answer).getNewData();
            snapshotStore.setInstallPath(snapshotTO.getPath());
            if (snapshotTO.getPhysicalSize() != null) {
                // For S3 delta snapshot, physical size is currently not set
                snapshotStore.setPhysicalSize(snapshotTO.getPhysicalSize());
            }
            if (snapshotTO.getParentSnapshotPath() == null) {
                snapshotStore.setParentSnapshotId(0L);
            }
            snapshotStoreDao.update(snapshotStore.getId(), snapshotStore);
            // update side-effect of snapshot operation
            if (snapshotTO.getVolume() != null && snapshotTO.getVolume().getPath() != null) {
                final VolumeVO vol = volumeDao.findByUuid(snapshotTO.getVolume().getUuid());
                if (vol != null) {
                    s_logger.info("Update volume path change due to snapshot operation, volume " + vol.getId() + " path: " + vol.getPath() + "->" + snapshotTO.getVolume().getPath());
                    vol.setPath(snapshotTO.getVolume().getPath());
                    volumeDao.update(vol.getId(), vol);
                } else {
                    s_logger.error("Cound't find the original volume with uuid: " + snapshotTO.getVolume().getUuid());
                }
            }
        } else {
            throw new CloudRuntimeException("Unknown answer: " + answer.getClass());
        }
    } catch (final RuntimeException ex) {
        if (event == ObjectInDataStoreStateMachine.Event.OperationFailed) {
            objectInStoreMgr.deleteIfNotReady(this);
        }
        throw ex;
    }
    this.processEvent(event);
}
Also used : SnapshotObjectTO(com.cloud.storage.to.SnapshotObjectTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeVO(com.cloud.storage.VolumeVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SnapshotDataStoreVO(com.cloud.storage.datastore.db.SnapshotDataStoreVO) CreateObjectAnswer(com.cloud.storage.command.CreateObjectAnswer) CopyCmdAnswer(com.cloud.storage.command.CopyCmdAnswer)

Example 3 with CopyCmdAnswer

use of com.cloud.storage.command.CopyCmdAnswer in project cosmic by MissionCriticalCloud.

the class StorageSystemDataMotionStrategy method handleCreateTemplateFromSnapshot.

private Void handleCreateTemplateFromSnapshot(final SnapshotInfo snapshotInfo, final TemplateInfo templateInfo, final AsyncCompletionCallback<CopyCommandResult> callback) {
    try {
        snapshotInfo.processEvent(Event.CopyingRequested);
    } catch (final Exception ex) {
        throw new CloudRuntimeException("This snapshot is not currently in a state where it can be used to create a template.");
    }
    final HostVO hostVO = getHost(snapshotInfo.getDataStore().getId());
    final DataStore srcDataStore = snapshotInfo.getDataStore();
    final String value = _configDao.getValue(Config.PrimaryStorageDownloadWait.toString());
    final int primaryStorageDownloadWait = NumbersUtil.parseInt(value, Integer.parseInt(Config.PrimaryStorageDownloadWait.getDefaultValue()));
    final CopyCommand copyCommand = new CopyCommand(snapshotInfo.getTO(), templateInfo.getTO(), primaryStorageDownloadWait, VirtualMachineManager.ExecuteInSequence.value());
    String errMsg = null;
    CopyCmdAnswer copyCmdAnswer = null;
    try {
        _volumeService.grantAccess(snapshotInfo, hostVO, srcDataStore);
        final Map<String, String> srcDetails = getSnapshotDetails(_storagePoolDao.findById(srcDataStore.getId()), snapshotInfo);
        copyCommand.setOptions(srcDetails);
        copyCmdAnswer = (CopyCmdAnswer) _agentMgr.send(hostVO.getId(), copyCommand);
    } catch (final Exception ex) {
        throw new CloudRuntimeException(ex.getMessage());
    } finally {
        try {
            _volumeService.revokeAccess(snapshotInfo, hostVO, srcDataStore);
        } catch (final Exception ex) {
            s_logger.debug(ex.getMessage(), ex);
        }
        if (copyCmdAnswer == null || !copyCmdAnswer.getResult()) {
            if (copyCmdAnswer != null && copyCmdAnswer.getDetails() != null && !copyCmdAnswer.getDetails().isEmpty()) {
                errMsg = copyCmdAnswer.getDetails();
            } else {
                errMsg = "Unable to perform host-side operation";
            }
        }
        try {
            if (errMsg == null) {
                snapshotInfo.processEvent(Event.OperationSuccessed);
            } else {
                snapshotInfo.processEvent(Event.OperationFailed);
            }
        } catch (final Exception ex) {
            s_logger.debug(ex.getMessage(), ex);
        }
    }
    final CopyCommandResult result = new CopyCommandResult(null, copyCmdAnswer);
    result.setResult(errMsg);
    callback.complete(result);
    return null;
}
Also used : CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CopyCommand(com.cloud.storage.command.CopyCommand) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) CopyCommandResult(com.cloud.engine.subsystem.api.storage.CopyCommandResult) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) HostVO(com.cloud.host.HostVO) CopyCmdAnswer(com.cloud.storage.command.CopyCmdAnswer)

Example 4 with CopyCmdAnswer

use of com.cloud.storage.command.CopyCmdAnswer in project cosmic by MissionCriticalCloud.

the class VolumeObject method processEvent.

@Override
public void processEvent(final ObjectInDataStoreStateMachine.Event event, final Answer answer) {
    try {
        if (dataStore.getRole() == DataStoreRole.Primary) {
            if (answer instanceof CopyCmdAnswer) {
                final CopyCmdAnswer cpyAnswer = (CopyCmdAnswer) answer;
                final VolumeVO vol = volumeDao.findById(getId());
                final VolumeObjectTO newVol = (VolumeObjectTO) cpyAnswer.getNewData();
                vol.setPath(newVol.getPath());
                if (newVol.getSize() != null) {
                    vol.setSize(newVol.getSize());
                }
                if (newVol.getFormat() != null) {
                    vol.setFormat(newVol.getFormat());
                }
                vol.setPoolId(getDataStore().getId());
                volumeDao.update(vol.getId(), vol);
            } else if (answer instanceof CreateObjectAnswer) {
                final CreateObjectAnswer createAnswer = (CreateObjectAnswer) answer;
                final VolumeObjectTO newVol = (VolumeObjectTO) createAnswer.getData();
                final VolumeVO vol = volumeDao.findById(getId());
                vol.setPath(newVol.getPath());
                if (newVol.getSize() != null) {
                    vol.setSize(newVol.getSize());
                }
                vol.setPoolId(getDataStore().getId());
                if (newVol.getFormat() != null) {
                    vol.setFormat(newVol.getFormat());
                }
                volumeDao.update(vol.getId(), vol);
            }
        } else {
            // image store or imageCache store
            if (answer instanceof DownloadAnswer) {
                final DownloadAnswer dwdAnswer = (DownloadAnswer) answer;
                final VolumeDataStoreVO volStore = volumeStoreDao.findByStoreVolume(dataStore.getId(), getId());
                volStore.setInstallPath(dwdAnswer.getInstallPath());
                volStore.setChecksum(dwdAnswer.getCheckSum());
                volumeStoreDao.update(volStore.getId(), volStore);
            } else if (answer instanceof CopyCmdAnswer) {
                final CopyCmdAnswer cpyAnswer = (CopyCmdAnswer) answer;
                final VolumeDataStoreVO volStore = volumeStoreDao.findByStoreVolume(dataStore.getId(), getId());
                final VolumeObjectTO newVol = (VolumeObjectTO) cpyAnswer.getNewData();
                volStore.setInstallPath(newVol.getPath());
                if (newVol.getSize() != null) {
                    volStore.setSize(newVol.getSize());
                }
                volumeStoreDao.update(volStore.getId(), volStore);
            }
        }
    } catch (final RuntimeException ex) {
        if (event == ObjectInDataStoreStateMachine.Event.OperationFailed) {
            objectInStoreMgr.deleteIfNotReady(this);
        }
        throw ex;
    }
    this.processEvent(event);
}
Also used : CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeVO(com.cloud.storage.VolumeVO) CreateObjectAnswer(com.cloud.storage.command.CreateObjectAnswer) VolumeDataStoreVO(com.cloud.storage.datastore.db.VolumeDataStoreVO) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO) DownloadAnswer(com.cloud.agent.api.storage.DownloadAnswer) CopyCmdAnswer(com.cloud.storage.command.CopyCmdAnswer)

Example 5 with CopyCmdAnswer

use of com.cloud.storage.command.CopyCmdAnswer in project cosmic by MissionCriticalCloud.

the class KvmStorageProcessor method copyVolumeFromImageCacheToPrimary.

@Override
public Answer copyVolumeFromImageCacheToPrimary(final CopyCommand cmd) {
    final DataTO srcData = cmd.getSrcTO();
    final DataTO destData = cmd.getDestTO();
    final DataStoreTO srcStore = srcData.getDataStore();
    final DataStoreTO destStore = destData.getDataStore();
    final VolumeObjectTO srcVol = (VolumeObjectTO) srcData;
    final ImageFormat srcFormat = srcVol.getFormat();
    final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) destStore;
    if (!(srcStore instanceof NfsTO)) {
        return new CopyCmdAnswer("can only handle nfs storage");
    }
    final NfsTO nfsStore = (NfsTO) srcStore;
    final String srcVolumePath = srcData.getPath();
    final String secondaryStorageUrl = nfsStore.getUrl();
    KvmStoragePool secondaryStoragePool = null;
    KvmStoragePool primaryPool;
    try {
        try {
            primaryPool = storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid());
        } catch (final CloudRuntimeException e) {
            if (e.getMessage().contains("not found")) {
                primaryPool = storagePoolMgr.createStoragePool(primaryStore.getUuid(), primaryStore.getHost(), primaryStore.getPort(), primaryStore.getPath(), null, primaryStore.getPoolType());
            } else {
                return new CopyCmdAnswer(e.getMessage());
            }
        }
        final String volumeName = UUID.randomUUID().toString();
        final int index = srcVolumePath.lastIndexOf(File.separator);
        final String volumeDir = srcVolumePath.substring(0, index);
        String srcVolumeName = srcVolumePath.substring(index + 1);
        secondaryStoragePool = storagePoolMgr.getStoragePoolByUri(secondaryStorageUrl + File.separator + volumeDir);
        if (!srcVolumeName.endsWith(".qcow2") && srcFormat == ImageFormat.QCOW2) {
            srcVolumeName = srcVolumeName + ".qcow2";
        }
        final KvmPhysicalDisk volume = secondaryStoragePool.getPhysicalDisk(srcVolumeName);
        volume.setFormat(PhysicalDiskFormat.valueOf(srcFormat.toString()));
        final KvmPhysicalDisk newDisk = storagePoolMgr.copyPhysicalDisk(volume, volumeName, primaryPool, cmd.getWaitInMillSeconds());
        final VolumeObjectTO newVol = new VolumeObjectTO();
        newVol.setFormat(ImageFormat.valueOf(newDisk.getFormat().toString().toUpperCase()));
        newVol.setPath(volumeName);
        return new CopyCmdAnswer(newVol);
    } catch (final CloudRuntimeException e) {
        logger.debug("Failed to ccopyVolumeFromImageCacheToPrimary: ", e);
        return new CopyCmdAnswer(e.toString());
    } finally {
        if (secondaryStoragePool != null) {
            storagePoolMgr.deleteStoragePool(secondaryStoragePool.getType(), secondaryStoragePool.getUuid());
        }
    }
}
Also used : PrimaryDataStoreTO(com.cloud.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) DataTO(com.cloud.agent.api.to.DataTO) PrimaryDataStoreTO(com.cloud.storage.to.PrimaryDataStoreTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO) NfsTO(com.cloud.agent.api.to.NfsTO) CopyCmdAnswer(com.cloud.storage.command.CopyCmdAnswer) ImageFormat(com.cloud.storage.Storage.ImageFormat)

Aggregations

CopyCmdAnswer (com.cloud.storage.command.CopyCmdAnswer)38 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)33 NfsTO (com.cloud.agent.api.to.NfsTO)22 InternalErrorException (com.cloud.exception.InternalErrorException)22 PrimaryDataStoreTO (com.cloud.storage.to.PrimaryDataStoreTO)20 DataTO (com.cloud.agent.api.to.DataTO)19 VolumeObjectTO (com.cloud.storage.to.VolumeObjectTO)19 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)18 Connection (com.xensource.xenapi.Connection)18 XenAPIException (com.xensource.xenapi.Types.XenAPIException)18 XmlRpcException (org.apache.xmlrpc.XmlRpcException)18 SR (com.xensource.xenapi.SR)17 VDI (com.xensource.xenapi.VDI)17 TemplateObjectTO (com.cloud.storage.to.TemplateObjectTO)15 URI (java.net.URI)15 SnapshotObjectTO (com.cloud.storage.to.SnapshotObjectTO)11 Task (com.xensource.xenapi.Task)7 CopyCommandResult (com.cloud.engine.subsystem.api.storage.CopyCommandResult)6 ConfigurationException (javax.naming.ConfigurationException)5 VolumeVO (com.cloud.storage.VolumeVO)4