Search in sources :

Example 16 with TemplateObjectTO

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

the class XenServerStorageProcessor method copyTemplateToPrimaryStorage.

@Override
public Answer copyTemplateToPrimaryStorage(final CopyCommand cmd) {
    final DataTO srcDataTo = cmd.getSrcTO();
    final DataTO destDataTo = cmd.getDestTO();
    final int wait = cmd.getWait();
    final DataStoreTO srcDataStoreTo = srcDataTo.getDataStore();
    try {
        if (srcDataStoreTo instanceof NfsTO && srcDataTo.getObjectType() == DataObjectType.TEMPLATE) {
            final NfsTO srcImageStore = (NfsTO) srcDataStoreTo;
            final TemplateObjectTO srcTemplateObjectTo = (TemplateObjectTO) srcDataTo;
            final String storeUrl = srcImageStore.getUrl();
            final URI uri = new URI(storeUrl);
            final String tmplPath = uri.getHost() + ":" + uri.getPath() + "/" + srcDataTo.getPath();
            final DataStoreTO destDataStoreTo = destDataTo.getDataStore();
            boolean managed = false;
            String storageHost = null;
            String managedStoragePoolName = null;
            String managedStoragePoolRootVolumeName = null;
            String managedStoragePoolRootVolumeSize = null;
            String chapInitiatorUsername = null;
            String chapInitiatorSecret = null;
            if (destDataStoreTo instanceof PrimaryDataStoreTO) {
                final PrimaryDataStoreTO destPrimaryDataStoreTo = (PrimaryDataStoreTO) destDataStoreTo;
                final Map<String, String> details = destPrimaryDataStoreTo.getDetails();
                if (details != null) {
                    managed = Boolean.parseBoolean(details.get(PrimaryDataStoreTO.MANAGED));
                    if (managed) {
                        storageHost = details.get(PrimaryDataStoreTO.STORAGE_HOST);
                        managedStoragePoolName = details.get(PrimaryDataStoreTO.MANAGED_STORE_TARGET);
                        managedStoragePoolRootVolumeName = details.get(PrimaryDataStoreTO.MANAGED_STORE_TARGET_ROOT_VOLUME);
                        managedStoragePoolRootVolumeSize = details.get(PrimaryDataStoreTO.VOLUME_SIZE);
                        chapInitiatorUsername = details.get(PrimaryDataStoreTO.CHAP_INITIATOR_USERNAME);
                        chapInitiatorSecret = details.get(PrimaryDataStoreTO.CHAP_INITIATOR_SECRET);
                    }
                }
            }
            final Connection conn = hypervisorResource.getConnection();
            final SR sr;
            if (managed) {
                final Map<String, String> details = new HashMap<>();
                details.put(DiskTO.STORAGE_HOST, storageHost);
                details.put(DiskTO.IQN, managedStoragePoolName);
                details.put(DiskTO.VOLUME_SIZE, managedStoragePoolRootVolumeSize);
                details.put(DiskTO.CHAP_INITIATOR_USERNAME, chapInitiatorUsername);
                details.put(DiskTO.CHAP_INITIATOR_SECRET, chapInitiatorSecret);
                sr = hypervisorResource.prepareManagedSr(conn, details);
            } else {
                final String srName = destDataStoreTo.getUuid();
                final Set<SR> srs = SR.getByNameLabel(conn, srName);
                if (srs.size() != 1) {
                    final String msg = "There are " + srs.size() + " SRs with same name: " + srName;
                    s_logger.warn(msg);
                    return new CopyCmdAnswer(msg);
                } else {
                    sr = srs.iterator().next();
                }
            }
            final String srUuid = sr.getUuid(conn);
            final String tmplUuid = copy_vhd_from_secondarystorage(conn, tmplPath, srUuid, wait);
            final VDI tmplVdi = getVDIbyUuid(conn, tmplUuid);
            final String uuidToReturn;
            final Long physicalSize = tmplVdi.getPhysicalUtilisation(conn);
            if (managed) {
                uuidToReturn = tmplUuid;
                tmplVdi.setNameLabel(conn, managedStoragePoolRootVolumeName);
            } else {
                final VDI snapshotVdi = tmplVdi.snapshot(conn, new HashMap<>());
                uuidToReturn = snapshotVdi.getUuid(conn);
                snapshotVdi.setNameLabel(conn, "Template " + srcTemplateObjectTo.getName());
                tmplVdi.destroy(conn);
            }
            sr.scan(conn);
            try {
                Thread.sleep(5000);
            } catch (final InterruptedException e) {
            }
            final TemplateObjectTO newVol = new TemplateObjectTO();
            newVol.setUuid(uuidToReturn);
            newVol.setPath(uuidToReturn);
            if (physicalSize != null) {
                newVol.setSize(physicalSize);
            }
            newVol.setFormat(ImageFormat.VHD);
            return new CopyCmdAnswer(newVol);
        }
    } catch (final Exception e) {
        final String msg = "Catch Exception " + e.getClass().getName() + " for template + " + " due to " + e.toString();
        s_logger.warn(msg, e);
        return new CopyCmdAnswer(msg);
    }
    return new CopyCmdAnswer("not implemented yet");
}
Also used : PrimaryDataStoreTO(com.cloud.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) HashMap(java.util.HashMap) Connection(com.xensource.xenapi.Connection) NfsTO(com.cloud.agent.api.to.NfsTO) URI(java.net.URI) XenAPIException(com.xensource.xenapi.Types.XenAPIException) InternalErrorException(com.cloud.exception.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataTO(com.cloud.agent.api.to.DataTO) PrimaryDataStoreTO(com.cloud.storage.to.PrimaryDataStoreTO) VDI(com.xensource.xenapi.VDI) TemplateObjectTO(com.cloud.storage.to.TemplateObjectTO) CopyCmdAnswer(com.cloud.storage.command.CopyCmdAnswer) SR(com.xensource.xenapi.SR)

Example 17 with TemplateObjectTO

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

the class Xenserver625StorageProcessor method createTemplateFromSnapshot2.

public Answer createTemplateFromSnapshot2(final CopyCommand cmd) {
    final Connection conn = hypervisorResource.getConnection();
    final SnapshotObjectTO snapshotObjTO = (SnapshotObjectTO) cmd.getSrcTO();
    final TemplateObjectTO templateObjTO = (TemplateObjectTO) cmd.getDestTO();
    if (!(snapshotObjTO.getDataStore() instanceof PrimaryDataStoreTO) || !(templateObjTO.getDataStore() instanceof NfsTO)) {
        return null;
    }
    NfsTO destStore = null;
    URI destUri = null;
    try {
        destStore = (NfsTO) templateObjTO.getDataStore();
        destUri = new URI(destStore.getUrl());
    } catch (final Exception ex) {
        s_logger.debug("Invalid URI", ex);
        return new CopyCmdAnswer("Invalid URI: " + ex.toString());
    }
    SR srcSr = null;
    SR destSr = null;
    final String destDir = templateObjTO.getPath();
    VDI destVdi = null;
    boolean result = false;
    try {
        final Map<String, String> srcDetails = cmd.getOptions();
        final String iScsiName = srcDetails.get(DiskTO.IQN);
        final String storageHost = srcDetails.get(DiskTO.STORAGE_HOST);
        final String chapInitiatorUsername = srcDetails.get(DiskTO.CHAP_INITIATOR_USERNAME);
        final String chapInitiatorSecret = srcDetails.get(DiskTO.CHAP_INITIATOR_SECRET);
        srcSr = hypervisorResource.getIscsiSR(conn, iScsiName, storageHost, iScsiName, chapInitiatorUsername, chapInitiatorSecret, true);
        final String destNfsPath = destUri.getHost() + ":" + destUri.getPath();
        final String localDir = "/var/cloud_mount/" + UUID.nameUUIDFromBytes(destNfsPath.getBytes());
        mountNfs(conn, destNfsPath, localDir);
        makeDirectory(conn, localDir + "/" + destDir);
        destSr = createFileSR(conn, localDir + "/" + destDir);
        // there should only be one VDI in this SR
        final VDI srcVdi = srcSr.getVDIs(conn).iterator().next();
        destVdi = srcVdi.copy(conn, destSr);
        final String nameLabel = "cloud-" + UUID.randomUUID().toString();
        destVdi.setNameLabel(conn, nameLabel);
        // scan makes XenServer pick up VDI physicalSize
        destSr.scan(conn);
        final String templateUuid = destVdi.getUuid(conn);
        final String templateFilename = templateUuid + ".vhd";
        final long virtualSize = destVdi.getVirtualSize(conn);
        final long physicalSize = destVdi.getPhysicalUtilisation(conn);
        // create the template.properties file
        String templatePath = destNfsPath + "/" + destDir;
        templatePath = templatePath.replaceAll("//", "/");
        result = hypervisorResource.postCreatePrivateTemplate(conn, templatePath, templateFilename, templateUuid, nameLabel, null, physicalSize, virtualSize, templateObjTO.getId());
        if (!result) {
            throw new CloudRuntimeException("Could not create the template.properties file on secondary storage dir");
        }
        final TemplateObjectTO newTemplate = new TemplateObjectTO();
        newTemplate.setPath(destDir + "/" + templateFilename);
        newTemplate.setFormat(Storage.ImageFormat.VHD);
        newTemplate.setHypervisorType(HypervisorType.XenServer);
        newTemplate.setSize(virtualSize);
        newTemplate.setPhysicalSize(physicalSize);
        newTemplate.setName(templateUuid);
        result = true;
        return new CopyCmdAnswer(newTemplate);
    // } catch (Exception ex) {
    // s_logger.error("Failed to create a template from a snapshot",
    // ex);
    // 
    // return new
    // CopyCmdAnswer("Failed to create a template from a snapshot: " +
    // ex.toString());
    } catch (final BadServerResponse e) {
        s_logger.error("Failed to create a template from a snapshot due to incomprehensible server response", e);
        return new CopyCmdAnswer("Failed to create a template from a snapshot: " + e.toString());
    } catch (final XenAPIException e) {
        s_logger.error("Failed to create a template from a snapshot due to xenapi error", e);
        return new CopyCmdAnswer("Failed to create a template from a snapshot: " + e.toString());
    } catch (final XmlRpcException e) {
        s_logger.error("Failed to create a template from a snapshot due to rpc error", e);
        return new CopyCmdAnswer("Failed to create a template from a snapshot: " + e.toString());
    } finally {
        if (!result) {
            if (destVdi != null) {
                try {
                    destVdi.destroy(conn);
                } catch (final Exception e) {
                    s_logger.debug("Cleaned up leftover VDI on destination storage due to failure: ", e);
                }
            }
        }
        if (srcSr != null) {
            hypervisorResource.removeSR(conn, srcSr);
        }
        if (destSr != null) {
            hypervisorResource.removeSR(conn, destSr);
        }
    }
}
Also used : SnapshotObjectTO(com.cloud.storage.to.SnapshotObjectTO) BadServerResponse(com.xensource.xenapi.Types.BadServerResponse) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) NfsTO(com.cloud.agent.api.to.NfsTO) URI(java.net.URI) InternalErrorException(com.cloud.exception.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) PrimaryDataStoreTO(com.cloud.storage.to.PrimaryDataStoreTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VDI(com.xensource.xenapi.VDI) TemplateObjectTO(com.cloud.storage.to.TemplateObjectTO) CopyCmdAnswer(com.cloud.storage.command.CopyCmdAnswer) XmlRpcException(org.apache.xmlrpc.XmlRpcException) SR(com.xensource.xenapi.SR)

Example 18 with TemplateObjectTO

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

the class Xenserver625StorageProcessor method createTemplateFromSnapshot.

@Override
public Answer createTemplateFromSnapshot(final CopyCommand cmd) {
    final Connection conn = hypervisorResource.getConnection();
    final DataTO srcData = cmd.getSrcTO();
    final DataTO destData = cmd.getDestTO();
    if (srcData.getDataStore() instanceof PrimaryDataStoreTO && destData.getDataStore() instanceof NfsTO) {
        return createTemplateFromSnapshot2(cmd);
    }
    final int wait = cmd.getWait();
    final SnapshotObjectTO srcObj = (SnapshotObjectTO) srcData;
    final TemplateObjectTO destObj = (TemplateObjectTO) destData;
    final NfsTO srcStore = (NfsTO) srcObj.getDataStore();
    final NfsTO destStore = (NfsTO) destObj.getDataStore();
    URI srcUri = null;
    URI destUri = null;
    try {
        srcUri = new URI(srcStore.getUrl());
        destUri = new URI(destStore.getUrl());
    } catch (final Exception e) {
        s_logger.debug("incorrect url", e);
        return new CopyCmdAnswer("incorrect url" + e.toString());
    }
    final String srcPath = srcObj.getPath();
    final int index = srcPath.lastIndexOf("/");
    final String srcDir = srcPath.substring(0, index);
    final String destDir = destObj.getPath();
    SR srcSr = null;
    SR destSr = null;
    VDI destVdi = null;
    boolean result = false;
    try {
        srcSr = createFileSr(conn, srcUri.getHost() + ":" + srcUri.getPath(), srcDir);
        final String destNfsPath = destUri.getHost() + ":" + destUri.getPath();
        final String localDir = "/var/cloud_mount/" + UUID.nameUUIDFromBytes(destNfsPath.getBytes());
        mountNfs(conn, destUri.getHost() + ":" + destUri.getPath(), localDir);
        makeDirectory(conn, localDir + "/" + destDir);
        destSr = createFileSR(conn, localDir + "/" + destDir);
        final String nameLabel = "cloud-" + UUID.randomUUID().toString();
        final String[] parents = srcObj.getParents();
        final List<VDI> snapshotChains = new ArrayList<>();
        if (parents != null) {
            for (int i = 0; i < parents.length; i++) {
                final String snChainPath = parents[i];
                final String uuid = getSnapshotUuid(snChainPath);
                final VDI chain = VDI.getByUuid(conn, uuid);
                snapshotChains.add(chain);
            }
        }
        final String snapshotUuid = getSnapshotUuid(srcPath);
        final VDI snapshotVdi = VDI.getByUuid(conn, snapshotUuid);
        snapshotChains.add(snapshotVdi);
        final long templateVirtualSize = snapshotChains.get(0).getVirtualSize(conn);
        destVdi = createVdi(conn, nameLabel, destSr, templateVirtualSize);
        final String destVdiUuid = destVdi.getUuid(conn);
        for (final VDI snapChain : snapshotChains) {
            final Task task = snapChain.copyAsync(conn, null, null, destVdi);
            // poll every 1 seconds ,
            hypervisorResource.waitForTask(conn, task, 1000, wait * 1000);
            hypervisorResource.checkForSuccess(conn, task);
            task.destroy(conn);
        }
        destVdi = VDI.getByUuid(conn, destVdiUuid);
        // scan makes XenServer pick up VDI physicalSize
        destSr.scan(conn);
        final String templateUuid = destVdi.getUuid(conn);
        final String templateFilename = templateUuid + ".vhd";
        final long virtualSize = destVdi.getVirtualSize(conn);
        final long physicalSize = destVdi.getPhysicalUtilisation(conn);
        String templatePath = destNfsPath + "/" + destDir;
        templatePath = templatePath.replaceAll("//", "/");
        result = hypervisorResource.postCreatePrivateTemplate(conn, templatePath, templateFilename, templateUuid, nameLabel, null, physicalSize, virtualSize, destObj.getId());
        if (!result) {
            throw new CloudRuntimeException("Could not create the template.properties file on secondary storage dir");
        }
        final TemplateObjectTO newTemplate = new TemplateObjectTO();
        newTemplate.setPath(destDir + "/" + templateFilename);
        newTemplate.setFormat(Storage.ImageFormat.VHD);
        newTemplate.setSize(destVdi.getVirtualSize(conn));
        newTemplate.setPhysicalSize(destVdi.getPhysicalUtilisation(conn));
        newTemplate.setName(destVdiUuid);
        result = true;
        return new CopyCmdAnswer(newTemplate);
    } catch (final Exception e) {
        s_logger.error("Failed create template from snapshot", e);
        return new CopyCmdAnswer("Failed create template from snapshot " + e.toString());
    } finally {
        if (!result) {
            if (destVdi != null) {
                try {
                    destVdi.destroy(conn);
                } catch (final Exception e) {
                    s_logger.debug("Clean up left over on dest storage failed: ", e);
                }
            }
        }
        if (srcSr != null) {
            hypervisorResource.removeSR(conn, srcSr);
        }
        if (destSr != null) {
            hypervisorResource.removeSR(conn, destSr);
        }
    }
}
Also used : SnapshotObjectTO(com.cloud.storage.to.SnapshotObjectTO) Task(com.xensource.xenapi.Task) Connection(com.xensource.xenapi.Connection) ArrayList(java.util.ArrayList) NfsTO(com.cloud.agent.api.to.NfsTO) URI(java.net.URI) InternalErrorException(com.cloud.exception.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) DataTO(com.cloud.agent.api.to.DataTO) PrimaryDataStoreTO(com.cloud.storage.to.PrimaryDataStoreTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VDI(com.xensource.xenapi.VDI) TemplateObjectTO(com.cloud.storage.to.TemplateObjectTO) CopyCmdAnswer(com.cloud.storage.command.CopyCmdAnswer) SR(com.xensource.xenapi.SR)

Example 19 with TemplateObjectTO

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

the class VolumeServiceImpl method managedCopyBaseImageCallback.

protected Void managedCopyBaseImageCallback(final AsyncCallbackDispatcher<VolumeServiceImpl, CopyCommandResult> callback, final ManagedCreateBaseImageContext<VolumeApiResult> context) {
    final CopyCommandResult result = callback.getResult();
    final VolumeInfo volumeInfo = context.getVolumeInfo();
    final VolumeApiResult res = new VolumeApiResult(volumeInfo);
    if (result.isSuccess()) {
        // volumeInfo.processEvent(Event.OperationSuccessed, result.getAnswer());
        final VolumeVO volume = volDao.findById(volumeInfo.getId());
        final CopyCmdAnswer answer = (CopyCmdAnswer) result.getAnswer();
        final TemplateObjectTO templateObjectTo = (TemplateObjectTO) answer.getNewData();
        volume.setPath(templateObjectTo.getPath());
        if (templateObjectTo.getFormat() != null) {
            volume.setFormat(templateObjectTo.getFormat());
        }
        volDao.update(volume.getId(), volume);
    } else {
        volumeInfo.processEvent(Event.DestroyRequested);
        res.setResult(result.getResult());
    }
    final AsyncCallFuture<VolumeApiResult> future = context.getFuture();
    future.complete(res);
    return null;
}
Also used : VolumeVO(com.cloud.storage.VolumeVO) VolumeInfo(com.cloud.engine.subsystem.api.storage.VolumeInfo) TemplateObjectTO(com.cloud.storage.to.TemplateObjectTO) CopyCommandResult(com.cloud.engine.subsystem.api.storage.CopyCommandResult) CopyCmdAnswer(com.cloud.storage.command.CopyCmdAnswer)

Example 20 with TemplateObjectTO

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

the class CloudStackPrimaryDataStoreDriverImpl method copyAsync.

@Override
public void copyAsync(final DataObject srcdata, final DataObject destData, final AsyncCompletionCallback<CopyCommandResult> callback) {
    final DataStore store = destData.getDataStore();
    if (store.getRole() == DataStoreRole.Primary) {
        if ((srcdata.getType() == DataObjectType.TEMPLATE && destData.getType() == DataObjectType.TEMPLATE)) {
            // For CLVM, we need to copy template to primary storage at all, just fake the copy result.
            final TemplateObjectTO templateObjectTO = new TemplateObjectTO();
            templateObjectTO.setPath(UUID.randomUUID().toString());
            templateObjectTO.setSize(srcdata.getSize());
            templateObjectTO.setPhysicalSize(srcdata.getSize());
            templateObjectTO.setFormat(Storage.ImageFormat.RAW);
            final CopyCmdAnswer answer = new CopyCmdAnswer(templateObjectTO);
            final CopyCommandResult result = new CopyCommandResult("", answer);
            callback.complete(result);
        } else if (srcdata.getType() == DataObjectType.TEMPLATE && destData.getType() == DataObjectType.VOLUME) {
            // For CLVM, we need to pass template on secondary storage to hypervisor
            final String value = configDao.getValue(Config.PrimaryStorageDownloadWait.toString());
            final int _primaryStorageDownloadWait = NumbersUtil.parseInt(value, Integer.parseInt(Config.PrimaryStorageDownloadWait.getDefaultValue()));
            final StoragePoolVO storagePoolVO = primaryStoreDao.findById(store.getId());
            final DataStore imageStore = templateManager.getImageStore(storagePoolVO.getDataCenterId(), srcdata.getId());
            final DataObject srcData = templateDataFactory.getTemplate(srcdata.getId(), imageStore);
            final CopyCommand cmd = new CopyCommand(srcData.getTO(), destData.getTO(), _primaryStorageDownloadWait, true);
            final EndPoint ep = epSelector.select(srcData, destData);
            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(cmd, false, errMsg);
            } else {
                answer = ep.sendMessage(cmd);
            }
            final CopyCommandResult result = new CopyCommandResult("", answer);
            callback.complete(result);
        }
    }
}
Also used : ResizeVolumeAnswer(com.cloud.agent.api.storage.ResizeVolumeAnswer) Answer(com.cloud.agent.api.Answer) CopyCmdAnswer(com.cloud.storage.command.CopyCmdAnswer) DataObject(com.cloud.engine.subsystem.api.storage.DataObject) CopyCommand(com.cloud.storage.command.CopyCommand) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO) TemplateObjectTO(com.cloud.storage.to.TemplateObjectTO) EndPoint(com.cloud.engine.subsystem.api.storage.EndPoint) CopyCommandResult(com.cloud.engine.subsystem.api.storage.CopyCommandResult) CopyCmdAnswer(com.cloud.storage.command.CopyCmdAnswer)

Aggregations

TemplateObjectTO (com.cloud.storage.to.TemplateObjectTO)24 CopyCmdAnswer (com.cloud.storage.command.CopyCmdAnswer)17 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)17 NfsTO (com.cloud.agent.api.to.NfsTO)15 InternalErrorException (com.cloud.exception.InternalErrorException)14 PrimaryDataStoreTO (com.cloud.storage.to.PrimaryDataStoreTO)12 XenAPIException (com.xensource.xenapi.Types.XenAPIException)11 XmlRpcException (org.apache.xmlrpc.XmlRpcException)11 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)10 DataTO (com.cloud.agent.api.to.DataTO)10 VDI (com.xensource.xenapi.VDI)10 Connection (com.xensource.xenapi.Connection)9 SR (com.xensource.xenapi.SR)9 URI (java.net.URI)8 VolumeObjectTO (com.cloud.storage.to.VolumeObjectTO)7 DiskTO (com.cloud.agent.api.to.DiskTO)6 URISyntaxException (java.net.URISyntaxException)6 ConfigurationException (javax.naming.ConfigurationException)6 IOException (java.io.IOException)5 Answer (com.cloud.agent.api.Answer)4