Search in sources :

Example 1 with RunMode

use of io.jpom.model.RunMode in project Jpom by dromara.

the class ManageEditProjectController method checkParameter.

/**
 * 基础检查
 *
 * @param projectInfo        项目实体
 * @param whitelistDirectory 白名单
 * @param previewData        预检查数据
 */
private void checkParameter(NodeProjectInfoModel projectInfo, String whitelistDirectory, boolean previewData) {
    String id = projectInfo.getId();
    String checkId = StrUtil.replace(id, StrUtil.DASHED, StrUtil.UNDERLINE);
    Validator.validateGeneral(checkId, 2, Const.ID_MAX_LEN, "项目id 长度范围2-20(英文字母 、数字和下划线)");
    Assert.state(!JpomApplication.SYSTEM_ID.equals(id), "项目id " + JpomApplication.SYSTEM_ID + " 关键词被系统占用");
    // 防止和Jpom冲突
    ConfigBean instance = ConfigBean.getInstance();
    Assert.state(!instance.applicationTag.equalsIgnoreCase(id), "当前项目id已经被Jpom占用");
    // 运行模式
    String runMode = getParameter("runMode");
    RunMode runMode1 = RunMode.ClassPath;
    try {
        runMode1 = RunMode.valueOf(runMode);
    } catch (Exception ignored) {
    }
    projectInfo.setRunMode(runMode1);
    // 监测
    if (runMode1 == RunMode.ClassPath || runMode1 == RunMode.JavaExtDirsCp) {
        Assert.hasText(projectInfo.getMainClass(), "ClassPath、JavaExtDirsCp 模式 MainClass必填");
    } else if (runMode1 == RunMode.Jar || runMode1 == RunMode.JarWar) {
        projectInfo.setMainClass(StrUtil.EMPTY);
    }
    if (runMode1 == RunMode.JavaExtDirsCp) {
        Assert.hasText(projectInfo.getJavaExtDirsCp(), "JavaExtDirsCp 模式 javaExtDirsCp必填");
    }
    // 判断是否为分发添加
    String strOutGivingProject = getParameter("outGivingProject");
    boolean outGivingProject = Boolean.parseBoolean(strOutGivingProject);
    projectInfo.setOutGivingProject(outGivingProject);
    if (!previewData) {
        // 不是预检查数据才效验白名单
        if (!whitelistDirectoryService.checkProjectDirectory(whitelistDirectory)) {
            if (outGivingProject) {
                whitelistDirectoryService.addProjectWhiteList(whitelistDirectory);
            } else {
                throw new IllegalArgumentException("请选择正确的项目路径,或者还没有配置白名单");
            }
        }
        String logPath = projectInfo.getLogPath();
        if (StrUtil.isNotEmpty(logPath)) {
            if (!whitelistDirectoryService.checkProjectDirectory(logPath)) {
                if (outGivingProject) {
                    whitelistDirectoryService.addProjectWhiteList(logPath);
                } else {
                    throw new IllegalArgumentException("请填写的项目日志存储路径,或者还没有配置白名单");
                }
            }
        }
    }
    // 
    String lib = projectInfo.getLib();
    Assert.state(StrUtil.isNotEmpty(lib) && !StrUtil.SLASH.equals(lib) && !Validator.isChinese(lib), "项目Jar路径不能为空,不能为顶级目录,不能包含中文");
    Assert.state(checkPathSafe(lib), "项目Jar路径存在提升目录问题");
    // java 程序副本
    if (runMode1 == RunMode.ClassPath || runMode1 == RunMode.Jar || runMode1 == RunMode.JarWar || runMode1 == RunMode.JavaExtDirsCp) {
        String javaCopyIds = getParameter("javaCopyIds");
        if (StrUtil.isEmpty(javaCopyIds)) {
            projectInfo.setJavaCopyItemList(null);
        } else {
            String[] split = StrUtil.splitToArray(javaCopyIds, StrUtil.COMMA);
            List<NodeProjectInfoModel.JavaCopyItem> javaCopyItemList = new ArrayList<>(split.length);
            for (String copyId : split) {
                String jvm = getParameter("jvm_" + copyId);
                String args = getParameter("args_" + copyId);
                // 
                NodeProjectInfoModel.JavaCopyItem javaCopyItem = new NodeProjectInfoModel.JavaCopyItem();
                javaCopyItem.setId(copyId);
                javaCopyItem.setParentId(id);
                javaCopyItem.setModifyTime(DateUtil.now());
                javaCopyItem.setJvm(StrUtil.emptyToDefault(jvm, StrUtil.EMPTY));
                javaCopyItem.setArgs(StrUtil.emptyToDefault(args, StrUtil.EMPTY));
                javaCopyItemList.add(javaCopyItem);
            }
            projectInfo.setJavaCopyItemList(javaCopyItemList);
        }
    } else {
        projectInfo.setJavaCopyItemList(null);
    }
}
Also used : NodeProjectInfoModel(io.jpom.model.data.NodeProjectInfoModel) RunMode(io.jpom.model.RunMode) ArrayList(java.util.ArrayList) ConfigBean(io.jpom.system.ConfigBean)

