Search in sources :

Example 1 with CommandExecLogModel

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

the class CommandService method execute.

/**
 * 执行命令
 *
 * @param commandModel        命令模版
 * @param commandExecLogModel 执行记录
 * @param sshModel            ssh
 * @param commandParamsLine   参数
 * @throws IOException io
 */
private void execute(CommandModel commandModel, CommandExecLogModel commandExecLogModel, SshModel sshModel, String commandParamsLine) throws IOException {
    File file = commandExecLogModel.logFile();
    try (BufferedOutputStream outputStream = FileUtil.getOutputStream(file)) {
        if (sshModel == null) {
            this.appendLine(outputStream, "ssh 不存在");
            return;
        }
        String command = commandModel.getCommand();
        String[] commands = StrUtil.splitToArray(command, StrUtil.LF);
        // 
        workspaceEnvVarService.formatCommand(commandModel.getWorkspaceId(), commands);
        // 
        Charset charset = sshModel.getCharsetT();
        sshService.exec(sshModel, (s, session) -> {
            final ChannelExec channel = (ChannelExec) JschUtil.createChannel(session, ChannelType.EXEC);
            channel.setCommand(StrUtil.bytes(s + StrUtil.SPACE + commandParamsLine, charset));
            channel.setInputStream(null);
            channel.setErrStream(outputStream, true);
            InputStream in = null;
            try {
                channel.connect();
                in = channel.getInputStream();
                IoUtil.readLines(in, charset, (LineHandler) line -> this.appendLine(outputStream, line));
                // 更新状态
                this.updateStatus(commandExecLogModel.getId(), CommandExecLogModel.Status.DONE);
            } catch (Exception e) {
                DefaultSystemLog.getLog().error("执行命令错误", e);
                // 更新状态
                this.updateStatus(commandExecLogModel.getId(), CommandExecLogModel.Status.ERROR);
                // 记录错误日志
                String stacktraceToString = ExceptionUtil.stacktraceToString(e);
                this.appendLine(outputStream, stacktraceToString);
            } finally {
                IoUtil.close(in);
                JschUtil.close(channel);
            }
            return null;
        }, commands);
    }
}
Also used : CronUtils(io.jpom.cron.CronUtils) ExceptionUtil(cn.hutool.core.exceptions.ExceptionUtil) DefaultSystemLog(cn.jiangzeyin.common.DefaultSystemLog) IdUtil(cn.hutool.core.util.IdUtil) BufferedOutputStream(java.io.BufferedOutputStream) JschUtil(cn.hutool.extra.ssh.JschUtil) HttpServletRequest(javax.servlet.http.HttpServletRequest) Charset(java.nio.charset.Charset) Service(org.springframework.stereotype.Service) WorkspaceEnvVarService(io.jpom.service.system.WorkspaceEnvVarService) IoUtil(cn.hutool.core.io.IoUtil) ChannelExec(com.jcraft.jsch.ChannelExec) BaseWorkspaceService(io.jpom.service.h2db.BaseWorkspaceService) LineHandler(cn.hutool.core.io.LineHandler) CommandModel(io.jpom.model.data.CommandModel) SshService(io.jpom.service.node.ssh.SshService) IOException(java.io.IOException) SystemUtil(cn.hutool.system.SystemUtil) ICron(io.jpom.cron.ICron) Collectors(java.util.stream.Collectors) File(java.io.File) ChannelType(cn.hutool.extra.ssh.ChannelType) StrUtil(cn.hutool.core.util.StrUtil) List(java.util.List) CommandExecLogModel(io.jpom.model.data.CommandExecLogModel) Task(cn.hutool.cron.task.Task) CharsetUtil(cn.hutool.core.util.CharsetUtil) SshModel(io.jpom.model.data.SshModel) FileUtil(cn.hutool.core.io.FileUtil) JSONObject(com.alibaba.fastjson.JSONObject) ThreadUtil(cn.hutool.core.thread.ThreadUtil) UserModel(io.jpom.model.data.UserModel) BaseServerController(io.jpom.common.BaseServerController) Assert(org.springframework.util.Assert) InputStream(java.io.InputStream) InputStream(java.io.InputStream) Charset(java.nio.charset.Charset) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) ChannelExec(com.jcraft.jsch.ChannelExec) IOException(java.io.IOException)

