Search in sources :

Example 11 with SnapshotObjectTO

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

the class LibvirtRevertSnapshotCommandWrapper method execute.

@Override
public Answer execute(final RevertSnapshotCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final SnapshotObjectTO snapshot = command.getData();
    final VolumeObjectTO volume = snapshot.getVolume();
    final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) volume.getDataStore();
    final DataStoreTO snapshotImageStore = snapshot.getDataStore();
    if (!(snapshotImageStore instanceof NfsTO)) {
        return new Answer(command, false, "revert snapshot on object storage is not implemented yet");
    }
    final NfsTO nfsImageStore = (NfsTO) snapshotImageStore;
    final String secondaryStoragePoolUrl = nfsImageStore.getUrl();
    final String volumePath = volume.getPath();
    String snapshotPath = null;
    String snapshotRelPath = null;
    KvmStoragePool secondaryStoragePool = null;
    try {
        final KvmStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
        secondaryStoragePool = storagePoolMgr.getStoragePoolByUri(secondaryStoragePoolUrl);
        final String ssPmountPath = secondaryStoragePool.getLocalPath();
        snapshotRelPath = snapshot.getPath();
        snapshotPath = ssPmountPath + File.separator + snapshotRelPath;
        final KvmPhysicalDisk snapshotDisk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), volumePath);
        final KvmStoragePool primaryPool = snapshotDisk.getPool();
        if (primaryPool.getType() == StoragePoolType.RBD) {
            return new Answer(command, false, "revert snapshot to RBD is not implemented yet");
        } else {
            final Script cmd = new Script(libvirtComputingResource.manageSnapshotPath(), libvirtComputingResource.getCmdsTimeout(), s_logger);
            cmd.add("-v", snapshotPath);
            cmd.add("-n", snapshotDisk.getName());
            cmd.add("-p", snapshotDisk.getPath());
            final String result = cmd.execute();
            if (result != null) {
                s_logger.debug("Failed to revert snaptshot: " + result);
                return new Answer(command, false, result);
            }
        }
        return new Answer(command, true, "RevertSnapshotCommand executes successfully");
    } catch (final CloudRuntimeException e) {
        return new Answer(command, false, e.toString());
    }
}
Also used : SnapshotObjectTO(com.cloud.legacymodel.to.SnapshotObjectTO) Answer(com.cloud.legacymodel.communication.answer.Answer) Script(com.cloud.utils.script.Script) PrimaryDataStoreTO(com.cloud.legacymodel.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.legacymodel.to.DataStoreTO) KvmStoragePool(com.cloud.agent.resource.kvm.storage.KvmStoragePool) PrimaryDataStoreTO(com.cloud.legacymodel.to.PrimaryDataStoreTO) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO) KvmStoragePoolManager(com.cloud.agent.resource.kvm.storage.KvmStoragePoolManager) KvmPhysicalDisk(com.cloud.agent.resource.kvm.storage.KvmPhysicalDisk) NfsTO(com.cloud.legacymodel.to.NfsTO)

Example 12 with SnapshotObjectTO

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

the class CloudStackPrimaryDataStoreDriverImpl method revertSnapshot.

@Override
public void revertSnapshot(final SnapshotInfo snapshot, final SnapshotInfo snapshotOnPrimaryStore, final AsyncCompletionCallback<CommandResult> callback) {
    final SnapshotObjectTO snapshotTO = (SnapshotObjectTO) snapshot.getTO();
    final RevertSnapshotCommand cmd = new RevertSnapshotCommand(snapshotTO);
    final CommandResult result = new CommandResult();
    try {
        final EndPoint ep = epSelector.select(snapshotOnPrimaryStore);
        if (ep == null) {
            final String errMsg = "No remote endpoint to send RevertSnapshotCommand, check if host or ssvm is down?";
            s_logger.error(errMsg);
            result.setResult(errMsg);
        } else {
            final Answer answer = ep.sendMessage(cmd);
            if (answer != null && !answer.getResult()) {
                result.setResult(answer.getDetails());
            }
        }
    } catch (final Exception ex) {
        s_logger.debug("Unable to revert snapshot " + snapshot.getId(), ex);
        result.setResult(ex.toString());
    }
    callback.complete(result);
}
Also used : SnapshotObjectTO(com.cloud.legacymodel.to.SnapshotObjectTO) Answer(com.cloud.legacymodel.communication.answer.Answer) ResizeVolumeAnswer(com.cloud.legacymodel.communication.answer.ResizeVolumeAnswer) CopyCmdAnswer(com.cloud.legacymodel.communication.answer.CopyCmdAnswer) EndPoint(com.cloud.engine.subsystem.api.storage.EndPoint) RevertSnapshotCommand(com.cloud.legacymodel.communication.command.RevertSnapshotCommand) StorageUnavailableException(com.cloud.legacymodel.exceptions.StorageUnavailableException) CommandResult(com.cloud.storage.command.CommandResult) CopyCommandResult(com.cloud.engine.subsystem.api.storage.CopyCommandResult)

Example 13 with SnapshotObjectTO

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

the class SnapshotObject method getTO.