Example 2 with RunMode

use of io.jpom.model.RunMode in project Jpom by dromara.

the class ManageEditProjectController method save.

/**
 * 保存项目
 *
 * @param projectInfo 项目
 * @param previewData 是否是预检查
 * @return 错误信息
 */
private String save(NodeProjectInfoModel projectInfo, boolean previewData) {
    projectInfo.setWorkspaceId(getWorkspaceId());
    NodeProjectInfoModel exits = projectInfoService.getItem(projectInfo.getId());
    try {
        this.checkPath(projectInfo);
        RunMode runMode = projectInfo.getRunMode();
        if (exits == null) {
            // 检查运行中的tag 是否被占用
            if (runMode != RunMode.File && runMode != RunMode.Dsl) {
                Assert.state(!AbstractProjectCommander.getInstance().isRun(projectInfo, null), "当前项目id已经被正在运行的程序占用");
            }
            if (previewData) {
                // 预检查数据
                return JsonMessage.getString(200, "检查通过");
            } else {
                projectInfoService.addItem(projectInfo);
                return JsonMessage.getString(200, "新增成功!");
            }
        }
        if (previewData) {
            // 预检查数据
            return JsonMessage.getString(200, "检查通过");
        } else {
            exits.setLog(projectInfo.getLog());
            exits.setLogPath(projectInfo.getLogPath());
            exits.setName(projectInfo.getName());
            exits.setGroup(projectInfo.getGroup());
            exits.setAutoStart(projectInfo.getAutoStart());
            exits.setMainClass(projectInfo.getMainClass());
            exits.setLib(projectInfo.getLib());
            exits.setJvm(projectInfo.getJvm());
            exits.setArgs(projectInfo.getArgs());
            exits.setWorkspaceId(this.getWorkspaceId());
            exits.setOutGivingProject(projectInfo.isOutGivingProject());
            exits.setRunMode(runMode);
            exits.setWhitelistDirectory(projectInfo.getWhitelistDirectory());
            exits.setToken(projectInfo.getToken());
            exits.setDslContent(projectInfo.getDslContent());
            exits.setJdkId(projectInfo.getJdkId());
            // 检查是否非法删除副本集
            List<NodeProjectInfoModel.JavaCopyItem> javaCopyItemList = exits.getJavaCopyItemList();
            List<NodeProjectInfoModel.JavaCopyItem> javaCopyItemList1 = projectInfo.getJavaCopyItemList();
            if (CollUtil.isNotEmpty(javaCopyItemList) && !CollUtil.containsAll(javaCopyItemList1, javaCopyItemList)) {
                // 重写了 equals
                return JsonMessage.getString(405, "修改中不能删除副本集、请到副本集中删除");
            }
            exits.setJavaCopyItemList(javaCopyItemList1);
            exits.setJavaExtDirsCp(projectInfo.getJavaExtDirsCp());
            // 
            moveTo(exits, projectInfo);
            projectInfoService.updateItem(exits);
            return JsonMessage.getString(200, "修改成功");
        }
    } catch (Exception e) {
        DefaultSystemLog.getLog().error(e.getMessage(), e);
        return JsonMessage.getString(500, "保存数据异常:" + e.getMessage());
    }
}
Also used : NodeProjectInfoModel(io.jpom.model.data.NodeProjectInfoModel) RunMode(io.jpom.model.RunMode)

Example 3 with RunMode

use of io.jpom.model.RunMode in project Jpom by dromara.

the class ProjectListController method getProjectItem.

/**
 * 获取项目的信息
 *
 * @param id id
 * @return item
 * @see NodeProjectInfoModel
 */
