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