Example 2 with CommandExecLogModel

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

the class CommandService method executeItem.

/**
 * 准备执行 某一个
 *
 * @param commandModel  命令模版
 * @param commandParams 参数
 * @param sshId         ssh id
 * @param batchId       批次ID
 */
private void executeItem(CommandModel commandModel, List<CommandModel.CommandParam> commandParams, String sshId, String batchId, int triggerExecType) {
    SshModel sshModel = sshService.getByKey(sshId, false);
    CommandExecLogModel commandExecLogModel = new CommandExecLogModel();
    commandExecLogModel.setCommandId(commandModel.getId());
    commandExecLogModel.setCommandName(commandModel.getName());
    commandExecLogModel.setBatchId(batchId);
    commandExecLogModel.setSshId(sshId);
    commandExecLogModel.setTriggerExecType(triggerExecType);
    if (sshModel != null) {
        commandExecLogModel.setSshName(sshModel.getName());
    }
    commandExecLogModel.setStatus(CommandExecLogModel.Status.ING.getCode());
    // 拼接参数
    String commandParamsLine;
    if (commandParams != null) {
        commandExecLogModel.setParams(JSONObject.toJSONString(commandParams));
        commandParamsLine = commandParams.stream().map(CommandModel.CommandParam::getValue).collect(Collectors.joining(StrUtil.SPACE));
    } else {
        commandParamsLine = StrUtil.EMPTY;
    }
    commandExecLogService.insert(commandExecLogModel);
    ThreadUtil.execute(() -> {
        try {
            this.execute(commandModel, commandExecLogModel, sshModel, commandParamsLine);
        } catch (Exception e) {
            DefaultSystemLog.getLog().error("命令模版执行链接异常", e);
            this.updateStatus(commandExecLogModel.getId(), CommandExecLogModel.Status.SESSION_ERROR);
        }
    });
}
Also used : SshModel(io.jpom.model.data.SshModel) CommandExecLogModel(io.jpom.model.data.CommandExecLogModel) IOException(java.io.IOException)

Example 3 with CommandExecLogModel

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

the class CommandLogController method batchList.

/**
 * 命令执行记录
 *
 * @param commandId 命令ID
 * @param batchId   批次ID
 * @return result
 *
 * @api {GET}  node/ssh_command_log/batch_list 命令执行记录
 * @apiGroup node/ssh_command_log
 * @apiUse defResultJson
 * @apiParam {String} commandId 命令ID
 * @apiParam {String} batchId 批次ID
 * @apiSuccess {Object} commandExecLogModels 命令执行记录
 * @apiSuccess {String} commandExecLogModels.commandId 命令ID
 * @apiSuccess {String} commandExecLogModels.batchId 批次ID
 * @apiSuccess {String} commandExecLogModels.sshId ssh Id
 * @apiSuccess {Number} commandExecLogModels.status Status
 * @apiSuccess {String} commandExecLogModels.commandName 命令名称
 * @apiSuccess {String} commandExecLogModels.sshName ssh 名称
 * @apiSuccess {String} commandExecLogModels.params 参数
 * @apiSuccess {Number} commandExecLogModels.triggerExecType 触发类型 {0,手动,1 自动触发}
 * @apiSuccess {Boolean} commandExecLogModels.hasLog 日志文件是否存在
 */
@GetMapping(value = "batch_list", produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.LIST)
public String batchList(@ValidatorItem String commandId, @ValidatorItem String batchId) {
    CommandExecLogModel commandExecLogModel = new CommandExecLogModel();
    commandExecLogModel.setCommandId(commandId);
    commandExecLogModel.setBatchId(batchId);
    List<CommandExecLogModel> commandExecLogModels = commandExecLogService.listByBean(commandExecLogModel);
    return JsonMessage.getString(200, "", commandExecLogModels);
}
Also used : CommandExecLogModel(io.jpom.model.data.CommandExecLogModel) ClassFeature(io.jpom.permission.ClassFeature) Feature(io.jpom.permission.Feature) MethodFeature(io.jpom.permission.MethodFeature)

