Search in sources :

Example 46 with NfsTO

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

the class DownloadManagerImpl method handleDownloadCommand.

@Override
public DownloadAnswer handleDownloadCommand(SecondaryStorageResource resource, DownloadCommand cmd) {
    int timeout = NumbersUtil.parseInt(cmd.getContextParam("vmware.package.ova.timeout"), 3600000);
    this._processTimeout = timeout;
    ResourceType resourceType = cmd.getResourceType();
    if (cmd instanceof DownloadProgressCommand) {
        return handleDownloadProgressCmd(resource, (DownloadProgressCommand) cmd);
    }
    if (cmd.getUrl() == null) {
        return new DownloadAnswer(resourceType.toString() + " is corrupted on storage due to an invalid url , cannot download", VMTemplateStorageResourceAssoc.Status.DOWNLOAD_ERROR);
    }
    if (cmd.getName() == null) {
        return new DownloadAnswer("Invalid Name", VMTemplateStorageResourceAssoc.Status.DOWNLOAD_ERROR);
    }
    if (!DigestHelper.isAlgorithmSupported(cmd.getChecksum())) {
        return new DownloadAnswer("invalid algorithm: " + cmd.getChecksum(), VMTemplateStorageResourceAssoc.Status.NOT_DOWNLOADED);
    }
    DataStoreTO dstore = cmd.getDataStore();
    String installPathPrefix = cmd.getInstallPath();
    // for NFS, we need to get mounted path
    if (dstore instanceof NfsTO) {
        installPathPrefix = resource.getRootDir(((NfsTO) dstore).getUrl(), _nfsVersion) + File.separator + installPathPrefix;
    }
    String user = null;
    String password = null;
    if (cmd.getAuth() != null) {
        user = cmd.getAuth().getUserName();
        password = cmd.getAuth().getPassword();
    }
    // TO DO - Define Volume max size as well
    long maxDownloadSizeInBytes = (cmd.getMaxDownloadSizeInBytes() == null) ? TemplateDownloader.DEFAULT_MAX_TEMPLATE_SIZE_IN_BYTES : (cmd.getMaxDownloadSizeInBytes());
    String jobId = null;
    if (dstore instanceof S3TO) {
        jobId = downloadS3Template((S3TO) dstore, cmd.getId(), cmd.getUrl(), cmd.getName(), cmd.getFormat(), cmd.isHvm(), cmd.getAccountId(), cmd.getDescription(), cmd.getChecksum(), installPathPrefix, user, password, maxDownloadSizeInBytes, cmd.getProxy(), resourceType);
    } else {
        jobId = downloadPublicTemplate(cmd.getId(), cmd.getUrl(), cmd.getName(), cmd.getFormat(), cmd.isHvm(), cmd.getAccountId(), cmd.getDescription(), cmd.getChecksum(), installPathPrefix, cmd.getInstallPath(), user, password, maxDownloadSizeInBytes, cmd.getProxy(), resourceType);
    }
    sleep();
    if (jobId == null) {
        return new DownloadAnswer("Internal Error", VMTemplateStorageResourceAssoc.Status.DOWNLOAD_ERROR);
    }
    return new DownloadAnswer(jobId, getDownloadPct(jobId), getDownloadError(jobId), getDownloadStatus2(jobId), getDownloadLocalPath(jobId), getInstallPath(jobId), getDownloadTemplateSize(jobId), getDownloadTemplateSize(jobId), getDownloadCheckSum(jobId));
}
Also used : DataStoreTO(com.cloud.agent.api.to.DataStoreTO) ResourceType(org.apache.cloudstack.storage.command.DownloadCommand.ResourceType) DownloadAnswer(com.cloud.agent.api.storage.DownloadAnswer) NfsTO(com.cloud.agent.api.to.NfsTO) S3TO(com.cloud.agent.api.to.S3TO) DownloadProgressCommand(org.apache.cloudstack.storage.command.DownloadProgressCommand)

Example 47 with NfsTO

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

the class NfsSecondaryStorageResource method registerTemplateOnSwift.