@Override
public DataTO getTO() {
    final DataTO to = store.getDriver().getTO(this);
    if (to == null) {
        SnapshotObjectTO snapshotObjectTO = new SnapshotObjectTO();
        snapshotObjectTO.setPath(this.getPath());
        snapshotObjectTO.setId(this.getId());
        final VolumeInfo vol = this.getBaseVolume();
        if (vol != null) {
            snapshotObjectTO.setVolume((VolumeObjectTO) vol.getTO());
            snapshotObjectTO.setVmName(vol.getAttachedVmName());
        }
        SnapshotInfo parentSnapshot = this.getParent();
        final ArrayList<String> parentsArry = new ArrayList<>();
        if (parentSnapshot != null) {
            snapshotObjectTO.setParentSnapshotPath(parentSnapshot.getPath());
            while (parentSnapshot != null) {
                parentsArry.add(parentSnapshot.getPath());
                parentSnapshot = parentSnapshot.getParent();
            }
            snapshotObjectTO.setParents(parentsArry.toArray(new String[parentsArry.size()]));
            ArrayUtils.reverse(snapshotObjectTO.getParents());
        }
        snapshotObjectTO.setDataStore(this.getDataStore().getTO());
        snapshotObjectTO.setName(this.getName());
        snapshotObjectTO.setHypervisorType(this.getHypervisorType());
        snapshotObjectTO.setQuiescevm(false);
        return snapshotObjectTO;
    }
    return to;
}
Also used : SnapshotObjectTO(com.cloud.legacymodel.to.SnapshotObjectTO) SnapshotInfo(com.cloud.engine.subsystem.api.storage.SnapshotInfo) DataTO(com.cloud.legacymodel.to.DataTO) ArrayList(java.util.ArrayList) VolumeInfo(com.cloud.engine.subsystem.api.storage.VolumeInfo)

Example 14 with SnapshotObjectTO

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

the class XenServerStorageProcessor method createVolumeFromSnapshot.

@Override
public Answer createVolumeFromSnapshot(final CopyCommand cmd) {
    final Connection conn = hypervisorResource.getConnection();
    final DataTO srcData = cmd.getSrcTO();
    final SnapshotObjectTO snapshot = (SnapshotObjectTO) srcData;
    final DataTO destData = cmd.getDestTO();
    final DataStoreTO imageStore = srcData.getDataStore();
    if (srcData.getDataStore() instanceof PrimaryDataStoreTO && destData.getDataStore() instanceof PrimaryDataStoreTO) {
        return createVolumeFromSnapshot2(cmd);
    }
    if (!(imageStore instanceof NfsTO)) {
        return new CopyCmdAnswer("unsupported protocol");
    }
    final NfsTO nfsImageStore = (NfsTO) imageStore;
    final String primaryStorageNameLabel = destData.getDataStore().getUuid();
    final String secondaryStorageUrl = nfsImageStore.getUrl();
    final int wait = cmd.getWait();
    boolean result = false;
    // Generic error message.
    String details = null;
    String volumeUUID = null;
    if (secondaryStorageUrl == null) {
        details += " because the URL passed: " + secondaryStorageUrl + " is invalid.";
        return new CopyCmdAnswer(details);
    }
    try {
        final SR primaryStorageSR = hypervisorResource.getSRByNameLabelandHost(conn, primaryStorageNameLabel);
        if (primaryStorageSR == null) {
            throw new InternalErrorException("Could not create volume from snapshot because the primary Storage SR could not be created from the name label: " + primaryStorageNameLabel);
        }
        // Get the absolute path of the snapshot on the secondary storage.
        String snapshotInstallPath = snapshot.getPath();
        final int index = snapshotInstallPath.lastIndexOf(nfsImageStore.getPathSeparator());
        final String snapshotName = snapshotInstallPath.substring(index + 1);
        if (!snapshotName.startsWith("VHD-") && !snapshotName.endsWith(".vhd")) {
            snapshotInstallPath = snapshotInstallPath + ".vhd";
        }
        final URI snapshotURI = new URI(secondaryStorageUrl + nfsImageStore.getPathSeparator() + snapshotInstallPath);
        final String snapshotPath = snapshotURI.getHost() + ":" + snapshotURI.getPath();
        final String srUuid = primaryStorageSR.getUuid(conn);
        volumeUUID = copy_vhd_from_secondarystorage(conn, snapshotPath, srUuid, wait);
        result = true;
        final VDI volume = VDI.getByUuid(conn, volumeUUID);
        final VDI.Record vdir = volume.getRecord(conn);
        final VolumeObjectTO newVol = new VolumeObjectTO();
        newVol.setPath(volumeUUID);
        newVol.setSize(vdir.virtualSize);
        return new CopyCmdAnswer(newVol);
    } catch (final XenAPIException e) {
        details += " due to " + e.toString();
        s_logger.warn(details, e);
    } catch (final Exception e) {
        details += " due to " + e.getMessage();
        s_logger.warn(details, e);
    }
    if (!result) {
        // Is this logged at a higher level?
        s_logger.error(details);
    }
    // In all cases return something.
    return new CopyCmdAnswer(details);
}
Also used : SnapshotObjectTO(com.cloud.legacymodel.to.SnapshotObjectTO) PrimaryDataStoreTO(com.cloud.legacymodel.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.legacymodel.to.DataStoreTO) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) InternalErrorException(com.cloud.legacymodel.exceptions.InternalErrorException) NfsTO(com.cloud.legacymodel.to.NfsTO) URI(java.net.URI) XenAPIException(com.xensource.xenapi.Types.XenAPIException) InternalErrorException(com.cloud.legacymodel.exceptions.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) DataTO(com.cloud.legacymodel.to.DataTO) PrimaryDataStoreTO(com.cloud.legacymodel.to.PrimaryDataStoreTO) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO) VDI(com.xensource.xenapi.VDI) CopyCmdAnswer(com.cloud.legacymodel.communication.answer.CopyCmdAnswer) SR(com.xensource.xenapi.SR)