Example 4 with CommandExecLogModel

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

the class CommandLogController method log.

/**
 * 获取日志
 *
 * @param id   id
 * @param line 需要获取的行号
 * @return json
 *
 * @api {POST} node/ssh_command_log/log 获取日志
 * @apiGroup node/ssh_command_log
 * @apiUse defResultJson
 * @apiParam {String} id 日志 id
 * @apiParam {Number} line 需要获取的行号
 * @apiSuccess {Boolean} run 运行状态
 */
@RequestMapping(value = "log", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.LIST)
public String log(@ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "没有数据") String id, @ValidatorItem(value = ValidatorRule.POSITIVE_INTEGER, msg = "line") int line) {
    CommandExecLogModel item = commandExecLogService.getByKey(id, getRequest());
    Assert.notNull(item, "没有对应数据");
    File file = item.logFile();
    if (!FileUtil.exist(file)) {
        return JsonMessage.getString(200, "还没有日志信息");
    }
    Assert.state(FileUtil.isFile(file), "日志文件错误");
    JSONObject data = FileUtils.readLogFile(file, line);
    // 运行中
    Integer status = item.getStatus();
    data.put("run", status != null && status == CommandExecLogModel.Status.ING.getCode());
    return JsonMessage.getString(200, "", data);
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) CommandExecLogModel(io.jpom.model.data.CommandExecLogModel) File(java.io.File) ClassFeature(io.jpom.permission.ClassFeature) Feature(io.jpom.permission.Feature) MethodFeature(io.jpom.permission.MethodFeature)

Example 5 with CommandExecLogModel

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

the class CommandLogController method del.

/**
 * 删除日志记录
 *
 * @param id id
 * @return result
 *
 * @api {POST} node/ssh_command_log/del 删除日志记录
 * @apiGroup node/ssh_command_log
 * @apiUse defResultJson
 * @apiParam {String} id 记录 id
 */
@RequestMapping(value = "del", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.DEL)
public String del(String id) {
    CommandExecLogModel execLogModel = commandExecLogService.getByKey(id);
    Assert.notNull(execLogModel, "没有对应的记录");
    File logFile = execLogModel.logFile();
    boolean fastDel = CommandUtil.systemFastDel(logFile);
    Assert.state(!fastDel, "清理日志文件失败");
    // 
    commandExecLogService.delByKey(id);
    return JsonMessage.getString(200, "操作成功");
}
Also used : CommandExecLogModel(io.jpom.model.data.CommandExecLogModel) File(java.io.File) ClassFeature(io.jpom.permission.ClassFeature) Feature(io.jpom.permission.Feature) MethodFeature(io.jpom.permission.MethodFeature)

Aggregations

CommandExecLogModel (io.jpom.model.data.CommandExecLogModel)7 ClassFeature (io.jpom.permission.ClassFeature)4 Feature (io.jpom.permission.Feature)4 MethodFeature (io.jpom.permission.MethodFeature)4 File (java.io.File)4 JSONObject (com.alibaba.fastjson.JSONObject)2 SshModel (io.jpom.model.data.SshModel)2 IOException (java.io.IOException)2 ExceptionUtil (cn.hutool.core.exceptions.ExceptionUtil)1 FileUtil (cn.hutool.core.io.FileUtil)1 IoUtil (cn.hutool.core.io.IoUtil)1 LineHandler (cn.hutool.core.io.LineHandler)1 ThreadUtil (cn.hutool.core.thread.ThreadUtil)1 CharsetUtil (cn.hutool.core.util.CharsetUtil)1 IdUtil (cn.hutool.core.util.IdUtil)1 StrUtil (cn.hutool.core.util.StrUtil)1 Task (cn.hutool.cron.task.Task)1 ChannelType (cn.hutool.extra.ssh.ChannelType)1 JschUtil (cn.hutool.extra.ssh.JschUtil)1 SystemUtil (cn.hutool.system.SystemUtil)1