Search in sources :

Example 81 with DataStoreTO

use of com.cloud.agent.api.to.DataStoreTO in project cloudstack by apache.

the class Xenserver625StorageProcessor method copyTemplateToPrimaryStorage.

@Override
public Answer copyTemplateToPrimaryStorage(final CopyCommand cmd) {
    final DataTO srcData = cmd.getSrcTO();
    final DataTO destData = cmd.getDestTO();
    final int wait = cmd.getWait();
    final DataStoreTO srcStore = srcData.getDataStore();
    final Connection conn = hypervisorResource.getConnection();
    SR srcSr = null;
    SR destSr = null;
    boolean removeSrAfterCopy = false;
    Task task = null;
    try {
        if (srcStore instanceof NfsTO && srcData.getObjectType() == DataObjectType.TEMPLATE) {
            final NfsTO srcImageStore = (NfsTO) srcStore;
            final TemplateObjectTO srcTemplate = (TemplateObjectTO) srcData;
            final String storeUrl = srcImageStore.getUrl();
            final URI uri = new URI(storeUrl);
            String volumePath = srcData.getPath();
            volumePath = StringUtils.stripEnd(volumePath, "/");
            final String[] splits = volumePath.split("/");
            String volumeDirectory = volumePath;
            if (splits.length > 4) {
                // "template/tmpl/dcid/templateId/templatename"
                final int index = volumePath.lastIndexOf("/");
                volumeDirectory = volumePath.substring(0, index);
            }
            srcSr = createFileSr(conn, uri.getHost() + ":" + uri.getPath(), volumeDirectory);
            final Set<VDI> setVdis = srcSr.getVDIs(conn);
            if (setVdis.size() != 1) {
                return new CopyCmdAnswer("Expected 1 VDI template, but found " + setVdis.size() + " VDI templates on: " + uri.getHost() + ":" + uri.getPath() + "/" + volumeDirectory);
            }
            final VDI srcVdi = setVdis.iterator().next();
            boolean managed = false;
            String storageHost = null;
            String managedStoragePoolName = null;
            String managedStoragePoolRootVolumeName = null;
            String managedStoragePoolRootVolumeSize = null;
            String chapInitiatorUsername = null;
            String chapInitiatorSecret = null;
            final PrimaryDataStoreTO destStore = (PrimaryDataStoreTO) destData.getDataStore();
            Map<String, String> details = destStore.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);
                    removeSrAfterCopy = Boolean.parseBoolean(details.get(PrimaryDataStoreTO.REMOVE_AFTER_COPY));
                }
            }
            if (managed) {
                details = new HashMap<String, String>();
                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);
                destSr = hypervisorResource.prepareManagedSr(conn, details);
            } else {
                final String srName = destStore.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 {
                    destSr = srs.iterator().next();
                }
            }
            task = srcVdi.copyAsync(conn, destSr, null, null);
            // poll every 1 seconds ,
            hypervisorResource.waitForTask(conn, task, 1000, wait * 1000);
            hypervisorResource.checkForSuccess(conn, task);
            final VDI tmplVdi = Types.toVDI(task, conn);
            final String uuidToReturn;
            final Long physicalSize = tmplVdi.getPhysicalUtilisation(conn);
            if (managed) {
                uuidToReturn = tmplVdi.getUuid(conn);
                tmplVdi.setNameLabel(conn, managedStoragePoolRootVolumeName);
            } else {
                final VDI snapshotVdi = tmplVdi.snapshot(conn, new HashMap<String, String>());
                uuidToReturn = snapshotVdi.getUuid(conn);
                snapshotVdi.setNameLabel(conn, "Template " + srcTemplate.getName());
                tmplVdi.destroy(conn);
            }
            destSr.scan(conn);
            try {
                Thread.sleep(5000);
            } catch (final Exception e) {
            }
            final TemplateObjectTO newVol = new TemplateObjectTO();
            newVol.setUuid(uuidToReturn);
            newVol.setPath(uuidToReturn);
            if (physicalSize != null) {
                newVol.setSize(physicalSize);
            }
            newVol.setFormat(Storage.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);
    } finally {
        if (task != null) {
            try {
                task.destroy(conn);
            } catch (final Exception e) {
                s_logger.debug("unable to destroy task (" + task.toWireString() + ") due to " + e.toString());
            }
        }
        if (srcSr != null) {
            hypervisorResource.removeSR(conn, srcSr);
        }
        if (removeSrAfterCopy && destSr != null) {
            hypervisorResource.removeSR(conn, destSr);
        }
    }
    return new CopyCmdAnswer("not implemented yet");
}
Also used : DataStoreTO(com.cloud.agent.api.to.DataStoreTO) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) Task(com.xensource.xenapi.Task) Connection(com.xensource.xenapi.Connection) 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(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) VDI(com.xensource.xenapi.VDI) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) SR(com.xensource.xenapi.SR)