Example 15 with SnapshotObjectTO

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

the class XenServerStorageProcessor method createSnapshot.

@Override
public Answer createSnapshot(final CreateObjectCommand cmd) {
    final Connection conn = hypervisorResource.getConnection();
    final SnapshotObjectTO snapshotTO = (SnapshotObjectTO) cmd.getData();
    final long snapshotId = snapshotTO.getId();
    final String snapshotName = snapshotTO.getName();
    String details = "create snapshot operation Failed for snapshotId: " + snapshotId;
    String snapshotUUID = null;
    try {
        final String volumeUUID = snapshotTO.getVolume().getPath();
        final VDI volume = VDI.getByUuid(conn, volumeUUID);
        final VDI snapshot = volume.snapshot(conn, new HashMap<>());
        if (snapshotName != null) {
            snapshot.setNameLabel(conn, snapshotName);
        }
        snapshotUUID = snapshot.getUuid(conn);
        final String preSnapshotUUID = snapshotTO.getParentSnapshotPath();
        // check if it is a empty snapshot
        if (preSnapshotUUID != null) {
            final SR sr = volume.getSR(conn);
            final String srUUID = sr.getUuid(conn);
            final String type = sr.getType(conn);
            final Boolean isISCSI = IsISCSI(type);
            final String snapshotParentUUID = getVhdParent(conn, srUUID, snapshotUUID, isISCSI);
            try {
                final String preSnapshotParentUUID = getVhdParent(conn, srUUID, preSnapshotUUID, isISCSI);
                if (snapshotParentUUID != null && snapshotParentUUID.equals(preSnapshotParentUUID)) {
                    // this is empty snapshot, remove it
                    snapshot.destroy(conn);
                    snapshotUUID = preSnapshotUUID;
                }
            } catch (final Exception e) {
                s_logger.debug("Failed to get parent snapshot", e);
            }
        }
        final SnapshotObjectTO newSnapshot = new SnapshotObjectTO();
        newSnapshot.setPath(snapshotUUID);
        return new CreateObjectAnswer(newSnapshot);
    } catch (final XenAPIException e) {
        details += ", reason: " + e.toString();
        s_logger.warn(details, e);
    } catch (final Exception e) {
        details += ", reason: " + e.toString();
        s_logger.warn(details, e);
    }
    return new CreateObjectAnswer(details);
}
Also used : SnapshotObjectTO(com.cloud.legacymodel.to.SnapshotObjectTO) Connection(com.xensource.xenapi.Connection) CreateObjectAnswer(com.cloud.legacymodel.communication.answer.CreateObjectAnswer) XenAPIException(com.xensource.xenapi.Types.XenAPIException) VDI(com.xensource.xenapi.VDI) XenAPIException(com.xensource.xenapi.Types.XenAPIException) InternalErrorException(com.cloud.legacymodel.exceptions.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) SR(com.xensource.xenapi.SR)

Aggregations

SnapshotObjectTO (com.cloud.legacymodel.to.SnapshotObjectTO)19 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)15 CopyCmdAnswer (com.cloud.legacymodel.communication.answer.CopyCmdAnswer)14 PrimaryDataStoreTO (com.cloud.legacymodel.to.PrimaryDataStoreTO)12 InternalErrorException (com.cloud.legacymodel.exceptions.InternalErrorException)10 NfsTO (com.cloud.legacymodel.to.NfsTO)10 DataTO (com.cloud.legacymodel.to.DataTO)9 Connection (com.xensource.xenapi.Connection)9 XenAPIException (com.xensource.xenapi.Types.XenAPIException)9 VDI (com.xensource.xenapi.VDI)9 XmlRpcException (org.apache.xmlrpc.XmlRpcException)9 DataStoreTO (com.cloud.legacymodel.to.DataStoreTO)8 SR (com.xensource.xenapi.SR)8 URI (java.net.URI)7 VolumeObjectTO (com.cloud.legacymodel.to.VolumeObjectTO)6 CreateObjectAnswer (com.cloud.legacymodel.communication.answer.CreateObjectAnswer)5 Answer (com.cloud.legacymodel.communication.answer.Answer)4 TemplateObjectTO (com.cloud.legacymodel.to.TemplateObjectTO)4 Script (com.cloud.utils.script.Script)4 Task (com.xensource.xenapi.Task)3