Search in sources :

Example 51 with NfsTO

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

the class NfsSecondaryStorageResource method copyFromNfsToS3.

protected Answer copyFromNfsToS3(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 S3TO s3 = (S3TO) destDataStore;
    try {
        final String templatePath = determineStorageTemplatePath(srcStore.getUrl(), srcData.getPath(), _nfsVersion);
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Found " + srcData.getObjectType() + " from directory " + templatePath + " to upload to S3.");
        }
        final String bucket = s3.getBucketName();
        File srcFile = findFile(templatePath);
        if (srcFile == null) {
            return new CopyCmdAnswer("Can't find src file:" + templatePath);
        }
        ImageFormat format = getTemplateFormat(srcFile.getName());
        String key = destData.getPath() + S3Utils.SEPARATOR + srcFile.getName();
        putFile(s3, srcFile, bucket, key).waitForCompletion();
        DataTO retObj = null;
        if (destData.getObjectType() == DataObjectType.TEMPLATE) {
            TemplateObjectTO newTemplate = new TemplateObjectTO();
            newTemplate.setPath(key);
            newTemplate.setSize(getVirtualSize(srcFile, format));
            newTemplate.setPhysicalSize(srcFile.length());
            newTemplate.setFormat(format);
            retObj = newTemplate;
        } else if (destData.getObjectType() == DataObjectType.VOLUME) {
            VolumeObjectTO newVol = new VolumeObjectTO();
            newVol.setPath(key);
            newVol.setSize(srcFile.length());
            retObj = newVol;
        } else if (destData.getObjectType() == DataObjectType.SNAPSHOT) {
            SnapshotObjectTO newSnapshot = new SnapshotObjectTO();
            newSnapshot.setPath(key);
            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) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) NfsTO(com.cloud.agent.api.to.NfsTO) S3TO(com.cloud.agent.api.to.S3TO) 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 52 with NfsTO

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

the class NfsSecondaryStorageResource method execute.

public Answer execute(GetDatadisksCommand cmd) {
    DataTO srcData = cmd.getData();
    String configurationId = cmd.getConfigurationId();
    TemplateObjectTO template = (TemplateObjectTO) srcData;
    DataStoreTO srcStore = srcData.getDataStore();
    if (!(srcStore instanceof NfsTO)) {
        return new CreateDatadiskTemplateAnswer("Unsupported protocol");
    }
    NfsTO nfsImageStore = (NfsTO) srcStore;
    String secondaryStorageUrl = nfsImageStore.getUrl();
    assert (secondaryStorageUrl != null);
    String templateUrl = secondaryStorageUrl + File.separator + srcData.getPath();
    Pair<String, String> templateInfo = decodeTemplateRelativePathAndNameFromUrl(secondaryStorageUrl, templateUrl, template.getName());
    String templateRelativeFolderPath = templateInfo.first();
    try {
        String secondaryMountPoint = getRootDir(secondaryStorageUrl, _nfsVersion);
        s_logger.info("MDOVE Secondary storage mount point: " + secondaryMountPoint);
        String srcOVAFileName = getTemplateOnSecStorageFilePath(secondaryMountPoint, templateRelativeFolderPath, templateInfo.second(), ImageFormat.OVA.getFileExtension());
        String ovfFilePath = getOVFFilePath(srcOVAFileName);
        if (ovfFilePath == null) {
            Script command = new Script("tar", 0, s_logger);
            command.add("--no-same-owner");
            command.add("--no-same-permissions");
            command.add("-xf", srcOVAFileName);
            command.setWorkDir(secondaryMountPoint + File.separator + templateRelativeFolderPath);
            s_logger.info("Executing command: " + command.toString());
            String result = command.execute();
            if (result != null) {
                String msg = "Unable to unpack snapshot OVA file at: " + srcOVAFileName;
                s_logger.error(msg);
                throw new Exception(msg);
            }
            command = new Script("chmod", 0, s_logger);
            command.add("-R");
            command.add("666", secondaryMountPoint + File.separator + templateRelativeFolderPath);
            result = command.execute();
            if (result != null) {
                s_logger.warn("Unable to set permissions for " + secondaryMountPoint + File.separator + templateRelativeFolderPath + " due to " + result);
            }
        }
        Script command = new Script("cp", _timeout, s_logger);
        command.add(ovfFilePath);
        command.add(ovfFilePath + ORIGINAL_FILE_EXTENSION);
        String result = command.execute();
        if (result != null) {
            String msg = "Unable to rename original OVF, error msg: " + result;
            s_logger.error(msg);
        }
        s_logger.debug("Reading OVF " + ovfFilePath + " to retrive the number of disks present in OVA");
        OVFHelper ovfHelper = new OVFHelper();
        List<DatadiskTO> disks = ovfHelper.getOVFVolumeInfoFromFile(ovfFilePath, configurationId);
        return new GetDatadisksAnswer(disks);
    } catch (Exception e) {
        String msg = "Get Datadisk Template Count failed due to " + e.getMessage();
        s_logger.error(msg, e);
        return new GetDatadisksAnswer(msg);
    }
}
Also used : Script(com.cloud.utils.script.Script) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) GetDatadisksAnswer(com.cloud.agent.api.storage.GetDatadisksAnswer) NfsTO(com.cloud.agent.api.to.NfsTO) 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) DataTO(com.cloud.agent.api.to.DataTO) DatadiskTO(com.cloud.agent.api.to.DatadiskTO) CreateDatadiskTemplateAnswer(com.cloud.agent.api.storage.CreateDatadiskTemplateAnswer) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) OVFHelper(com.cloud.agent.api.storage.OVFHelper)