protected Answer registerTemplateOnSwift(DownloadCommand cmd) {
    SwiftTO swiftTO = (SwiftTO) cmd.getDataStore();
    String path = cmd.getInstallPath();
    DataStoreTO cacheStore = cmd.getCacheStore();
    if (cacheStore == null || !(cacheStore instanceof NfsTO)) {
        return new DownloadAnswer("cache store can't be null", VMTemplateStorageResourceAssoc.Status.DOWNLOAD_ERROR);
    }
    File file = null;
    try {
        NfsTO nfsCacheStore = (NfsTO) cacheStore;
        String fileName = cmd.getName() + "." + cmd.getFormat().getFileExtension();
        file = downloadFromUrlToNfs(cmd.getUrl(), nfsCacheStore, path, fileName);
        String container = "T-" + cmd.getId();
        String swiftPath = SwiftUtil.putObject(swiftTO, file, container, null);
        long virtualSize = getVirtualSize(file, getTemplateFormat(file.getName()));
        long size = file.length();
        String uniqueName = cmd.getName();
        // put metda file
        File uniqDir = _storage.createUniqDir();
        String metaFileName = uniqDir.getAbsolutePath() + File.separator + _tmpltpp;
        _storage.create(uniqDir.getAbsolutePath(), _tmpltpp);
        File metaFile = swiftWriteMetadataFile(metaFileName, uniqueName, fileName, size, virtualSize);
        SwiftUtil.putObject(swiftTO, metaFile, container, _tmpltpp);
        metaFile.delete();
        uniqDir.delete();
        String md5sum = null;
        try (FileInputStream fs = new FileInputStream(file)) {
            md5sum = DigestUtils.md5Hex(fs);
        } catch (IOException e) {
            s_logger.debug("Failed to get md5sum: " + file.getAbsoluteFile());
        }
        DownloadAnswer answer = new DownloadAnswer(null, 100, null, VMTemplateStorageResourceAssoc.Status.DOWNLOADED, swiftPath, swiftPath, virtualSize, file.length(), md5sum);
        return answer;
    } catch (IOException e) {
        s_logger.debug("Failed to register template into swift", e);
        return new DownloadAnswer(e.toString(), VMTemplateStorageResourceAssoc.Status.DOWNLOAD_ERROR);
    } finally {
        if (file != null) {
            file.delete();
        }
    }
}
Also used : DataStoreTO(com.cloud.agent.api.to.DataStoreTO) SwiftTO(com.cloud.agent.api.to.SwiftTO) IOException(java.io.IOException) DownloadAnswer(com.cloud.agent.api.storage.DownloadAnswer) NfsTO(com.cloud.agent.api.to.NfsTO) File(java.io.File) S3Utils.putFile(com.cloud.utils.storage.S3.S3Utils.putFile) FileInputStream(java.io.FileInputStream)

Example 48 with NfsTO

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

the class NfsSecondaryStorageResource method copyFromNfsToNfs.

protected Answer copyFromNfsToNfs(CopyCommand cmd) {
    final DataTO srcData = cmd.getSrcTO();
    final DataTO destData = cmd.getDestTO();
    DataStoreTO srcDataStore = srcData.getDataStore();
    NfsTO srcStore = (NfsTO) srcDataStore;
    DataStoreTO destDataStore = destData.getDataStore();
    final NfsTO destStore = (NfsTO) destDataStore;
    try {
        File srcFile = new File(getDir(srcStore.getUrl(), _nfsVersion), srcData.getPath());
        File destFile = new File(getDir(destStore.getUrl(), _nfsVersion), destData.getPath());
        if (srcFile == null) {
            return new CopyCmdAnswer("Can't find source file at path: " + srcData.getPath() + " on datastore: " + srcDataStore.getUuid() + " to initiate file transfer");
        }
        ImageFormat format = getTemplateFormat(srcFile.getName());
        if (srcData instanceof TemplateObjectTO || srcData instanceof VolumeObjectTO) {
            File srcDir = null;
            if (srcFile.isFile() || srcFile.getName().contains(".")) {
                srcDir = new File(srcFile.getParent());
            } else if (!srcFile.isDirectory()) {
                srcDir = new File(srcFile.getParent());
            } else if (srcFile.isDirectory() && Arrays.stream(srcData.getPath().split(File.separator)).count() == 4) {
                destFile = new File(destFile.getPath(), srcFile.getName());
            }
            File destDir = null;
            if (destFile.isFile()) {
                destDir = new File(destFile.getParent());
            }
            try {
                FileUtils.copyDirectory((srcDir == null ? srcFile : srcDir), (destDir == null ? destFile : destDir));
            } catch (IOException e) {
                String msg = "Failed to copy file to destination";
                s_logger.info(msg);
                return new CopyCmdAnswer(msg);
            }
        } else {
            destFile = new File(destFile, srcFile.getName());
            try {
                if (srcFile.isFile()) {
                    FileUtils.copyFile(srcFile, destFile);
                } else {
                    // for vmware
                    srcFile = new File(srcFile.getParent());
                    FileUtils.copyDirectory(srcFile, destFile);
                }
            } catch (IOException e) {
                String msg = "Failed to copy file to destination";
                s_logger.info(msg);
                return new CopyCmdAnswer(msg);
            }
        }
        DataTO retObj = null;
        if (destData.getObjectType() == DataObjectType.TEMPLATE) {
            TemplateObjectTO newTemplate = new TemplateObjectTO();
            newTemplate.setPath(destData.getPath() + File.separator + srcFile.getName());
            newTemplate.setSize(getVirtualSize(srcFile, format));
            newTemplate.setPhysicalSize(srcFile.length());
            newTemplate.setFormat(format);
            retObj = newTemplate;
        } else if (destData.getObjectType() == DataObjectType.VOLUME) {
            VolumeObjectTO newVol = new VolumeObjectTO();
            if (srcFile.isFile()) {
                newVol.setPath(destData.getPath() + File.separator + srcFile.getName());
            } else {
                newVol.setPath(srcData.getPath());
            }
            newVol.setSize(getVirtualSize(srcFile, format));
            retObj = newVol;
        } else if (destData.getObjectType() == DataObjectType.SNAPSHOT) {
            SnapshotObjectTO newSnapshot = new SnapshotObjectTO();
            if (srcFile.isFile()) {
                newSnapshot.setPath(destData.getPath() + File.separator + destFile.getName());
            } else {
                newSnapshot.setPath(destData.getPath() + File.separator + destFile.getName() + File.separator + destFile.getName());
            }
            retObj = newSnapshot;
        }
        return new CopyCmdAnswer(retObj);
    } catch (Exception e) {
        s_logger.error("failed to copy file" + srcData.getPath(), e);
        return new CopyCmdAnswer("failed to copy file" + 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) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) IOException(java.io.IOException) 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) ImageFormat(com.cloud.storage.Storage.ImageFormat)