Example 82 with DataStoreTO

use of com.cloud.agent.api.to.DataStoreTO in project cloudstack by apache.

the class NfsSecondaryStorageResource method execute.

private Answer execute(SecStorageSetupCommand cmd) {
    if (!_inSystemVM) {
        return new Answer(cmd, true, null);
    }
    Answer answer = null;
    DataStoreTO dStore = cmd.getDataStore();
    if (dStore instanceof NfsTO) {
        String secUrl = cmd.getSecUrl();
        try {
            URI uri = new URI(secUrl);
            String nfsHostIp = getUriHostIp(uri);
            addRouteToInternalIpOrCidr(_storageGateway, _storageIp, _storageNetmask, nfsHostIp);
            String dir = mountUri(uri, cmd.getNfsVersion());
            configCerts(cmd.getCerts());
            nfsIps.add(nfsHostIp);
            answer = new SecStorageSetupAnswer(dir);
        } catch (Exception e) {
            String msg = "GetRootDir for " + secUrl + " failed due to " + e.toString();
            s_logger.error(msg);
            answer = new Answer(cmd, false, msg);
        }
    } else {
        // TODO: what do we need to setup for S3/Swift, maybe need to mount
        // to some cache storage
        answer = new Answer(cmd, true, null);
    }
    savePostUploadPSK(cmd.getPostUploadKey());
    startPostUploadServer();
    return answer;
}
Also used : ListTemplateAnswer(com.cloud.agent.api.storage.ListTemplateAnswer) UploadStatusAnswer(org.apache.cloudstack.storage.command.UploadStatusAnswer) GetStorageStatsAnswer(com.cloud.agent.api.GetStorageStatsAnswer) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) ListVolumeAnswer(com.cloud.agent.api.storage.ListVolumeAnswer) DownloadAnswer(com.cloud.agent.api.storage.DownloadAnswer) Answer(com.cloud.agent.api.Answer) CheckHealthAnswer(com.cloud.agent.api.CheckHealthAnswer) ReadyAnswer(com.cloud.agent.api.ReadyAnswer) SecStorageSetupAnswer(com.cloud.agent.api.SecStorageSetupAnswer) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) SecStorageSetupAnswer(com.cloud.agent.api.SecStorageSetupAnswer) NfsTO(com.cloud.agent.api.to.NfsTO) URI(java.net.URI) 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 83 with DataStoreTO

use of com.cloud.agent.api.to.DataStoreTO in project cloudstack by apache.

the class NfsSecondaryStorageResource method copyFromNfsToImage.

protected Answer copyFromNfsToImage(CopyCommand cmd) {
    DataTO destData = cmd.getDestTO();
    DataStoreTO destDataStore = destData.getDataStore();
    if (destDataStore instanceof S3TO) {
        return copyFromNfsToS3(cmd);
    } else if (destDataStore instanceof SwiftTO) {
        return copyFromNfsToSwift(cmd);
    } else {
        return new CopyCmdAnswer("unsupported ");
    }
}
Also used : DataStoreTO(com.cloud.agent.api.to.DataStoreTO) DataTO(com.cloud.agent.api.to.DataTO) SwiftTO(com.cloud.agent.api.to.SwiftTO) S3TO(com.cloud.agent.api.to.S3TO) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer)

