Search in sources :

Example 1 with CopyCmdAnswer

use of com.cloud.legacymodel.communication.answer.CopyCmdAnswer in project cosmic by MissionCriticalCloud.

the class NfsSecondaryStorageResource method createTemplateFromSnapshot.

protected Answer createTemplateFromSnapshot(final CopyCommand cmd) {
    final DataTO srcData = cmd.getSrcTO();
    final DataTO destData = cmd.getDestTO();
    final DataStoreTO srcDataStore = srcData.getDataStore();
    final DataStoreTO destDataStore = destData.getDataStore();
    if (srcDataStore.getRole() == DataStoreRole.Image || srcDataStore.getRole() == DataStoreRole.ImageCache || srcDataStore.getRole() == DataStoreRole.Primary) {
        if (!(srcDataStore instanceof NfsTO)) {
            s_logger.debug("only support nfs storage as src, when create template from snapshot");
            return Answer.createUnsupportedCommandAnswer(cmd);
        }
        if (destDataStore instanceof NfsTO) {
            return copySnapshotToTemplateFromNfsToNfs(cmd, (SnapshotObjectTO) srcData, (NfsTO) srcDataStore, (TemplateObjectTO) destData, (NfsTO) destDataStore);
        }
    }
    s_logger.debug("Failed to create templat from snapshot");
    return new CopyCmdAnswer("Unsupported prototcol");
}
Also used : DataStoreTO(com.cloud.legacymodel.to.DataStoreTO) DataTO(com.cloud.legacymodel.to.DataTO) NfsTO(com.cloud.legacymodel.to.NfsTO) CopyCmdAnswer(com.cloud.legacymodel.communication.answer.CopyCmdAnswer)

Example 2 with CopyCmdAnswer

use of com.cloud.legacymodel.communication.answer.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().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 3 with CopyCmdAnswer

use of com.cloud.legacymodel.communication.answer.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.legacymodel.to.SnapshotObjectTO) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) VolumeVO(com.cloud.storage.VolumeVO) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) SnapshotDataStoreVO(com.cloud.storage.datastore.db.SnapshotDataStoreVO) CreateObjectAnswer(com.cloud.legacymodel.communication.answer.CreateObjectAnswer) CopyCmdAnswer(com.cloud.legacymodel.communication.answer.CopyCmdAnswer)

Example 4 with CopyCmdAnswer

use of com.cloud.legacymodel.communication.answer.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 {
            processDownloadAnswer(answer);
        }
    } catch (final RuntimeException ex) {
        if (event == ObjectInDataStoreStateMachine.Event.OperationFailed) {
            objectInStoreMgr.deleteIfNotReady(this);
        }
        throw ex;
    }
    this.processEvent(event);
}
Also used : CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) VolumeVO(com.cloud.storage.VolumeVO) CreateObjectAnswer(com.cloud.legacymodel.communication.answer.CreateObjectAnswer) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO) CopyCmdAnswer(com.cloud.legacymodel.communication.answer.CopyCmdAnswer)

Example 5 with CopyCmdAnswer

use of com.cloud.legacymodel.communication.answer.CopyCmdAnswer in project cosmic by MissionCriticalCloud.

the class KvmStorageProcessor method backupSnapshot.