@RequestMapping(value = "getProjectItem", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public String getProjectItem(String id) {
    NodeProjectInfoModel nodeProjectInfoModel = projectInfoService.getItem(id);
    if (nodeProjectInfoModel != null) {
        RunMode runMode = nodeProjectInfoModel.getRunMode();
        if (runMode != RunMode.Dsl && runMode != RunMode.File) {
            // 返回实际执行的命令
            String command = AbstractProjectCommander.getInstance().buildJavaCommand(nodeProjectInfoModel, null);
            nodeProjectInfoModel.setRunCommand(command);
        }
        // 
        List<NodeProjectInfoModel.JavaCopyItem> javaCopyItemList = nodeProjectInfoModel.getJavaCopyItemList();
        if (javaCopyItemList != null) {
            for (NodeProjectInfoModel.JavaCopyItem javaCopyItem : javaCopyItemList) {
                File logBack = nodeProjectInfoModel.getLogBack(javaCopyItem);
                File log = nodeProjectInfoModel.getLog(javaCopyItem);
                javaCopyItem.setLogBack(FileUtil.getAbsolutePath(logBack));
                javaCopyItem.setLog(FileUtil.getAbsolutePath(log));
            }
        }
    }
    return JsonMessage.getString(200, "", nodeProjectInfoModel);
}
Also used : NodeProjectInfoModel(io.jpom.model.data.NodeProjectInfoModel) RunMode(io.jpom.model.RunMode) File(java.io.File) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with RunMode

use of io.jpom.model.RunMode in project Jpom by dromara.

the class AbstractProjectCommander method start.

/**
 * 启动
 *
 * @param nodeProjectInfoModel 项目
 * @return 结果
 * @throws Exception 异常
 */
public String start(NodeProjectInfoModel nodeProjectInfoModel, NodeProjectInfoModel.JavaCopyItem javaCopyItem) throws Exception {
    String msg = checkStart(nodeProjectInfoModel, javaCopyItem);
    if (msg != null) {
        return msg;
    }
    RunMode runMode = nodeProjectInfoModel.getRunMode();
    if (runMode == RunMode.Dsl) {
        // 
        String startDsl = this.runDsl(nodeProjectInfoModel, "start", baseProcess -> {
            String log = nodeProjectInfoModel.getAbsoluteLog(null);
            return DslScriptBuilder.run(baseProcess, nodeProjectInfoModel, log);
        });
        if (startDsl != null) {
            return startDsl;
        }
    } else {
        String command = this.buildJavaCommand(nodeProjectInfoModel, javaCopyItem);
        if (command == null) {
            return "没有需要执行的命令";
        }
        // 执行命令
        ThreadUtil.execute(() -> {
            try {
                File file = FileUtil.file(nodeProjectInfoModel.allLib());
                if (SystemUtil.getOsInfo().isWindows()) {
                    CommandUtil.execSystemCommand(command, file);
                } else {
                    CommandUtil.asyncExeLocalCommand(file, command);
                }
            } catch (Exception e) {
                DefaultSystemLog.getLog().error("执行命令失败", e);
            }
        });
    }
    // 
    this.loopCheckRun(nodeProjectInfoModel, javaCopyItem, true);
    String status = this.status(nodeProjectInfoModel, javaCopyItem);
    this.asyncWebHooks(nodeProjectInfoModel, javaCopyItem, "start", "result", status);
    return status;
}
Also used : RunMode(io.jpom.model.RunMode) JarFile(java.util.jar.JarFile) File(java.io.File) JpomRuntimeException(io.jpom.system.JpomRuntimeException) IOException(java.io.IOException)

Example 5 with RunMode

use of io.jpom.model.RunMode in project Jpom by dromara.

the class AbstractProjectCommander method status.

/**
 * 查询项目状态
 *
 * @param nodeProjectInfoModel 项目
 * @param javaCopyItem         副本
 * @return 状态
 */
public String status(NodeProjectInfoModel nodeProjectInfoModel, NodeProjectInfoModel.JavaCopyItem javaCopyItem) {
    RunMode runMode = nodeProjectInfoModel.getRunMode();
    if (runMode == RunMode.Dsl) {
        String status = this.runDsl(nodeProjectInfoModel, "status", baseProcess -> DslScriptBuilder.syncRun(baseProcess, nodeProjectInfoModel));
        List<String> split1 = StrUtil.split(status, StrUtil.CRLF);
        return CollUtil.getLast(split1);
    } else {
        String tag = javaCopyItem == null ? nodeProjectInfoModel.getId() : javaCopyItem.getTagId();
        return this.status(tag);
    }
}
Also used : RunMode(io.jpom.model.RunMode)

Aggregations

RunMode (io.jpom.model.RunMode)10 File (java.io.File)5 NodeProjectInfoModel (io.jpom.model.data.NodeProjectInfoModel)3 Tuple (cn.hutool.core.lang.Tuple)2 JpomRuntimeException (io.jpom.system.JpomRuntimeException)2 JarFile (java.util.jar.JarFile)2 CollUtil (cn.hutool.core.collection.CollUtil)1 FileUtil (cn.hutool.core.io.FileUtil)1 JarClassLoader (cn.hutool.core.lang.JarClassLoader)1 ArrayUtil (cn.hutool.core.util.ArrayUtil)1 StrUtil (cn.hutool.core.util.StrUtil)1 HtmlUtil (cn.hutool.http.HtmlUtil)1 XssFilter (cn.jiangzeyin.common.request.XssFilter)1 SpringUtil (cn.jiangzeyin.common.spring.SpringUtil)1 JSONObject (com.alibaba.fastjson.JSONObject)1 BaseJsonModel (io.jpom.model.BaseJsonModel)1 WhitelistDirectoryService (io.jpom.service.WhitelistDirectoryService)1 ConfigBean (io.jpom.system.ConfigBean)1 IOException (java.io.IOException)1 java.util (java.util)1