Example 49 with NfsTO

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

the class NfsSecondaryStorageResource method execute.

private Answer execute(ListVolumeCommand cmd) {
    if (!_inSystemVM) {
        return new ListVolumeAnswer(cmd.getSecUrl(), null);
    }
    DataStoreTO store = cmd.getDataStore();
    if (store instanceof NfsTO) {
        String root = getRootDir(cmd.getSecUrl(), _nfsVersion);
        Map<Long, TemplateProp> templateInfos = _dlMgr.gatherVolumeInfo(root);
        return new ListVolumeAnswer(cmd.getSecUrl(), templateInfos);
    } else if (store instanceof S3TO) {
        S3TO s3 = (S3TO) store;
        Map<Long, TemplateProp> templateInfos = s3ListVolume(s3);
        return new ListVolumeAnswer(s3.getBucketName(), templateInfos);
    } else {
        return new Answer(cmd, false, "Unsupported image data store: " + store);
    }
}
Also used : TemplateProp(com.cloud.storage.template.TemplateProp) GetDatadisksAnswer(com.cloud.agent.api.storage.GetDatadisksAnswer) CreateDatadiskTemplateAnswer(com.cloud.agent.api.storage.CreateDatadiskTemplateAnswer) ListTemplateAnswer(com.cloud.agent.api.storage.ListTemplateAnswer) HandleConfigDriveIsoAnswer(com.cloud.agent.api.HandleConfigDriveIsoAnswer) 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) ListVolumeAnswer(com.cloud.agent.api.storage.ListVolumeAnswer) NfsTO(com.cloud.agent.api.to.NfsTO) S3TO(com.cloud.agent.api.to.S3TO) Map(java.util.Map) HashMap(java.util.HashMap)

Example 50 with NfsTO

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

the class NfsSecondaryStorageResource method deleteVolume.

