Search in sources :

Example 1 with CommandModel

use of io.jpom.model.data.CommandModel 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 CommandModel

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

the class CommandInfoController method edit.

/**
 * 新建/编辑命令
 *
 * @param data 命令信息
 * @return result
 *
 * @api {POST} node/ssh_command/edit 新建/编辑命令
 * @apiGroup node/ssh_command
 * @apiUse defResultJson
 * @apiBody {String} name           命令名称
 * @apiBody {String} command        命令内容
 * @apiBody {String} [desc]         命令描述
 * @apiBody {String} defParams      默认参数
 * @apiBody {String} autoExecCron   定时构建表达式
 * @apiBody {String} id             命令主键 ID
 * @apiBody {String} [sshIds]       SSH 节点
 */
@RequestMapping(value = "edit", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.EDIT)
public String edit(@RequestBody JSONObject data) {
    String name = data.getString("name");
    String command = data.getString("command");
    String desc = data.getString("desc");
    String defParams = data.getString("defParams");
    Assert.hasText(name, "请输入命令名称");
    Assert.hasText(command, "请输入命令内容");
    String autoExecCron = data.getString("autoExecCron");
    String id = data.getString("id");
    // 
    CommandModel commandModel = new CommandModel();
    commandModel.setName(name);
    commandModel.setCommand(command);
    commandModel.setDesc(desc);
    commandModel.setSshIds(data.getString("sshIds"));
    if (StrUtil.isNotEmpty(autoExecCron)) {
        try {
            new CronPattern(autoExecCron);
        } catch (Exception e) {
            throw new IllegalArgumentException("定时构建表达式格式不正确");
        }
    }
    commandModel.setAutoExecCron(autoExecCron);
    // 
    if (StrUtil.isNotEmpty(defParams)) {
        List<CommandModel.CommandParam> params = CommandModel.params(defParams);
        if (params == null) {
            commandModel.setDefParams(StrUtil.EMPTY);
        } else {
            commandModel.setDefParams(JSONObject.toJSONString(params));
        }
    } else {
        commandModel.setDefParams(StrUtil.EMPTY);
    }
    if (StrUtil.isEmpty(id)) {
        commandService.insert(commandModel);
    } else {
        commandModel.setId(id);
        commandService.updateById(commandModel, getRequest());
    }
    return JsonMessage.getString(200, "操作成功");
}
Also used : CommandModel(io.jpom.model.data.CommandModel) CronPattern(cn.hutool.cron.pattern.CronPattern) IOException(java.io.IOException) Feature(io.jpom.permission.Feature) MethodFeature(io.jpom.permission.MethodFeature) ClassFeature(io.jpom.permission.ClassFeature) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

CommandModel (io.jpom.model.data.CommandModel)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 CronPattern (cn.hutool.cron.pattern.CronPattern)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 DefaultSystemLog (cn.jiangzeyin.common.DefaultSystemLog)1 JSONObject (com.alibaba.fastjson.JSONObject)1 ChannelExec (com.jcraft.jsch.ChannelExec)1 BaseServerController (io.jpom.common.BaseServerController)1 CronUtils (io.jpom.cron.CronUtils)1