Search in sources :

Example 36 with NodeProjectInfoModel

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

the class AgentWebSocketConsoleHandle method runMsg.

private void runMsg(ConsoleCommandOp consoleCommandOp, Session session, NodeProjectInfoModel nodeProjectInfoModel, String copyId, JSONObject reqJson) throws Exception {
    ConsoleService consoleService = SpringUtil.getBean(ConsoleService.class);
    // 
    NodeProjectInfoModel.JavaCopyItem copyItem = nodeProjectInfoModel.findCopyItem(copyId);
    JSONObject resultData = null;
    String strResult;
    boolean logUser = false;
    try {
        // 执行相应命令
        switch(consoleCommandOp) {
            case start:
            case restart:
                logUser = true;
                strResult = consoleService.execCommand(consoleCommandOp, nodeProjectInfoModel, copyItem);
                if (strResult.contains(AbstractProjectCommander.RUNNING_TAG)) {
                    resultData = JsonMessage.toJson(200, "操作成功:" + strResult);
                } else {
                    resultData = JsonMessage.toJson(400, strResult);
                }
                break;
            case stop:
                logUser = true;
                // 停止项目
                strResult = consoleService.execCommand(consoleCommandOp, nodeProjectInfoModel, copyItem);
                if (strResult.contains(AbstractProjectCommander.STOP_TAG)) {
                    resultData = JsonMessage.toJson(200, "操作成功:" + strResult);
                } else {
                    resultData = JsonMessage.toJson(500, strResult);
                }
                break;
            case status:
                // 获取项目状态
                strResult = consoleService.execCommand(consoleCommandOp, nodeProjectInfoModel, copyItem);
                if (strResult.contains(AbstractProjectCommander.RUNNING_TAG)) {
                    resultData = JsonMessage.toJson(200, "运行中", strResult);
                } else {
                    resultData = JsonMessage.toJson(404, "未运行", strResult);
                }
                break;
            case showlog:
                {
                    // 进入管理页面后需要实时加载日志
                    // 日志文件路径
                    String fileName = reqJson.getString("fileName");
                    File file;
                    if (StrUtil.isEmpty(fileName)) {
                        file = copyItem == null ? new File(nodeProjectInfoModel.getLog()) : nodeProjectInfoModel.getLog(copyItem);
                    } else {
                        file = FileUtil.file(nodeProjectInfoModel.allLib(), fileName);
                    }
                    try {
                        boolean watcher = AgentFileTailWatcher.addWatcher(file, session);
                        if (!watcher) {
                            SocketSessionUtil.send(session, "监听文件失败,可能文件不存在");
                        }
                    } catch (IOException io) {
                        DefaultSystemLog.getLog().error("监听日志变化", io);
                        SocketSessionUtil.send(session, io.getMessage());
                    }
                    break;
                }
            default:
                resultData = JsonMessage.toJson(404, "不支持的方式:" + consoleCommandOp.name());
                break;
        }
    } catch (Exception e) {
        DefaultSystemLog.getLog().error("执行命令失败", e);
        SocketSessionUtil.send(session, "执行命令失败,详情如下:");
        SocketSessionUtil.send(session, ExceptionUtil.stacktraceToString(e));
        return;
    } finally {
        if (logUser) {
            // 记录操作人
            NodeProjectInfoModel newNodeProjectInfoModel = projectInfoService.getItem(nodeProjectInfoModel.getId());
            String name = getOptUserName(session);
            newNodeProjectInfoModel.setModifyUser(name);
            projectInfoService.updateItem(newNodeProjectInfoModel);
        }
    }
    // 返回数据
    if (resultData != null) {
        reqJson.putAll(resultData);
        reqJson.put("JPOM_MSG", "JPOM_MSG");
        DefaultSystemLog.getLog().info(reqJson.toString());
        SocketSessionUtil.send(session, reqJson.toString());
    }
}
Also used : NodeProjectInfoModel(io.jpom.model.data.NodeProjectInfoModel) JSONObject(com.alibaba.fastjson.JSONObject) ConsoleService(io.jpom.service.manage.ConsoleService) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException)

Example 37 with NodeProjectInfoModel

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

the class ManageEditProjectController method saveProject.

