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);
}
}
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);
}
});
}
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);
}
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);
}
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, "操作成功");
}
Aggregations