Search in sources :

Example 1 with SftpConfig

use of com.dtstack.taier.pluginapi.sftp.SftpConfig in project Taier by DTStack.

the class ConsoleComponentService method mergeKrb5.

private synchronized String mergeKrb5(List<KerberosConfig> kerberosConfigs) {
    String mergeKrb5Content = "";
    if (CollectionUtils.isEmpty(kerberosConfigs)) {
        LOGGER.error("KerberosConfigs is null");
        return mergeKrb5Content;
    }
    String mergeDirPath = ConfigConstant.LOCAL_KRB5_MERGE_DIR_PARENT + ConfigConstant.SP + UUID.randomUUID();
    List<Long> clusterDownloadRecords = new ArrayList<>();
    try {
        String oldMergeKrb5Content = null;
        String mergeKrb5Path = mergeDirPath + ConfigConstant.SP + ConfigConstant.MERGE_KRB5_NAME;
        for (KerberosConfig kerberosConfig : kerberosConfigs) {
            String krb5Name = kerberosConfig.getKrbName();
            String remotePath = kerberosConfig.getRemotePath();
            Long clusterId = kerberosConfig.getClusterId();
            Integer componentCode = kerberosConfig.getComponentType();
            if (StringUtils.isNotEmpty(kerberosConfig.getMergeKrbContent()) && StringUtils.isEmpty(oldMergeKrb5Content)) {
                oldMergeKrb5Content = kerberosConfig.getMergeKrbContent();
            }
            String remoteKrb5Path = remotePath + ConfigConstant.SP + krb5Name;
            String localKrb5Path = mergeDirPath + remoteKrb5Path;
            try {
                String sftpComponent = componentService.getComponentByClusterId(clusterId, EComponentType.SFTP.getTypeCode(), false, String.class, null);
                SftpConfig sftpConfig = getSFTPConfig(sftpComponent, componentCode, "");
                SftpFileManage sftpFileManage = sftpFileManageBean.retrieveSftpManager(sftpConfig);
                if (clusterDownloadRecords.contains(clusterId)) {
                    continue;
                }
                boolean downRes = sftpFileManage.downloadFile(remoteKrb5Path, localKrb5Path);
                LOGGER.info("download remoteKrb5Path[{}] result {}", remoteKrb5Path, downRes);
                if (downRes) {
                    clusterDownloadRecords.add(clusterId);
                    if (!new File(mergeKrb5Path).exists()) {
                        FileUtils.copyFile(new File(localKrb5Path), new File(mergeKrb5Path));
                        mergeKrb5Content = Krb5FileUtil.convertMapToString(Krb5FileUtil.readKrb5ByPath(mergeKrb5Path));
                        continue;
                    }
                    mergeKrb5Content = Krb5FileUtil.mergeKrb5ContentByPath(mergeKrb5Path, localKrb5Path);
                }
            } catch (Exception e) {
                LOGGER.error("merge krb5.conf[{}] error : {}", localKrb5Path, e.getMessage());
            }
        }
        if (StringUtils.isNotEmpty(oldMergeKrb5Content)) {
            mergeKrb5Content = Krb5FileUtil.resetMergeKrb5Content(oldMergeKrb5Content, mergeKrb5Content);
        }
        LOGGER.info("mergeKrb5Content is {}", mergeKrb5Content);
        for (KerberosConfig kerberosConfig : kerberosConfigs) {
            kerberosConfig.setMergeKrbContent(mergeKrb5Content);
            consoleKerberosMapper.updateById(kerberosConfig);
            LOGGER.info("Krb5[{}/krb5.conf] merge successed!", kerberosConfig.getRemotePath());
        }
    } catch (Exception e) {
        LOGGER.error("Merge krb5 error! {}", e.getMessage());
    } finally {
        try {
            File mergeDir = new File(mergeDirPath);
            FileUtils.deleteDirectory(mergeDir);
        } catch (Exception e) {
        }
    }
    return mergeKrb5Content;
}
Also used : SftpFileManage(com.dtstack.taier.pluginapi.sftp.SftpFileManage) SftpConfig(com.dtstack.taier.pluginapi.sftp.SftpConfig) File(java.io.File) RdosDefineException(com.dtstack.taier.common.exception.RdosDefineException) IOException(java.io.IOException)

Example 2 with SftpConfig

use of com.dtstack.taier.pluginapi.sftp.SftpConfig in project Taier by DTStack.

the class ConsoleComponentService method updateResource.