protected Answer deleteVolume(final DeleteCommand cmd) {
    DataTO obj = cmd.getData();
    DataStoreTO dstore = obj.getDataStore();
    if (dstore instanceof NfsTO) {
        NfsTO nfs = (NfsTO) dstore;
        String relativeVolumePath = obj.getPath();
        String parent = getRootDir(nfs.getUrl(), _nfsVersion);
        if (relativeVolumePath.startsWith(File.separator)) {
            relativeVolumePath = relativeVolumePath.substring(1);
        }
        if (!parent.endsWith(File.separator)) {
            parent += File.separator;
        }
        String absoluteVolumePath = parent + relativeVolumePath;
        File volPath = new File(absoluteVolumePath);
        File tmpltParent = null;
        if (volPath.exists() && volPath.isDirectory()) {
            // for vmware, absoluteVolumePath represents a directory where volume files are located.
            tmpltParent = volPath;
        } else if (absoluteVolumePath.endsWith(File.separator + obj.getId())) {
            // the path ends with <account id>/<volume id>, if upload fails
            tmpltParent = volPath;
        } else {
            // for other hypervisors, the volume .vhd or .qcow2 file path is passed
            tmpltParent = new File(absoluteVolumePath).getParentFile();
        }
        String details = null;
        if (!tmpltParent.exists()) {
            details = "volume parent directory " + tmpltParent.getName() + " doesn't exist";
            s_logger.debug(details);
            return new Answer(cmd, true, details);
        }
        File[] tmpltFiles = tmpltParent.listFiles();
        if (tmpltFiles == null || tmpltFiles.length == 0) {
            details = "No files under volume parent directory " + tmpltParent.getName();
            s_logger.debug(details);
        } else {
            boolean found = false;
            for (File f : tmpltFiles) {
                if (!found && f.getName().equals("volume.properties")) {
                    found = true;
                }
                // Don't let this stop us from cleaning up the template
                if (f.isDirectory() && f.getName().equals("KVMHA")) {
                    s_logger.debug("Deleting KVMHA directory contents from template location");
                    File[] haFiles = f.listFiles();
                    for (File haFile : haFiles) {
                        haFile.delete();
                    }
                }
                if (!f.delete()) {
                    return new Answer(cmd, false, "Unable to delete file " + f.getName() + " under Volume path " + tmpltParent.getPath());
                }
            }
            if (!found) {
                details = "Can not find volume.properties under " + tmpltParent.getName();
                s_logger.debug(details);
            }
        }
        if (!tmpltParent.delete()) {
            details = "Unable to delete directory " + tmpltParent.getName() + " under Volume path " + tmpltParent.getPath();
            s_logger.debug(details);
            return new Answer(cmd, false, details);
        }
        return new Answer(cmd, true, null);
    } else if (dstore instanceof S3TO) {
        final S3TO s3 = (S3TO) dstore;
        final String path = obj.getPath();
        final String bucket = s3.getBucketName();
        try {
            S3Utils.deleteDirectory(s3, bucket, path);
            return new Answer(cmd, true, String.format("Deleted volume %1%s from bucket %2$s.", path, bucket));
        } catch (Exception e) {
            final String errorMessage = String.format("Failed to delete volume %1$s from bucket %2$s due to the following error: %3$s", path, bucket, e.getMessage());
            s_logger.error(errorMessage, e);
            return new Answer(cmd, false, errorMessage);
        }
    } else if (dstore instanceof SwiftTO) {
        Long volumeId = obj.getId();
        String path = obj.getPath();
        // assuming
        String filename = StringUtils.substringAfterLast(path, "/");
        // that
        // the
        // filename
        // is
        // the
        // last
        // section
        // in
        // the
        // path
        String result = swiftDelete((SwiftTO) dstore, "V-" + volumeId.toString(), filename);
        if (result != null) {
            String errMsg = "failed to delete volume " + filename + " , err=" + result;
            s_logger.warn(errMsg);
            return new Answer(cmd, false, errMsg);
        }
        return new Answer(cmd, true, "Deleted volume " + path + " from swift");
    } else {
        return new Answer(cmd, false, "Unsupported image data store: " + dstore);
    }
}
Also used : GetDatadisksAnswer(com.cloud.agent.api.storage.GetDatadisksAnswer) CreateDatadiskTemplateAnswer(com.cloud.agent.api.storage.CreateDatadiskTemplateAnswer) ListTemplateAnswer(com.cloud.agent.api.storage.ListTemplateAnswer) HandleConfigDriveIsoAnswer(com.cloud.agent.api.HandleConfigDriveIsoAnswer) 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) NfsTO(com.cloud.agent.api.to.NfsTO) File(java.io.File) S3Utils.putFile(com.cloud.utils.storage.S3.S3Utils.putFile) S3TO(com.cloud.agent.api.to.S3TO) 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

NfsTO (com.cloud.agent.api.to.NfsTO)142 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)110 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)79 DataTO (com.cloud.agent.api.to.DataTO)71 InternalErrorException (com.cloud.exception.InternalErrorException)58 CopyCmdAnswer (org.apache.cloudstack.storage.command.CopyCmdAnswer)52 PrimaryDataStoreTO (org.apache.cloudstack.storage.to.PrimaryDataStoreTO)39 TemplateObjectTO (org.apache.cloudstack.storage.to.TemplateObjectTO)38 XmlRpcException (org.apache.xmlrpc.XmlRpcException)37 XenAPIException (com.xensource.xenapi.Types.XenAPIException)36 URI (java.net.URI)36 Connection (com.xensource.xenapi.Connection)34 SR (com.xensource.xenapi.SR)34 VDI (com.xensource.xenapi.VDI)34 CopyCmdAnswer (com.cloud.storage.command.CopyCmdAnswer)32 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)32 Answer (com.cloud.agent.api.Answer)29 IOException (java.io.IOException)28 File (java.io.File)27 PrimaryDataStoreTO (com.cloud.storage.to.PrimaryDataStoreTO)26