Example 53 with NfsTO

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

the class NfsSecondaryStorageResource method execute.

private Answer execute(ListTemplateCommand cmd) {
    if (!_inSystemVM) {
        return new ListTemplateAnswer(null, null);
    }
    DataStoreTO store = cmd.getDataStore();
    if (store instanceof NfsTO) {
        NfsTO nfs = (NfsTO) store;
        String secUrl = nfs.getUrl();
        String root = getRootDir(secUrl, cmd.getNfsVersion());
        Map<String, TemplateProp> templateInfos = _dlMgr.gatherTemplateInfo(root);
        return new ListTemplateAnswer(secUrl, templateInfos);
    } else if (store instanceof SwiftTO) {
        SwiftTO swift = (SwiftTO) store;
        Map<String, TemplateProp> templateInfos = swiftListTemplate(swift);
        return new ListTemplateAnswer(swift.toString(), templateInfos);
    } else if (store instanceof S3TO) {
        S3TO s3 = (S3TO) store;
        Map<String, TemplateProp> templateInfos = s3ListTemplate(s3);
        return new ListTemplateAnswer(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) SwiftTO(com.cloud.agent.api.to.SwiftTO) ListTemplateAnswer(com.cloud.agent.api.storage.ListTemplateAnswer) NfsTO(com.cloud.agent.api.to.NfsTO) Map(java.util.Map) HashMap(java.util.HashMap) S3TO(com.cloud.agent.api.to.S3TO)

Example 54 with NfsTO

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

the class NfsSecondaryStorageResource method deleteTemplate.

protected Answer deleteTemplate(DeleteCommand cmd) {
    DataTO obj = cmd.getData();
    DataStoreTO dstore = obj.getDataStore();
    if (dstore instanceof NfsTO) {
        NfsTO nfs = (NfsTO) dstore;
        String relativeTemplatePath = obj.getPath();
        String parent = getRootDir(nfs.getUrl(), _nfsVersion);
        if (relativeTemplatePath.startsWith(File.separator)) {
            relativeTemplatePath = relativeTemplatePath.substring(1);
        }
        if (!parent.endsWith(File.separator)) {
            parent += File.separator;
        }
        String absoluteTemplatePath = parent + relativeTemplatePath;
        File tmpltPath = new File(absoluteTemplatePath);
        File tmpltParent = null;
        if (tmpltPath.exists() && tmpltPath.isDirectory()) {
            tmpltParent = tmpltPath;
        } else if (absoluteTemplatePath.endsWith(File.separator + obj.getId())) {
            // the path ends with <account id>/<template id>, if upload fails
            tmpltParent = tmpltPath;
        } else {
            tmpltParent = tmpltPath.getParentFile();
        }
        String details = null;
        if (!tmpltParent.exists()) {
            details = "template 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 template parent directory " + tmpltParent.getName();
            s_logger.debug(details);
        } else {
            boolean found = false;
            for (File f : tmpltFiles) {
                if (!found && f.getName().equals(_tmpltpp)) {
                    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 Template path " + relativeTemplatePath);
                }
            }
            if (!found) {
                details = "Can not find template.properties under " + tmpltParent.getName();
                s_logger.debug(details);
            }
        }
        if (!tmpltParent.delete()) {
            details = "Unable to delete directory " + tmpltParent.getName() + " under Template path " + relativeTemplatePath;
            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 template %1$s from bucket %2$s.", path, bucket));
        } catch (Exception e) {
            final String errorMessage = String.format("Failed to delete template %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) {
        SwiftTO swift = (SwiftTO) dstore;
        String container = "T-" + obj.getId();
        String object = "";
        try {
            String result = swiftDelete(swift, container, object);
            if (result != null) {
                String errMsg = "failed to delete object " + container + "/" + object + " , err=" + result;
                s_logger.warn(errMsg);
                return new Answer(cmd, false, errMsg);
            }
            return new Answer(cmd, true, "success");
        } catch (Exception e) {
            String errMsg = cmd + " Command failed due to " + e.toString();
            s_logger.warn(errMsg, e);
            return new Answer(cmd, false, errMsg);
        }
    } 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)

Example 55 with NfsTO

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

the class NfsSecondaryStorageResource method execute.

private Answer execute(HandleConfigDriveIsoCommand cmd) {
    if (cmd.isCreate()) {
        if (cmd.getIsoData() == null) {
            return new HandleConfigDriveIsoAnswer(cmd, "Invalid config drive ISO data");
        }
        String nfsMountPoint = getRootDir(cmd.getDestStore().getUrl(), _nfsVersion);
        File isoFile = new File(nfsMountPoint, cmd.getIsoFile());
        if (isoFile.exists()) {
            s_logger.debug("config drive iso already exists");
        }
        Path tempDir = null;
        try {
            tempDir = java.nio.file.Files.createTempDirectory(ConfigDrive.CONFIGDRIVEDIR);
            File tmpIsoFile = ConfigDriveBuilder.base64StringToFile(cmd.getIsoData(), tempDir.toAbsolutePath().toString(), cmd.getIsoFile());
            copyLocalToNfs(tmpIsoFile, new File(cmd.getIsoFile()), cmd.getDestStore());
        } catch (IOException | ConfigurationException e) {
            return new HandleConfigDriveIsoAnswer(cmd, "Failed due to exception: " + e.getMessage());
        } finally {
            try {
                if (tempDir != null) {
                    FileUtils.deleteDirectory(tempDir.toFile());
                }
            } catch (IOException ioe) {
                s_logger.warn("Failed to delete ConfigDrive temporary directory: " + tempDir.toString(), ioe);
            }
        }
        return new HandleConfigDriveIsoAnswer(cmd, NetworkElement.Location.SECONDARY, "Successfully saved config drive at secondary storage");
    } else {
        DataStoreTO dstore = cmd.getDestStore();
        if (dstore instanceof NfsTO) {
            NfsTO nfs = (NfsTO) dstore;
            String relativeTemplatePath = new File(cmd.getIsoFile()).getPath();
            String nfsMountPoint = getRootDir(nfs.getUrl(), _nfsVersion);
            File tmpltPath = new File(nfsMountPoint, relativeTemplatePath);
            try {
                Files.deleteIfExists(tmpltPath.toPath());
            } catch (IOException e) {
                return new HandleConfigDriveIsoAnswer(cmd, e);
            }
            return new HandleConfigDriveIsoAnswer(cmd);
        } else {
            return new HandleConfigDriveIsoAnswer(cmd, "Not implemented yet");
        }
    }
}
Also used : Path(java.nio.file.Path) HandleConfigDriveIsoAnswer(com.cloud.agent.api.HandleConfigDriveIsoAnswer) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) ConfigurationException(javax.naming.ConfigurationException) IOException(java.io.IOException) File(java.io.File) S3Utils.putFile(com.cloud.utils.storage.S3.S3Utils.putFile) NfsTO(com.cloud.agent.api.to.NfsTO)

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