@Override
public Answer backupSnapshot(final CopyCommand cmd) {
    final DataTO srcData = cmd.getSrcTO();
    final DataTO destData = cmd.getDestTO();
    final SnapshotObjectTO snapshot = (SnapshotObjectTO) srcData;
    final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) snapshot.getDataStore();
    final SnapshotObjectTO destSnapshot = (SnapshotObjectTO) destData;
    final DataStoreTO imageStore = destData.getDataStore();
    final NfsTO nfsImageStore = (NfsTO) imageStore;
    final String secondaryStoragePoolUrl = nfsImageStore.getUrl();
    // NOTE: snapshot name is encoded in snapshot path
    final int index = snapshot.getPath().lastIndexOf("/");
    // -1 means the snapshot is created from existing vm snapshot
    final boolean isCreatedFromVmSnapshot = index == -1;
    final String snapshotName = snapshot.getPath().substring(index + 1);
    String descName = snapshotName;
    final String volumePath = snapshot.getVolume().getPath();
    final String snapshotDestPath;
    final String snapshotRelPath;
    final String vmName = snapshot.getVmName();
    KvmStoragePool secondaryStoragePool = null;
    Connect conn = null;
    KvmPhysicalDisk snapshotDisk = null;
    KvmStoragePool primaryPool = null;
    try {
        conn = LibvirtConnection.getConnectionByVmName(vmName);
        secondaryStoragePool = this.storagePoolMgr.getStoragePoolByUri(secondaryStoragePoolUrl);
        final String ssPmountPath = secondaryStoragePool.getLocalPath();
        snapshotRelPath = destSnapshot.getPath();
        snapshotDestPath = ssPmountPath + File.separator + snapshotRelPath;
        snapshotDisk = this.storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), volumePath);
        primaryPool = snapshotDisk.getPool();
        long size = 0;
        if (primaryPool.getType() == StoragePoolType.RBD) {
            final String rbdSnapshot = snapshotDisk.getPath() + "@" + snapshotName;
            final String snapshotFile = snapshotDestPath + "/" + snapshotName;
            try {
                this.logger.debug("Attempting to backup RBD snapshot " + rbdSnapshot);
                final File snapDir = new File(snapshotDestPath);
                this.logger.debug("Attempting to create " + snapDir.getAbsolutePath() + " recursively for snapshot storage");
                FileUtils.forceMkdir(snapDir);
                final QemuImgFile srcFile = new QemuImgFile(KvmPhysicalDisk.rbdStringBuilder(primaryPool.getSourceHost(), primaryPool.getSourcePort(), primaryPool.getAuthUserName(), primaryPool.getAuthSecret(), rbdSnapshot));
                srcFile.setFormat(snapshotDisk.getFormat());
                final QemuImgFile destFile = new QemuImgFile(snapshotFile);
                destFile.setFormat(PhysicalDiskFormat.QCOW2);
                this.logger.debug("Backing up RBD snapshot " + rbdSnapshot + " to " + snapshotFile);
                final QemuImg q = new QemuImg(cmd.getWaitInMillSeconds());
                q.convert(srcFile, destFile);
                final File snapFile = new File(snapshotFile);
                if (snapFile.exists()) {
                    size = snapFile.length();
                }
                this.logger.debug("Finished backing up RBD snapshot " + rbdSnapshot + " to " + snapshotFile + " Snapshot size: " + size);
            } catch (final FileNotFoundException e) {
                this.logger.error("Failed to open " + snapshotDestPath + ". The error was: " + e.getMessage());
                return new CopyCmdAnswer(e.toString());
            } catch (final IOException e) {
                this.logger.error("Failed to create " + snapshotDestPath + ". The error was: " + e.getMessage());
                return new CopyCmdAnswer(e.toString());
            } catch (final QemuImgException e) {
                this.logger.error("Failed to backup the RBD snapshot from " + rbdSnapshot + " to " + snapshotFile + " the error was: " + e.getMessage());
                return new CopyCmdAnswer(e.toString());
            }
        } else {
            final Script command = new Script(this.manageSnapshotPath, cmd.getWaitInMillSeconds(), this.logger);
            command.add("-b", snapshotDisk.getPath());
            command.add("-n", snapshotName);
            command.add("-p", snapshotDestPath);
            if (isCreatedFromVmSnapshot) {
                descName = UUID.randomUUID().toString();
            }
            command.add("-t", descName);
            final String result = command.execute();
            if (result != null) {
                this.logger.debug("Failed to backup snaptshot: " + result);
                return new CopyCmdAnswer(result);
            }
            final File snapFile = new File(snapshotDestPath + "/" + descName);
            if (snapFile.exists()) {
                size = snapFile.length();
            }
        }
        final SnapshotObjectTO newSnapshot = new SnapshotObjectTO();
        newSnapshot.setPath(snapshotRelPath + File.separator + descName);
        newSnapshot.setPhysicalSize(size);
        return new CopyCmdAnswer(newSnapshot);
    } catch (final LibvirtException | CloudRuntimeException e) {
        this.logger.debug("Failed to backup snapshot: ", e);
        return new CopyCmdAnswer(e.toString());
    } finally {
        if (isCreatedFromVmSnapshot) {
            this.logger.debug("Ignoring removal of vm snapshot on primary as this snapshot is created from vm snapshot");
        } else {
            try {
                /* Delete the snapshot on primary */
                DomainState state = null;
                Domain vm = null;
                if (vmName != null) {
                    try {
                        vm = this.resource.getDomain(conn, vmName);
                        state = vm.getInfo().state;
                    } catch (final LibvirtException e) {
                        this.logger.trace("Ignoring libvirt error.", e);
                    }
                }
                final KvmStoragePool primaryStorage = this.storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid());
                if (state == DomainState.VIR_DOMAIN_RUNNING && !primaryStorage.isExternalSnapshot()) {
                    final DomainSnapshot snap = vm.snapshotLookupByName(snapshotName);
                    snap.delete(0);
                    /*
                         * libvirt on RHEL6 doesn't handle resume event emitted from
                         * qemu
                         */
                    vm = this.resource.getDomain(conn, vmName);
                    state = vm.getInfo().state;
                    if (state == DomainInfo.DomainState.VIR_DOMAIN_PAUSED) {
                        vm.resume();
                    }
                } else {
                    if (primaryPool.getType() != StoragePoolType.RBD) {
                        final Script command = new Script(this.manageSnapshotPath, this.cmdsTimeout, this.logger);
                        command.add("-d", snapshotDisk.getPath());
                        command.add("-n", snapshotName);
                        final String result = command.execute();
                        if (result != null) {
                            this.logger.debug("Failed to delete snapshot on primary: " + result);
                        // return new CopyCmdAnswer("Failed to backup snapshot: " + result);
                        }
                    }
                }
            } catch (final Exception ex) {
                this.logger.debug("Failed to delete snapshots on primary", ex);
            }
        }
        try {
            if (secondaryStoragePool != null) {
                secondaryStoragePool.delete();
            }
        } catch (final Exception ex) {
            this.logger.debug("Failed to delete secondary storage", ex);
        }
    }
}
Also used : SnapshotObjectTO(com.cloud.legacymodel.to.SnapshotObjectTO) LibvirtException(org.libvirt.LibvirtException) FileNotFoundException(java.io.FileNotFoundException) DataTO(com.cloud.legacymodel.to.DataTO) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) QemuImgException(com.cloud.agent.resource.kvm.storage.utils.QemuImgException) Script(com.cloud.utils.script.Script) PrimaryDataStoreTO(com.cloud.legacymodel.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.legacymodel.to.DataStoreTO) Connect(org.libvirt.Connect) DomainSnapshot(org.libvirt.DomainSnapshot) IOException(java.io.IOException) 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) QemuImg(com.cloud.agent.resource.kvm.storage.utils.QemuImg) PrimaryDataStoreTO(com.cloud.legacymodel.to.PrimaryDataStoreTO) QemuImgFile(com.cloud.agent.resource.kvm.storage.utils.QemuImgFile) DomainState(org.libvirt.DomainInfo.DomainState) Domain(org.libvirt.Domain) File(java.io.File) QemuImgFile(com.cloud.agent.resource.kvm.storage.utils.QemuImgFile) CopyCmdAnswer(com.cloud.legacymodel.communication.answer.CopyCmdAnswer)

Aggregations

CopyCmdAnswer (com.cloud.legacymodel.communication.answer.CopyCmdAnswer)39 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)33 InternalErrorException (com.cloud.legacymodel.exceptions.InternalErrorException)22 NfsTO (com.cloud.legacymodel.to.NfsTO)22 PrimaryDataStoreTO (com.cloud.legacymodel.to.PrimaryDataStoreTO)20 VolumeObjectTO (com.cloud.legacymodel.to.VolumeObjectTO)20 DataTO (com.cloud.legacymodel.to.DataTO)19 DataStoreTO (com.cloud.legacymodel.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.legacymodel.to.TemplateObjectTO)15 URI (java.net.URI)15 SnapshotObjectTO (com.cloud.legacymodel.to.SnapshotObjectTO)11 Task (com.xensource.xenapi.Task)7 CopyCommandResult (com.cloud.engine.subsystem.api.storage.CopyCommandResult)6 CreateObjectAnswer (com.cloud.legacymodel.communication.answer.CreateObjectAnswer)4 VolumeVO (com.cloud.storage.VolumeVO)4