Search in sources :

Example 21 with NodeProjectInfoModel

use of io.jpom.model.data.NodeProjectInfoModel in project Jpom by dromara.

the class BaseAgentController method getProjectInfoModel.

/**
 * 获取拦截器中缓存的项目信息
 *
 * @return NodeProjectInfoModel
 */
protected NodeProjectInfoModel getProjectInfoModel() {
    NodeProjectInfoModel nodeProjectInfoModel = tryGetProjectInfoModel();
    Objects.requireNonNull(nodeProjectInfoModel, "获取项目信息失败");
    return nodeProjectInfoModel;
}
Also used : NodeProjectInfoModel(io.jpom.model.data.NodeProjectInfoModel)

Example 22 with NodeProjectInfoModel

use of io.jpom.model.data.NodeProjectInfoModel in project Jpom by dromara.

the class BaseAgentController method getProjectInfoModel.

/**
 * 根据 项目ID 获取项目信息
 *
 * @return NodeProjectInfoModel
 */
protected NodeProjectInfoModel getProjectInfoModel(String id) {
    NodeProjectInfoModel nodeProjectInfoModel = tryGetProjectInfoModel(id);
    Objects.requireNonNull(nodeProjectInfoModel, "获取项目信息失败");
    return nodeProjectInfoModel;
}
Also used : NodeProjectInfoModel(io.jpom.model.data.NodeProjectInfoModel)

Example 23 with NodeProjectInfoModel

use of io.jpom.model.data.NodeProjectInfoModel in project Jpom by dromara.

the class ProjectFileControl method upload.

@RequestMapping(value = "upload", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public String upload() throws Exception {
    NodeProjectInfoModel pim = getProjectInfoModel();
    MultipartFileBuilder multipartFileBuilder = createMultipart().addFieldName("file").setUseOriginalFilename(true);
    // 压缩文件
    String type = getParameter("type");
    // 是否清空
    String clearType = getParameter("clearType");
    String levelName = getParameter("levelName");
    File lib;
    if (StrUtil.isEmpty(levelName)) {
        lib = new File(pim.allLib());
    } else {
        lib = FileUtil.file(pim.allLib(), levelName);
    }
    // 判断是否需要清空
    if ("clear".equalsIgnoreCase(clearType)) {
        if (!FileUtil.clean(lib)) {
            FileUtil.del(lib.toPath());
        // return JsonMessage.getString(500, "清除旧lib失败");
        }
    }
    if ("unzip".equals(type)) {
        multipartFileBuilder.setFileExt(StringUtil.PACKAGE_EXT);
        multipartFileBuilder.setSavePath(AgentConfigBean.getInstance().getTempPathName());
        String path = multipartFileBuilder.save();
        // 解压
        File file = new File(path);
        try {
            CompressionFileUtil.unCompress(file, lib);
        } finally {
            if (!FileUtil.del(file)) {
                DefaultSystemLog.getLog().error("删除文件失败:" + file.getPath());
            }
        }
    } else {
        multipartFileBuilder.setSavePath(FileUtil.getAbsolutePath(lib));
        // 保存
        multipartFileBuilder.save();
    }
    // 修改使用状态
    pim.setUseLibDesc("upload");
    projectInfoService.updateItem(pim);
    // 
    String after = getParameter("after");
    if (StrUtil.isNotEmpty(after)) {
        // 
        List<NodeProjectInfoModel.JavaCopyItem> javaCopyItemList = pim.getJavaCopyItemList();
        // 
        AfterOpt afterOpt = BaseEnum.getEnum(AfterOpt.class, Convert.toInt(after, AfterOpt.No.getCode()));
        if ("restart".equalsIgnoreCase(after) || afterOpt == AfterOpt.Restart) {
            String result = consoleService.execCommand(ConsoleCommandOp.restart, pim, null);
            // 自动处理副本集
            if (javaCopyItemList != null) {
                ThreadUtil.execute(() -> javaCopyItemList.forEach(javaCopyItem -> {
                    try {
                        consoleService.execCommand(ConsoleCommandOp.restart, pim, javaCopyItem);
                    } catch (Exception e) {
                        DefaultSystemLog.getLog().error("重启副本集失败", e);
                    }
                }));
            }
            return JsonMessage.getString(200, "上传成功并重启:" + result);
        }
        if (afterOpt == AfterOpt.Order_Restart || afterOpt == AfterOpt.Order_Must_Restart) {
            boolean restart = this.restart(pim, null, afterOpt);
            if (javaCopyItemList != null) {
                ThreadUtil.execute(() -> {
                    // 副本
                    for (NodeProjectInfoModel.JavaCopyItem javaCopyItem : javaCopyItemList) {
                        if (!this.restart(pim, javaCopyItem, afterOpt)) {
                            return;
                        }
                        // 休眠30秒 等待之前项目正常启动
                        try {
                            TimeUnit.SECONDS.sleep(30);
                        } catch (InterruptedException ignored) {
                        }
                    }
                });
            }
        }
    }
    return JsonMessage.getString(200, "上传成功");
}
Also used : SecureUtil(cn.hutool.crypto.SecureUtil) MultipartFileBuilder(cn.jiangzeyin.controller.multipart.MultipartFileBuilder) DefaultSystemLog(cn.jiangzeyin.common.DefaultSystemLog) ConsoleService(io.jpom.service.manage.ConsoleService) CollStreamUtil(cn.hutool.core.collection.CollStreamUtil) FileUtils(io.jpom.util.FileUtils) ServletUtil(cn.hutool.extra.servlet.ServletUtil) JsonMessage(cn.jiangzeyin.common.JsonMessage) JSONArray(com.alibaba.fastjson.JSONArray) HttpUtil(cn.hutool.http.HttpUtil) Charset(java.nio.charset.Charset) BaseEnum(io.jpom.model.BaseEnum) Map(java.util.Map) AbstractProjectCommander(io.jpom.common.commander.AbstractProjectCommander) AgentConfigBean(io.jpom.system.AgentConfigBean) DiffFileVo(io.jpom.controller.manage.vo.DiffFileVo) BaseAgentController(io.jpom.common.BaseAgentController) MediaType(org.springframework.http.MediaType) Set(java.util.Set) ConsoleCommandOp(io.jpom.socket.ConsoleCommandOp) CompressionFileUtil(io.jpom.util.CompressionFileUtil) AfterOpt(io.jpom.model.AfterOpt) Collectors(java.util.stream.Collectors) File(java.io.File) BooleanUtil(cn.hutool.core.util.BooleanUtil) WhitelistDirectoryService(io.jpom.service.WhitelistDirectoryService) TimeUnit(java.util.concurrent.TimeUnit) CollUtil(cn.hutool.core.collection.CollUtil) StrUtil(cn.hutool.core.util.StrUtil) List(java.util.List) ArrayUtil(cn.hutool.core.util.ArrayUtil) StringUtil(io.jpom.util.StringUtil) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) Convert(cn.hutool.core.convert.Convert) AgentWhitelist(io.jpom.model.data.AgentWhitelist) FileUtil(cn.hutool.core.io.FileUtil) JSONObject(com.alibaba.fastjson.JSONObject) ThreadUtil(cn.hutool.core.thread.ThreadUtil) NodeProjectInfoModel(io.jpom.model.data.NodeProjectInfoModel) ProjectCommanderUtil(io.jpom.util.ProjectCommanderUtil) Assert(org.springframework.util.Assert) NodeProjectInfoModel(io.jpom.model.data.NodeProjectInfoModel) AfterOpt(io.jpom.model.AfterOpt) MultipartFileBuilder(cn.jiangzeyin.controller.multipart.MultipartFileBuilder) File(java.io.File)