@RequestMapping(value = "saveProject", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public String saveProject(NodeProjectInfoModel projectInfo) {
    // 预检查数据
    String strPreviewData = getParameter("previewData");
    boolean previewData = Convert.toBool(strPreviewData, false);
    String whitelistDirectory = projectInfo.getWhitelistDirectory();
    // 
    this.checkParameter(projectInfo, whitelistDirectory, previewData);
    String id = projectInfo.getId();
    // 
    String allLib = projectInfo.allLib();
    // 重复lib
    List<NodeProjectInfoModel> list = projectInfoService.list();
    if (list != null) {
        for (NodeProjectInfoModel nodeProjectInfoModel : list) {
            if (!nodeProjectInfoModel.getId().equals(id) && nodeProjectInfoModel.allLib().equals(allLib)) {
                return JsonMessage.getString(401, "当前项目路径已经被【" + nodeProjectInfoModel.getName() + "】占用,请检查");
            }
        }
    }
    File checkFile = new File(allLib);
    Assert.state(!FileUtil.exist(checkFile) || FileUtil.isDirectory(checkFile), "项目路径是一个已经存在的文件");
    // 自动生成log文件
    String log = projectInfo.getLog();
    Assert.hasText(log, "项目log解析读取失败");
    checkFile = new File(log);
    Assert.state(!FileUtil.exist(checkFile) || FileUtil.isFile(checkFile), "项目log是一个已经存在的文件夹");
    // 
    String token = projectInfo.getToken();
    if (StrUtil.isNotEmpty(token)) {
        Validator.validateMatchRegex(RegexPool.URL_HTTP, token, "WebHooks 地址不合法");
    }
    // 判断空格
    Assert.state(!id.contains(StrUtil.SPACE) && !allLib.contains(StrUtil.SPACE), "项目Id、项目路径不能包含空格");
    String jdkId = projectInfo.getJdkId();
    if (StrUtil.isNotEmpty(jdkId)) {
        JdkInfoModel item = jdkInfoService.getItem(jdkId);
        Assert.notNull(item, "jdk 信息错误");
    }
    // 判断 yml
    this.checkDslYml(projectInfo);
    // 
    return save(projectInfo, previewData);
}
Also used : NodeProjectInfoModel(io.jpom.model.data.NodeProjectInfoModel) JdkInfoModel(io.jpom.model.data.JdkInfoModel) File(java.io.File) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 38 with NodeProjectInfoModel

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

the class ProjectListController method projectCopyList.

/**
 * 展示项目页面
 */
@RequestMapping(value = "project_copy_list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public String projectCopyList(String id) {
    NodeProjectInfoModel nodeProjectInfoModel = projectInfoService.getItem(id);
    Assert.notNull(nodeProjectInfoModel, "没有对应项目");
    List<NodeProjectInfoModel.JavaCopyItem> javaCopyItemList = nodeProjectInfoModel.getJavaCopyItemList();
    Assert.notEmpty(javaCopyItemList, "对应项目没有副本集");
    // }
    return JsonMessage.getString(200, "", javaCopyItemList);
}
Also used : NodeProjectInfoModel(io.jpom.model.data.NodeProjectInfoModel) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 39 with NodeProjectInfoModel

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

the class ProjectStatusController method getProjectStatus.

/**
 * 获取项目的进程id
 *
 * @param id 项目id
 * @return json
 */
@RequestMapping(value = "getProjectStatus", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public String getProjectStatus(@ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "项目id 不正确") String id, String getCopy) {
    NodeProjectInfoModel nodeProjectInfoModel = tryGetProjectInfoModel();
    Assert.notNull(nodeProjectInfoModel, "项目id不存在");
    int pid = 0;
    try {
        pid = AbstractProjectCommander.getInstance().getPid(nodeProjectInfoModel, null);
    } catch (Exception e) {
        DefaultSystemLog.getLog().error("获取项目pid 失败", e);
    }
    if (pid <= 0) {
        Assert.state(JvmUtil.jpsNormal, "当前服务器 jps 命令异常,请检查 jdk 是否完整,以及 java 环境变量是否配置正确");
    }
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("pId", pid);
    // 
    if (StrUtil.isNotEmpty(getCopy)) {
        List<NodeProjectInfoModel.JavaCopyItem> javaCopyItemList = nodeProjectInfoModel.getJavaCopyItemList();
        JSONArray copys = new JSONArray();
        if (javaCopyItemList != null) {
            for (NodeProjectInfoModel.JavaCopyItem javaCopyItem : javaCopyItemList) {
                JSONObject jsonObject1 = new JSONObject();
                jsonObject1.put("copyId", javaCopyItem.getId());
                boolean run = AbstractProjectCommander.getInstance().isRun(nodeProjectInfoModel, javaCopyItem);
                jsonObject1.put("status", run);
                copys.add(jsonObject1);
            }
        }
        jsonObject.put("copys", copys);
    }
    return JsonMessage.getString(200, "", jsonObject);
}
Also used : NodeProjectInfoModel(io.jpom.model.data.NodeProjectInfoModel) JSONObject(com.alibaba.fastjson.JSONObject) JSONArray(com.alibaba.fastjson.JSONArray) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 40 with NodeProjectInfoModel

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

the class ProjectInfoService method deleteItem.

// public HashSet<String> getAllGroup() {
// //获取所有分组
// List<NodeProjectInfoModel> nodeProjectInfoModels = list();
// HashSet<String> hashSet = new HashSet<>();
// if (nodeProjectInfoModels == null) {
// return hashSet;
// }
// for (NodeProjectInfoModel nodeProjectInfoModel : nodeProjectInfoModels) {
// hashSet.add(nodeProjectInfoModel.getGroup());
// }
// return hashSet;
// }
/**
 * 删除项目
 *
 * @param id 项目
 */
@Override
public void deleteItem(String id) {
    NodeProjectInfoModel projectInfo = getItem(id);
    String userId = BaseAgentController.getNowUserName();
    super.deleteItem(id);
    // 添加回收记录
    ProjectRecoverModel projectRecoverModel = new ProjectRecoverModel(projectInfo);
    projectRecoverModel.setDelUser(userId);
    projectRecoverService.addItem(projectRecoverModel);
}
Also used : NodeProjectInfoModel(io.jpom.model.data.NodeProjectInfoModel) ProjectRecoverModel(io.jpom.model.data.ProjectRecoverModel)

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