Example 84 with DataStoreTO

use of com.cloud.agent.api.to.DataStoreTO in project cloudstack by apache.

the class NfsSecondaryStorageResource method createTemplateFromSnapshot.

protected Answer createTemplateFromSnapshot(CopyCommand cmd) {
    DataTO srcData = cmd.getSrcTO();
    DataTO destData = cmd.getDestTO();
    DataStoreTO srcDataStore = srcData.getDataStore();
    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);
        } else if (destDataStore instanceof SwiftTO) {
            //create template on the same data store
            CopyCmdAnswer answer = (CopyCmdAnswer) copySnapshotToTemplateFromNfsToNfs(cmd, (SnapshotObjectTO) srcData, (NfsTO) srcDataStore, (TemplateObjectTO) destData, (NfsTO) srcDataStore);
            if (!answer.getResult()) {
                return answer;
            }
            s_logger.debug("starting copy template to swift");
            TemplateObjectTO newTemplate = (TemplateObjectTO) answer.getNewData();
            newTemplate.setDataStore(srcDataStore);
            CopyCommand newCpyCmd = new CopyCommand(newTemplate, destData, cmd.getWait(), cmd.executeInSequence());
            Answer result = copyFromNfsToSwift(newCpyCmd);
            cleanupStagingNfs(newTemplate);
            return result;
        } else if (destDataStore instanceof S3TO) {
            //create template on the same data store
            CopyCmdAnswer answer = (CopyCmdAnswer) copySnapshotToTemplateFromNfsToNfs(cmd, (SnapshotObjectTO) srcData, (NfsTO) srcDataStore, (TemplateObjectTO) destData, (NfsTO) srcDataStore);
            if (!answer.getResult()) {
                return answer;
            }
            TemplateObjectTO newTemplate = (TemplateObjectTO) answer.getNewData();
            newTemplate.setDataStore(srcDataStore);
            CopyCommand newCpyCmd = new CopyCommand(newTemplate, destData, cmd.getWait(), cmd.executeInSequence());
            Answer result = copyFromNfsToS3(newCpyCmd);
            cleanupStagingNfs(newTemplate);
            return result;
        }
    }
    s_logger.debug("Failed to create template from snapshot");
    return new CopyCmdAnswer("Unsupported protocol");
}
Also used : SnapshotObjectTO(org.apache.cloudstack.storage.to.SnapshotObjectTO) ListTemplateAnswer(com.cloud.agent.api.storage.ListTemplateAnswer) UploadStatusAnswer(org.apache.cloudstack.storage.command.UploadStatusAnswer) GetStorageStatsAnswer(com.cloud.agent.api.GetStorageStatsAnswer) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) ListVolumeAnswer(com.cloud.agent.api.storage.ListVolumeAnswer) DownloadAnswer(com.cloud.agent.api.storage.DownloadAnswer) Answer(com.cloud.agent.api.Answer) CheckHealthAnswer(com.cloud.agent.api.CheckHealthAnswer) ReadyAnswer(com.cloud.agent.api.ReadyAnswer) SecStorageSetupAnswer(com.cloud.agent.api.SecStorageSetupAnswer) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) DataTO(com.cloud.agent.api.to.DataTO) SwiftTO(com.cloud.agent.api.to.SwiftTO) CopyCommand(org.apache.cloudstack.storage.command.CopyCommand) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) NfsTO(com.cloud.agent.api.to.NfsTO) S3TO(com.cloud.agent.api.to.S3TO) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer)

Example 85 with DataStoreTO

use of com.cloud.agent.api.to.DataStoreTO in project cloudstack by apache.

the class NfsSecondaryStorageResource method copyFromNfsToSwift.

/**
     * Copies data from NFS and uploads it into a Swift container
     *
     * @param cmd CopyComand
     * @return CopyCmdAnswer
     */