Example 24 with NodeProjectInfoModel

use of io.jpom.model.data.NodeProjectInfoModel in project Jpom by dromara.

the class ProjectFileControl method remoteDownload.

/**
 * 下载远程文件
 *
 * @param id        项目id
 * @param url       远程 url 地址
 * @param levelName 保存的文件夹
 * @param unzip     是否为压缩包、true 将自动解压
 * @return json
 */
@PostMapping(value = "remote_download", produces = MediaType.APPLICATION_JSON_VALUE)
public String remoteDownload(String id, String url, String levelName, String unzip) {
    Assert.hasText(url, "请输入正确的远程地址");
    AgentWhitelist whitelist = whitelistDirectoryService.getWhitelist();
    Set<String> allowRemoteDownloadHost = whitelist.getAllowRemoteDownloadHost();
    Assert.state(CollUtil.isNotEmpty(allowRemoteDownloadHost), "还没有配置运行的远程地址");
    List<String> collect = allowRemoteDownloadHost.stream().filter(s -> StrUtil.startWith(url, s)).collect(Collectors.toList());
    Assert.state(CollUtil.isNotEmpty(collect), "不允许下载当前地址的文件");
    try {
        NodeProjectInfoModel pim = projectInfoService.getItem(id);
        File file = FileUtil.file(pim.allLib(), StrUtil.emptyToDefault(levelName, FileUtil.FILE_SEPARATOR));
        File downloadFile = HttpUtil.downloadFileFromUrl(url, file);
        if (BooleanUtil.toBoolean(unzip)) {
            // 需要解压文件
            try {
                CompressionFileUtil.unCompress(file, downloadFile);
            } finally {
                if (!FileUtil.del(downloadFile)) {
                    DefaultSystemLog.getLog().error("删除文件失败:" + file.getPath());
                }
            }
        }
        return JsonMessage.getString(200, "下载成功文件大小:" + FileUtil.readableFileSize(downloadFile));
    } catch (Exception e) {
        DefaultSystemLog.getLog().error("下载远程文件异常", e);
        return JsonMessage.getString(500, "下载远程文件失败:" + e.getMessage());
    }
}
Also used : SecureUtil(cn.hutool.crypto.SecureUtil) MultipartFileBuilder(cn.jiangzeyin.controller.multipart.MultipartFileBuilder) DefaultSystemLog(cn.jiangzeyin.common.DefaultSystemLog) ConsoleService(io.jpom.service.manage.ConsoleService) CollStreamUtil(cn.hutool.core.collection.CollStreamUtil) FileUtils(io.jpom.util.FileUtils) ServletUtil(cn.hutool.extra.servlet.ServletUtil) JsonMessage(cn.jiangzeyin.common.JsonMessage) JSONArray(com.alibaba.fastjson.JSONArray) HttpUtil(cn.hutool.http.HttpUtil) Charset(java.nio.charset.Charset) BaseEnum(io.jpom.model.BaseEnum) Map(java.util.Map) AbstractProjectCommander(io.jpom.common.commander.AbstractProjectCommander) AgentConfigBean(io.jpom.system.AgentConfigBean) DiffFileVo(io.jpom.controller.manage.vo.DiffFileVo) BaseAgentController(io.jpom.common.BaseAgentController) MediaType(org.springframework.http.MediaType) Set(java.util.Set) ConsoleCommandOp(io.jpom.socket.ConsoleCommandOp) CompressionFileUtil(io.jpom.util.CompressionFileUtil) AfterOpt(io.jpom.model.AfterOpt) Collectors(java.util.stream.Collectors) File(java.io.File) BooleanUtil(cn.hutool.core.util.BooleanUtil) WhitelistDirectoryService(io.jpom.service.WhitelistDirectoryService) TimeUnit(java.util.concurrent.TimeUnit) CollUtil(cn.hutool.core.collection.CollUtil) StrUtil(cn.hutool.core.util.StrUtil) List(java.util.List) ArrayUtil(cn.hutool.core.util.ArrayUtil) StringUtil(io.jpom.util.StringUtil) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) Convert(cn.hutool.core.convert.Convert) AgentWhitelist(io.jpom.model.data.AgentWhitelist) FileUtil(cn.hutool.core.io.FileUtil) JSONObject(com.alibaba.fastjson.JSONObject) ThreadUtil(cn.hutool.core.thread.ThreadUtil) NodeProjectInfoModel(io.jpom.model.data.NodeProjectInfoModel) ProjectCommanderUtil(io.jpom.util.ProjectCommanderUtil) Assert(org.springframework.util.Assert) NodeProjectInfoModel(io.jpom.model.data.NodeProjectInfoModel) AgentWhitelist(io.jpom.model.data.AgentWhitelist) File(java.io.File)