private String updateResource(Long clusterId, String componentConfig, List<Resource> resources, String kerberosFileName, Integer componentCode, String principals, String principal, Component addComponent, Component dbComponent) {
    // 上传资源依赖sftp组件
    String md5Key = "";
    if (CollectionUtils.isNotEmpty(resources)) {
        String sftpConfigStr = componentService.getComponentByClusterId(clusterId, EComponentType.SFTP.getTypeCode(), false, String.class, null);
        // 上传配置文件到sftp 供后续下载
        SftpConfig sftpConfig = getSFTPConfig(sftpConfigStr, componentCode, componentConfig);
        md5Key = uploadResourceToSftp(clusterId, resources, kerberosFileName, sftpConfig, addComponent, dbComponent, principals, principal);
    } else if (CollectionUtils.isEmpty(resources) && StringUtils.isNotBlank(principal)) {
        // 直接更新认证信息
        KerberosConfig kerberosConfig = consoleKerberosMapper.getByComponentType(clusterId, addComponent.getComponentTypeCode(), ComponentVersionUtil.isMultiVersionComponent(addComponent.getComponentTypeCode()) ? StringUtils.isNotBlank(addComponent.getVersionValue()) ? addComponent.getVersionValue() : componentMapper.getDefaultComponentVersionByClusterAndComponentType(clusterId, componentCode) : null);
        if (null != kerberosConfig) {
            kerberosConfig.setPrincipal(principal);
            kerberosConfig.setPrincipals(principals);
            consoleKerberosMapper.updateById(kerberosConfig);
        }
    }
    return md5Key;
}
Also used : SftpConfig(com.dtstack.taier.pluginapi.sftp.SftpConfig)

Example 3 with SftpConfig

use of com.dtstack.taier.pluginapi.sftp.SftpConfig in project Taier by DTStack.

the class ConsoleComponentService method downloadFile.

/**
 * 下载文件
 *
 * @param componentId
 * @param downloadType 0:kerberos配置文件 1:配置文件 2:模板文件
 * @return
 */
public File downloadFile(Long componentId, Integer downloadType, Integer componentType, String versionName, Long clusterId, Integer deployType) {
    String localDownLoadPath = "";
    String uploadFileName = "";
    if (null == componentId) {
        EComponentType type = EComponentType.getByCode(componentType);
        // 解析模版中的信息 作为默认值 返回json
        List<ClientTemplate> clientTemplates = this.loadTemplate(clusterId, type, versionName, null, deployType);
        if (CollectionUtils.isNotEmpty(clientTemplates)) {
            Map<String, Object> fileMap = ComponentConfigUtils.convertClientTemplateToMap(clientTemplates);
            uploadFileName = EComponentType.getByCode(componentType).name() + ".json";
            localDownLoadPath = USER_DIR_DOWNLOAD + File.separator + uploadFileName;
            try {
                FileUtils.write(new File(localDownLoadPath), JSONObject.toJSONString(fileMap));
            } catch (Exception e) {
                throw new RdosDefineException("file does not exist");
            }
        }
    } else {
        Component component = componentMapper.selectById(componentId);
        if (null == component) {
            throw new RdosDefineException("Component does not exist");
        }
        SftpConfig sftpConfig = componentService.getComponentByClusterId(clusterId, EComponentType.SFTP.getTypeCode(), false, SftpConfig.class, null);
        if (null == sftpConfig) {
            throw new RdosDefineException("sftp component does not exist");
        }
        localDownLoadPath = USER_DIR_DOWNLOAD + File.separator + component.getComponentName();
        String remoteDir = sftpConfig.getPath() + File.separator + this.buildSftpPath(clusterId, component.getComponentTypeCode());
        SftpFileManage sftpFileManage = null;
        if (DownloadType.Kerberos.getCode() == downloadType) {
            remoteDir = remoteDir + File.separator + KERBEROS;
            localDownLoadPath = localDownLoadPath + File.separator + KERBEROS;
            sftpFileManage = SftpFileManage.getSftpManager(sftpConfig);
            sftpFileManage.downloadDir(remoteDir, localDownLoadPath);
        } else {
            if (StringUtils.isBlank(component.getUploadFileName())) {
                // 一种是  全部手动填写的 如flink
                EComponentType type = EComponentType.getByCode(componentType);
                String componentConfig = componentService.getComponentByClusterId(clusterId, type.getTypeCode(), true, String.class, component.getVersionValue());
                try {
                    localDownLoadPath = localDownLoadPath + ".json";
                    FileUtils.write(new File(localDownLoadPath), filterConfigMessage(componentConfig));
                } catch (IOException e) {
                    LOGGER.error("write upload file {} error", componentConfig, e);
                }
            } else {
                sftpFileManage = SftpFileManage.getSftpManager(sftpConfig);
                // 一种是 上传配置文件的需要到sftp下载
                sftpFileManage.downloadDir(remoteDir + File.separator + component.getUploadFileName(), localDownLoadPath);
            }
        }
        uploadFileName = component.getUploadFileName();
    }
    File file = new File(localDownLoadPath);
    if (!file.exists()) {
        throw new RdosDefineException("file does not exist");
    }
    String zipFilename = StringUtils.isBlank(uploadFileName) ? "download.zip" : uploadFileName;
    if (file.isDirectory()) {
        // 将文件夹压缩成zip文件
        return zipFile(componentId, downloadType, componentType, file, zipFilename);
    } else {
        return new File(localDownLoadPath);
    }
}
Also used : SftpFileManage(com.dtstack.taier.pluginapi.sftp.SftpFileManage) RdosDefineException(com.dtstack.taier.common.exception.RdosDefineException) ClientTemplate(com.dtstack.taier.scheduler.impl.pojo.ClientTemplate) SftpConfig(com.dtstack.taier.pluginapi.sftp.SftpConfig) IOException(java.io.IOException) EComponentType(com.dtstack.taier.common.enums.EComponentType) RdosDefineException(com.dtstack.taier.common.exception.RdosDefineException) IOException(java.io.IOException) JSONObject(com.alibaba.fastjson.JSONObject) File(java.io.File)