protected Answer copyFromNfsToSwift(CopyCommand cmd) {
    final DataTO srcData = cmd.getSrcTO();
    final DataTO destData = cmd.getDestTO();
    DataStoreTO srcDataStore = srcData.getDataStore();
    NfsTO srcStore = (NfsTO) srcDataStore;
    DataStoreTO destDataStore = destData.getDataStore();
    File srcFile = getFile(srcData.getPath(), srcStore.getUrl(), _nfsVersion);
    SwiftTO swift = (SwiftTO) destDataStore;
    long pathId = destData.getId();
    try {
        if (destData instanceof SnapshotObjectTO) {
            pathId = ((SnapshotObjectTO) destData).getVolume().getId();
        }
        String containerName = SwiftUtil.getContainerName(destData.getObjectType().toString(), pathId);
        String swiftPath = SwiftUtil.putObject(swift, srcFile, containerName, srcFile.getName());
        DataTO retObj = null;
        if (destData.getObjectType() == DataObjectType.TEMPLATE) {
            TemplateObjectTO destTemplateData = (TemplateObjectTO) destData;
            String uniqueName = destTemplateData.getName();
            swiftUploadMetadataFile(swift, srcFile, containerName, uniqueName);
            TemplateObjectTO newTemplate = new TemplateObjectTO();
            newTemplate.setPath(swiftPath);
            newTemplate.setSize(getVirtualSize(srcFile, getTemplateFormat(srcFile.getName())));
            newTemplate.setPhysicalSize(srcFile.length());
            newTemplate.setFormat(getTemplateFormat(srcFile.getName()));
            retObj = newTemplate;
        } else if (destData.getObjectType() == DataObjectType.VOLUME) {
            VolumeObjectTO newVol = new VolumeObjectTO();
            newVol.setPath(containerName);
            newVol.setSize(getVirtualSize(srcFile, getTemplateFormat(srcFile.getName())));
            retObj = newVol;
        } else if (destData.getObjectType() == DataObjectType.SNAPSHOT) {
            SnapshotObjectTO newSnapshot = new SnapshotObjectTO();
            newSnapshot.setPath(containerName + File.separator + srcFile.getName());
            retObj = newSnapshot;
        }
        return new CopyCmdAnswer(retObj);
    } catch (Exception e) {
        s_logger.error("failed to upload " + srcData.getPath(), e);
        return new CopyCmdAnswer("failed to upload " + srcData.getPath() + e.toString());
    }
}
Also used : SnapshotObjectTO(org.apache.cloudstack.storage.to.SnapshotObjectTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) DataTO(com.cloud.agent.api.to.DataTO) SwiftTO(com.cloud.agent.api.to.SwiftTO) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) NfsTO(com.cloud.agent.api.to.NfsTO) File(java.io.File) S3Utils.putFile(com.cloud.utils.storage.S3.S3Utils.putFile) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) 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)

Aggregations

DataStoreTO (com.cloud.agent.api.to.DataStoreTO)91 NfsTO (com.cloud.agent.api.to.NfsTO)66 CopyCmdAnswer (org.apache.cloudstack.storage.command.CopyCmdAnswer)54 PrimaryDataStoreTO (org.apache.cloudstack.storage.to.PrimaryDataStoreTO)50 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)47 DataTO (com.cloud.agent.api.to.DataTO)46 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)36 InternalErrorException (com.cloud.exception.InternalErrorException)30 TemplateObjectTO (org.apache.cloudstack.storage.to.TemplateObjectTO)28 Answer (com.cloud.agent.api.Answer)26 SnapshotObjectTO (org.apache.cloudstack.storage.to.SnapshotObjectTO)20 UnsupportedEncodingException (java.io.UnsupportedEncodingException)19 IOException (java.io.IOException)17 S3TO (com.cloud.agent.api.to.S3TO)16 ConfigurationException (javax.naming.ConfigurationException)16 XmlRpcException (org.apache.xmlrpc.XmlRpcException)16 XenAPIException (com.xensource.xenapi.Types.XenAPIException)15 RemoteException (java.rmi.RemoteException)15 SwiftTO (com.cloud.agent.api.to.SwiftTO)14 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)14