Example 25 with NodeProjectInfoModel

use of io.jpom.model.data.NodeProjectInfoModel in project Jpom by dromara.

the class ProjectFileControl method download.

/**
 * 将执行文件下载到客户端 本地
 *
 * @param id        项目id
 * @param filename  文件名
 * @param levelName 文件夹名
 * @return 正常情况返回文件流,非正在返回 text plan
 */
@GetMapping(value = "download", produces = MediaType.APPLICATION_JSON_VALUE)
public String download(String id, String filename, String levelName) {
    Assert.hasText(filename, "请选择文件");
    // }
    try {
        NodeProjectInfoModel pim = projectInfoService.getItem(id);
        File file = FileUtil.file(pim.allLib(), StrUtil.emptyToDefault(levelName, FileUtil.FILE_SEPARATOR), filename);
        if (file.isDirectory()) {
            return "暂不支持下载文件夹";
        }
        ServletUtil.write(getResponse(), file);
    } catch (Exception e) {
        DefaultSystemLog.getLog().error("下载文件异常", e);
    }
    return "下载失败。请刷新页面后重试";
}
Also used : NodeProjectInfoModel(io.jpom.model.data.NodeProjectInfoModel) File(java.io.File)

Aggregations

NodeProjectInfoModel (io.jpom.model.data.NodeProjectInfoModel)40 File (java.io.File)18 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)18 JSONObject (com.alibaba.fastjson.JSONObject)11 JSONArray (com.alibaba.fastjson.JSONArray)7 AgentWhitelist (io.jpom.model.data.AgentWhitelist)6 AbstractProjectCommander (io.jpom.common.commander.AbstractProjectCommander)5 Charset (java.nio.charset.Charset)5 DiffFileVo (io.jpom.controller.manage.vo.DiffFileVo)4 ConsoleService (io.jpom.service.manage.ConsoleService)4 CollStreamUtil (cn.hutool.core.collection.CollStreamUtil)3 CollUtil (cn.hutool.core.collection.CollUtil)3 Convert (cn.hutool.core.convert.Convert)3 FileUtil (cn.hutool.core.io.FileUtil)3 ThreadUtil (cn.hutool.core.thread.ThreadUtil)3 ArrayUtil (cn.hutool.core.util.ArrayUtil)3 BooleanUtil (cn.hutool.core.util.BooleanUtil)3 StrUtil (cn.hutool.core.util.StrUtil)3 SecureUtil (cn.hutool.crypto.SecureUtil)3 ServletUtil (cn.hutool.extra.servlet.ServletUtil)3