Example 4 with SftpConfig

use of com.dtstack.taier.pluginapi.sftp.SftpConfig in project Taier by DTStack.

the class ConsoleComponentService method uploadKerberos.

@Transactional(rollbackFor = Exception.class)
public String uploadKerberos(List<Resource> resources, Long clusterId, Integer componentCode, String versionName) {
    if (CollectionUtils.isEmpty(resources)) {
        throw new RdosDefineException("Please upload a kerberos file!");
    }
    Resource resource = resources.get(0);
    String kerberosFileName = resource.getFileName();
    if (!kerberosFileName.endsWith(ZIP_SUFFIX)) {
        throw new RdosDefineException("Kerberos upload files are not in zip format");
    }
    String sftpComponent = componentService.getComponentByClusterId(clusterId, EComponentType.SFTP.getTypeCode(), false, String.class, null);
    SftpConfig sftpConfig = getSFTPConfig(sftpComponent, componentCode, "");
    SftpFileManage sftpFileManage = sftpFileManageBean.retrieveSftpManager(sftpConfig);
    String remoteDir = sftpConfig.getPath() + File.separator + this.buildSftpPath(clusterId, componentCode);
    Component addComponent = new Component();
    addComponent.setComponentTypeCode(componentCode);
    addComponent.setVersionName(versionName);
    updateComponentKerberosFile(clusterId, addComponent, sftpFileManage, remoteDir, resource, null, null);
    List<KerberosConfig> kerberosConfigs = consoleKerberosMapper.listAll();
    return mergeKrb5(kerberosConfigs);
}
Also used : SftpFileManage(com.dtstack.taier.pluginapi.sftp.SftpFileManage) RdosDefineException(com.dtstack.taier.common.exception.RdosDefineException) Resource(com.dtstack.taier.dao.dto.Resource) SftpConfig(com.dtstack.taier.pluginapi.sftp.SftpConfig) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with SftpConfig

use of com.dtstack.taier.pluginapi.sftp.SftpConfig in project Taier by DTStack.

the class DummyClient method testConnect.

@Override
public ComponentTestResult testConnect(String pluginInfo) {
    ComponentTestResult componentTestResult = new ComponentTestResult();
    try {
        SftpConfig sftpConfig = PublicUtil.jsonStrToObject(pluginInfo, SftpConfig.class);
        // check sftpConfig 准确性
        SftpFileManage sftpFileManage = SftpFileManage.getSftpManager(sftpConfig);
        // 测试路径是否存在
        Vector res = sftpFileManage.listFile(sftpConfig.getPath());
        if (null != res) {
            componentTestResult.setResult(true);
        }
    } catch (Exception e) {
        componentTestResult.setErrorMsg(ExceptionUtil.getErrorMessage(e));
        componentTestResult.setResult(false);
    }
    return componentTestResult;
}
Also used : SftpFileManage(com.dtstack.taier.pluginapi.sftp.SftpFileManage) ComponentTestResult(com.dtstack.taier.pluginapi.pojo.ComponentTestResult) SftpConfig(com.dtstack.taier.pluginapi.sftp.SftpConfig) Vector(java.util.Vector) IOException(java.io.IOException)

Aggregations

SftpConfig (com.dtstack.taier.pluginapi.sftp.SftpConfig)5 SftpFileManage (com.dtstack.taier.pluginapi.sftp.SftpFileManage)4 RdosDefineException (com.dtstack.taier.common.exception.RdosDefineException)3 IOException (java.io.IOException)3 File (java.io.File)2 JSONObject (com.alibaba.fastjson.JSONObject)1 EComponentType (com.dtstack.taier.common.enums.EComponentType)1 Resource (com.dtstack.taier.dao.dto.Resource)1 ComponentTestResult (com.dtstack.taier.pluginapi.pojo.ComponentTestResult)1 ClientTemplate (com.dtstack.taier.scheduler.impl.pojo.ClientTemplate)1 Vector (java.util.Vector)1 Transactional (org.springframework.transaction.annotation.Transactional)1